node-lightify
node-lightify copied to clipboard
Handling multiple connections fails
I have a setup using Node Red. How do I handle multiple requests? This code seems to work ok for 2 requests but has problems with more than 2.
if(msg.topic.indexOf('zigbee_light') !== -1)
{
var lightify = global.get('lightify');
var connection = new lightify.lightify('192.168.0.41');
connection.connect().then(function()
{
return connection.discover();
})
.then(function(data)
{
var devices = data.result;
if(msg.topic.endsWith('/set'))
{
var device_no = (msg.topic.substring(
msg.topic.indexOf('zigbee_light') + 12,
msg.topic.indexOf('zigbee_light') + 13));
var state = parseInt(msg.payload, 10);
devices.forEach(
device =>
{
var check = 'A19 W 60 0' + device_no;
if(device.name === check)
{
new Promise(function(resolve, reject) {
resolve(connection.nodeSoftOnOff(device.mac,
state, 0))
reject(function()
{
node.warn('Rejected');
connection.dispose();
flow.set('connection', undefined);
});
}).then(function(data)
{
return connection.nodeOnOff(device.mac, state,
0);
})
.then(function(data)
{
connection.dispose();
flow.set('connection', undefined);
})
.catch(function(error)
{
node.warn(error);
flow.set('connection', undefined)
});
}
}
);
}
}
You should have a buffer to handle incoming requests as well as some watchdog timers to handle a non-responsive gateway - which happens quite often. Here is my flow for using Lightify. It works nicely. The input node can accept commands from HomeKit if you're into that. There are some inject timers that initialize it in the case of a node-red restart. I don't have time at the moment to explain it in detail but if you run into issues let me know.
Edit: removed pasted code and created a pastebin. See comments below.
Greatly appreciated! I will do as much as I can without you. Thank you for taking the time and providing the code!
No problem, I hope it works out for you! If you can improve it let me know.
Sent from my iPhone
Greatly appreciated! I will do as much as I can without you. Thank you for taking the time and providing the code!
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
Copy the text and import it into node-red using the “import from clipboard” menu.
Sent from my iPhone
On Mar 20, 2020, at 8:12 PM, nospam2k [email protected] wrote:
Github doesn't seem to like the cut/paste. I'm getting json formatting errors. Zip?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
I'm so sorry for the issues we are having here. I've honestly never exported a flow for someone before. So, there are 2 ways of exporting from Node-RED and I don't know the difference. There is "Compact" and there is "Formatted". I exported both into 2 separate pastebins for you. Hopefully one of them will work. If not, we can try an email next. The links are below.
Also, in regards to getting away from the app, I would love to do that as well especially since they have announced they are turning off their servers in 2021. I will paste a link to a GitHub where the local protocol has been mostly reverse engineered. You can start there. However, I did some experimenting once where I removed internet connectivity from the gateway and tried pairing a new device. It would not work so, it seems a connection to the Lightify servers is required.
The good news is that I have been looking at the Hubitat hub and it seems to support Lightify and has a local API.
Compact: https://pastebin.com/ZFLD3u5b Formatted: https://pastebin.com/WYbcAEtV
Lightify Protocol: https://github.com/noctarius/lightify-binary-protocol
Habitat Hub: https://hubitat.com
Ninja Edit: btw, I removed a lot of unnecessary commented out code so it should be much smaller now.
No problem. I have got the bulbs working to my satisfaction using chained promises and it seems to be working. It's far more simple then your flows so I'm sure I'm not covering all the bases but at the moment I only have 4 bulbs controlled by node-red and being via a node implementation of Alexa, so I'm at least working.
I also was able to hack away (as mentioned in my previous posts) at your export and replace the missing elements by converting the raw html on the page to the appropriate characters. As I mentioned, just using the code back ticks seems to be sufficient for fixing that issue. I will grab the pastebins and compare to make sure nothing was lost.
As far as the pairing. I have looked at that reverse of the protocol you mentioned and will be looking further into it in the future. I didn't know about the server shutdowns so I will be looking more urgently into a resolution. I think pairing will be the biggest thing. I'm going to mess with it and see if I can figure out the pairing sequence from reversing the Android App. I was able to do a similar thing with an Epson Wifi Projector so If I get anywhere, I'll be sure to let you know. I would be happy to connect with you at a more personal level to corroborate. I'm not sure how to do that on Github.
Ok, the pastebin worked fine. I will attach as file here in case pastebin goes away. lightify-with-comments.json.zip