Skip to content

Instantly share code, notes, and snippets.

@getlarge
Created November 13, 2019 19:56
Show Gist options
  • Save getlarge/ef6d683ae7c5b2de4f2117a761560860 to your computer and use it in GitHub Desktop.
Save getlarge/ef6d683ae7c5b2de4f2117a761560860 to your computer and use it in GitHub Desktop.
node-red-device/snips-agent
[
{
"id": "e6f7a943.65973",
"type": "subflow",
"name": "set-text",
"info": "",
"category": "function",
"in": [
{
"x": 160,
"y": 160,
"wires": [
{
"id": "e05d5089.40be68"
}
]
}
],
"out": [
{
"x": 440,
"y": 160,
"wires": [
{
"id": "e05d5089.40be68",
"port": 0
}
]
}
],
"env": [
{
"name": "resource_id",
"type": "str",
"value": ""
},
{
"name": "debug",
"type": "bool",
"value": "false"
}
],
"icon": "font-awesome/fa-check"
},
{
"id": "e05d5089.40be68",
"type": "function",
"z": "e6f7a943.65973",
"name": "setText",
"func": "try {\n const debug = env.get(\"debug\");\n if (!msg || msg === null || !msg.payload) throw new Error(\"No input to parse\");\n const resourceId = env.get(\"resource_id\");\n if (!resourceId) throw new Error(\"no resource id set\");\n const value = msg.payload;\n if (!value) throw new Error(\"no sensor value found\");\n if (debug) {\n console.log(`get-text for ${resourceId} :`, value);\n }\n if (typeof value === \"string\") {\n msg.payload = value; \n } else if (typeof value === \"number\") {\n msg.payload = value.toString();\n } else if (typeof value === \"boolean\") {\n msg.payload = value.toString();\n } else if (typeof value === \"object\" && value.type && value.data){\n const bufferOriginal = Buffer.from(value.data);\n msg.payload = bufferOriginal.toString('utf-8');\n // msg.payload = JSON.parse(msg.payload);\n } else if (Buffer.isBuffer(value)){\n msg.payload = value.toString('utf-8');\n } else if (value instanceof Array) {\n msg.payload = Buffer.from(value).toString('utf-8');\n } else {\n throw new Error(\"No value found\");\n }\n if (debug) {\n console.log(`set-text for ${resourceId} : ${msg.payload}`);\n }\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 310,
"y": 160,
"wires": [
[]
]
},
{
"id": "863585af.407078",
"type": "subflow",
"name": "set-boolean",
"info": "",
"category": "function",
"in": [
{
"x": 380,
"y": 120,
"wires": [
{
"id": "5d0a29bc.ba8cf"
}
]
}
],
"out": [
{
"x": 740,
"y": 120,
"wires": [
{
"id": "5d0a29bc.ba8cf",
"port": 0
}
]
}
],
"env": [
{
"name": "resource_id",
"type": "str",
"value": ""
},
{
"name": "debug",
"type": "bool",
"value": "false"
}
],
"icon": "font-awesome/fa-check"
},
{
"id": "5d0a29bc.ba8cf",
"type": "function",
"z": "863585af.407078",
"name": "setBoolean",
"func": "try {\n const debug = env.get(\"debug\");\n if (!msg || msg === null || !msg.payload) throw new Error(\"No input to parse\");\n const resourceId = env.get(\"resource_id\");\n if (!resourceId) throw new Error(\"no resource id set\");\n const value = msg.payload;\n if (debug) {\n console.log(`get-boolean for ${resourceId} : ${typeof value}`);\n }\n if (typeof value === \"string\") {\n msg.payload = value; \n } else if (typeof value === \"number\") {\n msg.payload = value.toString(); \n } else if (typeof value === \"boolean\") {\n msg.payload = value; \n } else if (typeof value === \"object\" && value.type && value.data){\n // msg.payload = value.data.toString('utf-8');\n msg.payload = Buffer.from(value.data).toString('utf-8');\n } else if (Buffer.isBuffer(value)){\n msg.payload = value.toString('utf-8');\n } else if (value instanceof Array) {\n msg.payload = Buffer.from(value).toString('utf-8');\n } else {\n throw new Error(\"No value found\");\n }\n if (msg.payload === \"true\" || msg.payload === \"1\" || msg.payload === 1) {\n msg.payload = true;\n } else if (msg.payload === \"false\" || msg.payload === \"0\" || msg.payload === 0) {\n msg.payload = false;\n }\n if (debug) {\n console.log(`set-boolean for ${resourceId} : ${msg.payload}`);\n }\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 570,
"y": 120,
"wires": [
[]
]
},
{
"id": "ef1d2cf3.7621f8",
"type": "subflow",
"name": "set-float",
"info": "",
"category": "function",
"in": [
{
"x": 180,
"y": 120,
"wires": [
{
"id": "ae1344be.b0d56"
}
]
}
],
"out": [
{
"x": 440,
"y": 120,
"wires": [
{
"id": "ae1344be.b0d56",
"port": 0
}
]
}
],
"env": [
{
"name": "resource_id",
"type": "str",
"value": ""
},
{
"name": "precision",
"type": "num",
"value": "0"
},
{
"name": "unit",
"type": "str",
"value": ""
},
{
"name": "debug",
"type": "bool",
"value": "false"
}
],
"icon": "font-awesome/fa-check"
},
{
"id": "ae1344be.b0d56",
"type": "function",
"z": "ef1d2cf3.7621f8",
"name": "setFloat",
"func": "try {\n const debug = env.get(\"debug\");\n if (!msg || msg === null || !msg.payload) throw new Error(\"No input to parse\");\n const resourceId = env.get(\"resource_id\");\n if (!resourceId) throw new Error(\"no resource id set\")\n const value = msg.payload;\n if (value === undefined || value === null) throw new Error(\"no sensor value found\")\n if (debug) {\n console.log(`get-float for ${resourceId} :`, value)\n }\n if (typeof value === \"string\") {\n if (value === \"1\" || value === \"true\") {\n msg.payload = 1;\n } else if (value === \"0\" || value === \"false\") {\n msg.payload = 0;\n } else {\n msg.payload = Number(value); \n }\n } else if (typeof value === \"number\") {\n msg.payload = value;\n } else if (typeof value === \"boolean\") {\n if (value === true) {\n msg.payload = 1;\n } else if (value === false) {\n msg.payload = 0;\n } \n } else if (typeof value === \"object\" && value.type && value.data){\n msg.payload = Number(value.data.toString('utf-8'));\n } else if (Buffer.isBuffer(value)){\n msg.payload = Number(value.toString('utf-8'));\n } else if (value instanceof Array) {\n msg.payload = Buffer.from(value).toString('utf-8');\n } else throw new Error(\"no valid payload to parse\");\n \n if (msg.payload === undefined) throw new Error(\"no payload\");\n const precision = env.get(\"precision\") || msg.sensor.resources[\"5701\"] || null;\n const unit = env.get(\"unit\");\n if (unit && unit !== null) {\n msg.unit = unit;\n }\n //msg.payload = Number(msg.payload);\n //if (precision && precision !== null) {\n // msg.payload = msg.payload.toPrecision(precision)\n //}\n if (debug) {\n console.log(`set-float for ${resourceId} : ${msg.payload}`)\n }\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 330,
"y": 120,
"wires": [
[]
]
},
{
"id": "b2ff20a7.5182d8",
"type": "tab",
"label": "Device flow",
"disabled": false,
"info": ""
},
{
"id": "61fc3f06.512d38",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Sensors presentations",
"info": "## Protocol\n\n+prefixedDevEui = devEui+inPrefix\n+method = 0 (\"HEAD\") | 1 (\"POST\") | 2 (\"GET\") | 3 (\"Internal\") | 4 (\"STREAM\")\n\n+prefixedDevEui/+method/+omaObjectId/+nativeSensorId/omaResourceId",
"x": 180,
"y": 680,
"wires": []
},
{
"id": "f4680cca.062e68",
"type": "inject",
"z": "b2ff20a7.5182d8",
"name": "HEAD/3306/1/5850",
"topic": "0/3306/1/5850",
"payload": "Digital_input",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1",
"x": 200,
"y": 760,
"wires": [
[
"eca508b3.4063c"
]
]
},
{
"id": "13966d25.5274a3",
"type": "inject",
"z": "b2ff20a7.5182d8",
"name": "HEAD/3306/2/5850",
"topic": "0/3306/2/5850",
"payload": "Digital_input",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1.1",
"x": 200,
"y": 800,
"wires": [
[
"eca508b3.4063c"
]
]
},
{
"id": "2037b2c3.effb26",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Interfaces",
"info": "",
"x": 140,
"y": 1080,
"wires": []
},
{
"id": "eca508b3.4063c",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "audio-player-out-presentations",
"links": [
"30b4fe3f.48f18a",
"c1119ede.9f9ad"
],
"x": 515,
"y": 760,
"wires": []
},
{
"id": "a5d02d46.c6b15",
"type": "ui_switch",
"z": "b2ff20a7.5182d8",
"name": "POST/3306/1/5850",
"label": "switch",
"tooltip": "",
"group": "9e660515.a8a9f",
"order": 2,
"width": 0,
"height": 0,
"passthru": false,
"decouple": "false",
"topic": "1/3306/1/5850",
"style": "",
"onvalue": "true",
"onvalueType": "bool",
"onicon": "",
"oncolor": "",
"offvalue": "false",
"offvalueType": "bool",
"officon": "",
"offcolor": "",
"x": 450,
"y": 1340,
"wires": [
[
"9f900528.16a108"
]
]
},
{
"id": "86558c32.a3c01",
"type": "ui_slider",
"z": "b2ff20a7.5182d8",
"name": "POST/3306/1/5851",
"label": "slider",
"tooltip": "update 3306/1/5851",
"group": "9e660515.a8a9f",
"order": 3,
"width": 0,
"height": 0,
"passthru": false,
"outs": "end",
"topic": "1/3306/1/5851",
"min": 0,
"max": "100",
"step": 1,
"x": 450,
"y": 1400,
"wires": [
[
"9f900528.16a108"
]
]
},
{
"id": "3fa65441.5e8024",
"type": "link in",
"z": "b2ff20a7.5182d8",
"name": "SWITCH/1/3306/1/5850",
"links": [
"e3ecbc29.f06558",
"ada10eb5.a3fc2"
],
"x": 115,
"y": 1340,
"wires": [
[
"a5d02d46.c6b15"
]
]
},
{
"id": "9f900528.16a108",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "interfaces->",
"links": [
"30b4fe3f.48f18a",
"c1119ede.9f9ad"
],
"x": 705,
"y": 1380,
"wires": []
},
{
"id": "8166cee7.57642",
"type": "link in",
"z": "b2ff20a7.5182d8",
"name": "SLIDER/1/3306/1/5851",
"links": [
"ade007f6.03d3f8",
"e69589f0.2ab448",
"85f8141b.44bb08"
],
"x": 115,
"y": 1400,
"wires": [
[
"6446e01a.588b2"
]
]
},
{
"id": "4d02541a.1ebe0c",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "3306-resourceId",
"property": "parts[4]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "5850",
"vt": "num"
},
{
"t": "eq",
"v": "5851",
"vt": "num"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 710,
"y": 180,
"wires": [
[
"6143d3e4.e79c94"
],
[
"38f4d2d3.4d45e6"
]
]
},
{
"id": "3ee2e86f.e9729",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "Method",
"property": "parts[1]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "eq",
"v": "2",
"vt": "str"
},
{
"t": "eq",
"v": "3",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 280,
"y": 200,
"wires": [
[
"e96f2c7d.64c86"
],
[],
[]
]
},
{
"id": "e96f2c7d.64c86",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "POST-objectId",
"property": "parts[2]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "3306",
"vt": "str"
},
{
"t": "eq",
"v": "3339",
"vt": "str"
},
{
"t": "eq",
"v": "3341",
"vt": "str"
},
{
"t": "eq",
"v": "3349",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 4,
"x": 480,
"y": 200,
"wires": [
[
"4d02541a.1ebe0c"
],
[],
[
"357c7d5.8f7b702"
],
[]
]
},
{
"id": "6143d3e4.e79c94",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "5850-sensorId",
"property": "parts[3]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "eq",
"v": "2",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 960,
"y": 180,
"wires": [
[
"30ae4e84.96499a"
],
[
"c2f2eb1b.cd8dd"
],
[]
]
},
{
"id": "38f4d2d3.4d45e6",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "5851-sensorId",
"property": "parts[3]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "eq",
"v": "2",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 960,
"y": 320,
"wires": [
[
"e69589f0.2ab448"
],
[
"f66ca9b8.acf268"
],
[]
]
},
{
"id": "83ddc4f9.58fae",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/2/5850->",
"links": [],
"x": 1275,
"y": 180,
"wires": []
},
{
"id": "f66ca9b8.acf268",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/2/5851->",
"links": [],
"x": 1215,
"y": 320,
"wires": []
},
{
"id": "e69589f0.2ab448",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/1/5851->",
"links": [
"8166cee7.57642"
],
"x": 1215,
"y": 280,
"wires": []
},
{
"id": "e3ecbc29.f06558",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/1/5850->",
"links": [
"3fa65441.5e8024",
"feb214a3.57fff"
],
"x": 1275,
"y": 140,
"wires": []
},
{
"id": "13a68248.eaa156",
"type": "inject",
"z": "b2ff20a7.5182d8",
"name": "HEAD/3341/4/5527",
"topic": "0/3341/4/5527",
"payload": "Text_input",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1",
"x": 200,
"y": 880,
"wires": [
[
"eca508b3.4063c"
]
]
},
{
"id": "357c7d5.8f7b702",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "3341-resourceId",
"property": "parts[4]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "5527",
"vt": "str"
},
{
"t": "eq",
"v": "5528",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 710,
"y": 460,
"wires": [
[
"d40bc075.e388e"
],
[]
]
},
{
"id": "d40bc075.e388e",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "5527-sensorId",
"property": "parts[3]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "3",
"vt": "str"
},
{
"t": "eq",
"v": "4",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 960,
"y": 480,
"wires": [
[
"d425c764.ea47a"
],
[
"ebb36884.b74918"
]
]
},
{
"id": "353cea05.bafed6",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "/1/3341/4/5527->",
"links": [
"2c47d2d9.f30fbe",
"98b93f1a.759b38"
],
"x": 1275,
"y": 500,
"wires": []
},
{
"id": "99e450b5.c7712",
"type": "ui_template",
"z": "b2ff20a7.5182d8",
"group": "9e660515.a8a9f",
"name": "device-status",
"order": 1,
"width": "10",
"height": "1",
"format": "<!DOCTYPE html>\n<svg viewBox=\"0 0 250 40\" id=\"{{'devices-status-'+ $id}}\">\n <text x=\"0\" y=\"25\" style=\"{{'font-weight:700'}}\" >{{$deviceName}}</text>\n <circle id=\"{{'devices-status-icon-'+$id}}\" x=\"0\" y=\"0\" cx=\"250\" cy=\"20\" r=\"15\" fill=\"{{$statusColor}}\" ></circle>\n</svg>\n<script>\n(function(scope) {\n scope.$watch('msg', (msg) => {\n try{\n scope.$statusText = \"Waiting\";\n scope.$deviceName = \"\";\n scope.$statusColor = \"#ff4500\";\n if (msg && msg.device) {\n scope.$deviceName = msg.device.name;\n if (msg && (msg.device.status === true || msg.device.status === \"true\")) {\n scope.$statusColor = \"#baff00\";\n scope.$statusText = \"Ready\";\n }\n }\n return msg;\n } catch(error){\n return error;\n }\n });\n})(scope);\n</script>",
"storeOutMessages": true,
"fwdInMessages": true,
"templateScope": "local",
"x": 580,
"y": 1200,
"wires": [
[]
]
},
{
"id": "65affc40.024d6c",
"type": "status",
"z": "b2ff20a7.5182d8",
"name": "device-rx status",
"scope": [
"633b4043.f2982",
"13ad05b4.c80fe2"
],
"x": 180,
"y": 1200,
"wires": [
[
"32e8012f.8582e6"
]
]
},
{
"id": "32e8012f.8582e6",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "parseStatus",
"func": "try {\n let status = false;\n msg.device = {};\n msg.device.name = env.get(\"DEVICE_NAME\");\n if (msg.status && msg.status.text && msg.status.text.startsWith(\"node-red:common.status.\")) {\n if (msg.status.text.endsWith(\"connected\")) {\n msg.device.status = true;\n } else if (msg.status.text.endsWith(\"disconnected\")) {\n msg.device.status = false;\n }\n }\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 390,
"y": 1200,
"wires": [
[
"99e450b5.c7712"
]
]
},
{
"id": "58666778.c0731",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "MQTT IN",
"info": "## Protocol\n\n+prefixedDevEui = devEui+inPrefix\n+method = 0 (\"HEAD\") | 1 (\"POST\") | 2 (\"GET\") | 3 (\"Internal\") | 4 (\"STREAM\")\n\n+prefixedDevEui/+method/+omaObjectId/+nativeSensorId/omaResourceId",
"x": 140,
"y": 120,
"wires": []
},
{
"id": "6fe80635.1279a8",
"type": "inject",
"z": "b2ff20a7.5182d8",
"name": "refreshState",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "120",
"crontab": "",
"once": true,
"onceDelay": "1.5",
"x": 780,
"y": 760,
"wires": [
[
"5f1445ff.06d1e4",
"cf4e7c48.bdd238",
"81b2b97f.0f98a8"
]
]
},
{
"id": "5f1445ff.06d1e4",
"type": "change",
"z": "b2ff20a7.5182d8",
"name": "sensor-1-5850",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "sensor-1.resources[\"5850\"]",
"tot": "global"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1020,
"y": 760,
"wires": [
[
"ada10eb5.a3fc2"
]
]
},
{
"id": "b9cba7e.6c64158",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Sensors State",
"info": "",
"x": 750,
"y": 680,
"wires": []
},
{
"id": "ada10eb5.a3fc2",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/1/5850",
"links": [
"3fa65441.5e8024"
],
"x": 1155,
"y": 760,
"wires": []
},
{
"id": "cf4e7c48.bdd238",
"type": "change",
"z": "b2ff20a7.5182d8",
"name": "sensor-2-5850",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "sensor-2.resources[\"5850\"]",
"tot": "global"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1020,
"y": 840,
"wires": [
[]
]
},
{
"id": "81b2b97f.0f98a8",
"type": "change",
"z": "b2ff20a7.5182d8",
"name": "sensor-1-5851",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "sensor-1.resources[\"5851\"]",
"tot": "global"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1020,
"y": 800,
"wires": [
[
"85f8141b.44bb08"
]
]
},
{
"id": "85f8141b.44bb08",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "1/3306/1/5851",
"links": [
"8166cee7.57642"
],
"x": 1155,
"y": 800,
"wires": []
},
{
"id": "4ea53fcc.1f567",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "DEVICE",
"info": "",
"x": 160,
"y": 1140,
"wires": []
},
{
"id": "1cdb1dea.17ac32",
"type": "change",
"z": "b2ff20a7.5182d8",
"name": "sensor-3-5527",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "sensor-3.resources[\"5527\"]",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "HEAD/3341/3/5527",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1020,
"y": 880,
"wires": [
[
"3c95ce76.b5c4f2"
]
]
},
{
"id": "3c95ce76.b5c4f2",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "4/3341/4/5527",
"links": [
"2c47d2d9.f30fbe"
],
"x": 1155,
"y": 880,
"wires": []
},
{
"id": "2302fccd.8295e4",
"type": "link in",
"z": "b2ff20a7.5182d8",
"name": "->refresh-state",
"links": [
"a148b4ca.58c968"
],
"x": 695,
"y": 840,
"wires": [
[
"5f1445ff.06d1e4",
"81b2b97f.0f98a8",
"cf4e7c48.bdd238"
]
]
},
{
"id": "6446e01a.588b2",
"type": "subflow:ef1d2cf3.7621f8",
"z": "b2ff20a7.5182d8",
"name": "",
"env": [
{
"name": "resource_id",
"type": "str",
"value": "5851"
},
{
"name": "unit",
"type": "str",
"value": "%"
}
],
"x": 260,
"y": 1400,
"wires": [
[
"86558c32.a3c01"
]
]
},
{
"id": "30ae4e84.96499a",
"type": "subflow:863585af.407078",
"z": "b2ff20a7.5182d8",
"name": "",
"env": [
{
"name": "resource_id",
"type": "str",
"value": "5850"
}
],
"x": 1170,
"y": 140,
"wires": [
[
"e3ecbc29.f06558"
]
]
},
{
"id": "ebb36884.b74918",
"type": "subflow:e6f7a943.65973",
"z": "b2ff20a7.5182d8",
"name": "",
"env": [
{
"name": "resource_id",
"type": "str",
"value": "5527"
}
],
"x": 1160,
"y": 500,
"wires": [
[
"353cea05.bafed6"
]
]
},
{
"id": "c2f2eb1b.cd8dd",
"type": "subflow:863585af.407078",
"z": "b2ff20a7.5182d8",
"name": "",
"env": [
{
"name": "resource_id",
"type": "str",
"value": "5850"
}
],
"x": 1170,
"y": 180,
"wires": [
[
"83ddc4f9.58fae"
]
]
},
{
"id": "16761718.b4a679",
"type": "inject",
"z": "b2ff20a7.5182d8",
"name": "HEAD/3341/3/5527",
"topic": "0/3341/3/5527",
"payload": "Text_output",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1",
"x": 200,
"y": 840,
"wires": [
[
"eca508b3.4063c"
]
]
},
{
"id": "29f203c3.9efb44",
"type": "mqtt in",
"z": "b2ff20a7.5182d8",
"name": "",
"topic": "hermes/#",
"qos": "0",
"datatype": "auto",
"broker": "acad220b.65b9e8",
"x": 160,
"y": 1660,
"wires": [
[
"d3935088.ecba1"
]
]
},
{
"id": "c0b1d8d2.7b07b8",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "-> Snips MQTT client",
"info": "",
"x": 200,
"y": 1580,
"wires": []
},
{
"id": "1a975680.2a94ca",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "",
"property": "parts[1]",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "asr",
"vt": "str"
},
{
"t": "eq",
"v": "hotword",
"vt": "str"
},
{
"t": "eq",
"v": "dialogueManager",
"vt": "str"
},
{
"t": "eq",
"v": "intent",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 4,
"x": 450,
"y": 1660,
"wires": [
[
"23a02e95.a54232"
],
[
"639a5155.47c0c"
],
[
"d256ec9b.a477a8"
],
[
"ecfd4d44.dc0248"
]
]
},
{
"id": "639a5155.47c0c",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "hotword-detected",
"func": "try {\n console.log(\"hotwordDetected\");\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 690,
"y": 1660,
"wires": [
[]
]
},
{
"id": "23a02e95.a54232",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "listening-state",
"func": "try {\n if (msg.command) {\n if (msg.command === \"startListening\") {\n console.log(\"startListening\");\n } else if (msg.command === \"stopListening\") {\n console.log(\"stopListening\");\n }\n }\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 680,
"y": 1620,
"wires": [
[]
]
},
{
"id": "4806ba01.f2a31c",
"type": "switch",
"z": "b2ff20a7.5182d8",
"name": "",
"property": "intent.intentName",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "getlarge:getInstanceByName",
"vt": "str"
},
{
"t": "eq",
"v": "getlarge:postToInstanceByName",
"vt": "str"
},
{
"t": "eq",
"v": "getlarge:specifyDeviceName",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 4,
"x": 390,
"y": 1920,
"wires": [
[
"231ca419.d824d4"
],
[
"849bab27.efa89"
],
[
"8940b8a6.371de8"
],
[]
]
},
{
"id": "d3935088.ecba1",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "split",
"func": "try {\n msg.parts = msg.topic.split(\"/\");\n if (msg.parts[1] === \"audioServer\") return null;\n msg.module = msg.parts[1];\n msg.command = msg.parts[2];\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 310,
"y": 1660,
"wires": [
[
"1a975680.2a94ca"
]
]
},
{
"id": "ecfd4d44.dc0248",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "intent-detected",
"func": "try {\n msg.payload = JSON.parse(msg.payload);\n msg.intent = msg.payload.intent;\n console.log(\"intentDetected\", msg.intent);\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 680,
"y": 1740,
"wires": [
[
"42344eb9.cf3ec"
]
]
},
{
"id": "231ca419.d824d4",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "get-instance-by-name",
"func": "try {\n if (msg.slots) {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\") || \"inMemory\";\n const payload = msg.slots;\n if (!payload.method || payload.method.toLowerCase() !== \"get\") return null;\n payload.intent = msg.intent.intentName;\n // console.log(\"payload\", payload);\n if (payload.deviceName) {\n if (payload.deviceUserId) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserId}`;\n delete payload.deviceUserId;\n }\n if (payload.deviceUserCount) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserCount}`;\n delete payload.deviceUserCount;\n }\n }\n \n if (payload.sensorName) {\n if (payload.sensorUserId) {\n payload.nativeSensorId = `${payload.sensorUserId}`;\n delete payload.sensorUserId;\n }\n if (payload.sensorUserCount) {\n payload.nativeSensorId = `${payload.sensorUserCount}`;\n delete payload.sensorUserCount;\n } \n }\n \n if (!payload.deviceName) {\n if (payload.sensorName) {\n global.set(\"pendingRequest\", payload, storeType, err => {\n if (err) throw err;\n });\n msg.topic = \"hermes/dialogueManager/continueSession\";\n const randomTexts = [\n \"Please mention which device is concerned\", \n \"For which device are you doing this request ?\", \n \"Could you tell me the device name ?\"\n ];\n // const intentFilter = [\"getlarge:deviceElicitation\"];\n const intentFilter = [\"getlarge:specifyDeviceName\"];\n const text = randomTexts[Math.floor(Math.random() * randomTexts.length)];\n const question = {sessionId: msg.payload.sessionId, siteId: msg.payload.siteId, text, intentFilter };\n msg.payload = JSON.stringify(question);\n return [null,msg];\n }\n return [null,null];\n }\n // topic = `${method.toUpperCase()}/`\n msg.topic = \"1/3341/4/5527\";\n msg.payload = JSON.stringify(payload);\n return [msg,null];\n }\n return [null,null];\n} catch(error) {\n return error;\n}",
"outputs": 2,
"noerr": 0,
"x": 640,
"y": 1880,
"wires": [
[
"91be23ae.53ef18",
"fa37bf1e.d30998"
],
[
"3cd89d6d.a919ca",
"2c5a960f.27ca2a"
]
]
},
{
"id": "d5305632.b41608",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "send-notification",
"func": "try {\n msg.topic = \"hermes/dialogueManager/startSession\";\n // const action = {siteId: \"default\", init: {type: \"action\", text: \"For which device ?\", canBeEnqueued: true, intentFilter: [\"getInstanceByName\"]}, customData: \"\"};\n const notification = {init: {type: \"notification\", text: \"everything will be allright\"}};\n msg.payload = JSON.stringify(notification);\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 470,
"y": 2240,
"wires": [
[
"bdec4fde.2e3e98"
]
]
},
{
"id": "bdec4fde.2e3e98",
"type": "mqtt out",
"z": "b2ff20a7.5182d8",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "acad220b.65b9e8",
"x": 810,
"y": 2240,
"wires": []
},
{
"id": "fa37bf1e.d30998",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "bot-request->",
"links": [
"30b4fe3f.48f18a",
"c1119ede.9f9ad"
],
"x": 1055,
"y": 1860,
"wires": []
},
{
"id": "91be23ae.53ef18",
"type": "debug",
"z": "b2ff20a7.5182d8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 690,
"y": 1820,
"wires": []
},
{
"id": "d256ec9b.a477a8",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "dialogue-state",
"func": "try {\n if (msg.command) {\n if (msg.command === \"sessionStarted\") {\n console.log(\"sessionStarted\");\n } else if (msg.command === \"sessionEnded\") {\n console.log(\"sessionEnded\");\n }\n }\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 680,
"y": 1700,
"wires": [
[]
]
},
{
"id": "8940b8a6.371de8",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "specify-device-name",
"func": "try {\n if (msg.slots) {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\") || \"inMemory\";\n const payload = msg.slots;\n if (!payload.deviceName) {\n // if sensor data, save them to reuse after response\n msg.topic = \"hermes/dialogueManager/endSession\";\n const randomTexts = [\n \"Request cancelled\", \n \"Sorry i can't answer your request\", \n \"Please try again\"\n ];\n const text = randomTexts[Math.floor(Math.random() * randomTexts.length)];\n const message = {sessionId: msg.payload.sessionId, siteId: msg.payload.siteId, text};\n msg.payload = JSON.stringify(message);\n return [null, msg];\n }\n // restore payload from previous request\n let savedPayload;\n global.get(\"pendingRequest\", storeType, (err, res) => {\n if (err) throw err;\n savedPayload = res;\n });\n if (payload.deviceUserId) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserId}`;\n // delete payload.deviceUserId;\n }\n if (payload.deviceUserCount) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserCount}`;\n // delete payload.deviceUserCount;\n }\n savedPayload.deviceName = payload.deviceName;\n savedPayload.deviceType = payload.deviceType;\n msg.topic = \"1/3341/4/5527\";\n msg.payload = JSON.stringify(savedPayload);\n console.log(\"specified device name\", savedPayload);\n return [msg, null];\n }\n return [null,null];\n} catch(error) {\n return error;\n}",
"outputs": 2,
"noerr": 0,
"x": 640,
"y": 1960,
"wires": [
[
"fa37bf1e.d30998",
"91be23ae.53ef18"
],
[
"2c5a960f.27ca2a",
"3cd89d6d.a919ca"
]
]
},
{
"id": "2c5a960f.27ca2a",
"type": "debug",
"z": "b2ff20a7.5182d8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 670,
"y": 2020,
"wires": []
},
{
"id": "f6ab2b0a.332c28",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Aloes MQTT client ->",
"info": "",
"x": 1140,
"y": 1820,
"wires": []
},
{
"id": "42344eb9.cf3ec",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "parse-slots",
"func": "try {\n if (msg.payload.slots) {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\") || \"inMemory\";\n const slots = msg.payload.slots;\n const payload = {};\n slots.forEach(slot => {\n const slotName = slot.slotName;\n switch(slotName) {\n case \"deviceName\" : \n payload.deviceType = slot.value.value;\n payload[slotName] = slot.rawValue;\n break;\n case \"deviceUserId\" : \n payload[slotName] = slot.value.value;\n break;\n case \"deviceUserCount\" : \n payload[slotName] = slot.value.value;\n break;\n case \"sensorName\" : \n // search in OMA tables to also define object Id and resource Id ?\n payload[slotName] = slot.value.value;\n break;\n case \"sensorUserId\" : \n payload[slotName] = slot.value.value;\n break;\n case \"sensorUserCount\" : \n payload[slotName] = slot.value.value;\n break;\n case \"method\" : \n payload[slotName] = slot.value.value.toUpperCase();\n // payload.method = slot.value.value.toUpperCase();\n break; \n case \"collection\" : \n payload[slotName] = slot.value.value;\n break; \n case \"booleanValue\" : \n payload[slotName] = slot.value.value;\n break; \n case \"floatValue\" : \n payload[slotName] = slot.value.value;\n break; \n case \"colorValue\" : \n payload[slotName] = slot.value.value;\n break; \n }\n })\n msg.slots = payload;\n return msg;\n }\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 210,
"y": 1920,
"wires": [
[
"4806ba01.f2a31c"
]
]
},
{
"id": "849bab27.efa89",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "post-to-instance-by-name",
"func": "try {\n if (msg.slots) {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\") || \"inMemory\";\n const payload = msg.slots;\n if (!payload.method || payload.method.toLowerCase() !== \"post\") return null;\n \n // check if method is an event ( start, stop, close, open );\n // if no event find booleanValue - floatValue or colorValue\n \n console.log(\"payload\", payload)\n if (payload.deviceName) {\n if (payload.deviceUserId) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserId}`;\n delete payload.deviceUserId;\n }\n if (payload.deviceUserCount) {\n payload.deviceName = `${payload.deviceName}-${payload.deviceUserCount}`;\n delete payload.deviceUserCount;\n }\n }\n \n if (payload.sensorName) {\n if (payload.sensorUserId) {\n payload.sensorName = `${payload.sensorName}-${payload.sensorUserId}`;\n delete payload.sensorUserId;\n }\n if (payload.sensorUserCount) {\n payload.sensorName = `${payload.sensorName}-${payload.sensorUserCount}`;\n delete payload.sensorUserCount;\n } \n }\n \n if (!payload.deviceName) {\n if (payload.sensorName) {\n global.set(\"pendingRequest\", payload, storeType, err => {\n if (err) throw err;\n });\n msg.topic = \"hermes/dialogueManager/continueSession\";\n const randomTexts = [\n \"Please mention which device is concerned\", \n \"For which device are you doing this request ?\", \n \"Could you tell me the device name ?\"\n ];\n const intentFilter = [\"getlarge:specifyDeviceName\"];\n const text = randomTexts[Math.floor(Math.random() * randomTexts.length)];\n const question = {sessionId: msg.payload.sessionId, siteId: msg.payload.siteId, text, intentFilter };\n msg.payload = JSON.stringify(question);\n return [null,msg];\n }\n return [null,null];\n }\n msg.topic = \"1/3341/4/5527\";\n msg.payload = JSON.stringify(payload);\n return [msg,null];\n }\n return [null,null];\n} catch(error) {\n return error;\n}",
"outputs": 2,
"noerr": 0,
"x": 650,
"y": 1920,
"wires": [
[
"fa37bf1e.d30998",
"91be23ae.53ef18"
],
[
"3cd89d6d.a919ca",
"2c5a960f.27ca2a"
]
]
},
{
"id": "d425c764.ea47a",
"type": "subflow:e6f7a943.65973",
"z": "b2ff20a7.5182d8",
"name": "",
"env": [
{
"name": "resource_id",
"type": "str",
"value": "5527"
}
],
"x": 1160,
"y": 460,
"wires": [
[
"c5cd5326.72bf3"
]
]
},
{
"id": "c5cd5326.72bf3",
"type": "link out",
"z": "b2ff20a7.5182d8",
"name": "/1/3341/3/5527->",
"links": [
"41b27925.61a3c8"
],
"x": 1275,
"y": 460,
"wires": []
},
{
"id": "41b27925.61a3c8",
"type": "link in",
"z": "b2ff20a7.5182d8",
"name": "TEXT/1/3341/3/5527",
"links": [
"c5cd5326.72bf3"
],
"x": 115,
"y": 2240,
"wires": [
[]
]
},
{
"id": "699f5524.11c214",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "-> Aloes MQTT client",
"info": "",
"x": 200,
"y": 2180,
"wires": []
},
{
"id": "6c664728.aa882",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Snips MQTT client ->",
"info": "",
"x": 1140,
"y": 1900,
"wires": []
},
{
"id": "3cd89d6d.a919ca",
"type": "mqtt out",
"z": "b2ff20a7.5182d8",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "acad220b.65b9e8",
"x": 1090,
"y": 1940,
"wires": []
},
{
"id": "50d7d06a.ad2828",
"type": "change",
"z": "b2ff20a7.5182d8",
"name": "sensor-4-5527",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "sensor-4.resources[\"5527\"]",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "HEAD/3341/4/5527",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1020,
"y": 920,
"wires": [
[]
]
},
{
"id": "e5873963.655f28",
"type": "comment",
"z": "b2ff20a7.5182d8",
"name": "Snips MQTT client ->",
"info": "",
"x": 840,
"y": 2180,
"wires": []
},
{
"id": "e6a2be65.251d88",
"type": "function",
"z": "b2ff20a7.5182d8",
"name": "send-action",
"func": "try {\n msg.topic = \"hermes/dialogueManager/startSession\";\n const action = {siteId: \"default\", init: {type: \"action\", text: \"For which device ?\", canBeEnqueued: true, intentFilter: [\"specifyDeviceName\"]}, customData: \"\"};\n msg.payload = JSON.stringify(action);\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 450,
"y": 2300,
"wires": [
[
"bdec4fde.2e3e98"
]
]
},
{
"id": "718fa75e.952fc",
"type": "link in",
"z": "b2ff20a7.5182d8",
"name": "",
"links": [
"e5332056.f7fd5"
],
"x": 135,
"y": 200,
"wires": [
[
"3ee2e86f.e9729"
]
]
},
{
"id": "9e660515.a8a9f",
"type": "ui_group",
"z": "",
"name": "device-interfaces",
"tab": "83d1f894.d8943",
"order": 1,
"disp": false,
"width": "10",
"collapse": false
},
{
"id": "acad220b.65b9e8",
"type": "mqtt-broker",
"z": "",
"name": "Snips broker",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
},
{
"id": "83d1f894.d8943",
"type": "ui_tab",
"z": "",
"name": "settings",
"icon": "dashboard",
"order": 2,
"disabled": false,
"hidden": false
}
]
@getlarge
Copy link
Author

Interpretation of intents detected by Snips Agent.

Prerequisites :

  • Your own instance of device-manager or an account on aloes.io
  • A machine running node-red-bridge with node-red-bridge/aloes-core configured to your Aloes account, and snips flow
  • A device registered on your Aloes account with Snips Platform and node-red-device with this flow installed
  • Snips Agent installed and configured ( with Aloes asistant ) on the same device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment