[ { "id": "51fcf615.b332a", "type": "tab", "label": "Hubitat Mochad Connector", "disabled": false, "info": "" }, { "id": "3e556f69.d072e8", "type": "tcp in", "z": "51fcf615.b332a", "name": "Server holding connections at :1025", "server": "server", "host": "", "port": "1025", "datamode": "stream", "datatype": "utf8", "newline": "\\n", "topic": "text", "base64": false, "x": 140, "y": 280, "wires": [ [ "24d7b8f4.6a284", "325b0be1.f51cbc" ] ] }, { "id": "f83623de.638968", "type": "tcp out", "z": "51fcf615.b332a", "host": "", "port": "", "beserver": "reply", "base64": false, "end": false, "name": "reply: all open connections will be messaged", "x": 690, "y": 400, "wires": [] }, { "id": "24d7b8f4.6a284", "type": "tcp request", "z": "51fcf615.b332a", "server": "localhost", "port": "1099", "out": "sit", "splitc": " ", "name": "mochad", "x": 340, "y": 400, "wires": [ [ "c808e86e.f8b6a" ] ] }, { "id": "c808e86e.f8b6a", "type": "function", "z": "51fcf615.b332a", "name": "Parse mochad Messages", "func": "msg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['X10NodeRed'] = 'DeviceUpdate';\n \nvar lastCommand = global.get(\"LastCommand\");\nif (typeof(lastCommand) != \"undefined\") {\n lastCommand = lastCommand.toLowerCase().trim();\n node.warn(lastCommand); \n} else {\n lastCommand = \"\"\n}\n\n\nconst inLines = new Buffer(msg.payload, 'hex'); \nnode.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)+\"\\r\\n\";\n //msg.headers['eventData'] = msg.payload;\n //node.warn(\"1 eventData \" + JSON.stringify(eventData))\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+([:xX0-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)+\"\\r\\n\";\n msg.headers['eventData'] = msg.payload;\n //node.warn(\"2 eventData \" + JSON.stringify(eventData))\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)+\"\\r\\n\";\n msg.headers['eventData'] = msg.payload;\n //node.warn(\"3 eventData \" + JSON.stringify(eventData))\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)+\"\\r\\n\";\n msg.headers['eventData'] = msg.payload;\n //node.warn(\"4 eventData \" + JSON.stringify(eventData))\n return msg; //{payload: JSON.stringify(eventData)};\n }\n } \n} \ncatch(err) {\n node.warn(\"exception in parsing: \" + err.message); \n return null; //ignore the exception, its probably a message we don't understand\n \n} \n \nreturn null;\n\n", "outputs": 1, "noerr": 0, "x": 490, "y": 280, "wires": [ [ "f83623de.638968", "325b0be1.f51cbc" ] ] }, { "id": "325b0be1.f51cbc", "type": "debug", "z": "51fcf615.b332a", "name": "", "active": false, "console": "false", "complete": "false", "x": 450, "y": 180, "wires": [] }, { "id": "25779b9a.1526dc", "type": "inject", "z": "51fcf615.b332a", "name": "Startup", "topic": "", "payload": "", "payloadType": "date", "repeat": "", "crontab": "", "once": true, "x": 100, "y": 380, "wires": [ [ "24d7b8f4.6a284" ] ] } ]