-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (41 loc) · 1.07 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
const http = require('http');
const qs = require('querystring');
const net = require('net');
const _ = require('./services/common');
http.createServer(function(req,res) {
var body = '';
req.on('data', function(data) {
body += data;
if (body.length > 1e6) {
req.connection.destroy();
}
});
req.on('end', function() {
var post = qs.parse(body);
post && intiConnection(post);
});
res.setHeader('Access-Control-Allow-Origin', '*');
res.write('SUCCESS');
res.end();
}).listen(8080);
console.log('LED Service Started. Listening Port: 8080');
process.on('uncaughtException', function(err) {
console.log(new Error(err));
});
function intiConnection (data) {
const socket = net.createConnection({
port: 5200,
host: data.ip
});
socket.on('connect', () => {
var res = _.CP5200Write(data);
socket.write(res, 'hex');
});
socket.on('data', (data) => {
socket.destroy();
});
socket.on('error', (err) => {
console.log(new Error(err));
socket.destroy();
});
}