@wanderxjtu/homebridge-yeelighter
Version:
Yeelight support for Homebridge with particular support of ceiling lights
67 lines • 2.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Discovery = void 0;
const node_events_1 = require("node:events");
const node_dgram_1 = __importDefault(require("node:dgram"));
const node_url_1 = __importDefault(require("node:url"));
const yeedevice_1 = require("./yeedevice");
const http_headers_1 = __importDefault(require("http-headers"));
const options = {
port: 1982,
multicastAddr: "239.255.255.250",
discoveryMsg: "M-SEARCH * HTTP/1.1\r\nMAN: \"ssdp:discover\"\r\nST: wifi_bulb\r\n",
};
class Discovery extends node_events_1.EventEmitter {
constructor() {
super();
this.onMessage = (response) => {
const headers = (0, http_headers_1.default)(response, true);
const device = { ...yeedevice_1.EMPTY_DEVICEINFO };
for (const header of Object.keys(headers)) {
const value = headers[header];
switch (typeof yeedevice_1.EMPTY_DEVICEINFO[header]) {
case "number":
device[header] = Number(value);
break;
case "boolean":
device[header] = value === "on";
break;
case "string":
device[header] = value;
break;
default:
// device[header] = value;
break;
}
}
if (device.id && device.location && device.id !== "") {
const parsedUrl = node_url_1.default.parse(device.location);
device.host = parsedUrl.hostname || "";
device.port = Number(parsedUrl.port);
this.emit("didDiscoverDevice", device);
}
};
this.socket = node_dgram_1.default.createSocket({ type: "udp4", reuseAddr: true });
}
discover() {
const buffer = Buffer.from(options.discoveryMsg);
this.socket.send(buffer, 0, buffer.length, options.port, options.multicastAddr);
}
listen() {
this.socket.on("listening", () => {
this.discover();
this.emit("started");
});
this.socket.on("message", this.onMessage);
this.socket.bind(options.port, () => {
this.socket.setBroadcast(true);
this.socket.setMulticastTTL(128);
this.socket.addMembership(options.multicastAddr);
});
}
}
exports.Discovery = Discovery;
//# sourceMappingURL=discovery.js.map