Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Refactor machine related logic code #579

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/app/containers/Settings/MachineSettings/MachineSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Select from 'react-select';
import isEqual from 'lodash/isEqual';
import { NumberInput } from '../../../components/Input';
import i18n from '../../../lib/i18n';
import { actions } from '../../../flux/machine';
import { actions as machineActions } from '../../../flux/machine';
import styles from '../form.styl';
import { MACHINE_SERIES } from '../../../constants';

Expand Down Expand Up @@ -54,7 +54,7 @@ class MachineSettings extends PureComponent {
setEnclosureState: PropTypes.func.isRequired,
getZAxisModuleState: PropTypes.func.isRequired,
setZAxisModuleState: PropTypes.func.isRequired,
updateConnectionTimeout: PropTypes.func.isRequired
setConnectionTimeout: PropTypes.func.isRequired
};

state = {
Expand Down Expand Up @@ -143,7 +143,8 @@ class MachineSettings extends PureComponent {
this.props.updateMachineSize(this.state.size);
this.props.setEnclosureState(this.state.enclosureDoorDetection);
this.props.setZAxisModuleState(this.state.zAxisModule);
this.props.updateConnectionTimeout(this.state.connectionTimeout);

this.props.setConnectionTimeout(this.state.connectionTimeout);
}
};

Expand Down Expand Up @@ -365,13 +366,13 @@ const mapStateToProps = (state) => {

const mapDispatchToProps = (dispatch) => {
return {
updateMachineSeries: (series) => dispatch(actions.updateMachineSeries(series)),
updateMachineSize: (size) => dispatch(actions.updateMachineSize(size)),
getEnclosureState: () => dispatch(actions.getEnclosureState()),
setEnclosureState: (on) => dispatch(actions.setEnclosureState(on)),
getZAxisModuleState: () => dispatch(actions.getZAxisModuleState()),
setZAxisModuleState: (moduleId) => dispatch(actions.setZAxisModuleState(moduleId)),
updateConnectionTimeout: (time) => dispatch(actions.updateConnectionTimeout(time))
updateMachineSeries: (series) => dispatch(machineActions.updateMachineSeries(series)),
updateMachineSize: (size) => dispatch(machineActions.updateMachineSize(size)),
getEnclosureState: () => dispatch(machineActions.getEnclosureState()),
setEnclosureState: (on) => dispatch(machineActions.setEnclosureState(on)),
getZAxisModuleState: () => dispatch(machineActions.getZAxisModuleState()),
setZAxisModuleState: (moduleId) => dispatch(machineActions.setZAxisModuleState(moduleId)),
setConnectionTimeout: (connectionTimeout) => dispatch(machineActions.connect.setConnectionType(connectionTimeout))
};
};

Expand Down
79 changes: 1 addition & 78 deletions src/app/flux/develop-tools/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import isEmpty from 'lodash/isEmpty';
import {
ABSENT_OBJECT,
WORKFLOW_STATE_IDLE,
CONNECTION_TYPE_SERIAL,
CONNECTION_TYPE_WIFI, PROTOCOL_SCREEN
PROTOCOL_SCREEN
} from '../../constants';
import { screenController } from '../../lib/controller';

const STATUS_UNKNOWN = 'UNKNOWN';
// const STATUS_IDLE = 'IDLE';
// const STATUS_RUNNING = 'RUNNING';
// const STATUS_PAUSED = 'PAUSED';

let statusTimer = null;

const INITIAL_STATE = {
// Serial port
Expand Down Expand Up @@ -157,9 +150,6 @@ export const actions = {
});
},

updatePort: (port) => (dispatch) => {
dispatch(actions.updateState({ port: port }));
},
// executeGcode: (gcode, context) => (dispatch, getState) => {
executeGcode: (gcode, context) => (dispatch, getState) => {
const machine = getState().machine;
Expand Down Expand Up @@ -214,74 +204,7 @@ export const actions = {
},
setZAxisModuleState: (moduleId) => () => {
screenController.writeln(`M1025 M${moduleId}`, { source: 'query' });
},
// Server
discoverServers: () => (dispatch, getState) => {
dispatch(actions.updateState({ discovering: true }));

setTimeout(() => {
const state = getState().machine;
if (state.discovering) {
dispatch(actions.updateState({ discovering: false }));
}
}, 3000);

screenController.listHTTPServers();
},
setServer: (server) => (dispatch) => {
// Update server
dispatch(actions.updateState({
server,
workflowStatus: STATUS_UNKNOWN,
isConnected: true,
connectionType: CONNECTION_TYPE_WIFI
}));

// TODO: Fix the issue that sometimes will get multiple machines' status simultaneously
// Cancel previous status polling
if (statusTimer) {
clearTimeout(statusTimer);
statusTimer = null;
}

// Get status of server frequently
const getStatus = () => {
server.requestStatus((err, res) => {
if (!err) {
dispatch(actions.updateState({
workflowStatus: res.body.status,
workPosition: {
x: server.x.toFixed(3),
y: server.y.toFixed(3),
z: server.z.toFixed(3)
}
}));
} else {
dispatch(actions.updateState({ workflowStatus: STATUS_UNKNOWN }));
}

// If status timer is not cancelled, then re-schedule a new timeout
if (statusTimer !== null) {
statusTimer = setTimeout(getStatus, 1500);
}
});
};
statusTimer = setTimeout(getStatus);
},
unsetServer: () => (dispatch) => {
dispatch(actions.updateState({
server: ABSENT_OBJECT,
workflowStatus: STATUS_UNKNOWN,
isConnected: false,
connectionType: ''
}));

if (statusTimer) {
clearTimeout(statusTimer);
statusTimer = null;
}
}

};

