machine03Class.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {dataToHexHandle, orderSplit, orderToData} from '../ble/hex';
  2. import {logs, sleep} from "../util";
  3. import {showToast} from "../app";
  4. import {bleBase} from "@/utils/ble";
  5. import {ProtocolFormat03} from "@/utils/machine/manufacturerConfig";
  6. import {equimentConnectConfig} from "@/utils/machine/config";
  7. export class Machine03Class extends bleBase {
  8. constructor(topClass) {
  9. super(topClass)
  10. /******************* 常规配置 *******************/
  11. this.mType = 3
  12. this.typeKey = 'machine03'
  13. this.option = equimentConnectConfig[3]
  14. /******************* 常规配置 *******************/
  15. this.machine = {} // 设备蓝牙信息
  16. this.machineInfo = {} // 网关信息
  17. this.notify = false
  18. this.notifyThis = null
  19. this.init()
  20. this.allMachine = {} // 所有设备地址
  21. this.config = {
  22. group: -1,
  23. status: 0,
  24. list: {
  25. // init 0 未上线;1 已上线;2 错误;
  26. // status -1 未上线;0 待检测;1 被遮挡;2 触发;3 未安装;
  27. },
  28. track: {},
  29. }
  30. this.interval = 0
  31. }
  32. async watch(str) {
  33. const {code, order} = orderSplit(str, ProtocolFormat03);
  34. if (code != 'D1') logs('watch str', str)
  35. // logs('code', code, order)
  36. let result = order;
  37. let acceptOrder = this.getAcceptOrder(code)
  38. if (acceptOrder) result = orderToData(order, acceptOrder)
  39. // logs('code', code)
  40. // logs('acceptOrder', acceptOrder)
  41. // logs('result', result)
  42. let runThat = this.notify ? this.notifyThis : null
  43. if (!runThat) return
  44. switch (code) {
  45. // 断线
  46. case '99':
  47. result = '设备断线了'
  48. this.closeBle()
  49. break
  50. default:
  51. console.log('未知内容', code, order)
  52. break
  53. }
  54. return !0;
  55. }
  56. init() {
  57. /* #ifdef H5 */
  58. window.machine03 = async (str) => {
  59. await this.watch(str)
  60. }
  61. /* #endif */
  62. this.topClass.notificationFun[this.typeKey] = async ({str}) => {
  63. await this.watch(str)
  64. }
  65. this.topClass.connectionStateFun[this.typeKey] = async () => {
  66. await this.closeBle()
  67. await this.watch('029900')
  68. }
  69. }
  70. /**
  71. * 连接机器
  72. */
  73. connectBle(machine) {
  74. return new Promise(async (resolve, reject) => {
  75. try {
  76. if (!machine) throw '请确保设备 deviceId 不为空'
  77. let {deviceId} = machine
  78. uni.showLoading({
  79. title: '正在连接',
  80. })
  81. await this.connectionBle(deviceId)
  82. this.machine = machine
  83. await sleep(1000)
  84. // 连接并监听
  85. logs('✔ 设备连接成功,可以开始监听')
  86. uni.hideLoading()
  87. return resolve()
  88. } catch (error) {
  89. console.error('connectBle', error)
  90. await this.closeBle() // 失败断开并清除连接信息
  91. uni.hideLoading()
  92. showToast(error, 4000)
  93. return reject()
  94. }
  95. })
  96. }
  97. async closeBle() {
  98. await this.closeConnection()
  99. }
  100. /**
  101. * @returns {string}
  102. */
  103. async sendCmd(cmd, data = null) {
  104. const cmdMap = {
  105. '07': '0207020000',
  106. '14': () => {
  107. return {
  108. p: `02140500${dataToHexHandle(data.t, 8)}`,
  109. o: data.o,
  110. }
  111. },
  112. }
  113. let result = cmdMap[cmd]
  114. if (!result) return
  115. if (typeof result === 'function') {
  116. result = result(data);
  117. }
  118. // console.log('result', result)
  119. if (!Array.isArray(result)) {
  120. result = [result]
  121. }
  122. // 如果返回的是数组,则逐个加入队列
  123. result.forEach(str => {
  124. let d = {
  125. t: 1,
  126. p: '',
  127. o: null,
  128. }
  129. if (typeof str == 'string') {
  130. d.p = str
  131. } else if (typeof str == 'object') {
  132. d = Object.assign(d, str)
  133. }
  134. // this.sentOrder(d.p, d.t, d.o)
  135. this.topClass.queueAdd({
  136. mT: this.mType,
  137. p: d.p,
  138. o: d.o,
  139. });
  140. });
  141. return true
  142. }
  143. getAcceptOrder(cmd) {
  144. let order = {
  145. '87': [
  146. {k: 'battery'},
  147. {k: 'k1'},
  148. {k: 'group', l: 8},
  149. {k: 'time', l: 8},
  150. ],
  151. '94': [
  152. {k: 'time', l: 8},
  153. ],
  154. '97': [
  155. {k: 'ok'},
  156. {k: 'k1', l: 8},
  157. ],
  158. 'D1': [
  159. {k: 'k1'},
  160. {k: 'time', l: 8},
  161. ],
  162. }
  163. return order[cmd] || null
  164. }
  165. }