-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (152 loc) · 4.97 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
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
'use strict';
let http = require('http');
class MegaD {
constructor({host = '192.168.0.14', port = 80, password = 'sec'} = {}) {
this.host = host;
this.port = port;
this.password = password;
this.deviceConfig = {};
this.portsConfig = [];
}
static discover() {
let interfaces = require('os').networkInterfaces();
let promise = Promise.resolve();
let result = [];
Object.keys(interfaces).map(iface =>
interfaces[iface].map(address => {
if (address.family == 'IPv4' && !address.internal && address.address) {
promise = promise.then(r => (result = result.concat(r || []), this.discoverOnAddress(address.address)));
}
})
);
return promise.then(() => result);
}
static discoverOnAddress(ip) {
ip = ip.replace(/\.\d+$/, '.255');
let result = [];
let message = new Buffer([0xAA, 0x00, 0x0C]);
let dgram = require('dgram');
let client = dgram.createSocket('udp4');
client.bind(42000, () => client.setBroadcast(true));
client.on('error', err => console.error(ip, err));
client.on('message', (msg, rinfo) => (msg[0] == 0xAA && result.push(rinfo.address)));
client.send(message, 0, message.length, 52000, ip);
return new Promise(resolve => {
setTimeout(() => (client.close(), resolve(result)), 2000);
})
}
getInternalTemp() {
return this._send({tget: 1});
}
getPortsState() {
return this._send({cmd: 'all'});
}
getPortState(port) {
return this._send({pt: port, cmd: 'get'});
}
getDeviceConfig() {
return this._send({cf: 1})
.then(data => this._parseForm(data))
.then(data => {
let {eip = (this.host, this.port).join(':'), pwd} = data;
eip = eip.split(':');
this.host = eip[0];
this.port = parseInt(eip[1]) || 80;
this.password = pwd;
this.deviceConfig = data;
return data;
});
}
setDeviceConfig(config) {
return this._send(Object.assign({cf: 1}, this.deviceConfig, config))
.then(() => this.getDeviceConfig());
}
getPortsConfig() {
let port = 0;
let getPortConfig = () =>
this.getPortConfig(port++)
.then(() => getPortConfig())
.catch(() => this.portsConfig);
return getPortConfig();
}
getPortConfig(port) {
return this._send({pt: port})
.then(data => this._parseForm(data))
.then(data => (this.portsConfig[port] = data, data));
}
setPortConfig(port, config) {
return this._send(Object.assign({pn: port}, this.portsConfig[port], config))
.then(() => this.getPortConfig(port));
}
sendCommand(port, value) {
return this._send({cmd: [port, value].join(':')});
}
sendCounter(port, value = 0) {
return this._send({pt: port, value});
}
_parseForm(html) {
let data = {};
let inputs = html.match(/<input[^>]+name="?[\w]+"?[^>]*>/g) || [];
inputs.map(input => {
let args = {};
(input.match(/(\w+)=([^<>\s]+)/g) || [])
.map(arg => arg.split('='))
.map(arg => args[arg[0]] = arg.slice(1).join('=').replace(/^"(.*)"$/, '$1'));
data[args.name] = args.value || '';
if (args.type == 'checkbox' && input.indexOf('checked') == -1) {
data[args.name] = 0;
}
if (args.type == 'hidden') {
delete data[args.name];
}
});
let selects = html.match(/<select[^>]+name="?[\w]+"?[^>]*>.+?<\/select>/g) || [];
selects.map(select => {
let args = {};
(select.match(/(\w+)=([^<>\s]+)/g) || [])
.map(arg => arg = arg.split('='))
.map(arg => args[arg[0]] = arg[1].replace(/^"(.*)"$/, '$1'));
let option = select.match(/<option value="?(\d+)"? selected>/) || [];
data[args.name] = parseInt(option[1], 10) || 0;
});
if (data.pty === undefined) {
if (html.indexOf('>Type In<') != -1) {
data.pty = 0;
} else if (html.indexOf('>Type Out<') != -1) {
data.pty = 1;
} else if (html.match(/<br>A\d+\//)) {
data.pty = 2;
}
} else {
data.pty = parseInt(data.pty, 10);
}
if (data.pty == 1) {
data.m = data.m || 0;
data.pwm = data.pwm || 0;
}
if (data.ecmd === 'ð=') {
data.ecmd = '';
}
['m', 'd', 'misc', 'pwm', 'pn', 'naf']
.map(i => data[i] !== undefined ? data[i] = (parseInt(data[i], 10) || 0) : undefined);
return data;
}
_send(params) {
let data = Object.keys(params).map(k => [k, params[k]].join('=')).join('&');
let options = {
host: this.host,
port: this.port,
path: '/' + this.password + '/?' + data
};
return new Promise((resolve, reject) =>
http.get(options, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('error', e => (console.error(e), reject(e)));
res.on('data', chunk => (data += chunk));
res.on('end', () => (res.statusCode == 200 ? resolve(data) : reject(data)));
}).on('error', e => (console.error('Got error by post request', e), reject(e)))
);
}
}
module.exports = MegaD;