@bobfrankston/lxlan 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +5 -0
- package/client.d.ts +36 -18
- package/client.d.ts.map +1 -1
- package/client.js +92 -35
- package/client.js.map +1 -1
- package/device.d.ts +58 -4
- package/device.d.ts.map +1 -1
- package/device.js +111 -11
- package/device.js.map +1 -1
- package/events.d.ts +24 -0
- package/events.d.ts.map +1 -0
- package/events.js +7 -0
- package/events.js.map +1 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -1
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +14 -6
- package/protocol.d.ts +23 -0
- package/protocol.d.ts.map +1 -1
- package/protocol.js +27 -0
- package/protocol.js.map +1 -1
- package/transport.d.ts +30 -0
- package/transport.d.ts.map +1 -0
- package/transport.js +9 -0
- package/transport.js.map +1 -0
- package/types.d.ts +13 -0
- package/types.d.ts.map +1 -1
- package/types.js +8 -0
- package/types.js.map +1 -1
- package/.claude/settings.local.json +0 -13
- package/client.ts +0 -214
- package/device.ts +0 -170
- package/index.ts +0 -4
- package/notes.md +0 -530
- package/protocol.ts +0 -209
- package/tsconfig.json +0 -20
- package/types.ts +0 -129
package/client.ts
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import { hsbk16ToHsbk } from '@bobfrankston/colorlib';
|
|
3
|
-
import { UdpSocket, UdpRetryOptions, getBroadcastAddresses } from '@bobfrankston/lxudp';
|
|
4
|
-
import { LxDevice } from './device.js';
|
|
5
|
-
import { LxMessage, MessageType, LIFX_PORT } from './types.js';
|
|
6
|
-
import { encodeMessage, decodeMessage, decodeState, decodeStatePower, decodeStateLabel, decodeStateVersion, decodeStateGroup, decodeStateService } from './protocol.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Options for LxClient constructor
|
|
10
|
-
*/
|
|
11
|
-
export interface LxClientOptions {
|
|
12
|
-
/** UDP port to bind (default 56700) */
|
|
13
|
-
port?: number;
|
|
14
|
-
/** Broadcast addresses for discovery (auto-detected from network interfaces if not specified) */
|
|
15
|
-
broadcastAddresses?: string[];
|
|
16
|
-
/** Auto-discovery interval in ms (0 = manual only, default 0) */
|
|
17
|
-
discoveryInterval?: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* LIFX LAN client for device discovery and control.
|
|
22
|
-
*
|
|
23
|
-
* Events:
|
|
24
|
-
* - `device` (device: LxDevice) - new device discovered
|
|
25
|
-
* - `message` (device: LxDevice, msg: LxMessage) - any message received
|
|
26
|
-
* - `state` (device: LxDevice) - light state updated
|
|
27
|
-
* - `power` (device: LxDevice) - power state updated
|
|
28
|
-
* - `label` (device: LxDevice) - label updated
|
|
29
|
-
* - `group` (device: LxDevice) - group info updated
|
|
30
|
-
* - `location` (device: LxDevice) - location info updated
|
|
31
|
-
* - `version` (device: LxDevice) - version info received
|
|
32
|
-
* - `error` (err: Error) - transport or protocol error
|
|
33
|
-
*/
|
|
34
|
-
export class LxClient extends EventEmitter {
|
|
35
|
-
private socket: UdpSocket;
|
|
36
|
-
private port: number;
|
|
37
|
-
private broadcastAddresses: string[];
|
|
38
|
-
private discoveryTimer: NodeJS.Timeout;
|
|
39
|
-
|
|
40
|
-
/** Cached devices by MAC address (lowercase) */
|
|
41
|
-
devices: Map<string, LxDevice> = new Map();
|
|
42
|
-
|
|
43
|
-
constructor(options: LxClientOptions = {}) {
|
|
44
|
-
super();
|
|
45
|
-
this.port = options.port ?? LIFX_PORT;
|
|
46
|
-
this.broadcastAddresses = options.broadcastAddresses ?? getBroadcastAddresses();
|
|
47
|
-
|
|
48
|
-
this.socket = new UdpSocket({
|
|
49
|
-
port: this.port,
|
|
50
|
-
reuseAddr: true
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
this.socket.on('message', (data, rinfo) => {
|
|
54
|
-
this.handleMessage(data, rinfo.address, rinfo.port);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
this.socket.on('error', (err) => {
|
|
58
|
-
this.emit('error', err);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
if (options.discoveryInterval && options.discoveryInterval > 0) {
|
|
62
|
-
this.discoveryTimer = setInterval(() => this.discover(), options.discoveryInterval);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Start listening */
|
|
67
|
-
async start(): Promise<void> {
|
|
68
|
-
await this.socket.bind();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Stop and cleanup */
|
|
72
|
-
stop(): void {
|
|
73
|
-
if (this.discoveryTimer) clearInterval(this.discoveryTimer);
|
|
74
|
-
this.socket.close();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Set retry options at runtime */
|
|
78
|
-
setRetryOptions(options: UdpRetryOptions): void {
|
|
79
|
-
this.socket.setRetryOptions(options);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Get current retry options */
|
|
83
|
-
getRetryOptions(): Required<UdpRetryOptions> {
|
|
84
|
-
return this.socket.getRetryOptions();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Broadcast discovery to all interfaces */
|
|
88
|
-
discover(): void {
|
|
89
|
-
const msg = encodeMessage({
|
|
90
|
-
type: MessageType.GetService,
|
|
91
|
-
tagged: true
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
for (const addr of this.broadcastAddresses) {
|
|
95
|
-
this.socket.send(addr, this.port, msg);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Get device by MAC address
|
|
101
|
-
* @param mac - MAC address (case-insensitive, with or without colons)
|
|
102
|
-
* @returns LxDevice or undefined if not found
|
|
103
|
-
*/
|
|
104
|
-
getDevice(mac: string): LxDevice {
|
|
105
|
-
return this.devices.get(mac.toLowerCase());
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Add device manually (bypassing discovery).
|
|
110
|
-
* Use when you know the device IP from external source.
|
|
111
|
-
* @param mac - MAC address
|
|
112
|
-
* @param ip - IP address
|
|
113
|
-
* @param port - UDP port (default 56700)
|
|
114
|
-
* @returns LxDevice instance (new or existing)
|
|
115
|
-
*/
|
|
116
|
-
addDevice(mac: string, ip: string, port: number = LIFX_PORT): LxDevice {
|
|
117
|
-
const normalizedMac = mac.toLowerCase();
|
|
118
|
-
let device = this.devices.get(normalizedMac);
|
|
119
|
-
|
|
120
|
-
if (!device) {
|
|
121
|
-
device = new LxDevice(normalizedMac, ip, this.socket);
|
|
122
|
-
device.port = port;
|
|
123
|
-
this.devices.set(normalizedMac, device);
|
|
124
|
-
this.emit('device', device);
|
|
125
|
-
} else {
|
|
126
|
-
device.ip = ip;
|
|
127
|
-
device.port = port;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return device;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** Handle incoming message */
|
|
134
|
-
private handleMessage(data: Buffer, fromIp: string, fromPort: number): void {
|
|
135
|
-
let msg: LxMessage;
|
|
136
|
-
try {
|
|
137
|
-
msg = decodeMessage(data);
|
|
138
|
-
} catch (e: any) {
|
|
139
|
-
this.emit('error', new Error(`Decode error: ${e.message}`));
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const mac = msg.target;
|
|
144
|
-
if (mac === '00:00:00:00:00:00') return; /** broadcast response without target */
|
|
145
|
-
|
|
146
|
-
let device = this.devices.get(mac);
|
|
147
|
-
const isNew = !device;
|
|
148
|
-
|
|
149
|
-
if (!device) {
|
|
150
|
-
device = new LxDevice(mac, fromIp, this.socket);
|
|
151
|
-
device.port = fromPort;
|
|
152
|
-
this.devices.set(mac, device);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
device.ip = fromIp;
|
|
156
|
-
device.port = fromPort;
|
|
157
|
-
device.markSeen();
|
|
158
|
-
|
|
159
|
-
if (isNew) {
|
|
160
|
-
this.emit('device', device);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
this.emit('message', device, msg);
|
|
164
|
-
|
|
165
|
-
switch (msg.type) {
|
|
166
|
-
case MessageType.StateService: {
|
|
167
|
-
const info = decodeStateService(msg.payload);
|
|
168
|
-
device.port = info.port;
|
|
169
|
-
break;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
case MessageType.State: {
|
|
173
|
-
const state = decodeState(msg.payload);
|
|
174
|
-
device.color = hsbk16ToHsbk(state.hsbk);
|
|
175
|
-
device.power = state.power > 0;
|
|
176
|
-
device.label = state.label;
|
|
177
|
-
this.emit('state', device);
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
case MessageType.StatePower: {
|
|
182
|
-
device.power = decodeStatePower(msg.payload);
|
|
183
|
-
this.emit('power', device);
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
case MessageType.StateLabel: {
|
|
188
|
-
device.label = decodeStateLabel(msg.payload);
|
|
189
|
-
this.emit('label', device);
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
case MessageType.StateVersion: {
|
|
194
|
-
const ver = decodeStateVersion(msg.payload);
|
|
195
|
-
device.vendor = ver.vendor;
|
|
196
|
-
device.product = ver.product;
|
|
197
|
-
this.emit('version', device);
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
case MessageType.StateGroup: {
|
|
202
|
-
device.group = decodeStateGroup(msg.payload);
|
|
203
|
-
this.emit('group', device);
|
|
204
|
-
break;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
case MessageType.StateLocation: {
|
|
208
|
-
device.location = decodeStateGroup(msg.payload);
|
|
209
|
-
this.emit('location', device);
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
package/device.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { HSBK, ColorInput, parseColor, hsbkTo16 } from '@bobfrankston/colorlib';
|
|
2
|
-
import { UdpSocket } from '@bobfrankston/lxudp';
|
|
3
|
-
import { GroupInfo, MessageType, Products, LIFX_PORT } from './types.js';
|
|
4
|
-
import { encodeMessage, encodeSetPower, encodeSetColor, encodeSetLabel, encodeSetGroup, encodeSetLocation } from './protocol.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Represents a LIFX device on the LAN.
|
|
8
|
-
* State is cached and updated via events from LxClient.
|
|
9
|
-
*/
|
|
10
|
-
export class LxDevice {
|
|
11
|
-
/** MAC address (lowercase, colon-separated) */
|
|
12
|
-
readonly mac: string;
|
|
13
|
-
/** Current IP address */
|
|
14
|
-
ip: string;
|
|
15
|
-
/** UDP port (default 56700) */
|
|
16
|
-
port: number = LIFX_PORT;
|
|
17
|
-
|
|
18
|
-
/** Power state (true = on) */
|
|
19
|
-
power: boolean = false;
|
|
20
|
-
/** Current color in HSBK format */
|
|
21
|
-
color: HSBK = { h: 0, s: 0, b: 0, k: 3500 };
|
|
22
|
-
/** Device label (user-assigned name) */
|
|
23
|
-
label: string = '';
|
|
24
|
-
/** Whether device is responding */
|
|
25
|
-
online: boolean = false;
|
|
26
|
-
/** Last message timestamp (Date.now()) */
|
|
27
|
-
lastSeen: number = 0;
|
|
28
|
-
|
|
29
|
-
/** Vendor ID (1 = LIFX) */
|
|
30
|
-
vendor: number = 0;
|
|
31
|
-
/** Product ID (see Products table) */
|
|
32
|
-
product: number = 0;
|
|
33
|
-
|
|
34
|
-
/** Group membership */
|
|
35
|
-
group: GroupInfo = { id: '', label: '', updatedAt: 0 };
|
|
36
|
-
/** Location membership */
|
|
37
|
-
location: GroupInfo = { id: '', label: '', updatedAt: 0 };
|
|
38
|
-
|
|
39
|
-
private socket: UdpSocket;
|
|
40
|
-
|
|
41
|
-
constructor(mac: string, ip: string, socket: UdpSocket) {
|
|
42
|
-
this.mac = mac.toLowerCase();
|
|
43
|
-
this.ip = ip;
|
|
44
|
-
this.socket = socket;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Get product name from product ID */
|
|
48
|
-
get productName(): string {
|
|
49
|
-
return Products[this.product] ?? `Unknown (${this.product})`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Send raw message to device */
|
|
53
|
-
send(type: number, payload?: Buffer): void {
|
|
54
|
-
const msg = encodeMessage({
|
|
55
|
-
type,
|
|
56
|
-
target: this.mac,
|
|
57
|
-
payload,
|
|
58
|
-
ackRequired: false,
|
|
59
|
-
resRequired: true
|
|
60
|
-
});
|
|
61
|
-
this.socket.send(this.ip, this.port, msg);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Set power on/off */
|
|
65
|
-
setPower(on: boolean): void {
|
|
66
|
-
const msg = encodeMessage({
|
|
67
|
-
type: MessageType.SetPower,
|
|
68
|
-
target: this.mac,
|
|
69
|
-
payload: encodeSetPower(on),
|
|
70
|
-
ackRequired: true
|
|
71
|
-
});
|
|
72
|
-
this.socket.send(this.ip, this.port, msg);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Set color - accepts flexible input formats
|
|
77
|
-
* @param color - Color as hex "#ff0000", RGB {r,g,b}, HSL {h,s,l}, HSB {h,s,b}, HSBK {h,s,b,k}, or Kelvin number
|
|
78
|
-
* @param duration - Transition time in milliseconds (default 0)
|
|
79
|
-
*/
|
|
80
|
-
setColor(color: ColorInput, duration: number = 0): void {
|
|
81
|
-
const hsbk = parseColor(color);
|
|
82
|
-
const hsbk16 = hsbkTo16(hsbk);
|
|
83
|
-
const msg = encodeMessage({
|
|
84
|
-
type: MessageType.SetColor,
|
|
85
|
-
target: this.mac,
|
|
86
|
-
payload: encodeSetColor(hsbk16, duration),
|
|
87
|
-
ackRequired: true
|
|
88
|
-
});
|
|
89
|
-
this.socket.send(this.ip, this.port, msg);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Set white color temperature
|
|
94
|
-
* @param kelvin - Color temperature (1500-9000, warm to cool)
|
|
95
|
-
* @param brightness - Brightness 0-100 (default 100)
|
|
96
|
-
* @param duration - Transition time in milliseconds (default 0)
|
|
97
|
-
*/
|
|
98
|
-
setWhite(kelvin: number, brightness: number = 100, duration: number = 0): void {
|
|
99
|
-
this.setColor({ h: 0, s: 0, b: brightness, k: kelvin }, duration);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Query full state */
|
|
103
|
-
getState(): void {
|
|
104
|
-
this.send(MessageType.Get);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** Query power state */
|
|
108
|
-
getPower(): void {
|
|
109
|
-
this.send(MessageType.GetPower);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Set label */
|
|
113
|
-
setLabel(label: string): void {
|
|
114
|
-
const msg = encodeMessage({
|
|
115
|
-
type: MessageType.SetLabel,
|
|
116
|
-
target: this.mac,
|
|
117
|
-
payload: encodeSetLabel(label),
|
|
118
|
-
ackRequired: true
|
|
119
|
-
});
|
|
120
|
-
this.socket.send(this.ip, this.port, msg);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** Query label */
|
|
124
|
-
getLabel(): void {
|
|
125
|
-
this.send(MessageType.GetLabel);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Set group */
|
|
129
|
-
setGroup(id: string, label: string): void {
|
|
130
|
-
const msg = encodeMessage({
|
|
131
|
-
type: MessageType.SetGroup,
|
|
132
|
-
target: this.mac,
|
|
133
|
-
payload: encodeSetGroup(id, label),
|
|
134
|
-
ackRequired: true
|
|
135
|
-
});
|
|
136
|
-
this.socket.send(this.ip, this.port, msg);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Query group */
|
|
140
|
-
getGroup(): void {
|
|
141
|
-
this.send(MessageType.GetGroup);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** Set location */
|
|
145
|
-
setLocation(id: string, label: string): void {
|
|
146
|
-
const msg = encodeMessage({
|
|
147
|
-
type: MessageType.SetLocation,
|
|
148
|
-
target: this.mac,
|
|
149
|
-
payload: encodeSetLocation(id, label),
|
|
150
|
-
ackRequired: true
|
|
151
|
-
});
|
|
152
|
-
this.socket.send(this.ip, this.port, msg);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Query location */
|
|
156
|
-
getLocation(): void {
|
|
157
|
-
this.send(MessageType.GetLocation);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** Query version */
|
|
161
|
-
getVersion(): void {
|
|
162
|
-
this.send(MessageType.GetVersion);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** Update last seen timestamp and online status */
|
|
166
|
-
markSeen(): void {
|
|
167
|
-
this.lastSeen = Date.now();
|
|
168
|
-
this.online = true;
|
|
169
|
-
}
|
|
170
|
-
}
|
package/index.ts
DELETED