-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.client
More file actions
76 lines (67 loc) · 1.95 KB
/
Copy pathtcp.client
File metadata and controls
76 lines (67 loc) · 1.95 KB
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
// body-parse is necessary for posting data
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const ejs = require('ejs');
const crc = require('crc');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
const logger = require('morgan');
app.use(logger());
app.use('assets', express.static('public'));
app.engine('html', ejs.__express);
app.set('view engine', 'html');
// parse application/json
// app.use(bodyParser.json());
app.use(function (req, res, next) {
console.log('Time: %f', Date.now());
// res.write('home');
next(); //!!!!!!!!! Important if you want to following request
});
app.get('/login', (req, res, next) => {
res.render('form');
next();
});
app.post('/login', (req, res, next) => {
// res.setHeader('Content-Type', 'text/plain')
let arr = [parseInt('fe', 16).toString(10)];
if (req.body.commd.toString().length > 0) {
arr.push(req.body.commd.toString());
} else {
arr.push('5');
}
if (req.body.path > 0) {
arr.push('00');
arr.push((req.body.path - 1).toString());
} else {
arr.push('00');
arr.push('00');
}
if (!req.body.switch) {
arr.push('00');
arr.push('00');
}
else {
arr.push('ff');
arr.push('00');
}
let tempBuf = Buffer.from(arr);
let buf = crc16Check(arr, tempBuf);
// console.log('arr', arr);
console.log('buf', buf);
// res.end(JSON.stringify(req.body, null, 2));
next();
});
function crc16Check(arr, buf) {
let crcStr = crc.crc16modbus(buf).toString(16);
arr.push(parseInt(crcStr.slice(crcStr.length - 2, crcStr.length), 16).toString(10));
arr.push(parseInt(crcStr.substr(0, crcStr.length - 2), 16).toString(10));
return Buffer.from(arr);
}
const PORT = 3005;
app.listen(PORT, (err) => {
if (err) throw err
else {
console.log(`server listening to ${PORT}`)
}
})