jangxx / node-magichome

An incomplete implementation of the functionality of the "Magic Home" app. Partially a port of https://github.com/Danielhiversen/flux_led to Node.js
ISC License
124 stars 26 forks source link

Poor discovery success rate #35

Closed AndrewJLang closed 2 years ago

AndrewJLang commented 3 years ago

For some reason, the device discovery for me is performing very poorly, I'm able to discover my devices very rarely. I am looking to use this functionality, but could this be converted over to communicate purely over the local network? Or if not, is there any enhancements that could be done in the discovery.scan method?

jangxx commented 3 years ago

The way the discovery works is purely over the local network already. It broadcasts a UDP packet and each controller that receives it will reply with a UDP packet of its own. The problem is that these packets can get lost very easily and there is nothing you can really do about that. One idea might be to run the scan method multiple times with a short timeout and then use a longer timeout for the last call, so send out the discovery packet multiple times.

This could look something like:

const { Discovery } = require("magic-home");

const clients = [].concat(
    await Discovery.scan(500),
    await Discovery.scan(500),
    await Discovery.scan(500),
    await Discovery.scan(500),
    await Discovery.scan(2000),
);

This will take a total of 4 seconds and send out the discovery packet five times during that time. Unfortunately there is nothing else we can really do, since the discovery procedure is really not that well designed on the controllers' side.