@evops/lightwaverf 0.0.7 → 0.0.8
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/dist/LightwaveAccount.d.ts +13 -0
- package/dist/LightwaveAccount.js +81 -0
- package/dist/LightwaveDevice.d.ts +26 -0
- package/dist/LightwaveDevice.js +64 -0
- package/dist/LightwaveJsonMessageProcessor.d.ts +10 -0
- package/dist/LightwaveJsonMessageProcessor.js +24 -0
- package/dist/LightwaveMessage.d.ts +9 -0
- package/dist/LightwaveMessage.js +2 -0
- package/dist/LightwaveMessageProcessor.d.ts +7 -0
- package/dist/LightwaveMessageProcessor.js +3 -0
- package/dist/LightwaveRFClient.d.ts +32 -0
- package/dist/LightwaveRFClient.js +202 -0
- package/dist/LightwaveTextMessageProcessor.d.ts +10 -0
- package/dist/LightwaveTextMessageProcessor.js +25 -0
- package/dist/LightwaveTransaction.d.ts +8 -0
- package/dist/LightwaveTransaction.js +2 -0
- package/dist/Queue.d.ts +7 -0
- package/dist/Queue.js +19 -0
- package/dist/index.d.ts +13 -74
- package/dist/index.js +116 -343
- package/package.json +2 -2
- package/src/LightwaveAccount.ts +107 -0
- package/src/LightwaveDevice.ts +66 -0
- package/src/LightwaveJsonMessageProcessor.ts +29 -0
- package/src/LightwaveMessageProcessor.ts +8 -0
- package/src/LightwaveRFClient.ts +236 -0
- package/src/LightwaveTextMessageProcessor.ts +30 -0
- package/src/LightwaveTransaction.ts +9 -0
- package/src/Queue.ts +17 -0
- package/src/index.ts +332 -0
- package/index.ts +0 -586
package/src/index.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import Debug from 'debug';
|
|
3
|
+
import { LightwaveRFClient } from './LightwaveRFClient';
|
|
4
|
+
import { LightwaveAccount } from './LightwaveAccount';
|
|
5
|
+
import { LightwaveDevice } from './LightwaveDevice';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LightwaveRFConfiguration {
|
|
9
|
+
timeout?: number = 1000;
|
|
10
|
+
ip?: string;
|
|
11
|
+
file?: any;
|
|
12
|
+
host?: any;
|
|
13
|
+
email?: any;
|
|
14
|
+
pin?: any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare interface ILightwaveRF {
|
|
18
|
+
on(event: 'deviceTurnedOn', listener: (roomId: number, deviceId: number) => void): this;
|
|
19
|
+
on(event: 'deviceTurnedOff', listener: (roomId: number, deviceId: number) => void): this;
|
|
20
|
+
on(event: 'deviceDimmed', listener: (roomId: number, deviceId: number, percentage: number) => void): this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** * LightwaveRF API
|
|
24
|
+
*
|
|
25
|
+
* @param object config The config
|
|
26
|
+
*
|
|
27
|
+
* An instance of the LightwaveRF API
|
|
28
|
+
*/
|
|
29
|
+
export default class LightwaveRF extends EventEmitter implements ILightwaveRF {
|
|
30
|
+
timeout: number = 1000;
|
|
31
|
+
queue: any = [];
|
|
32
|
+
ready = true;
|
|
33
|
+
awaitRegistrration = false;
|
|
34
|
+
currentTransactionNumber: number = 4674773;
|
|
35
|
+
devices: Array<LightwaveDevice> = [];
|
|
36
|
+
messageCounter = 0;
|
|
37
|
+
config: LightwaveRFConfiguration = {};
|
|
38
|
+
responseListeners = new Map<number, any>();
|
|
39
|
+
lwClient: LightwaveRFClient;
|
|
40
|
+
debug: Debug.Debugger;
|
|
41
|
+
|
|
42
|
+
constructor(config: LightwaveRFConfiguration, callback: (config: any, error: any) => void) {
|
|
43
|
+
super();
|
|
44
|
+
this.debug = Debug('lightwaverf');
|
|
45
|
+
|
|
46
|
+
this.debug('Initialising LightwaveRF Client')
|
|
47
|
+
this.lwClient = new LightwaveRFClient(this.debug, config.ip)
|
|
48
|
+
this.lwClient.on('ready', () => {
|
|
49
|
+
this.debug('LightwaveRF ready');
|
|
50
|
+
this.initialiseConfiguration(callback);
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
this.timeout = config.timeout || 1000;
|
|
54
|
+
|
|
55
|
+
this.devices = [];//[{roomId:0,roomName:'',
|
|
56
|
+
//deviceId:0,deviceName:'',
|
|
57
|
+
//deviceType:''}];
|
|
58
|
+
|
|
59
|
+
//Config
|
|
60
|
+
this.config = config;
|
|
61
|
+
|
|
62
|
+
const self = this;
|
|
63
|
+
|
|
64
|
+
this.lwClient.on('deviceTurnedOn', function () {
|
|
65
|
+
self.emit('deviceTurnedOn', ...arguments);
|
|
66
|
+
})
|
|
67
|
+
this.lwClient.on('deviceTurnedOff', function () {
|
|
68
|
+
self.emit('deviceTurnedOff', ...arguments);
|
|
69
|
+
})
|
|
70
|
+
this.lwClient.on('deviceDimmed', function () {
|
|
71
|
+
self.emit('deviceDimmed', ...arguments);
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
//Receive message
|
|
75
|
+
// this.receiveSocket.on("message", function (message: Buffer, rinfo: dgram.RemoteInfo) {
|
|
76
|
+
// // If we were using broadcast IP, we have now
|
|
77
|
+
// // discovered Link device IP and can switch off
|
|
78
|
+
// // broadcast
|
|
79
|
+
// if (self.config.ip == '255.255.255.255') {
|
|
80
|
+
// console.log("We have now discovered Link IP address: %s", rinfo.address);
|
|
81
|
+
// self.config.ip = rinfo.address
|
|
82
|
+
// self.sendSocket.setBroadcast(false)
|
|
83
|
+
// }
|
|
84
|
+
|
|
85
|
+
// //Check this came from the lightwave unit
|
|
86
|
+
// if (rinfo.address !== self.config.ip) {
|
|
87
|
+
// //Came from wrong ip]
|
|
88
|
+
// console.warn("Response came from a different IP than our configured", rinfo.address, self.config.ip)
|
|
89
|
+
// return false;
|
|
90
|
+
// }
|
|
91
|
+
|
|
92
|
+
// const parseResponse = (buffer: Buffer) => {
|
|
93
|
+
// const response: any = new Object();
|
|
94
|
+
// const message = buffer.toString('utf-8');
|
|
95
|
+
// if (message.match(/^\*!/)) {
|
|
96
|
+
// const jsonResponse = JSON.parse(message.replace(/^\*!/, ''))
|
|
97
|
+
// self.currentTransactionNumber = jsonResponse.trans + 1;
|
|
98
|
+
// Object.assign(response, jsonResponse)
|
|
99
|
+
|
|
100
|
+
// response.error = response.pkt === "error" ? response.fn : null;
|
|
101
|
+
// } else {
|
|
102
|
+
// //Split off the code for the message
|
|
103
|
+
// var parts = message.split(",");
|
|
104
|
+
// var trans = parts.splice(0, 1);
|
|
105
|
+
// var content = parts.join(",").replace(/(\r\n|\n|\r)/gm, "");
|
|
106
|
+
// response.trans = parseInt(trans[0]);
|
|
107
|
+
// response.message = content;
|
|
108
|
+
// response.error = content.match("^ERR") ? content : null;
|
|
109
|
+
// }
|
|
110
|
+
|
|
111
|
+
// response.trans = response.trans !== null ? parseInt(response.trans) : null;
|
|
112
|
+
|
|
113
|
+
// return response;
|
|
114
|
+
// }
|
|
115
|
+
|
|
116
|
+
// let linkResponse = parseResponse(message)
|
|
117
|
+
// debug(">>>>>>>> Received response msg: %s, response: %s, rinfo: %s", message, linkResponse, rinfo);
|
|
118
|
+
// if (linkResponse.error === "nonRegistered" && !self.awaitRegistrration) {
|
|
119
|
+
// console.warn("Your device is not registered, please accept registration on the Link devices")
|
|
120
|
+
// self.register(() => { });
|
|
121
|
+
// }
|
|
122
|
+
|
|
123
|
+
// if (linkResponse.msg === "success" && linkResponse.pairType) {
|
|
124
|
+
// self.awaitRegistrration = false;
|
|
125
|
+
// }
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
// debug(self.responseListeners);
|
|
130
|
+
// var responseListenerData = self.responseListeners.get(linkResponse.trans);
|
|
131
|
+
// if (!responseListenerData) {
|
|
132
|
+
// debug("We haven't got anyone to respond to, ignoring the message")
|
|
133
|
+
// return;
|
|
134
|
+
// }
|
|
135
|
+
|
|
136
|
+
// debug(`[Transaction: ${linkResponse.trans}] Processing time: ${new Date().getTime() - responseListenerData.time}`)
|
|
137
|
+
|
|
138
|
+
// responseListenerData.listener(
|
|
139
|
+
// linkResponse.error,
|
|
140
|
+
// linkResponse.fn,
|
|
141
|
+
// );
|
|
142
|
+
|
|
143
|
+
// self.responseListeners.delete(linkResponse.trans);
|
|
144
|
+
// });
|
|
145
|
+
|
|
146
|
+
// this.receiveSocket.on("listening", function () {
|
|
147
|
+
// var address = self.receiveSocket.address();
|
|
148
|
+
// debug("Receiver socket listening " + address.address + ":" + address.port);
|
|
149
|
+
|
|
150
|
+
// self.send('@H', (code, err) => {
|
|
151
|
+
// if (err) {
|
|
152
|
+
// console.log('code', code, 'error', err)
|
|
153
|
+
// return
|
|
154
|
+
// }
|
|
155
|
+
|
|
156
|
+
// self.initialiseConfiguration(callback);
|
|
157
|
+
// })
|
|
158
|
+
// });
|
|
159
|
+
|
|
160
|
+
// this.sendSocket.bind();
|
|
161
|
+
|
|
162
|
+
// this.sendSocket.on('listening', () => {
|
|
163
|
+
// debug("Send socket is ready")
|
|
164
|
+
// debug("Setting up receiver socket")
|
|
165
|
+
// //Bind to the receive port
|
|
166
|
+
// self.receiveSocket.bind(9761);
|
|
167
|
+
// })
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
process.on('SIGINT', () => {
|
|
171
|
+
this.stop();
|
|
172
|
+
this.lwClient.stop();
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
stop() {
|
|
177
|
+
this.debug("Stopping server sockets")
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
initialiseConfiguration(callback: (config: any, error: string) => void) {
|
|
181
|
+
//Check config
|
|
182
|
+
const lwAccount = new LightwaveAccount(this.debug, this.lwClient, this.config.email, this.config.pin)
|
|
183
|
+
lwAccount.getConfiguration(callback)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Register this device with the Wi-Fi Link
|
|
189
|
+
*
|
|
190
|
+
* @param Function callback The callback function
|
|
191
|
+
*
|
|
192
|
+
* @return void
|
|
193
|
+
*/
|
|
194
|
+
register(callback: any) {
|
|
195
|
+
this.awaitRegistrration = true;
|
|
196
|
+
this.sendUdp("!F*p", callback);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Turn a device off
|
|
201
|
+
*
|
|
202
|
+
* @param integer roomId The room ID
|
|
203
|
+
* @param integer deviceId The device ID
|
|
204
|
+
* @param Function callback The callback for if there are any errors
|
|
205
|
+
*
|
|
206
|
+
* @return void
|
|
207
|
+
*/
|
|
208
|
+
turnDeviceOff(roomId: number, deviceId: number, callback?: any) {
|
|
209
|
+
var state = "0";
|
|
210
|
+
this.exec("!R" + roomId + "D" + deviceId + "F" + state + "|\0", callback);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Turn a device on
|
|
215
|
+
*
|
|
216
|
+
* @param integer roomId The room ID
|
|
217
|
+
* @param integer deviceId The device ID
|
|
218
|
+
* @param Function callback The callback for if there are any errors
|
|
219
|
+
*
|
|
220
|
+
* @return void
|
|
221
|
+
*/
|
|
222
|
+
turnDeviceOn(roomId: number, deviceId: number, callback?: any) {
|
|
223
|
+
// this.devices.find(d => d.roomId == roomId && d.deviceId == deviceId)?.turnOn();
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Set the dim percentage of a device
|
|
227
|
+
*
|
|
228
|
+
* @param integer roomId The room ID
|
|
229
|
+
* @param integer deviceId The device ID
|
|
230
|
+
* @param integer dimPercentage The percentage to set the device dim
|
|
231
|
+
* @param Function callback The callback for if there are any errors
|
|
232
|
+
*
|
|
233
|
+
* @return void
|
|
234
|
+
*/
|
|
235
|
+
setDeviceDim(roomId: string, deviceId: string, dimPercentage: number, callback: any) {
|
|
236
|
+
// var dimAmount = dimPercentage * 0.32; //Dim is on a scale from 0 to 32
|
|
237
|
+
|
|
238
|
+
// if (dimAmount === 0) {
|
|
239
|
+
// this.turnDeviceOff(roomId, deviceId, callback);
|
|
240
|
+
// } else {
|
|
241
|
+
// this.exec("!R" + roomId + "D" + deviceId + "FdP" + dimAmount + "|\0", callback);
|
|
242
|
+
// }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Get message code
|
|
247
|
+
*
|
|
248
|
+
* @return string
|
|
249
|
+
*/
|
|
250
|
+
private getTransactionNumber(): number {
|
|
251
|
+
return this.currentTransactionNumber;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
private exec(...args: any[]) {
|
|
256
|
+
// Check if the queue has a reasonable size
|
|
257
|
+
if (this.queue.length > 100) {
|
|
258
|
+
this.queue.pop();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
this.debug("Ading to queue: " + args.join(" "));
|
|
262
|
+
this.queue.push(args);
|
|
263
|
+
this.process();
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
private send(cmd: string, callback: (code: any, err: any) => void) {
|
|
267
|
+
this.sendUdp(cmd, callback);
|
|
268
|
+
//if (callback) callback();
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Send a message over udp
|
|
272
|
+
*
|
|
273
|
+
* @param string message The message to send
|
|
274
|
+
* @param Function callback The callback for if there are any errors
|
|
275
|
+
*
|
|
276
|
+
* @return void
|
|
277
|
+
*/
|
|
278
|
+
private sendUdp(message: string, callback: any) {
|
|
279
|
+
//Add to message
|
|
280
|
+
const transactionNumber = this.getTransactionNumber();
|
|
281
|
+
//Prepend code to message
|
|
282
|
+
message = `${transactionNumber},${message}`;
|
|
283
|
+
|
|
284
|
+
this.debug(`[${this.config.ip}][trans: ${transactionNumber}] Sending message: ${message}`);
|
|
285
|
+
|
|
286
|
+
//Create buffer from message
|
|
287
|
+
const messageBuffer = Buffer.from(message, 'utf-8');
|
|
288
|
+
|
|
289
|
+
this.debug("Callback for message: " + message, callback);
|
|
290
|
+
//Add listener
|
|
291
|
+
if (callback) {
|
|
292
|
+
this.debug("Registering call back with transaction number: " + transactionNumber);
|
|
293
|
+
this.responseListeners.set(transactionNumber, {
|
|
294
|
+
time: new Date().getTime(),
|
|
295
|
+
listener: callback
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
this.debug(this.responseListeners)
|
|
299
|
+
|
|
300
|
+
// Expire request, trigger retry
|
|
301
|
+
setTimeout(() => {
|
|
302
|
+
const listener = this.responseListeners.get(transactionNumber);
|
|
303
|
+
if (listener) {
|
|
304
|
+
this.debug(`[Transaction $(transactionNumber)] The listener is still there, triggering error`);
|
|
305
|
+
this.responseListeners.delete(transactionNumber);
|
|
306
|
+
callback("ERR:EXPIRED", undefined);
|
|
307
|
+
}
|
|
308
|
+
}, 1000);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
//Broadcast the message
|
|
312
|
+
// this.sendSocket.send(messageBuffer, 0, messageBuffer.length, 9760, this.config.ip);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private process() {
|
|
316
|
+
this.debug("Checking queue")
|
|
317
|
+
if (this.queue.length === 0) return;
|
|
318
|
+
if (!this.ready) return;
|
|
319
|
+
var self = this;
|
|
320
|
+
this.ready = false;
|
|
321
|
+
this.debug("Processing queue...");
|
|
322
|
+
this.debug("Items in the queue", this.queue.length);
|
|
323
|
+
this.send.apply(this, this.queue.shift());
|
|
324
|
+
setTimeout(function () {
|
|
325
|
+
self.ready = true;
|
|
326
|
+
self.process();
|
|
327
|
+
}, this.timeout);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
}
|