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.

77 lines
2.2 KiB

1 year ago
  1. const WXAPI = require('./request')
  2. class IotTask{
  3. constructor(lockId,doTask){
  4. this.lockId = lockId
  5. this.response = null;
  6. this.taskContent = '' //锁返回的
  7. this.timer = null //定时器
  8. this.interval = 1000; //任务执行间隔
  9. this.doTask = doTask;
  10. }
  11. run(){
  12. this.timer = setInterval(async () => {
  13. await this.getTask()
  14. const lockeResult = await this.doTask(this.response)
  15. this.taskContent = lockeResult
  16. }, this.interval);
  17. }
  18. clearTimer(){
  19. clearInterval(this.timer)
  20. this.timer = null
  21. }
  22. async getTask(){
  23. //如果获取任务为空
  24. const params = {
  25. code: "zg/bleIotTransmit",
  26. data: {
  27. lockerId: this.lockId,
  28. hexString: this.taskContent
  29. }
  30. }
  31. let res = null
  32. try{
  33. res = await WXAPI.sendCommand(params)
  34. console.log('后台获取的任务',res)
  35. if(res.code != 200){
  36. this.clearTimer()
  37. return
  38. }
  39. if(res.data.sendContentsHexString){
  40. wx.showLoading({
  41. title: '同步中',
  42. })
  43. const len = res.data.sendContentsHexString.length
  44. console.log(len)
  45. this.response = res.data.sendContentsHexString.substring(36,len)
  46. console.log(this.response)
  47. if(!this.response){
  48. wx.showToast({
  49. title: '没有可同步的任务',
  50. icon:'error'
  51. })
  52. wx.hideLoading()
  53. this.clearTimer()
  54. }
  55. }else{
  56. wx.showToast({
  57. title: '没有可同步的任务',
  58. icon:'error'
  59. })
  60. this.clearTimer()
  61. }
  62. }catch(e){
  63. if(e.errMsg == 'request:fail fail:time out'){
  64. wx.showToast({
  65. title: '接口请求超时',
  66. icon: 'error'
  67. })
  68. this.clearTimer()
  69. return
  70. }
  71. }
  72. }
  73. }
  74. module.exports = IotTask