-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
68 lines (54 loc) · 1.34 KB
/
index.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
const tcp = require('./lib/tcp_connector');
var tcpClient = null;
const init = (options) => {
tcpClient = new tcp.Client(options);
};
const generateError = (errorCode) => {
var errorMessage = null;
switch (errorCode) {
case 100:
errorMessage = 'NOT_FOUND';
break;
case 200:
errorMessage = 'SYNTAX_ERROR';
break;
default:
errorMessage = 'ERROR';
}
return new Error(errorMessage);
};
const command = (command) => {
return tcpClient.request(command)
.then(function (response) {
const result = JSON.parse(response);
if (result.data.error > 0) {
throw (generateError(result.data.error));
}
return result;
});
};
const listActions = () => {
return command('{"cmd": "listactions"}');
};
const listLocations = () => {
return command('{"cmd": "listlocations"}');
};
const listEnergy = () => {
return command('{"cmd": "listenergy"}');
};
const systemInfo = () => {
return command('{"cmd": "systeminfo"}');
};
const executeActions = (id, value) => {
return command('{"cmd": "executeactions", "id":' + id + ', "value1": ' + value + '}');
};
module.exports = {
init: init,
command: command,
listLocations: listLocations,
listActions: listActions,
listEnergy: listEnergy,
systemInfo: systemInfo,
executeActions: executeActions,
events: tcp.eventsBus
};