Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StonegateGuy/c3efe0d5dca07872d36409b7290d6497 to your computer and use it in GitHub Desktop.
Save StonegateGuy/c3efe0d5dca07872d36409b7290d6497 to your computer and use it in GitHub Desktop.
[
{
"id": "759be734.ad2c58",
"type": "tab",
"label": "Smartthings X10 Connector -1",
"disabled": false,
"info": ""
},
{
"id": "410ba795.a13af8",
"type": "debug",
"z": "759be734.ad2c58",
"name": "",
"active": false,
"console": "false",
"complete": "payload",
"x": 630,
"y": 840,
"wires": []
},
{
"id": "a40d5239.ea007",
"type": "function",
"z": "759be734.ad2c58",
"name": "Parse mochad Messages",
"func": "msg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['X10NodeRed'] = 'DeviceUpdate';\n \nvar lastCommand = global.get(\"LastCommand\");\nlastCommand = lastCommand.toLowerCase().trim();\n//node.warn(lastCommand);\n\nconst inLines = new Buffer(msg.payload, 'hex'); \n//node.warn(inLines.toString());\nvar lastCodeSeen = context.get(\"lastCodeSeen\");\nif(typeof lastCodeSeen == \"undefined\") {\n lastCodeSeen = {\n housecode: \"x\",\n unitcode: \"0\"\n };\n context.set(\"lastCodeSeen\", lastCodeSeen);\n}\n\ntry {\n\n var eventData, m;\n \n if (m = /^\\d{2}\\/\\d{2}\\s+(?:\\d{2}:){2}\\d{2}\\s(Tx|Rx)\\s+(RF|PL)\\s+House:\\s+([a-pA-P])\\s+Func:\\s+All\\s+(units|lights)\\s+(on|off)$/m.exec(inLines)) {\n eventData = {\n protocol: m[2].toLowerCase().trim(),\n direction: m[1].toLowerCase().trim(),\n housecode: m[3].toLowerCase().trim(),\n unitcode: \"*\" + m[4].trim(),\n state: m[5].toLowerCase().trim()\n };\n \n msg.payload = JSON.stringify(eventData);\n msg.headers['eventData'] = msg.payload;\n return msg; //{payload: JSON.stringify(eventData)};\n }\n if (m = /^\\d{2}\\/\\d{2}\\s+(?:\\d{2}:){2}\\d{2}\\s(Tx|Rx)\\s+(RFSEC)\\s+Addr:\\s+([xX:0-9a-fA-F]+)(\\s+)Func:\\s+(.+)$/m.exec(inLines)) {\n eventData = {\n protocol: m[2].toLowerCase().trim(),\n direction: m[1].toLowerCase().trim(),\n housecode: m[3].toLowerCase().trim(),\n unitcode: \"*\" + m[4].trim(),\n state: m[5].toLowerCase().trim()\n };\n msg.payload = JSON.stringify(eventData);\n msg.headers['eventData'] = msg.payload;\n return msg; //{payload: JSON.stringify(eventData)};\n }\n if (m = /^\\d{2}\\/\\d{2}\\s+(?:\\d{2}:){2}\\d{2}\\s(Tx|Rx)\\s+(RF|PL)\\s+HouseUnit:\\s+([a-pA-P])(\\d{1,2})\\s+Func:\\s+(On|Off)/m.exec(inLines)) {\n eventData = {\n protocol: m[2].toLowerCase().trim(),\n direction: m[1].toLowerCase().trim(),\n housecode: m[3].toLowerCase().trim(),\n unitcode: parseInt(m[4].trim(), 10),\n state: m[5].toLowerCase().trim()\n };\n msg.payload = JSON.stringify(eventData);\n msg.headers['eventData'] = msg.payload;\n return msg; //{payload: JSON.stringify(eventData)};\n } else if (m = /^\\d{2}\\/\\d{2}\\s+(?:\\d{2}:){2}\\d{2}\\s(?:Rx)\\s+(?:RF|PL)\\s+HouseUnit:\\s+([a-pA-P])(\\d{1,2})/m.exec(inLines)) {\n lastCodeSeen.housecode = m[1].toLowerCase().trim();\n lastCodeSeen.unitcode = parseInt(m[2].trim(), 10);\n context.set(\"lastCodeSeen\", lastCodeSeen);\n }\n if (lastCodeSeen.housecode && lastCodeSeen.unitcode && (m = /\\d{2}\\/\\d{2}\\s+(?:\\d{2}:){2}\\d{2}\\s(Rx|Tx)\\s+(RF|PL)\\s+House:\\s+([a-pA-P])\\s+Func:\\s+(On|Off|Dim|Bright)$/m.exec(inLines))) {\n eventData = {\n protocol: m[2].toLowerCase().trim(),\n direction: m[1].toLowerCase().trim(),\n housecode: m[3].toLowerCase().trim(),\n unitcode: null,\n state: m[4].toLowerCase().trim()\n };\n if (eventData.housecode === lastCodeSeen.housecode) {\n eventData.unitcode = lastCodeSeen.unitcode;\n msg.payload = JSON.stringify(eventData);\n msg.headers['eventData'] = msg.payload;\n return msg; //{payload: JSON.stringify(eventData)};\n }\n }\n} \ncatch(err) {\n return null; //ignore the exception, its probably a message we don't understand\n //console.log(\"exception in parsing: \" + err.message); \n} \n \nreturn null;\n\n",
"outputs": 1,
"noerr": 0,
"x": 1010,
"y": 160,
"wires": [
[
"7b6686db.8bf4d8",
"bb828198.21511"
]
]
},
{
"id": "53b0b22e.41f7ec",
"type": "http in",
"z": "759be734.ad2c58",
"name": "/register",
"url": "/register",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 80,
"y": 751,
"wires": [
[
"a5136172.f638f",
"2b4308d9.662dd8"
]
]
},
{
"id": "241efcf7.10eaa4",
"type": "http response",
"z": "759be734.ad2c58",
"name": "Send respone to ST",
"statusCode": "",
"headers": {},
"x": 640,
"y": 760,
"wires": []
},
{
"id": "95250397.05ab2",
"type": "http request",
"z": "759be734.ad2c58",
"name": "http request to ST",
"method": "POST",
"ret": "txt",
"url": "",
"tls": "",
"x": 1090,
"y": 320,
"wires": [
[]
]
},
{
"id": "bb828198.21511",
"type": "debug",
"z": "759be734.ad2c58",
"name": "",
"active": false,
"console": "false",
"complete": "true",
"x": 1290,
"y": 160,
"wires": []
},
{
"id": "893204d1.3a6288",
"type": "file in",
"z": "759be734.ad2c58",
"name": "Read ST setting after restart",
"filename": "/home/pi/.red-st-connect.conf",
"format": "utf8",
"chunk": false,
"sendError": false,
"x": 366.5,
"y": 80,
"wires": [
[
"59682da1.910794"
]
]
},
{
"id": "515a9b5.4671864",
"type": "inject",
"z": "759be734.ad2c58",
"name": "Startup",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "",
"x": 91,
"y": 81,
"wires": [
[
"893204d1.3a6288",
"ff097e8e.acf89",
"54f513e6.21fecc"
]
]
},
{
"id": "2b4308d9.662dd8",
"type": "file",
"z": "759be734.ad2c58",
"name": "Persist ST settings to File",
"filename": "/home/pi/.red-st-connect.conf",
"appendNewline": false,
"createDir": true,
"overwriteFile": "true",
"x": 316.5,
"y": 719,
"wires": [
[]
]
},
{
"id": "b3f8dc66.18df1",
"type": "comment",
"z": "759be734.ad2c58",
"name": "setup config file path",
"info": "If you are not running as pi or /home/pi is\nnot writable change the location of where\nthis flow will read and write ST settings\nsettigns will be stored in /home/pi/.red-st-connect.conf",
"x": 348,
"y": 43,
"wires": []
},
{
"id": "59682da1.910794",
"type": "function",
"z": "759be734.ad2c58",
"name": "Set ST Settings in global context",
"func": "try {\n var STsettings = JSON.parse(msg.payload);\n if (typeof obj !== 'string' ) \n global.set(\"STsettings\", STsettings);\n global.set(\"LastCommand\", \" \");\n} catch (e) {\n // ignore errors\n}\n\nmsg.payload = \"Smartthings settings: \" + JSON.stringify (STsettings);\n\nreturn msg\n\n\n\n",
"outputs": 1,
"noerr": 0,
"x": 674,
"y": 80,
"wires": [
[]
]
},
{
"id": "7b6686db.8bf4d8",
"type": "function",
"z": "759be734.ad2c58",
"name": "Setup http request",
"func": "\n// if you want to hard code the url to ST uncomment these two lines and\n// delete or comment out the rest of the lines in this node\n// msg.url. = \"http://ip_address_of_Smartthings_hub:39500/\";\n// return msg;\n\nvar STsettings = global.get(\"STsettings\");\nif(typeof STsettings == \"undefined\") {\n node.error(\"Can't continue. Don't know where Smartthings hub is.\");\n return null;\n}\nnode.log(\"Please ignore the message properties override warning\");\nmsg.url =\"http://\" + STsettings.ip_for_st + \":\" + STsettings.port_for_st;\nreturn msg;\n\n\n\n\n\n",
"outputs": 1,
"noerr": 0,
"x": 1110,
"y": 220,
"wires": [
[
"bb828198.21511",
"95250397.05ab2"
]
]
},
{
"id": "e7a09a52.153db8",
"type": "http in",
"z": "759be734.ad2c58",
"name": "/push",
"url": "/push",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 61,
"y": 500,
"wires": [
[
"3e83fa4a.350b96",
"5a73f4fb.2bab2c"
]
]
},
{
"id": "a5136172.f638f",
"type": "function",
"z": "759be734.ad2c58",
"name": "Registration response",
"func": "msg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['X10NodeRed'] = 'Registered';\nmsg.payload = {\"X10NodeRed\":\"Registered\"};\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 310,
"y": 792,
"wires": [
[
"241efcf7.10eaa4",
"410ba795.a13af8"
]
]
},
{
"id": "7f0f0d79.50c584",
"type": "comment",
"z": "759be734.ad2c58",
"name": "setup mochad host ip",
"info": "This is the mochad listener you should \nsetup the mochad host address here\n",
"x": 280,
"y": 140,
"wires": []
},
{
"id": "5a73f4fb.2bab2c",
"type": "function",
"z": "759be734.ad2c58",
"name": "parse send message",
"func": "var jsonText = JSON.stringify(msg.payload); \nvar obj = JSON.parse(jsonText);\n\nif (typeof obj !== 'string' ) {\n var device = obj.device.replace(/-/g, \" \");\n msg.payload = device + \" \" + obj.action+\"\\n\";\n global.set(\"LastCommand\", msg.payload);\n return msg;\n}",
"outputs": 1,
"noerr": 0,
"x": 260,
"y": 420,
"wires": [
[
"bdb625b4.a6e068",
"1b01c91e.572dc7",
"54f513e6.21fecc"
]
]
},
{
"id": "5597b8ec.7b2898",
"type": "comment",
"z": "759be734.ad2c58",
"name": "setup config file path",
"info": "If you are not running as pi or /home/pi is\nnot writable change the location of where\nthis flow will read and write ST settings\nsettigns will be stored in /home/pi/.red-st-connect.conf",
"x": 310,
"y": 676,
"wires": []
},
{
"id": "bdb625b4.a6e068",
"type": "function",
"z": "759be734.ad2c58",
"name": "OK response",
"func": "msg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.payload = {\"X10NodeRed\":\"OK\"};\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 450,
"y": 600,
"wires": [
[
"241efcf7.10eaa4"
]
]
},
{
"id": "1b01c91e.572dc7",
"type": "debug",
"z": "759be734.ad2c58",
"name": "parse debug",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 490,
"y": 420,
"wires": []
},
{
"id": "3e83fa4a.350b96",
"type": "debug",
"z": "759be734.ad2c58",
"name": "",
"active": false,
"console": "false",
"complete": "payload",
"x": 230,
"y": 600,
"wires": []
},
{
"id": "cd5c6aaf.ea2568",
"type": "function",
"z": "759be734.ad2c58",
"name": "Rate Limiter",
"func": "var interval = (500); // minimum interval between messages (ms)\ncontext.lastTime = context.lastTime || 0;\n\nvar now = Date.now();\n\nif (now-context.lastTime > interval) {\n context.lastTime = now;\n return msg;\n} else {\n return null;\n}",
"outputs": 1,
"noerr": 0,
"x": 750,
"y": 220,
"wires": [
[
"a40d5239.ea007"
]
]
},
{
"id": "ff097e8e.acf89",
"type": "tcp request",
"z": "759be734.ad2c58",
"server": "localhost",
"port": "1099",
"out": "sit",
"splitc": " ",
"name": "mochad always open",
"x": 440,
"y": 220,
"wires": [
[
"cd5c6aaf.ea2568"
]
]
},
{
"id": "54f513e6.21fecc",
"type": "tcp request",
"z": "759be734.ad2c58",
"server": "localhost",
"port": "1099",
"out": "immed",
"splitc": " ",
"name": "mochad close",
"x": 450,
"y": 320,
"wires": [
[
"cd5c6aaf.ea2568"
]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment