Skip to content

Commit

Permalink
feat: new state machine from xstate
Browse files Browse the repository at this point in the history
  • Loading branch information
biancode committed Nov 17, 2019
1 parent bc30267 commit 57aef99
Show file tree
Hide file tree
Showing 9 changed files with 662 additions and 436 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function doc (cb) {
.pipe(jsdoc(cb))
}

exports.default = series(cleanProject, releaseWebContent, releaseJSContent, releaseLocal, releasePublicData, releaseIcons, doc, docIcons, docImages)
exports.clean = cleanProject
exports.build = series(cleanProject, releaseWebContent, releaseJSContent, releaseLocal)
exports.buildDocs = series(doc, docIcons, docImages)
Expand Down
2 changes: 1 addition & 1 deletion npm-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ npm cache verify

npm outdated --depth=0

npm install
npm i
2 changes: 1 addition & 1 deletion npm-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ npm outdated --depth=0

ncu -u

npm install
npm i
675 changes: 411 additions & 264 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "node-red-contrib-modbus",
"version": "5.0.1-alpha.0",
"private": false,
"private": true,
"description": "The all in one Modbus TCP and Serial contribution package for Node-RED.",
"dependencies": {
"bson": "4.0.2",
"debug": "4.1.1",
"jsmodbus": "4.0.0",
"line-by-line": "0.1.6",
"modbus-serial": "7.7.3",
"jsmodbus": "4.0.0",
"source-map-support": "0.5.16",
"stately.js": "1.3.0",
"vm2": "3.8.4"
"vm2": "3.8.4",
"@xstate/fsm": "1.1.0"
},
"optionalDependencies": {
"serialport": "8.0.5"
Expand All @@ -26,11 +26,11 @@
"opcua"
],
"engines": {
"node": ">=8.0.0"
"node": ">=10"
},
"repository": {
"type": "git",
"url": "git+https://github.com/biancode/node-red-contrib-modbus.git"
"url": "git+https://github.com/biancoroyal/node-red-contrib-modbus.git"
},
"node-red": {
"nodes": {
Expand Down Expand Up @@ -58,27 +58,27 @@
"modbus"
],
"bugs": {
"url": "https://github.com/biancode/node-red-contrib-modbus/issues/"
"url": "https://github.com/biancoroyal/node-red-contrib-modbus/issues/"
},
"homepage": "https://biancode.github.io/node-red-contrib-modbus/",
"scripts": {
"test": "standard --fix && mocha test/* --reporter dot --timeout 5000",
"test-with-coverage": "istanbul cover _mocha --report lcovonly -- --recursive --timeout 5000 -R spec && cat ./coverage/lcov.info | codacy-coverage --token a047b284d2124df78f999c27ae874e9c && rm -rf ./coverage",
"coverage": "standard --fix && gulp build && npm test && istanbul cover _mocha -- --recursive --timeout 5000",
"build": "standard --fix && gulp publish",
"build": "standard --fix && gulp",
"prepublishOnly": "standard --fix && npm run build && npm test && mocha test --recursive --timeout 5000 --reporter dot",
"ci-publish": "ci-publish",
"release": "standard-version -a",
"rewrite-changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
},
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@node-red/nodes": "^1.0.2",
"chai": "^4.2.0",
"codacy-coverage": "^3.4.0",
"conventional-changelog-cli": "^2.0.25",
"conventional-changelog-cli": "^2.0.27",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-clean": "^0.4.0",
Expand All @@ -101,7 +101,7 @@
"standard": "^14.3.1",
"standard-version": "^7.0.0",
"supertest": "^4.0.2",
"uglify-js": "^3.6.5",
"uglify-js": "^3.6.9",
"uglify-js-harmony": "^2.7.7",
"when": "^3.7.8"
},
Expand Down
85 changes: 54 additions & 31 deletions src/core/modbus-client-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,69 @@ var de = de || { biancoroyal: { modbus: { core: { client: {} } } } } // eslint-d
de.biancoroyal.modbus.core.client.internalDebug = de.biancoroyal.modbus.core.client.internalDebug || require('debug')('contribModbus:core:client') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.core.client.internalDebugFSM = de.biancoroyal.modbus.core.client.internalDebugFSM || require('debug')('contribModbus:core:client:fsm') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.core.client.modbusSerialDebug = de.biancoroyal.modbus.core.client.modbusSerialDebug || require('debug')('modbus-serial') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.core.client.Stately = de.biancoroyal.modbus.core.client.Stately || require('stately.js') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.core.client.XStateFSM = de.biancoroyal.modbus.core.client.XStateFSM || require('@xstate/fsm') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.core.client.stateLogEnabled = de.biancoroyal.modbus.core.client.stateLogEnabled || false // eslint-disable-line no-use-before-define

de.biancoroyal.modbus.core.client.networkErrors = ['ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNRESET', 'ENETRESET',
'ECONNABORTED', 'ECONNREFUSED', 'ENETUNREACH', 'ENOTCONN',
'ESHUTDOWN', 'EHOSTDOWN', 'ENETDOWN', 'EWOULDBLOCK', 'EAGAIN', 'EHOSTUNREACH']

de.biancoroyal.modbus.core.client.createStatelyMachine = function () {
de.biancoroyal.modbus.core.client.createStateMachineService = function () {
this.stateLogEnabled = false

return de.biancoroyal.modbus.core.client.Stately.machine({
NEW: { init: 'INIT', stop: 'STOPED' },
BROKEN: { init: 'INIT', stop: 'STOPED' },
INIT: { openserial: 'OPENED', connect: 'CONNECTED', failure: 'FAILED' },
OPENED: { connect: 'CONNECTED', failure: 'FAILED', close: 'CLOSED' },
CONNECTED: { close: 'CLOSED', activate: 'ACTIVATED', failure: 'FAILED' },
ACTIVATED: {
close: 'CLOSED',
read: 'READING',
write: 'WRITING',
queue: 'QUEUEING',
failure: 'FAILED'
},
QUEUEING: {
activate: 'ACTIVATED',
read: 'READING',
write: 'WRITING',
empty: 'EMPTY',
failure: 'FAILED',
close: 'CLOSED'
},
EMPTY: { queue: 'QUEUEING', failure: 'FAILED', close: 'CLOSED' },
READING: { activate: 'ACTIVATED', failure: 'FAILED' },
WRITING: { activate: 'ACTIVATED', failure: 'FAILED' },
CLOSED: { failure: 'FAILED', break: 'BROKEN' },
FAILED: { close: 'CLOSED', break: 'BROKEN', stop: 'STOPED' },
STOPED: { queue: 'STOPED', activate: 'STOPED' }
}, 'NEW')
return this.XStateFSM.createMachine({
id: 'modbus',
initial: 'new',
states: {
new: {
on: { INIT: 'init', STOP: 'stoped' }
},
broken: {
on: { INIT: 'init', STOP: 'stoped', FAILURE: 'failed', CLOSE: 'closed' }
},
init: {
on: { OPENSERIAL: 'opened', CONNECT: 'connected', FAILURE: 'failed' }
},
opened: {
on: { CONNECT: 'connected', FAILURE: 'failed', CLOSE: 'closed' }
},
connected: {
on: { CLOSE: 'closed', ACTIVATE: 'activated', FAILURE: 'failed' }
},
activated: {
on: {
CLOSE: 'closed',
READ: 'reading',
WRITE: 'writing',
QUEUE: 'queueing',
FAILURE: 'failed'
}
},
queueing: {
on: {
CONNECT: 'connected',
ACTIVATE: 'activated',
READ: 'reading',
WRITE: 'writing',
EMPTY: 'empty',
FAILURE: 'failed',
CLOSE: 'closed'
}
},
empty: { on: { QUEUE: 'queueing', FAILURE: 'failed', CLOSE: 'closed' } },
reading: { on: { ACTIVATE: 'activated', FAILURE: 'failed' } },
writing: { on: { ACTIVATE: 'activated', FAILURE: 'failed' } },
closed: { on: { FAILURE: 'failed', BREAK: 'broken' } },
failed: { on: { CLOSE: 'closed', BREAK: 'broken', STOP: 'stoped' } },
stoped: { on: { QUEUE: 'stoped', ACTIVATE: 'stoped' } }
}
})
}

de.biancoroyal.modbus.core.client.messagesAllowedStates = ['ACTIVATED', 'QUEUEING', 'EMPTY']
de.biancoroyal.modbus.core.client.startStateService = function (toggleMachine) {
return this.XStateFSM.interpret(toggleMachine).start()
}

de.biancoroyal.modbus.core.client.messagesAllowedStates = ['activated', 'queueing', 'empty']

module.exports = de.biancoroyal.modbus.core.client
Loading

0 comments on commit 57aef99

Please sign in to comment.