Last active
November 20, 2025 23:10
-
-
Save WietseWind/fae73817d75c671cc6645b34792dec45 to your computer and use it in GitHub Desktop.
Ugly code: Flic bridge SDK + TP Link HS100 relay
This file contains hidden or 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
| var buttonManager = require("buttons") | |
| var buttons = buttonManager.getButtons() | |
| const net = require('net') | |
| var arwenTimeout | |
| for (var i = 0; i < buttons.length; i++) { | |
| var button = buttons[i]; | |
| console.log(JSON.stringify(button)) | |
| } | |
| buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { | |
| var button = buttonManager.getButton(obj.bdaddr) | |
| var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold" | |
| console.log(JSON.stringify({type: clickType, name: button.name})) | |
| if (button.name === 'Arwèn') { | |
| clearTimeout(arwenTimeout) | |
| switchLamp(clickType !== 'hold') | |
| console.log('Switching ' + (clickType !== 'hold' ? 'on' : 'off') + ' ', new Date()) | |
| if (clickType !== 'hold') { | |
| console.log('Timeout set to clear later') | |
| arwenTimeout = setTimeout(function () { | |
| switchLamp(false) | |
| console.log('Switched off', new Date()) | |
| }, 1000 * 15) | |
| } | |
| } | |
| }) | |
| console.log("Started") | |
| switchLamp(false) | |
| function switchLamp(lampState) { | |
| const socket = net.Socket() | |
| const connection = socket.connect({port:9999, host: '192.168.1.99'}, function () { | |
| console.log('Connected') | |
| const prefix = [ | |
| 0x00, 0x00, 0x00, 0x2a, 0xd0, 0xf2, 0x81, 0xf8, 0x8b, 0xff, | |
| 0x9a, 0xf7, 0xd5, 0xef, 0x94, 0xb6, 0xc5, 0xa0, 0xd4, 0x8b, | |
| 0xf9, 0x9c, 0xf0, 0x91, 0xe8, 0xb7, 0xc4, 0xb0, 0xd1, 0xa5, | |
| 0xc0, 0xe2, 0xd8, 0xa3, 0x81, 0xf2, 0x86, 0xe7, 0x93, 0xf6, | |
| 0xd4, 0xee, | |
| ] | |
| const off = [ | |
| 0xde, 0xa3, 0xde, 0xa3 | |
| ] | |
| const on = [ | |
| 0xdf, 0xa2, 0xdf, 0xa2 | |
| ] | |
| console.log('Lamp to ' + (!lampState ? 'off' : 'on')) | |
| connection.write(new Buffer([].concat(prefix).concat(!lampState ? off : on))) | |
| connection.end() | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment