Skip to content

Read ip

cflurin edited this page Nov 4, 2018 · 6 revisions

The dsm reads the WAN router ip and saves it (sm.data.ip). If the ip changes, the output will show the new ip.

readip

Configuration

{
    "currentState": "idle",
    "states": {
        "idle": {
            "read": "reading"
        },
        "reading": {
            "evaluade": "idle"
        }
    },
    "data": {
        "url": "http://api.ipify.org/",
        "ip": null
    },
    "methods": {
	"init": [
	    "sm.request = require('request');"
	],
        "read": [
            "sm.request.get({url: sm.data.url}, function(error, response, body) {",
            "   if (!error && response.statusCode == 200) {",
            "       sm.data.current_ip = body;",
            "       resume('evaluade', msg);",
            "   } else {",
            "       node.warn(error);",
            "       sm.text = error.message;",
            "       sm.fill = 'red';",
            "   }",
            "});",
            "sm.text = 'reading';",
            "output = false;"
        ],
        "evaluade": [
            "if (sm.data.current_ip !== sm.data.ip) {",
            "   sm.data.ip = sm.data.current_ip;",
            "   msg.payload = sm.data.ip;",
            "   msg.topic = 'WAN IP';",
            "   node.send(msg);",
            "   sm.text = 'new ip='+sm.data.ip;",
            "   sm.fill = 'yellow';",
            "} else {",
            "   sm.text = 'unchanged ip='+sm.data.ip;",
            "   sm.fill = 'green';",
            "}",
            "output = false;"
        ],
        "status": {
            "fill": {
                "get": "sm.fill || 'blue';"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text || 'dummy';"
            }
        }
    }
}

Flow

[{"id":"b57294a9.594a58","type":"dsm","z":"85cbcdb3.ef251","name":"read ip","sm_config":"{\n    \"currentState\": \"idle\",\n    \"states\": {\n        \"idle\": {\n            \"read\": \"reading\"\n        },\n        \"reading\": {\n            \"evaluade\": \"idle\"\n        }\n    },\n    \"data\": {\n        \"url\": \"http://api.ipify.org/\",\n        \"ip\": null\n    },\n    \"methods\": {\n\t\t\"init\": [\n\t\t    \"sm.request = require('request');\"\n\t\t],\n        \"read\": [\n            \"sm.request.get({url: sm.data.url}, function(error, response, body) {\",\n            \"   if (!error && response.statusCode == 200) {\",\n            \"       sm.data.current_ip = body;\",\n            \"       resume('evaluade', msg);\",\n            \"   } else {\",\n            \"       node.warn(error);\",\n            \"       sm.text = error.message;\",\n            \"       sm.fill = 'red';\",\n            \"   }\",\n            \"});\",\n            \"sm.text = 'reading';\",\n            \"output = false;\"\n        ],\n        \"evaluade\": [\n            \"if (sm.data.current_ip !== sm.data.ip) {\",\n            \"   sm.data.ip = sm.data.current_ip;\",\n            \"   msg.payload = sm.data.ip;\",\n            \"   msg.topic = 'WAN IP';\",\n            \"   node.send(msg);\",\n            \"   sm.text = 'new ip='+sm.data.ip;\",\n            \"   sm.fill = 'yellow';\",\n            \"} else {\",\n            \"   sm.text = 'unchanged ip='+sm.data.ip;\",\n            \"   sm.fill = 'green';\",\n            \"}\",\n            \"output = false;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.fill || 'blue';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text || 'dummy';\"\n            }\n        }\n    }\n}\n","x":240,"y":1800,"wires":[["7027ffea.5623a"]]},{"id":"7027ffea.5623a","type":"debug","z":"85cbcdb3.ef251","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":1800,"wires":[]},{"id":"534650cf.6a398","type":"inject","z":"85cbcdb3.ef251","name":"","topic":"read","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1800,"wires":[["b57294a9.594a58"]]}]
Clone this wiki locally