-
Notifications
You must be signed in to change notification settings - Fork 8
/
Orvibo.js
246 lines (202 loc) · 7.6 KB
/
Orvibo.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const net = require('net');
const Packet = require('./OrviboPacket');
const PacketBuilder = require('./PacketBuilder');
const Utils = require('./Utils');
const Settings = require('./OrviboSettings');
const EventEmitter = require('events').EventEmitter;
let logger = console;
let ORVIBO_KEY = Settings.ORVIBO_KEY;
let LOG_PACKET = Settings.LOG_PACKET;
let PLUG_INFO = Settings.plugInfo;
const Orvibo = function(userSettings = {}) {
// Allow user to pass in settings
if('ORVIBO_KEY' in userSettings) ORVIBO_KEY = userSettings.ORVIBO_KEY;
if('plugInfo' in userSettings) PLUG_INFO = userSettings.plugInfo;
if('PLUG_INFO' in userSettings) PLUG_INFO = userSettings.PLUG_INFO;
if('LOG_PACKET' in userSettings) LOG_PACKET = userSettings.LOG_PACKET;
if (ORVIBO_KEY === '') {
logger.log('Please pass Orvibo PK key details via the constructor or add to OrviboSettings.js file. See Readme');
process.exit(1);
}
};
Object.assign(Orvibo.prototype, EventEmitter.prototype);
let HEARTBEAT = 32;
let HELLO = 0;
let HANDSHAKE = 6;
let STATE_UPDATE = 42;
let STATE_UPDATE_CONFIRM = 15;
let UNKNOWN_CMD = 'UNKNOWN_CMD';
let port = 10001;
let bindHost = '0.0.0.0';
let plugConnections = [];
let packetData = {};
let getNameForUid = (uid) => {
let item = PLUG_INFO.find(item => item.uid === uid);
return item != null ? item.name : 'unknown';
};
let getData = (id) => {
return packetData[id];
};
let setData = (id, data) => {
packetData[id] = data;
};
let respondAndSetData = (data, socket, packetFunction) => {
setData(socket.id, data);
socket.write(packetFunction(data));
};
let helloHandler = (plugPacket, socket) => {
let pkData = {
serial: plugPacket.getSerial(),
encryptionKey: Utils.generateRandomTextValue(16),
id: Utils.generateRandomHexValue(32),
modelId: plugPacket.getModelId(),
orviboKey: plugPacket.getOrviboKey()
};
respondAndSetData(pkData, socket, PacketBuilder.helloPacket);
};
let handshakeHandler = function(plugPacket, socket, socketData) {
let uid = plugPacket.getUid();
let pkData = Object.assign({}, socketData, {
serial: plugPacket.getSerial(),
uid,
name: getNameForUid(uid)
});
respondAndSetData(pkData, socket, PacketBuilder.handshakePacket);
logger.log(`Connected ${pkData.uid} name = ${pkData.name}`);
this.emit('plugConnected', {uid:pkData.uid, name: pkData.name});
};
let heartbeatHandler = function(plugPacket, socket, socketData) {
let pkData = Object.assign({}, socketData, {
serial: plugPacket.getSerial(),
uid: plugPacket.getUid()
});
respondAndSetData(pkData, socket, PacketBuilder.heartbeatPacket);
logger.log(`Plug ${pkData.name} ${pkData.uid} sent heartbeat`);
this.emit('gotHeartbeat', {uid:pkData.uid, name: pkData.name});
};
let stateUpdateHandler = function(plugPacket, socket, socketData) {
let pkData = Object.assign({}, socketData, {
serial: plugPacket.getSerial(),
uid: plugPacket.getUid(),
state: plugPacket.getValue1()
});
respondAndSetData(pkData, socket, PacketBuilder.comfirmStatePacket);
logger.log(`Plug ${pkData.name} ${pkData.uid} updated state ${pkData.state}`);
this.emit('plugStateUpdated', {uid:pkData.uid, state: pkData.state, name: pkData.name});
};
let stateConfirmHandler = function() {
// Do nothing at this stage
};
let unknownCmdHandler = function(plugPacket, socket, socketData) {
let pkData = Object.assign({}, socketData, {
serial: plugPacket.getSerial(),
uid: plugPacket.getUid(),
});
respondAndSetData(pkData, socket, PacketBuilder.defaultPacket);
};
Orvibo.prototype.handlers = function() {
return {
[HELLO]: helloHandler.bind(this),
[HANDSHAKE]: handshakeHandler.bind(this),
[HEARTBEAT]: heartbeatHandler.bind(this),
[STATE_UPDATE]: stateUpdateHandler.bind(this),
[STATE_UPDATE_CONFIRM] : stateConfirmHandler,
[UNKNOWN_CMD]: unknownCmdHandler.bind(this)
};
};
Orvibo.prototype.startServer = function() {
let self = this;
let handlers = this.handlers();
logger.log(`Starting server Orvibo socket server on port ${port}`);
this.server = net.createServer(function(socket) {
socket.id = Utils.generateRandomTextValue(16);
socket.setKeepAlive(true, 10000);
plugConnections.push(socket);
socket.on('data', function (data) {
let socketData = getData(socket.id);
let plugPacket = new Packet(data);
if (!plugPacket.validCRC()) {
logger.log('Got invalid CRC');
return;
}
try {
if (plugPacket.packetTypeText() === 'pk') {
plugPacket.processPacket(ORVIBO_KEY);
} else {
plugPacket.processPacket(socketData.encryptionKey);
}
} catch(err) {
logger.log('Failed to parse packet: ' + err);
return;
}
LOG_PACKET && plugPacket.logPacket('Socket -> ');
let handler = handlers[plugPacket.getCommand()];
if (handler != null) {
handler(plugPacket, socket, socketData);
} else {
handlers[UNKNOWN_CMD](plugPacket, socket, socketData);
}
});
socket.on('end', function () {
let pkData = getData(socket.id);
logger.log(`Plug ${pkData.uid} - ${pkData.name} disconnected`);
self.emit('plugDisconnected', {uid: pkData.uid, name: pkData.name});
delete packetData[socket.id];
plugConnections.splice(plugConnections.indexOf(socket), 1);
});
socket.on('error', (err) => {
logger.log(err);
logger.log(`Plug ${socket.id} - ${socket.name} disconnected with error`);
self.emit('plugDisconnectedWithError', getData(socket.id));
delete packetData[socket.id];
plugConnections.splice(plugConnections.indexOf(socket), 1);
});
});
this.server.listen(port, bindHost);
};
Orvibo.prototype.toggleSocket = function(uid) {
let socketId = null;
for (const key of Object.keys(packetData)) {
if (packetData[key].uid === uid) {
socketId = key;
break
}
}
if (socketId === null) {
logger.log('Could not find socket ' + uid);
return;
}
let socket = plugConnections.find(s => s.id === socketId);
if (socket != null) {
let socketData = getData(socketId);
let data = Object.assign({}, socketData,{
state: socketData.state === 1 ? 0 : 1,
serial: Utils.generateRandomNumber(8),
clientSessionId: socketData.clientSessionId ? socketData.clientSessionId : Utils.generateRandomHexValue(32),
deviceId: socketData.deviceId ? socketData.deviceId : Utils.generateRandomHexValue(32),
});
setData(socket.id, data);
let packet = PacketBuilder.updatePacket(data);
socket.write(packet);
} else {
logger.log('Can not find socket ');
}
};
Orvibo.prototype.getConnectedSockets = function() {
let sockets = [];
for (const key of Object.keys(packetData)) {
let socketData = getData(key);
sockets.push({
name: socketData.name,
state: socketData.state,
uid: socketData.uid,
modelId: socketData.modelId
});
}
return sockets;
};
Orvibo.prototype.getConnectedSocket = Orvibo.prototype.getConnectedSockets;
Orvibo.prototype.setLogger = function(newLogger) {
logger = newLogger;
};
module.exports = Orvibo;