-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlants.ts
116 lines (102 loc) · 3.66 KB
/
lants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as lantsDevice from "./lants-device.js";
import { LifxLanDevice, Integer, Duration, passure } from "./lants-device.js";
export { LifxLanDevice };
import * as LifxLanColor from './lants-color.js';
import { LifxLanColorAny, LifxLanColorCSS, LifxLanColorHSB, LifxLanColorRGB, LifxLanColorXyb } from "./lants-color.js";
export { LifxLanColor, LifxLanColorAny, LifxLanColorCSS, LifxLanColorHSB, LifxLanColorRGB, LifxLanColorXyb };
/* ------------------------------------------------------------------
* node-lifx-lan - lifx-lan.js
*
* Inspired by version from Futomi Hatano
* Copyright (c) 2018-2019, Bob Frankston - major changes in original code
* Released under the MIT license
* Date: 2018-08-08
* ---------------------------------------------------------------- */
import { LifxLanUdp, udpParsed } from './lants-udp.js';
export const delayms = (msec?: number) => { return new Promise(resolve => setTimeout(resolve, msec || 50)); };
export let LZVerbose = true;
/**
*
* @param vb Set Verbaose mode
*/
export function setVerbose(vb: boolean) {
LZVerbose = vb;
}
/**
* Discover current devices.
* Note that this is not reliable
* @param [optional] params {wait: Millseconds}
* @returns {LifxLanDevice[]} Table of devices
*/
export async function discover(params?: { wait?: Integer }) {
params = passure(params);
// await this.init();
const UDP = await LifxLanUdp.GetUDP();
try {
const found_list = await UDP.discover(params);
let devices: { [ipmac: string]: LifxLanDevice } = {};
// if (this._device_list) {
// this._device_list.forEach((dev) => {
// let k = dev['ip'] + ' ' + dev['mac'];
// devices[k] = dev;
// });
// }
let device_list: LifxLanDevice[] = []; // { [ipmac: string]: LifxLanDevice } = {};
found_list.forEach((res: udpParsed) => {
let ip = res.address;
let mac_parts = res.header.target.split(':');
let mac = mac_parts.slice(0, 6).join(':');
let k = ip + ' ' + mac;
if (devices[k]) {
device_list.push(devices[k]);
} else {
let lifxdev = new LifxLanDevice({
mac: mac,
ip: ip,
// udp: mLifxUdp
});
device_list.push(lifxdev);
}
});
const _device_list = await _discoverGetDeviceInfo(device_list);
// Quick hack
// UDP.device_list_hack = {};
// this._device_list.forEach(dev => UDP.device_list_hack[dev.ip] = dev);
return [..._device_list];
}
finally {
UDP.destroy();
}
};
async function _discoverGetDeviceInfo(dev_list: LifxLanDevice[]) {
try {
await (Promise as any).allSettled(dev_list.map(dev => dev.getDeviceInfo()));
}
catch (e: any) {
const full = dev_list.length;
dev_list = dev_list.filter(dev => dev.deviceInfo); // Keep only those that succeeded
const count = dev_list.reduce((prev, cur) => prev += cur.deviceInfo ? 1 : 0, 0);
if (LZVerbose) console.error(`_discoverGetDeviceInfo Found ${count} of ${full} devices\n$ Error: ${e.message}`);
throw e;
}
return [...dev_list]; // Why a copy?
};
// TODO consider caching the device info to avoid repeating GetDeviceInfo
/**
* Create a new device object. This can be used in place of or in addition to discovery
* @params {ip, MAC} params {ip IP Address, MAC Mac address}
* @returns LifxLanDevice object
*/
export async function createDevice(params: { ip: string, mac: string }) {
return new LifxLanDevice({ ip: params.ip, mac: params.mac });;
};
/**
* Normalize MAC to AA:99 ...
* @param mac Address to be normalize.
* @returns Address in upper case with only hex characters separated by :
*/
export function normalizeMac(mac: string) {
mac = mac.toUpperCase()?.replace(/[^A-Z\d]/g, "");
return mac.match(/(..?)/g).join(":"); // COmpatability till we fix
// return mac.toUpperCase().replace(/-/g, ":")
}