Skip to content

Commit

Permalink
Fix: v4.10.2 part4 (#2453)
Browse files Browse the repository at this point in the history
* Fix: J1 get enclosure light state

* Fix: Hidden fan when connect with j1

* Fix: Can't get enclosure, rotatry, ari purifier status connect with wifi/serial

* Fix: Original serial connect failed
- lack of logic to determine if the machine is ready(marlin)

* Fix: Wifi connection aifpurifier status

* Fix: Remove useless code

* Fix: Remove Enclosure component debug log
  • Loading branch information
IDKHTS authored Jan 16, 2024
1 parent 4f5b0c7 commit 6712031
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 36 deletions.
33 changes: 24 additions & 9 deletions src/app/flux/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export const actions = {
* Handing of remote events.
*/
__initRemoteEvents: () => (dispatch, getState) => {
const compareAndSet = (obj: object, compareObj: object, key: string, value: boolean | number | string) => {
if (isNil(value)) {
return;
}
if (value !== compareObj[key]) {
obj[key] = value;
}
};
const controllerEvents = {
// connecting state from remote
[SocketEvent.ConnectionConnecting]: (options: { requireAuth?: boolean }) => {
Expand Down Expand Up @@ -157,6 +165,22 @@ export const actions = {
machineSeries = series;
}

if (!isNil(moduleStatusList)) {
const data = {};
const currentState = getState().workspace;
const enclosureOnline = moduleStatusList.enclosure;
const rotateModuleOnline = moduleStatusList.rotateModuleOnline;
const airPurifier = moduleStatusList.airPurifier;

compareAndSet(data, currentState, 'moduleStatusList', moduleStatusList);
compareAndSet(data, currentState, 'enclosureOnline', enclosureOnline);
compareAndSet(data, currentState, 'rotateModuleOnline', rotateModuleOnline);
compareAndSet(data, currentState, 'airPurifier', airPurifier);

dispatch(baseActions.updateState(data));
}


log.info(`machine = ${machineSeries}`);
log.info(`tool = ${toolHead}`);

Expand Down Expand Up @@ -262,15 +286,6 @@ export const actions = {
const { state } = options;
const { headType, pos, originOffset, headStatus, headPower, temperature, zFocus, isHomed, zAxisModule, laser10WErrorState } = state;

const compareAndSet = (obj: object, compareObj: object, key: string, value: boolean | number | string) => {
if (isNil(value)) {
return;
}
if (value !== compareObj[key]) {
obj[key] = value;
}
};

const data = {};

const currentState = getState().workspace;
Expand Down
12 changes: 3 additions & 9 deletions src/app/ui/widgets/Connection/NetworkConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const NetworkConnection: React.FC = () => {
moduleList,
moduleStatusList,
airPurifier,
airPurifierHasPower,
heatedBedTemperature,
laserCamera,
} = useSelector((state: RootState) => state.workspace, shallowEqual);
Expand Down Expand Up @@ -570,16 +571,8 @@ const NetworkConnection: React.FC = () => {
if (moduleStatusList[key]) {
newModuleStatusList.push({
moduleName: i18n._(`key-Workspace/Connection-${key}`),
status: moduleStatusList[key]
status: key === 'airPurifier' ? airPurifierHasPower : moduleStatusList[key]
});
} else {
// compatible code
if (key === 'airPurifier' && airPurifier) {
newModuleStatusList.push({
moduleName: i18n._('key-Workspace/Connection-airPurifier'),
status: moduleStatusList[key]
});
}
}
});

Expand All @@ -602,6 +595,7 @@ const NetworkConnection: React.FC = () => {
toolHead,
emergencyStopButtonStatus,
airPurifier,
airPurifierHasPower,
airPurifierStatus,
rotaryModuleStatus,
enclosureStatus,
Expand Down
4 changes: 2 additions & 2 deletions src/app/ui/widgets/Connection/modals/SelectMachineModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const SelectMachineModal: React.FC = () => {

dispatch(workspaceActions.updateMachineState({
machineIdentifier: selectedMachineIdentifier,
toolHead: selectedToolHead.identifier,
headType: selectedToolHead.metadata.headType,
toolHead: selectedToolHead?.identifier,
headType: selectedToolHead?.metadata.headType,
}));

// TODO: For SM 2.0 with laser/CNC tool head, execute G54 right after selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const WifiTransport: React.FC<FileTransferViewProps> = (props) => {
});

const onSelectFile = useCallback((_selectFileName, name, event, needToUnselect = true) => {
if (event && event.target && event.target.className) {
if (event && event.target && event.target.className && typeof event.target.className.indexOf === 'function') {
if (event.target.className.indexOf('input-select') > -1
|| event.target.className.indexOf('fa-check') > -1) {
return;
Expand Down
21 changes: 13 additions & 8 deletions src/app/ui/widgets/Enclosure/Enclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { controller } from '../../../communication/socket-communication';
import i18n from '../../../lib/i18n';
import log from '../../../lib/log';
import Switch from '../../components/Switch';
import { SnapmakerJ1Machine } from '../../../machines';

const Enclosure: React.FC = () => {
const { isConnected } = useSelector((state: RootState) => state.workspace);
Expand All @@ -16,6 +17,7 @@ const Enclosure: React.FC = () => {
enclosureLight,
enclosureFan,
isDoorEnabled: doorEnabled,
machineIdentifier
} = useSelector((state: RootState) => state.workspace, shallowEqual);

const [isLedReady, setIsLedReady] = useState(true);
Expand Down Expand Up @@ -77,14 +79,17 @@ const Enclosure: React.FC = () => {
disabled={(!isLedReady) || !isConnected}
/>
</div>
<div className="sm-flex justify-space-between margin-vertical-8">
<span>{i18n._('key-Workspace/Enclosure-Exhaust Fan')}</span>
<Switch
onClick={actions.onHandleCoolingFans}
checked={Boolean(enclosureFan)}
disabled={(!isFanReady) || !isConnected}
/>
</div>

{machineIdentifier !== SnapmakerJ1Machine.identifier && (
<div className="sm-flex justify-space-between margin-vertical-8">
<span>{i18n._('key-Workspace/Enclosure-Exhaust Fan')}</span>
<Switch
onClick={actions.onHandleCoolingFans}
checked={Boolean(enclosureFan)}
disabled={(!isFanReady) || !isConnected}
/>
</div>
)}
{/* Disable adjustment for door detection
{(isConnected && connectionType === 'wifi' && headType !== '3dp') && (
<TipTrigger
Expand Down
16 changes: 12 additions & 4 deletions src/server/controllers/Marlin/MarlinController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SnapmakerA250Machine,
SnapmakerA350Machine,
SnapmakerArtisanMachine,
SnapmakerOriginalMachine,
SnapmakerRayMachine
} from '../../../app/machines';
import { DEFAULT_BAUDRATE } from '../../constants';
Expand Down Expand Up @@ -164,6 +165,8 @@ class MarlinController extends EventEmitter {
};


seriesSize = null

queryState = (() => {
let index = 0;
const typeOf3dp = [
Expand Down Expand Up @@ -372,7 +375,6 @@ class MarlinController extends EventEmitter {
});
this.controller.on('series', (res) => {
let machineIdentifier = '';

if (res.seriesSize === 'Ray2023') {
machineIdentifier = 'Ray';
}
Expand Down Expand Up @@ -456,10 +458,16 @@ class MarlinController extends EventEmitter {
}
});
this.controller.on('temperature', (res) => {
// this.checkMachineIsReady();
if (!this.seriesSize) {
let machineIdentifier = '';
// The Original Machine does not have a seriesSize message.
// We use the temperature message as a symbol that the machine is ready.
machineIdentifier = SnapmakerOriginalMachine.identifier;
this.checkMachineIsReady({ machineIdentifier });
}

// log.silly(`controller.on('temperature'): source=${this.history.writeSource},
// line=${JSON.stringify(this.history.writeLine)}, res=${JSON.stringify(res)}`);
log.silly(`controller.on('temperature'): source=${this.history.writeSource},
line=${JSON.stringify(this.history.writeLine)}, res=${JSON.stringify(res)}`);
if (includes([WRITE_SOURCE_CLIENT, WRITE_SOURCE_FEEDER, WRITE_SOURCE_SENDER], this.history.writeSource)) {
this.emitAll('serialport:read', { data: res.raw });
}
Expand Down
39 changes: 38 additions & 1 deletion src/server/services/machine/channels/SacpChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class SacpChannelBase extends Channel implements

public subscribeEnclosureInfoCallback: ResponseCallback;

public subscribeEnclosureLightInfoCallback: ResponseCallback;

public subscribePurifierInfoCallback: ResponseCallback;

private filamentAction = false;
Expand Down Expand Up @@ -139,6 +141,24 @@ class SacpChannelBase extends Channel implements
}
}

public subscribePurifierInfo() {
this.subscribePurifierInfoCallback = (data) => {
const { airPurifierStatus: { fanState, speedLevel, lifeLevel, powerState } } = new AirPurifierInfo().fromBuffer(data.response.data);
this.socket && this.socket.emit('Marlin:state', {
state: {
airPurifier: true,
airPurifierHasPower: powerState,
airPurifierSwitch: fanState,
airPurifierFanSpeed: speedLevel,
airPurifierFilterHealth: lifeLevel - 1
}
});
};
this.sacpClient.subscribePurifierInfo({ interval: 1000 }, this.subscribePurifierInfoCallback).then(res => {
log.info(`subscribe purifier info, ${res.response.result}`);
});
}

public async startHeartbeat(): Promise<void> {
log.info('Start heartbeat.');

Expand Down Expand Up @@ -180,7 +200,7 @@ class SacpChannelBase extends Channel implements
};

const res = await this.sacpClient.subscribeHeartbeat({ interval: 2000 }, subscribeHeartbeatCallback);

this.subscribePurifierInfo();
log.info(`Subscribe heartbeat, result = ${res.code}`);
}

Expand Down Expand Up @@ -1114,6 +1134,23 @@ class SacpChannelBase extends Channel implements
this.sacpClient.subscribeEnclosureInfo({ interval: 1000 }, this.subscribeEnclosureInfoCallback).then(res => {
log.info(`subscribe enclosure info, ${res.response.result}`);
});
// FIXME: init enclosure light Info for j1, only use for j1
this.subscribeEnclosureLightInfoCallback = (data) => {
const module = this.getEnclosureModule();
if (!module) {
return;
}
const buffer = data.response.data;
const lightIntensity = readUint8(buffer, 1);
stateData = {
...stateData,
ledValue: Math.round(lightIntensity / 255 * 100), // lightIntensity range from 0 to 255
};
};
this.sacpClient.subscribeEnclosureLightInfo({ interval: 1000 }, this.subscribeEnclosureLightInfoCallback).then(res => {
log.info(`subscribe enclosure light info, ${res.response.result}`);
});

this.subscribePurifierInfoCallback = (data) => {
const { airPurifierStatus: { fanState, speedLevel, lifeLevel, powerState } } = new AirPurifierInfo().fromBuffer(data.response.data);
stateData = {
Expand Down
2 changes: 2 additions & 0 deletions src/server/services/machine/channels/SacpSerialChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ class SacpSerialChannel extends SacpChannelBase {
this.serialport.write('\r\n');
await new Promise<void>((r) => setTimeout(() => r(), 100));

// for artisan
this.serialport.write('M2000 S5 P1\r\n');
await new Promise<void>((r) => setTimeout(() => r(), 100));

// for ray
this.serialport.write('$PS\r\n');
await new Promise<void>((r) => setTimeout(() => r(), 100));

Expand Down
13 changes: 11 additions & 2 deletions src/server/services/machine/instances/RayInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { PeerId } from '@snapmaker/snapmaker-sacp-sdk/dist/communication/Header'
import { includes } from 'lodash';

import {
AIR_PURIFIER_MODULE_IDS,
EMERGENCY_STOP_BUTTON,
ENCLOSURE_MODULE_IDS,
LASER_HEAD_MODULE_IDS,
MODULEID_TOOLHEAD_MAP,
ROTARY_MODULE_IDS,
Expand Down Expand Up @@ -39,6 +41,7 @@ class RayMachineInstance extends MachineInstance {

// module info
const moduleInfos = await (this.channel as SacpChannelBase).getModuleInfo();
console.log('moduleInfos: ', JSON.stringify(moduleInfos));

/*
moduleInfos = [
Expand All @@ -56,9 +59,9 @@ class RayMachineInstance extends MachineInstance {
*/

const moduleListStatus = {
// airPurifier: false,
airPurifier: false,
emergencyStopButton: false,
// enclosure: false,
enclosure: false,
rotaryModule: false
};

Expand All @@ -74,6 +77,12 @@ class RayMachineInstance extends MachineInstance {
if (includes(EMERGENCY_STOP_BUTTON, module.moduleId)) {
moduleListStatus.emergencyStopButton = true;
}
if (includes(AIR_PURIFIER_MODULE_IDS, module.moduleId)) {
moduleListStatus.airPurifier = true;
}
if (includes(ENCLOSURE_MODULE_IDS, module.moduleId)) {
moduleListStatus.enclosure = true;
}
});
state.moduleStatusList = moduleListStatus;

Expand Down
6 changes: 6 additions & 0 deletions src/server/services/machine/sacp/SacpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,12 @@ export default class SacpClient extends Dispatcher {
});
}

public async subscribeEnclosureLightInfo({ interval = 1000 }, callback: ResponseCallback) {
return this.subscribe(0x15, 0xa1, interval, callback).then(({ response, packet }) => {
return { response, packet, data: {} };
});
}

public async subscribePurifierInfo({ interval = 1000 }, callback: ResponseCallback) {
return this.subscribe(0x17, 0xa0, interval, callback).then(({ response, packet }) => {
return { response, packet, data: {} };
Expand Down

0 comments on commit 6712031

Please sign in to comment.