Snippets

Pete Matthews M8KGG: Untitled snippet

Created by Pete Matthews last modified
[{"id":"916f2a42.8e3","type":"function","z":"75293b5b.80b7f4","name":"","func":"\nfunction colorLuminance(hex, lum) {\n    // Validate hex string\n    hex = String(hex).replace(/[^0-9a-f]/gi, \"\");\n    if (hex.length < 6) {\n        hex = hex.replace(/(.)/g, '$1$1');\n    }\n    lum = lum || 0;\n    // Convert to decimal and change luminosity\n    var rgb = \"#\",\n        c;\n    for (var i = 0; i < 3; ++i) {\n        c = parseInt(hex.substr(i * 2, 2), 16);\n        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);\n        rgb += (\"00\" + c).substr(c.length);\n    }\n    return rgb;\n}\n\nif (typeof context.global.rgbState === \"undefined\") {\ncontext.global.rgbState = 0 }\nif (typeof context.global.rgbLevel === \"undefined\") {\ncontext.global.rgbLevel = 0 }\nif (typeof context.global.rgbColour === \"undefined\") {\ncontext.global.rgbColour = \"AAAAAAAA\" }\nswitch (msg.req.params.actionName){\n case \"setStatus\": \n    context.global.rgbState  = msg.req.params.actionParameter; \n    if (context.global.rgbState == \"0\") {context.global.rgbLevel = 0 } \n    else {context.global.rgbLevel = 100 } \n    break; \n case \"setColor\":  \n    context.global.rgbColour = msg.req.params.actionParameter;  \n    break; \n case \"setLevel\":  \n    context.global.rgbLevel  = msg.req.params.actionParameter;  \n    if (context.global.rgbLevel == \"0\") {context.global.rgbState = 0 }\n    else {context.global.rgbState = 1 } \n    break;\n default: \n break;\n}\nif (context.global.rgbState == '0' || context.global.rgbLevel == '0') {\n    msg.payload = 'RGB = 0,0,0';\n} else {\n\n    hexColor = colorLuminance(context.global.rgbColour.slice(2), -1+(parseInt(context.global.rgbLevel)*0.01));\n\n    msg.payload = 'RGB = ' + parseInt(hexColor.substr(1,2), 16) + ',' + parseInt(hexColor.substr(3,2), 16) + ',' + parseInt(hexColor.substr(5,2), 16); \n    } \nreturn msg;\n","outputs":1,"noerr":0,"x":477,"y":267,"wires":[["f6911e6f.1c69a"]]},{"id":"7719fdf3.bdddcc","type":"http in","z":"75293b5b.80b7f4","name":"","url":"/test1/system","method":"get","swaggerDoc":"","x":172,"y":58,"wires":[["ebe296fb.dd86d8"]]},{"id":"dc1a490e.b06c","type":"http in","z":"75293b5b.80b7f4","name":"","url":"/test1/devices","method":"get","swaggerDoc":"","x":173,"y":144,"wires":[["b907ccdc.a65f7"]]},{"id":"5d0cd67e.a82748","type":"http in","z":"75293b5b.80b7f4","name":"","url":"/test1/rooms","method":"get","swaggerDoc":"","x":162,"y":100,"wires":[["17e0313b.1a60cf"]]},{"id":"5f3ed3fa.5ecf74","type":"http response","z":"75293b5b.80b7f4","name":"","x":512,"y":98,"wires":[]},{"id":"ebe296fb.dd86d8","type":"function","z":"75293b5b.80b7f4","name":"","func":"msg.payload = { \"id\": \"Fred2\", \"apiversion\": 1};\nreturn msg;","outputs":1,"noerr":0,"x":352,"y":58,"wires":[["5f3ed3fa.5ecf74"]]},{"id":"b907ccdc.a65f7","type":"function","z":"75293b5b.80b7f4","name":"","func":"\nvar rgbState  = context.global.rgbState  || 0;\nvar rgbLevel  = context.global.rgbLevel  || 0;\nvar rgbColour = context.global.rgbColour || \"AAAAAAAA\";\n\n\n\nmsg.payload = {\"devices\": [\n{ \"id\": \"rgb01\", \"name\": \"RGB Light\", \"type\": \"DevRGBLight\", \"room\": \"roomID1_fred2\", \"params\": [{\"key\": \"dimmable\", \"value\": \"1\"}, {\"key\": \"whitechannel\", \"value\": \"0\"}, {\"key\": \"Status\", \"value\": rgbState}, {\"key\": \"Level\", \"value\": rgbLevel}, {\"key\": \"color\", \"value\": rgbColour}]}\n//{ \"id\": \"rgb01\", \"name\": \"RGB Light\", \"type\": \"DevRGBLight\", \"room\": \"roomID1ps\", \"params\": [{\"key\": \"dimmable\"}, {\"key\": \"whitechannel\"}, {\"key\": \"Status\", \"value\": rgbState}, {\"key\": \"Level\", \"value\": rgbLevel}, {\"key\": \"color\", \"value\": rgbColour}]}\n]};\nreturn msg;","outputs":1,"noerr":0,"x":353,"y":144,"wires":[["5f3ed3fa.5ecf74","6d4676ba.732e08"]]},{"id":"17e0313b.1a60cf","type":"function","z":"75293b5b.80b7f4","name":"","func":"msg.payload = {\"rooms\": [{ \"id\": \"roomID1_fred2\", \"name\": \"Fred2 RGB Test Room\" }]};\nreturn msg;","outputs":1,"noerr":0,"x":353,"y":99,"wires":[["5f3ed3fa.5ecf74"]]},{"id":"4d671c8e.dd9dac","type":"http in","z":"75293b5b.80b7f4","name":"ISS Rest Actions","url":"/test1/devices/:deviceID/action/:actionName/:actionParameter","method":"get","swaggerDoc":"","x":182,"y":218,"wires":[["e61362a1.26c17","4ff5ff8f.85e1c","9d2b0adf.5a1d7"]]},{"id":"e61362a1.26c17","type":"switch","z":"75293b5b.80b7f4","name":"","property":"req.params.deviceID","rules":[{"t":"eq","v":"rgb01"}],"checkall":"true","outputs":1,"x":352,"y":218,"wires":[["d29f1206.c54b28","916f2a42.8e3"]]},{"id":"f6911e6f.1c69a","type":"debug","z":"75293b5b.80b7f4","name":"","active":true,"console":"false","complete":"payload","x":658,"y":267,"wires":[]},{"id":"3e063f95.909e2","type":"http response","z":"75293b5b.80b7f4","name":"","x":632,"y":218,"wires":[]},{"id":"d29f1206.c54b28","type":"function","z":"75293b5b.80b7f4","name":"","func":"msg.payload = { \"success\": true, \"errormsg\": \"ok\" };\nreturn msg;","outputs":1,"noerr":0,"x":512,"y":218,"wires":[["3e063f95.909e2"]]},{"id":"6d4676ba.732e08","type":"debug","z":"75293b5b.80b7f4","name":"","active":false,"console":"false","complete":"false","x":582,"y":151,"wires":[]},{"id":"3e1b642d.5edcfc","type":"inject","z":"75293b5b.80b7f4","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":149,"y":303.5,"wires":[["f13e5a54.a07cd"]]},{"id":"f13e5a54.a07cd","type":"function","z":"75293b5b.80b7f4","name":"","func":"context.global.rgbState = \"0\";\ncontext.global.rgbLevel = \"45\";\nreturn msg;","outputs":1,"noerr":0,"x":327,"y":408.5,"wires":[[]]},{"id":"4ff5ff8f.85e1c","type":"debug","z":"75293b5b.80b7f4","name":"","active":true,"console":"false","complete":"req.params.actionName","x":551,"y":328,"wires":[]},{"id":"9d2b0adf.5a1d7","type":"debug","z":"75293b5b.80b7f4","name":"","active":true,"console":"false","complete":"req.params.actionParameter","x":583,"y":376,"wires":[]}]

Comments (0)