|
|
const WXAPI = require('./request')
|
|
class IotTask{
|
|
constructor(lockId,doTask){
|
|
this.lockId = lockId
|
|
this.response = null;
|
|
this.taskContent = '' //锁返回的
|
|
this.timer = null //定时器
|
|
this.interval = 1000; //任务执行间隔
|
|
this.doTask = doTask;
|
|
}
|
|
run(){
|
|
this.timer = setInterval(async () => {
|
|
await this.getTask()
|
|
const lockeResult = await this.doTask(this.response)
|
|
this.taskContent = lockeResult
|
|
}, this.interval);
|
|
}
|
|
|
|
clearTimer(){
|
|
clearInterval(this.timer)
|
|
this.timer = null
|
|
}
|
|
|
|
async getTask(){
|
|
//如果获取任务为空
|
|
const params = {
|
|
code: "zg/bleIotTransmit",
|
|
data: {
|
|
lockerId: this.lockId,
|
|
hexString: this.taskContent
|
|
}
|
|
}
|
|
let res = null
|
|
try{
|
|
res = await WXAPI.sendCommand(params)
|
|
console.log('后台获取的任务',res)
|
|
if(res.code != 200){
|
|
this.clearTimer()
|
|
return
|
|
}
|
|
if(res.data.sendContentsHexString){
|
|
wx.showLoading({
|
|
title: '同步中',
|
|
})
|
|
const len = res.data.sendContentsHexString.length
|
|
console.log(len)
|
|
this.response = res.data.sendContentsHexString.substring(36,len)
|
|
console.log(this.response)
|
|
if(!this.response){
|
|
wx.showToast({
|
|
title: '没有可同步的任务',
|
|
icon:'error'
|
|
})
|
|
wx.hideLoading()
|
|
this.clearTimer()
|
|
}
|
|
}else{
|
|
wx.showToast({
|
|
title: '没有可同步的任务',
|
|
icon:'error'
|
|
})
|
|
this.clearTimer()
|
|
}
|
|
}catch(e){
|
|
if(e.errMsg == 'request:fail fail:time out'){
|
|
wx.showToast({
|
|
title: '接口请求超时',
|
|
icon: 'error'
|
|
})
|
|
this.clearTimer()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = IotTask
|