Skip to content

Watchdog

cflurin edited this page Feb 27, 2019 · 10 revisions

A Watchdog is a timer that is used to detect and recover from malfunctions. During normal operation, the watchdog is regularly reset to prevent it from elapsing, or timing out. If due to an error the watchdog is not reset, the timer will elapse and a timeout message is generated. [Wikipedia]

Additionally the dsm watchdog will repeat sending the message until the count reaches the limit .

To trigger the watchdog send a message with msg.topic = start.

You can also stop the watchdog at every time sending a message with msg.topic = stop.

watchdog

Settings

    "data": {
        "timeout": 3,
        "limit": 4,
        "count": 0
    },
  • "timeout": time in seconds
  • "limit": max number of messages to be sent.
  • "count": it is used to count the number of messages. Do not change this value.

Note: Please, install the latest dsm version.

Configuration

{
    "triggerInput": "topic",
    "stateOutput": "currentState",
    "currentState": "stopped",
    "states": {
        "stopped": {
            "start": "started"
        },
        "repeat": {
            "stop": "stopped",
            "start": "started",
            "repeat": "repeat"
        },
        "started": {
            "start": "started",
            "stop": "stopped",
            "repeat": "repeat"
        }
    },
    "data": {
        "timeout": 3,
        "limit": 4,
        "count": 0
    },
    "methods": {
        "init": [
            "sta.fill = 'yellow';"
        ],
        "start": [
            "sm.data.count = 0;",
            "sm.fill = 'green';",
            "sm.text = 'ok';",
            "resume('repeat', msg);"
        ],
        "stop": [
            "clearTimeout(timeout.id);",
            "sm.data.count = 0;",
            "sm.fill = 'yellow';",
            "sm.text = 'stopped';"
        ],
        "repeat": [
            "if (sm.data.count <= sm.data.limit) {",
                "clearTimeout(timeout.id);",
                "timeout.id = setTimeout(function() {",
                    "sm.data.count++;",
                    "node.send(msg);",
                    "sm.fill = 'red';",
                    "sm.text = 'timeout ' + sm.data.count + ' of ' + sm.data.limit;",
                    "resume('repeat', msg);",
                "}, sm.data.timeout*1000);",
            "} else {",
                "resume('stop', msg);",            
            "}"
        ],
        "onAfterTransition": [
            "output = false;"
        ],
        "status": {
            "fill": {
                "get": "sm.fill;"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text;"
            }
        }
    }
}

Flow

[{"id":"413e403e.e145e","type":"dsm","z":"7c6a9ec3.f1512","name":"watchdog","sm_config":"{\n    \"triggerInput\": \"topic\",\n    \"stateOutput\": \"currentState\",\n    \"currentState\": \"stopped\",\n    \"states\": {\n        \"stopped\": {\n            \"start\": \"started\"\n        },\n        \"repeat\": {\n            \"stop\": \"stopped\",\n            \"start\": \"started\",\n            \"repeat\": \"repeat\"\n        },\n        \"started\": {\n            \"start\": \"started\",\n            \"stop\": \"stopped\",\n            \"repeat\": \"repeat\"\n        }\n    },\n    \"data\": {\n        \"timeout\": 3,\n        \"limit\": 4,\n        \"count\": 0\n    },\n    \"methods\": {\n        \"init\": [\n            \"sta.fill = 'yellow';\"\n        ],\n        \"start\": [\n            \"sm.data.count = 0;\",\n            \"sm.fill = 'green';\",\n            \"sm.text = 'ok';\",\n            \"resume('repeat', msg);\"\n        ],\n        \"stop\": [\n            \"clearTimeout(timeout.id);\",\n            \"sm.data.count = 0;\",\n            \"sm.fill = 'yellow';\",\n            \"sm.text = 'stopped';\"\n        ],\n        \"repeat\": [\n            \"if (sm.data.count <= sm.data.limit) {\",\n                \"clearTimeout(timeout.id);\",\n                \"timeout.id = setTimeout(function() {\",\n                    \"sm.data.count++;\",\n                    \"node.send(msg);\",\n                    \"sm.fill = 'red';\",\n                    \"sm.text = 'timeout ' + sm.data.count + ' of ' + sm.data.limit;\",\n                    \"resume('repeat', msg);\",\n                \"}, sm.data.timeout*1000);\",\n            \"} else {\",\n                \"resume('stop', msg);\",            \n            \"}\"\n        ],\n        \"onAfterTransition\": [\n            \"output = false;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.fill;\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text;\"\n            }\n        }\n    }\n}\n","x":330,"y":300,"wires":[["b9700ee6.378c8"]]},{"id":"8ab65573.460278","type":"inject","z":"7c6a9ec3.f1512","name":"","topic":"start","payload":"my msg","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":280,"wires":[["413e403e.e145e"]]},{"id":"b9700ee6.378c8","type":"debug","z":"7c6a9ec3.f1512","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":410,"y":240,"wires":[]},{"id":"8d78597d.5ba768","type":"inject","z":"7c6a9ec3.f1512","name":"","topic":"stop","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":320,"wires":[["413e403e.e145e"]]}]
Clone this wiki locally