You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.6 KiB

8 months ago
const { SerialPort } = require('serialport')
const mqtt = require("mqtt");
require('events').EventEmitter.defaultMaxListeners = 0; // 解除限制
const serialPortParams = {
path: "COM5",
baudRate: 9600, //波特率
dataBits: 8, //数据位
parity: 'none', //奇偶校验
stopBits: 1, //停止位
flowControl: false,
autoOpen: false, //不自动打开
// highWaterMark: 64 * 1024,
highWaterMark: 32,
}
class AppBootHook {
constructor(app) {
this.app = app;
}
configWillLoad() {
// 此时 config 文件已经被读取并合并,但是还并未生效
// 这是应用层修改配置的最后时机
// 注意:此函数只支持同步调用
}
async didLoad() {
// 所有的配置已经加载完毕
// 可以用来加载应用自定义的文件,启动自定义的服务
}
async willReady() {
// 所有的插件都已启动完毕,但是应用整体还未 ready
// 可以做一些数据初始化等操作,这些操作成功才会启动应用
}
async didReady() {
// 应用已经启动完毕
}
async serverDidReady() {
// http / https server 已启动,开始接受外部请求
// 此时可以从 app.server 拿到 server 的实例
//生成串口连接
function genSerialport(options) {
return new SerialPort({ ...serialPortParams, ...options })
}
//转矩转速功率测量仪下"COM1"
//转矩转速功率测量仪中"COM2"
//转矩转速功率测量仪上"COM3"
//wlk-10A控制器(下上一体)"COM4"
//XSD5-2隔离型 "COM5"
//初始化串口
this.app.comKey = [
{
key: "XSD5",
ripCom: "COM5",
sendRipId: "01 03 00 00 00 06",
ripBackStart: "01 03",
recieveRipLength: 17,
ripComInstence: genSerialport({ path: "COM5" }),
},
{
key: "TR4DU",
ripCom: "COM3",
sendRipId: "05 03 00 00 00 09",
ripBackStart: "05 03",
recieveRipLength: 23,
ripComInstence: genSerialport({ path: "COM3" }),
},
{
key: "WLK10A",
ripCom: "COM4",
execFun: ["WLK10AU", "WLK10AD"],
sendRipId: "01 03 00 00 00 02",
ripBackStart: "01 03",
recieveRipLength: 9,
ripComInstence: genSerialport({ path: "COM4" }),
},
{
key: "TR4DM",
ripCom: "COM2",
sendRipId: "05 03 00 00 00 09",
ripBackStart: "05 03",
recieveRipLength: 23,
ripComInstence: genSerialport({ path: "COM2" }),
},
{
key: "TR4DD",
ripCom: "COM1",
sendRipId: "05 03 00 00 00 09",
ripBackStart: "05 03",
recieveRipLength: 23,
ripComInstence: genSerialport({ path: "COM1" }),
},
{
key: "relay",
ripCom: "COM7",
// sendRipId: ["08 01 00 41 00 08", "02 01 00 41 00 08"],
sendRipId: ["08 01 00 41 00 08"],
// ripBackStart: ["01 01 01", "02 01 01"],
ripBackStart: ["08 01 01"],
recieveRipLength: 6,
ripComInstence: genSerialport({ path: "COM7" }),
},
// {
// key: "COM6",
// ripCom: "COM6",
// sendRipId: ["01 01 00 41 00 08", "02 01 00 41 00 08"],
// ripBackStart: ["01 01 01", "02 01 01"],
// recieveRipLength: 6,
// ripComInstence: genSerialport({ path: "COM6" }),
// },
]
this.app.mqttServe = mqtt.connect("mqtt://127.0.0.1:1883", {
clean: true,
connectTimeout: 4000,
clientId: 'COM-' + Math.random().toString(16).substring(2),
});
this.app.mqttServe.on("connect", function () {
console.log("appmqtt服务器连接成功");
});
}
}
module.exports = AppBootHook;