-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
60 lines (53 loc) · 1.39 KB
/
Copy pathutils.js
File metadata and controls
60 lines (53 loc) · 1.39 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
/*
* File: /utils.js
* Project: dtu-server
* Description:
* Created By: Tao.Hu 2019-10-15
* -----
* Last Modified: 2019-10-15 08:11:53 pm
* Modified By: Tao.Hu
* -----
* Copyright (c) 2019 Kideasoft Tech Co.,Ltd
*/
const { crc16modbus } = require('crc');
const { cmdConfig, CMD_QUEUE } = require('./config');
function getCrc(str) {
const data = crc16modbus(Buffer.from(str, 'hex')).toString(16).padStart(4, '0');
const l = data.slice(0, 2);
const h = data.slice(2, 4);
return `${h}${l}`;
}
/**
* 增加crc16校验,生成命令buffer
* @param {string} cmd
* @returns {Buffer} cmd buffer
*/
function addCrc16(cmd) {
const crc = getCrc(cmd);
return Buffer.from(`${cmd}${crc}`, 'hex');
}
function crcCheck(msg) {
if (!msg || msg.length < 8) {
console.log('crc check failed:', msg);
return false;
}
const data = msg.slice(0, msg.length - 4);
const crc = msg.slice(msg.length - 4, msg.length);
return crc === getCrc(data);
}
function getNextCmd(currentCmd) {
if (!currentCmd) return CMD_QUEUE[0];
const index = CMD_QUEUE.findIndex((e) => e.cmd === currentCmd);
return CMD_QUEUE[index + 1] || CMD_QUEUE[0];
}
// 开井
const openWell = (devId) => addCrc16(`${devId}${cmdConfig.open_well.cmd}`);
// 关井
const closeWell = (devId) => addCrc16(`${devId}${cmdConfig.close_well.cmd}`);
module.exports = {
addCrc16,
crcCheck,
openWell,
closeWell,
getNextCmd,
};