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.
121 lines
3.3 KiB
121 lines
3.3 KiB
'use strict';
|
|
const Service = require('egg').Service;
|
|
const _ = require("lodash");
|
|
class HomeService extends Service {
|
|
// 开始定时任务
|
|
async startSend(params) {
|
|
let { app, ctx } = this;
|
|
await ctx.service.utils.sleep(500);
|
|
if (!_.isEmpty(params)) {
|
|
let newArr = []
|
|
for (let i = 0; i < app.comKey.length; i++) {
|
|
let element = app.comKey[i];
|
|
let item = _.find(params, o => o.key === element.key)
|
|
if (item) {
|
|
newArr.push({ ...element })
|
|
}
|
|
}
|
|
app.timerTemp = true
|
|
while (app.timerTemp) {
|
|
await this.exec(newArr)
|
|
}
|
|
}
|
|
}
|
|
//初始化发送一次查询命令
|
|
async startSendOnce(params) {
|
|
let { app } = this;
|
|
let newArr = []
|
|
for (let i = 0; i < app.comKey.length; i++) {
|
|
let element = app.comKey[i];
|
|
let item = _.find(params, o => o.key === element.key)
|
|
if (item) {
|
|
newArr.push({ ...element })
|
|
}
|
|
}
|
|
await this.exec(newArr)
|
|
return {
|
|
haserror: false,
|
|
msg: '查询完成',
|
|
}
|
|
}
|
|
|
|
//结束定时任务
|
|
async endSend() {
|
|
let { app } = this;
|
|
app.timerTemp = false
|
|
return {
|
|
haserror: false,
|
|
msg: `成功关闭该页面定时任务`,
|
|
}
|
|
}
|
|
//执行任务
|
|
async exec(newArr) {
|
|
let { ctx } = this;
|
|
for (let i = 0; i < newArr.length; i++) {
|
|
let element = newArr[i];
|
|
await ctx.service.message.index(element);
|
|
}
|
|
}
|
|
// ArrayBuffer转16进制字符串
|
|
arrayBuffer2Hex(buffer) {
|
|
let hexArr = Array.prototype.map.call(
|
|
new Uint8Array(buffer),
|
|
function (bit) {
|
|
return ('00' + bit.toString(16)).slice(-2)
|
|
}
|
|
)
|
|
return hexArr.join(" ")
|
|
}
|
|
//收到数据后发送数据
|
|
async sendData(params, data, isDirct = false) {
|
|
if (data) {
|
|
let { app } = this;
|
|
let resData = this.arrayBuffer2Hex(Buffer.from(data))
|
|
//像端口发布信息
|
|
if (isDirct) {
|
|
app.mqttServe.publish(`/${params.ripCom}`, resData, {
|
|
qos: 2,
|
|
retain: false,
|
|
})
|
|
} else {
|
|
let keyList = ["relay", "WLK10A"]
|
|
if (keyList.indexOf(params.key) === -1) {
|
|
if (!resData.indexOf(params.ripBackStart)) {
|
|
app.mqttServe.publish(`/${params.key}`, resData, {
|
|
qos: 2,
|
|
retain: false,
|
|
})
|
|
}
|
|
} else {
|
|
if (params.key === "WLK10A") {
|
|
if (!resData.indexOf(params.ripBackStart)) {
|
|
for (let j = 0; j < params.execFun.length; j++) {
|
|
let execItem = params.execFun[j];
|
|
app.mqttServe.publish(`/${execItem}`, resData, {
|
|
qos: 2,
|
|
retain: false,
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
if (params.key === "relay") {
|
|
if (!resData.indexOf(params.ripBackStart[0])) {
|
|
app.mqttServe.publish(`/${params.key}1`, resData, {
|
|
qos: 2,
|
|
retain: false,
|
|
})
|
|
} else if (!resData.indexOf(params.ripBackStart[1])) {
|
|
app.mqttServe.publish(`/${params.key}2`, resData, {
|
|
qos: 2,
|
|
retain: false,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = HomeService;
|