export default function reducer(state = INITIAL_STATE, action) {
Expand Down
6 changes: 4 additions & 2 deletions src/app/flux/machine/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Server extends events.EventEmitter {
return `http://${this.address}:${this.port}`;
}

equal(server) {
equals(server) {
const { name, address, model } = server;
if (name && name === this.name && address && address === this.address) {
return !(model && model !== this.model);
Expand Down Expand Up @@ -186,9 +186,11 @@ export class Server extends events.EventEmitter {
return;
}
this.errorCount = 0;
if (code === 204) {

if (code === 204) { // No Content
return;
}

const { status, x, y, z, offsetX, offsetY, offsetZ } = data;
this.status = status.toLowerCase();
this.state.workPosition = {
Expand Down
13 changes: 13 additions & 0 deletions src/app/flux/machine/action-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const ACTION_UPDATE_STATE = 'machine/ACTION_UPDATE_STATE';

// Update state directly
function updateState(state) {
return {
type: ACTION_UPDATE_STATE,
state
};
}

export default {
updateState
};
83 changes: 83 additions & 0 deletions src/app/flux/machine/action-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import includes from 'lodash/includes';
import isInteger from 'lodash/isInteger';

import { CONNECTION_TYPE_SERIAL, CONNECTION_TYPE_WIFI } from '../../constants';
import { machineStore } from '../../store/local-storage';

import baseActions from './action-base';

const init = () => (dispatch) => {
const connectionType = machineStore.get('connection.type') || CONNECTION_TYPE_SERIAL;
const connectionTimeout = machineStore.get('connection.timeout') || 3000;

dispatch(baseActions.updateState({
connectionType,
connectionTimeout
}));
};

const setConnectionType = (connectionType) => (dispatch) => {
if (!includes([CONNECTION_TYPE_WIFI, CONNECTION_TYPE_SERIAL], connectionType)) return;

dispatch(baseActions.updateState({ connectionType }));

machineStore.set('connection.type', connectionType);
};

const setConnectionTimeout = (connectionTimeout) => (dispatch) => {
connectionTimeout = isInteger(connectionTimeout) && connectionTimeout > 0 ? connectionTimeout : 3000;

dispatch(baseActions.updateState({ connectionTimeout }));

machineStore.set('connection.timeout', connectionTimeout);
};

/**
* Set selected server.
*
* Update state only, we will save the server when connection established.
*/
const setSelectedServer = (server) => (dispatch) => {
dispatch(baseActions.updateState({ server }));
};

const setServerAddress = (serverAddress) => (dispatch) => {
dispatch(baseActions.updateState({ serverAddress: serverAddress }));

machineStore.set('server.address', serverAddress);
};

const setServerToken = (token) => (dispatch) => {
dispatch(baseActions.updateState({ serverToken: token }));

machineStore.set('server.token', token);
};

const setManualIP = (manualIp) => (dispatch) => {
dispatch(baseActions.updateState({ manualIp }));

machineStore.set('manualIp', manualIp);
};

const setMachineSerialPort = (port) => (dispatch) => {
dispatch(baseActions.updateState({ port }));

// TODO: rename key `port`
machineStore.set('port', port);
};

export default {
init,

setConnectionType,
setConnectionTimeout,

setSelectedServer,

setMachineSerialPort,

// TODO: refactor methods below
setServerAddress,
setServerToken,
setManualIP
};
55 changes: 55 additions & 0 deletions src/app/flux/machine/action-discover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { controller } from '../../lib/controller';

import { Server } from './Server';
import baseActions from './action-base';


const init = () => (dispatch, getState) => {
const controllerEvents = {
// Receive when new servers discovered
'http:discover': (objects) => {
// Note that we may receive this event many times.
const { servers } = getState().machine;

const newServers = [];
for (const object of objects) {
const find = servers.find(v => v.equals(object));
if (find) {
// use old Server instance
newServers.push(find);
} else {
const server = new Server(object.name, object.address, object.model);
newServers.push(server);
}
}

dispatch(baseActions.updateState({ servers: newServers }));
}
};

Object.keys(controllerEvents).forEach(event => {
controller.on(event, controllerEvents[event]);
});
};

/**
* Discover servers on Snapmaker 2.0.
*/
const discoverSnapmakerServers = () => (dispatch, getState) => {
dispatch(baseActions.updateState({ serverDiscovering: true }));

// reset discover state whatever search is done or not
setTimeout(() => {
const state = getState().machine;
if (state.serverDiscovering) {
dispatch(baseActions.updateState({ serverDiscovering: false }));
}
}, 3000);

controller.listHTTPServers();
};

export default {
init,
discoverSnapmakerServers
};
Loading