@evops/lightwaverf 0.0.5 → 0.0.9
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/LICENSE +7 -0
- package/README.md +2 -82
- 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 +98 -0
- package/dist/index.js +278 -0
- package/package.json +14 -11
- 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/bin/lightwaverf.js +0 -27
- package/index.js +0 -561
- package/test.js +0 -19
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import dgram from 'dgram';
|
|
2
|
+
import events from 'events';
|
|
3
|
+
import Debug, { Debugger } from 'debug';
|
|
4
|
+
import LightwaveMessageProcessor from './LightwaveMessageProcessor';
|
|
5
|
+
import LightwaveTextMessageProcessor from './LightwaveTextMessageProcessor';
|
|
6
|
+
import { LightwaveJsonMessageProcessor } from './LightwaveJsonMessageProcessor';
|
|
7
|
+
import { LightwaveTransaction } from './LightwaveTransaction';
|
|
8
|
+
import { Queue } from './Queue';
|
|
9
|
+
|
|
10
|
+
declare interface LightwaveRFClientInterface {
|
|
11
|
+
on(event: 'ready', listener: (client: LightwaveRFClient) => void): this;
|
|
12
|
+
on(event: 'deviceTurnedOn', listener: (room: number, device: number) => void): this;
|
|
13
|
+
on(event: 'deviceTurnedOff', listener: (room: number, device: number) => void): this;
|
|
14
|
+
on(event: 'deviceDimmed', listener: (room: number, device: number, dimPercentage: number) => void): this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class LightwaveRFClient extends events.EventEmitter implements LightwaveRFClientInterface {
|
|
18
|
+
private senderSocket: dgram.Socket;
|
|
19
|
+
private receiverSocket: dgram.Socket;
|
|
20
|
+
|
|
21
|
+
private senderSocketReady: boolean = false;
|
|
22
|
+
private receiverSocketReady: boolean = false;
|
|
23
|
+
|
|
24
|
+
private commandQueue: Queue<LightwaveTransaction> = new Queue<LightwaveTransaction>();
|
|
25
|
+
|
|
26
|
+
private messageProcessors: LightwaveMessageProcessor[] = [];
|
|
27
|
+
|
|
28
|
+
private debug: Debug.Debugger;
|
|
29
|
+
private ip: string;
|
|
30
|
+
private transactionListeners: Map<number, any> = new Map();
|
|
31
|
+
delay: number = 125;
|
|
32
|
+
|
|
33
|
+
constructor(debug: Debugger, ip: string = "255.255.255.255", port: number = 9761) {
|
|
34
|
+
super();
|
|
35
|
+
|
|
36
|
+
this.ip = ip;
|
|
37
|
+
this.debug = debug.extend('client');
|
|
38
|
+
this.senderSocket = dgram.createSocket('udp4');
|
|
39
|
+
this.receiverSocket = dgram.createSocket('udp4');
|
|
40
|
+
|
|
41
|
+
this.messageProcessors.push(new LightwaveJsonMessageProcessor(this.debug));
|
|
42
|
+
this.messageProcessors.push(new LightwaveTextMessageProcessor(this.debug));
|
|
43
|
+
|
|
44
|
+
this.receiverSocket.on('message', (buffer: Buffer) => {
|
|
45
|
+
this.debug("RAW: %o", buffer.toString('utf-8'));
|
|
46
|
+
});
|
|
47
|
+
this.receiverSocket.on('message', this.processRawMessage.bind(this));
|
|
48
|
+
|
|
49
|
+
this.receiverSocket.once('listening', () => {
|
|
50
|
+
this.debug("Receiver socket bound", this.receiverSocket.address());
|
|
51
|
+
this.receiverSocketReady = true
|
|
52
|
+
});
|
|
53
|
+
this.receiverSocket.once('listening', this.socketReady.bind(this));
|
|
54
|
+
|
|
55
|
+
this.senderSocket.once('listening', () => {
|
|
56
|
+
this.debug("Sender socket bound", this.senderSocket.address());
|
|
57
|
+
this.senderSocketReady = true
|
|
58
|
+
});
|
|
59
|
+
this.senderSocket.once('listening', this.socketReady.bind(this));
|
|
60
|
+
|
|
61
|
+
this.senderSocket.unref();
|
|
62
|
+
|
|
63
|
+
this.senderSocket.bind();
|
|
64
|
+
|
|
65
|
+
this.debug("Binding receiver socket on address %s and port %d", ip, port);
|
|
66
|
+
this.receiverSocket.bind(port);
|
|
67
|
+
|
|
68
|
+
this.receiverSocket.on('error', () => {
|
|
69
|
+
throw new Error("Error binding receiver socket");
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
process.on('SIGINT', () => {
|
|
73
|
+
this.debug("\nClosing sockets");
|
|
74
|
+
this.stop();
|
|
75
|
+
process.exit();
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public stop() {
|
|
80
|
+
try {
|
|
81
|
+
this.receiverSocket.close();
|
|
82
|
+
} catch { }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private socketReady() {
|
|
86
|
+
if (!this.senderSocketReady) return;
|
|
87
|
+
if (!this.receiverSocketReady) return;
|
|
88
|
+
|
|
89
|
+
this.checkRegistration();
|
|
90
|
+
this.processQueue();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private checkRegistration() {
|
|
94
|
+
this.debug("Checking registration");
|
|
95
|
+
this.send("@H", (transaction: LightwaveTransaction | null, error: Error) => {
|
|
96
|
+
if (error) {
|
|
97
|
+
this.debug('Error: %o', error);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
this.debug(transaction);
|
|
101
|
+
this.emit('ready');
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public send(message: (string | Buffer), callback: (transaction: LightwaveTransaction | null, error: Error) => void) {
|
|
106
|
+
const transaction = Math.round(Math.random() * Math.pow(10, 8));
|
|
107
|
+
const messageWithTransaction = `${transaction},!${message}`;
|
|
108
|
+
const transactionDebug = this.debug.extend("transaction:" + transaction);
|
|
109
|
+
|
|
110
|
+
this.transactionListeners.set(transaction, {
|
|
111
|
+
message: message,
|
|
112
|
+
debug: transactionDebug,
|
|
113
|
+
delay: this.delay,
|
|
114
|
+
callback: callback
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
this.debug("Queueing message: %s", messageWithTransaction);
|
|
118
|
+
|
|
119
|
+
this.commandQueue.add(<LightwaveTransaction>{
|
|
120
|
+
id: transaction,
|
|
121
|
+
message: messageWithTransaction,
|
|
122
|
+
debug: transactionDebug,
|
|
123
|
+
callback
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
this.processQueue();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async processQueue() {
|
|
130
|
+
if (this.commandQueue.busy) {
|
|
131
|
+
setTimeout(this.processQueue.bind(this), this.delay);
|
|
132
|
+
return
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const command = this.commandQueue.receive();
|
|
136
|
+
if (!command) return;
|
|
137
|
+
|
|
138
|
+
this.commandQueue.busy = true;
|
|
139
|
+
const transactionListener = this.transactionListeners.get(command.id);
|
|
140
|
+
const originalCallback = transactionListener.callback;
|
|
141
|
+
|
|
142
|
+
transactionListener.callback = (transaction: LightwaveTransaction, error: Error) => {
|
|
143
|
+
this.commandQueue.busy = false;
|
|
144
|
+
setTimeout(this.processQueue.bind(this), this.delay);
|
|
145
|
+
originalCallback(transaction, error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
setTimeout(() => {
|
|
149
|
+
const listener = this.transactionListeners.get(command.id);
|
|
150
|
+
if (!listener) return;
|
|
151
|
+
|
|
152
|
+
originalCallback(null, new Error("Execution expired"));
|
|
153
|
+
this.transactionListeners.delete(command.id);
|
|
154
|
+
this.processQueue();
|
|
155
|
+
}, 5000)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
const transactionDebug = this.debug.extend(`transaction:${command?.id}`);
|
|
159
|
+
transactionDebug("Starting new transaction");
|
|
160
|
+
this.exec(command!.message, (message: LightwaveTransaction | null, error: Error) => { });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private exec(message: (Buffer | string | undefined), callback: (message: LightwaveTransaction | null, error: Error) => void) {
|
|
164
|
+
if (message === undefined) return;
|
|
165
|
+
|
|
166
|
+
this.debug("Sending message: %s to %s on port %d", message, this.ip, 9760);
|
|
167
|
+
this.senderSocket.setBroadcast(this.ip === "255.255.255.255");
|
|
168
|
+
this.senderSocket.send(message, 9760, this.ip, (error: Error | null, bytes: number) => {
|
|
169
|
+
if (error) {
|
|
170
|
+
this.debug("Message send errror: %o", error);
|
|
171
|
+
}
|
|
172
|
+
this.debug("Message sent: %s, length: %d", message, bytes);
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private processRawMessage(message: Buffer, remoteInfo: dgram.RemoteInfo) {
|
|
177
|
+
this.debug("Message has come through from %o", remoteInfo);
|
|
178
|
+
this.debug("Message received: %o", message.toString('ascii'));
|
|
179
|
+
|
|
180
|
+
for (const messageProcessor of this.messageProcessors) {
|
|
181
|
+
if (messageProcessor.canProcess(message)) {
|
|
182
|
+
this.debug("Message can be processed by %s", messageProcessor.constructor.name);
|
|
183
|
+
const lightwaveMessage = messageProcessor.process(message);
|
|
184
|
+
this.processLightwaveMessage(lightwaveMessage);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
throw "Message cannot be processed";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
processLightwaveMessage(lightwaveMessage: any) {
|
|
193
|
+
this.debug("Processing lightwave message: %o", lightwaveMessage);
|
|
194
|
+
this.debug("Current listeners: %o", this.transactionListeners);
|
|
195
|
+
|
|
196
|
+
this.debug("Link response fn", lightwaveMessage.fn)
|
|
197
|
+
if (lightwaveMessage.fn === "on") {
|
|
198
|
+
this.emit("deviceTurnedOn", lightwaveMessage.room, lightwaveMessage.dev);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (lightwaveMessage.fn === "off") {
|
|
202
|
+
this.emit("deviceTurnedOff", lightwaveMessage.room, lightwaveMessage.dev);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (lightwaveMessage.fn === "dim") {
|
|
206
|
+
this.emit("deviceDimmed", lightwaveMessage.room, lightwaveMessage.dev, Math.round(lightwaveMessage.param / 32 * 100));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const listener = this.transactionListeners.get(lightwaveMessage.id);
|
|
210
|
+
if (!listener) return;
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
if (lightwaveMessage.error?.match(/^ERR,6,/)) {
|
|
214
|
+
listener.delay = listener.delay * 2;
|
|
215
|
+
const msg = `${lightwaveMessage.id},!${listener.message}`
|
|
216
|
+
this.debug("message errorred, retrying: %o, %o with delay: %o", msg, listener, listener.delay);
|
|
217
|
+
setTimeout(() => {
|
|
218
|
+
this.exec(msg, (message: LightwaveTransaction | null, error: Error) => {
|
|
219
|
+
this.debug("message: %o, error: %o", message, error)
|
|
220
|
+
});
|
|
221
|
+
}, Math.round(listener.delay));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (listener) {
|
|
226
|
+
listener.debug("Found transaction listener");
|
|
227
|
+
listener.callback(lightwaveMessage, null);
|
|
228
|
+
listener.debug("Transaction completed, removing listener");
|
|
229
|
+
this.transactionListeners.delete(lightwaveMessage.id);
|
|
230
|
+
this.commandQueue.busy = false;
|
|
231
|
+
} else {
|
|
232
|
+
this.debug("Listener not found for message: %o", lightwaveMessage)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Debugger } from "debug";
|
|
2
|
+
import "./LightwaveTransaction";
|
|
3
|
+
import LightwaveMessageProcessor from "./LightwaveMessageProcessor";
|
|
4
|
+
|
|
5
|
+
export default class LightwaveTextMessageProcessor implements LightwaveMessageProcessor {
|
|
6
|
+
debug: Debugger;
|
|
7
|
+
constructor(debug: Debugger) {
|
|
8
|
+
this.debug = debug.extend('textMessageProcessor');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
process(message: Buffer): any {
|
|
12
|
+
this.debug('Processing message')
|
|
13
|
+
const textMessage = message.toString('utf-8');
|
|
14
|
+
var parts = textMessage.split(",");
|
|
15
|
+
var trans = parts.splice(0, 1);
|
|
16
|
+
var content = parts.join(",").replace(/(\r\n|\n|\r)/gm, "");
|
|
17
|
+
|
|
18
|
+
const response: any = {};
|
|
19
|
+
response.id = parseInt(trans[0]);
|
|
20
|
+
response.message = content;
|
|
21
|
+
response.error = content.match("^ERR") ? content : null;
|
|
22
|
+
return response;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
canProcess(message: Buffer): boolean {
|
|
26
|
+
this.debug("Checking if can process message");
|
|
27
|
+
return message.toString('utf-8').endsWith("\n");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
}
|
package/src/Queue.ts
ADDED
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
|
+
}
|
package/bin/lightwaverf.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const LightwaveRF = require('..');
|
|
4
|
-
|
|
5
|
-
require('../index');
|
|
6
|
-
|
|
7
|
-
new LightwaveRF({
|
|
8
|
-
ip: '255.255.255.255',
|
|
9
|
-
email: 'stan@wozniak.com',
|
|
10
|
-
pin: 2212
|
|
11
|
-
}, (devices, lr) => {
|
|
12
|
-
console.log("Welcome to LightwaveRF V1")
|
|
13
|
-
lr.turnDeviceOn(1, 1, handleResponse)
|
|
14
|
-
setTimeout(() => {
|
|
15
|
-
|
|
16
|
-
lr.turnDeviceOff(1, 1, handleResponse)
|
|
17
|
-
}, 1111)
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
function handleResponse(stringResponse, jsonResponse, err) {
|
|
21
|
-
if (err) {
|
|
22
|
-
console.log("We could not turn the device on!", stringResponse, jsonResponse, err)
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
console.log("Lamp is on!", stringResponse, jsonResponse)
|
|
27
|
-
}
|