Skip to content

Instantly share code, notes, and snippets.

@wdoganowski
Created September 5, 2014 06:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdoganowski/1d4a770262c1d5c4d359 to your computer and use it in GitHub Desktop.
Save wdoganowski/1d4a770262c1d5c4d359 to your computer and use it in GitHub Desktop.
Loop with start, stop and toggle actions

This is an example of a loop which can be controlled by the msg.payload. Following actions are allowed: "start", "stop" and "toggle". The msg.payload on the output provides current loop state: "started", "stopped", and "loop: n".

The upper output is intended as a trigger for the rest of flow, the bottom output is intended to be looped back to the input through the delay function.

Enjoy.

[{"id":"db51de0f.24ae2","type":"function","name":"loop","func":"// Loop function\n// Top output provides triger for next actions\n// Botton output should be connected to the input through a dealy\n// The msg.payload can consis one of actions: start, stop, toggle\n// Other content is ignored\n// On the outoput the msg.payload contains current loop state\n\ncontext.loop = context.loop || \"stop\";\ncontext.loops = context.loops || 0;\n\n//console.log(\"topic :\" + msg.topic);\n//console.log(\"loop :\" + context.loop);\n//console.log(\"loops :\" + context.loops);\n//console.log(\"action:\" + msg.payload);\n\nswitch (msg.payload) {\n\tcase \"stop\":\n\t\tcontext.loops = context.loops + 1;\n\t\tmsg.payload = \"stopped\";\n\t\tcontext.loop = \"stop\";\n\t\treturn [msg,null];\n\tcase \"toggle\":\n\t\tif (context.loop == \"start\") {\n\t\t\tmsg.payload = \"stopped\";\n\t\t\tcontext.loop = \"stop\";\n\t\t\treturn [msg,null];\n\t\t} else {\n\t\t\tmsg.payload = \"started\";\n\t\t\tcontext.loop = \"loop\";\n\t\t\tcontext.loops = 1;\n\t\t\treturn [msg,msg];\n\t\t}\n\tcase \"start\":\n\t\tmsg.payload = \"started\";\n\t\tcontext.loop = \"loop\";\n\t\tcontext.loops = 1;\n\t\treturn [msg,msg];\n\tdefault:\n\t\tif (context.loop == \"loop\") {\n\t\t\tcontext.loops = context.loops + 1;\n\t\t\tmsg.payload = \"loop:\" + context.loops;\n\t\t\treturn [msg,msg];\n\t\t} else {\n\t\t\treturn [null,null]; \n\t\t}\n}","outputs":"2","x":466,"y":117,"z":"1fe2ab2d.e01d55","wires":[["7a3aabe9.85c554"],["9994e4b6.666b18"]]},{"id":"9994e4b6.666b18","type":"delay","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":472,"y":243,"z":"1fe2ab2d.e01d55","wires":[["db51de0f.24ae2"]]},{"id":"7a3aabe9.85c554","type":"debug","name":"","active":true,"console":"false","complete":"false","x":681,"y":109,"z":"1fe2ab2d.e01d55","wires":[]},{"id":"90cc1395.6f33f","type":"inject","name":"toggle loop 1","topic":"loop1","payload":"toggle","payloadType":"string","repeat":"","crontab":"","once":false,"x":258,"y":116,"z":"1fe2ab2d.e01d55","wires":[["db51de0f.24ae2"]]},{"id":"1c4074bd.e3bf8b","type":"function","name":"loop","func":"// Loop function\n// Top output provides triger for next actions\n// Botton output should be connected to the input through a dealy\n// The msg.payload can consis one of actions: start, stop, toggle\n// Other content is ignored\n// On the outoput the msg.payload contains current loop state\n\ncontext.loop = context.loop || \"stop\";\ncontext.loops = context.loops || 0;\n\n//console.log(\"topic :\" + msg.topic);\n//console.log(\"loop :\" + context.loop);\n//console.log(\"loops :\" + context.loops);\n//console.log(\"action:\" + msg.payload);\n\nswitch (msg.payload) {\n\tcase \"stop\":\n\t\tcontext.loops = context.loops + 1;\n\t\tmsg.payload = \"stopped\";\n\t\tcontext.loop = \"stop\";\n\t\treturn [msg,null];\n\tcase \"toggle\":\n\t\tif (context.loop == \"start\") {\n\t\t\tmsg.payload = \"stopped\";\n\t\t\tcontext.loop = \"stop\";\n\t\t\treturn [msg,null];\n\t\t} else {\n\t\t\tmsg.payload = \"started\";\n\t\t\tcontext.loop = \"loop\";\n\t\t\tcontext.loops = 1;\n\t\t\treturn [msg,msg];\n\t\t}\n\tcase \"start\":\n\t\tmsg.payload = \"started\";\n\t\tcontext.loop = \"loop\";\n\t\tcontext.loops = 1;\n\t\treturn [msg,msg];\n\tdefault:\n\t\tif (context.loop == \"loop\") {\n\t\t\tcontext.loops = context.loops + 1;\n\t\t\tmsg.payload = \"loop:\" + context.loops;\n\t\t\treturn [msg,msg];\n\t\t} else {\n\t\t\treturn [null,null]; \n\t\t}\n}","outputs":"2","x":482,"y":363,"z":"1fe2ab2d.e01d55","wires":[["7b60d417.849f2c"],["a380b8b5.5c7f48"]]},{"id":"a380b8b5.5c7f48","type":"delay","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":482,"y":490,"z":"1fe2ab2d.e01d55","wires":[["1c4074bd.e3bf8b"]]},{"id":"7b60d417.849f2c","type":"debug","name":"","active":true,"console":"false","complete":"false","x":671,"y":358,"z":"1fe2ab2d.e01d55","wires":[]},{"id":"8cf655a0.7309a8","type":"inject","name":"start loop 2","topic":"loop2","payload":"start","payloadType":"string","repeat":"","crontab":"","once":false,"x":260,"y":328,"z":"1fe2ab2d.e01d55","wires":[["1c4074bd.e3bf8b"]]},{"id":"c59c9644.3a6368","type":"inject","name":"stop loop 2","topic":"loop2","payload":"stop","payloadType":"string","repeat":"","crontab":"","once":false,"x":262,"y":404,"z":"1fe2ab2d.e01d55","wires":[["1c4074bd.e3bf8b"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment