From de489ef27a41f0ff0e30689fc3f7a4683fba8e1f Mon Sep 17 00:00:00 2001 From: Dean Cording Date: Thu, 11 Jan 2018 23:16:20 +1000 Subject: [PATCH] Initial release --- LICENSE | 6 + README.md | 16 ++- package-lock.json | 13 ++ package.json | 34 +++++ state-machine.html | 345 +++++++++++++++++++++++++++++++++++++++++++++ state-machine.js | 104 ++++++++++++++ 6 files changed, 517 insertions(+), 1 deletion(-) create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 state-machine.html create mode 100644 state-machine.js diff --git a/LICENSE b/LICENSE index be4b02e..bf229c4 100644 --- a/LICENSE +++ b/LICENSE @@ -19,3 +19,9 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + +NOTE: Be aware that the javascript-state-machine library has a non-commercial +LGPL-3.0 licence and requires a commercial licence to be used in commercial +applications. diff --git a/README.md b/README.md index 1b72b57..ac4fbc6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ # node-red-contrib-state-machine -A Node Red node that wraps around Javascript State Machine +A Node Red node that wraps around the [Javascript State Machine](https://www.npmjs.com/package/javascript-state-machine) to implement a [finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine) for Node Red. + +The node is configured with a number of states and triggers that will cause the node to transition from one +state to another. At any time, the node can only be in one of the defined states and it will only transition +to another state when it receives a trigger defined for the current state. The same trigger can be used to +cause transitions from more than one state. Triggers that are not valid for the current state are ignored. +The node will always start in the first state on the state list and will emit a message with the initial +state if the state output is set to a message property. + +Global and flow context properties can be used as trigger inputs but state transitions will only occur when the node receives a message. + +The current state can be set to a msg property or stored as a flow or global context property. If the state +output is set to a msg property, that property is set in the original message and passed through, otherwise +no messages are output. + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4c79ffb --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "node-red-contrib-state-machine", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "javascript-state-machine": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/javascript-state-machine/-/javascript-state-machine-3.0.1.tgz", + "integrity": "sha1-MnMgM2ogNk4mtR2RZiwU8U1B6lI=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..378ba3d --- /dev/null +++ b/package.json @@ -0,0 +1,34 @@ +{ + "name": "node-red-contrib-state-machine", + "version": "1.0.0", + "description": "A Node Red node for implementing a finite state machine.", + "dependencies": { + "javascript-state-machine": "3.*" + }, + "author": { + "name": "Dean Cording", + "email": "dean@cording.id.au" + }, + "license": "MIT", + "keywords": [ + "node-red", + "iot", + "state machine", + "finite state machine", + "state", + "transition" + ], + "bugs": { + "url": "https://github.com/DeanCording/node-red-contrib-state-machine/issues" + }, + "homepage": "https://github.com/DeanCording/node-red-contrib-state-machine", + "repository": { + "type": "git", + "url": "https://github.com/DeanCording/node-red-contrib-state-machine.git" + }, + "node-red": { + "nodes": { + "state-machine": "state-machine.js" + } + } +} diff --git a/state-machine.html b/state-machine.html new file mode 100644 index 0000000..f49007b --- /dev/null +++ b/state-machine.html @@ -0,0 +1,345 @@ + + + + + + + + diff --git a/state-machine.js b/state-machine.js new file mode 100644 index 0000000..5efdace --- /dev/null +++ b/state-machine.js @@ -0,0 +1,104 @@ +/***** + +node-red-contrib-state-machine - A Node Red node to implement a state machine using javascript-state-machine + +(https://www.npmjs.com/package/java-script-state-machine) + +MIT License + +Copyright (c) 2018 Dean Cording + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without +limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +// Core dependency +const StateMachine = require('javascript-state-machine'); +const util = require('util'); + +module.exports = function(RED) { + function StateMachineNode(config) { + RED.nodes.createNode(this,config); + var node = this; + + node.triggerProperty = config.triggerProperty || 'topic'; + node.triggerPropertyType = config.triggerPropertyType || 'msg'; + node.stateProperty = config.stateProperty || 'topic'; + node.statePropertyType = config.statePropertyType || 'msg'; + + var states = config.states || []; + var transitions = config.transitions || []; + + try { + node.fsm = new StateMachine({ + init: states[0], + transitions: transitions + }); + } catch (e) { + node.status({fill:"red",shape:"dot",text: e.message}); + throw(e); + } + + + node.status({fill:"green",shape:"dot",text: states[0]}); + + if (node.statePropertyType === 'flow') { + node.context().flow.set(node.stateProperty,states[0]); + } else if (node.statePropertyType === 'global') { + node.context().global.set(node.stateProperty,states[0]); + } + + node.startup = function(){ + if (node.statePropertyType === 'msg') { + + msg = {}; + RED.util.setMessageProperty(msg,node.stateProperty,node.fsm.state); + + node.send(msg); + } + } + + RED.events.on("nodes-started", node.startup); + + node.on('close', function() { + RED.events.removeListener("nodes-started", node.startup); + }); + + node.on('input', function(msg) { + + var trigger = RED.util.evaluateNodeProperty(node.triggerProperty, + node.triggerPropertyType,node,msg); + + if (node.fsm.can(trigger)) { + node.fsm[trigger](); + } + + if (node.statePropertyType === 'msg') { + RED.util.setMessageProperty(msg,node.stateProperty,node.fsm.state); + } else if (node.statePropertyType === 'flow') { + node.context().flow.set(node.stateProperty,node.fsm.state); + } else if (node.statePropertyType === 'global') { + node.context().global.set(node.stateProperty,node.fsm.state); + } + + node.status({fill:"green",shape:"dot",text: node.fsm.state}); + + node.send(msg); + + }); + } + RED.nodes.registerType("state-machine",StateMachineNode); +}; +