Skip to content

Interval

cflurin edited this page Oct 8, 2018 · 7 revisions

Features:

  • Start/Stop
  • The interval can be set without redeploying

Note: version 0.12.1 or newer is required.

interval2

Configuration

{
    "stateOutput": "state",
    "currentState": "stopped",
    "states": {
        "stopped": {
            "start": "started"
        },
        "started": {
            "run": "running",
            "stop": "stopped"
        },
        "running": {
            "run": "running",
            "stop": "stopped"
        }
    },
    "data": {
        "interval": 2000
    },
    "methods": {
        "interval": {
            "name": "setData"
        },
        "start":  [
            "if (sm.currentState === 'started') {",
            "   node.send(msg);",
            "   resume('run',msg);",
            "}"
        ],
        "run": [
            "timeout.interval = setTimeout(function() {",
            "   node.send(msg);",
            "   resume('run',msg);",
            "}, sm.data.interval);"
        ],
        "stop": [
            "if (timeout.interval) {",
            "   clearTimeout(timeout.interval);",
            "}"
        ],
        "onAfterTransition": "output = false;",
        "status": {
            "fill": {
                "get": "sm.currentState === 'running' ? 'green' : 'grey'"
            },
            "shape": "dot",
            "text": {
                "get": "sm.currentState === 'running' ? 'interval ' + sm.data.interval : sm.currentState"
            }
        }
    }
}

Flow

[{"id":"f24fc42e.4e66f8","type":"inject","z":"12f1321f.454d1e","name":"start","topic":"start","payload":"my payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":2980,"wires":[["d778099a.5e4988"]]},{"id":"92d37192.ca9dc","type":"debug","z":"12f1321f.454d1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":490,"y":2980,"wires":[]},{"id":"d778099a.5e4988","type":"dsm","z":"12f1321f.454d1e","name":"interval","sm_config":"{\n    \"stateOutput\": \"state\",\n    \"currentState\": \"stopped\",\n    \"states\": {\n        \"stopped\": {\n            \"start\": \"started\"\n        },\n        \"started\": {\n            \"run\": \"running\",\n            \"stop\": \"stopped\"\n        },\n        \"running\": {\n            \"run\": \"running\",\n            \"stop\": \"stopped\"\n        }\n    },\n    \"data\": {\n        \"interval\": 2000\n    },\n    \"methods\": {\n        \"interval\": {\n            \"name\": \"setData\"\n        },\n        \"start\":  [\n            \"if (sm.currentState === 'started') {\",\n            \"   node.send(msg);\",\n            \"   resume('run',msg);\",\n            \"}\"\n        ],\n        \"run\": [\n            \"timeout.interval = setTimeout(function() {\",\n            \"   node.send(msg);\",\n            \"   resume('run',msg);\",\n            \"}, sm.data.interval);\"\n        ],\n        \"stop\": [\n            \"if (timeout.interval) {\",\n            \"   clearTimeout(timeout.interval);\",\n            \"}\"\n        ],\n        \"onAfterTransition\": \"output = false;\",\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'running' ? 'green' : 'grey'\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.currentState === 'running' ? 'interval ' + sm.data.interval : sm.currentState\"\n            }\n        }\n    }\n}","x":320,"y":2980,"wires":[["92d37192.ca9dc"]]},{"id":"8a912ca9.9cdf6","type":"inject","z":"12f1321f.454d1e","name":"stop","topic":"stop","payload":"stopped","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3040,"wires":[["d778099a.5e4988"]]},{"id":"77f73c92.73d814","type":"inject","z":"12f1321f.454d1e","name":"","topic":"interval","payload":"5000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":2920,"wires":[["d778099a.5e4988"]]},{"id":"2a22c043.38f95","type":"inject","z":"12f1321f.454d1e","name":"","topic":"interval","payload":"1000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":2880,"wires":[["d778099a.5e4988"]]}]