Created
December 27, 2021 23:47
-
-
Save NSURLSession0/54428294840963167e4a2a1f0a11b6b3 to your computer and use it in GitHub Desktop.
Aqara Magic Cube (Xiaomi) Node-RED function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const code = msg.payload.event.event; | |
const codes = { | |
"side1": [1002, 1003, 1004, 1005, 1006], | |
"side2": [2001, 2003, 2004, 2005, 2006], | |
"side3": [3001, 3002, 3004, 3005, 3006], | |
"side4": [4001, 4002, 4003, 4005, 4006], | |
"side5": [5001, 5002, 5003, 5004, 5006], | |
"side6": [6001, 6002, 6003, 6004, 6005], | |
"push": [1000, 2000, 3000, 4000, 5000, 6000], | |
"doubletap": [1001, 2002, 3003, 4004, 5005, 6006], | |
"shake": [7007], | |
"drop": [7008], | |
"wake": [7000] | |
}; | |
const outputIndexPerAction = { | |
"side1": 0, | |
"side2": 1, | |
"side3": 2, | |
"side4": 3, | |
"side5": 4, | |
"side6": 5, | |
"push": 6, | |
"doubletap": 7, | |
"shake": 8, | |
"drop": 9, | |
"rotatecw": 10, | |
"rotateccw": 11, | |
"wake": 12 | |
} | |
let action; | |
for (let a in codes) { | |
if (codes[a].includes(code)) { | |
action = a; | |
break; | |
} | |
} | |
if (!action) { | |
const strCode = String(code); | |
if (strCode.substr(1,2) !== "00" || strCode.length !== 4) { | |
action = code > 0 ? "rotatecw" : "rotateccw"; | |
} | |
} | |
const payload = { action: action, code: code } | |
if (action.startsWith("side")) { | |
payload.change = { | |
from: parseInt(String(code).substr(0, 1)), | |
to: parseInt(String(code).substr(3, 1)) | |
}; | |
} | |
payload.event = msg.payload.event; | |
const output = new Array(node.outputCount) | |
let outputIndex = outputIndexPerAction[action]; | |
output[outputIndex] = { payload: payload }; | |
return output; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"6335da097f800869","type":"function","z":"c3639465.ce006","name":"Magic Cube Action","func":"const code = msg.payload.event.event;\n\nconst codes = {\n\t\"side1\": [1002, 1003, 1004, 1005, 1006],\n\t\"side2\": [2001, 2003, 2004, 2005, 2006],\n\t\"side3\": [3001, 3002, 3004, 3005, 3006],\n\t\"side4\": [4001, 4002, 4003, 4005, 4006],\n\t\"side5\": [5001, 5002, 5003, 5004, 5006],\n\t\"side6\": [6001, 6002, 6003, 6004, 6005],\n\t\"push\": [1000, 2000, 3000, 4000, 5000, 6000],\n\t\"doubletap\": [1001, 2002, 3003, 4004, 5005, 6006],\n\t\"shake\": [7007],\n\t\"drop\": [7008],\n\t\"wake\": [7000]\n};\n\nconst outputIndexPerAction = {\n \"side1\": 0,\n \"side2\": 1,\n \"side3\": 2,\n \"side4\": 3,\n \"side5\": 4,\n \"side6\": 5,\n \"push\": 6,\n \"doubletap\": 7,\n \"shake\": 8,\n \"drop\": 9,\n \"rotatecw\": 10,\n \"rotateccw\": 11,\n \"wake\": 12\n}\n\nlet action;\n\nfor (let a in codes) {\n\tif (codes[a].includes(code)) {\n\t\taction = a;\n\t\tbreak;\n\t}\n}\n\nif (!action) {\n\tconst strCode = String(code);\n\tif (strCode.substr(1,2) !== \"00\" || strCode.length !== 4) {\n\t\taction = code > 0 ? \"rotatecw\" : \"rotateccw\";\n\t}\n}\n\nconst payload = { action: action, code: code }\n\nif (action.startsWith(\"side\")) {\n payload.change = {\n from: parseInt(String(code).substr(0, 1)),\n to: parseInt(String(code).substr(3, 1))\n };\n}\n\npayload.event = msg.payload.event;\n\nconst output = new Array(node.outputCount)\nlet outputIndex = outputIndexPerAction[action];\noutput[outputIndex] = { payload: payload };\n\nreturn output;","outputs":13,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":1140,"wires":[[],[],[],[],[],[],[],["48d301aadbbfaeb9"],["060901c4b4400ebb"],[],["1b703e6d9b98c288"],["1b703e6d9b98c288"],[]],"inputLabels":["Deconz Events"],"outputLabels":["Side 1","Side 2","Side 3","Side 4","Side 5","Side 6","Push","Double Tap","Shake","Drop","Rotate Clockwise","Rotate Counter Clockwise","Wake"],"icon":"font-awesome/fa-square"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment