index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {Machine03Class} from "./machine03Class";
  2. import * as tools from "@/utils/ble/tools";
  3. import {formatZero, logs, sleep} from "@/utils/util";
  4. import {showError, showToast} from "@/utils/app";
  5. import {isBluetoothEnabled} from "@/utils/uniFunction";
  6. import {isH5} from "@/utils/platform";
  7. import {audioClass} from "@/utils/machine/audioClass";
  8. import {ab2hex} from "@/utils/ble/hex";
  9. export class Machine {
  10. constructor() {
  11. logs('machineV2 类加载')
  12. this.isRunCreate = false
  13. this.open = false
  14. this.connectList = {};
  15. this.foundRun = !1
  16. this.foundCallback = null
  17. this.notificationFun = {}
  18. this.connectionStateFun = {}
  19. this.audioClass = null
  20. this.queueCreated = false
  21. this.queueRun = false
  22. this.queueInterval = null
  23. this.queueLists = []
  24. this.queueOutNum = 0
  25. this.Machine03Class = null
  26. /* #ifdef H5 */
  27. window.$machine = this
  28. /* #endif */
  29. }
  30. /**
  31. * @returns {Machine03Class}
  32. */
  33. machine03() {
  34. if (!this.Machine03Class) {
  35. this.Machine03Class = new Machine03Class(this)
  36. }
  37. return this.Machine03Class
  38. }
  39. /**
  40. *
  41. * @returns {audioClass}
  42. */
  43. audioHelper(obj = null) {
  44. if (!this.audioClass) this.audioClass = new audioClass(this)
  45. if (obj) this.audioClass.setOption(obj)
  46. return this.audioClass
  47. }
  48. async init() {
  49. if (this.isRunCreate) return true;
  50. logs('machineV2 init')
  51. if (isH5) {
  52. showToast('H5不支持蓝牙功能')
  53. return false
  54. }
  55. await tools._openAdapter();
  56. await sleep(1000)
  57. this.open = await isBluetoothEnabled()
  58. let that = this
  59. this.bleDeviceFound(null)
  60. /* #ifndef H5 */
  61. uni.onBluetoothAdapterStateChange(({available}) => {
  62. if (available == false && that.open == true) {
  63. showError('请检查手机蓝牙是否已开启')
  64. if (that.connectionStateFun.length > 0) {
  65. for (const k in that.connectionStateFun) {
  66. that.connectionStateFun[k]()
  67. }
  68. }
  69. } else if (available == true && that.open == false) {
  70. // 是否需要再次init
  71. tools._openAdapter();
  72. }
  73. if (that.open != available) {
  74. console.log(`蓝牙状态被更新 原:${that.open}`)
  75. that.open = available
  76. console.log(`蓝牙状态被更新 现:${available}`)
  77. }
  78. })
  79. /* #endif */
  80. // 内容监听
  81. /* #ifndef H5 */
  82. uni.onBLECharacteristicValueChange(async (res) => {
  83. res.str = ab2hex(res.value)
  84. let key = this.connectList[res.deviceId]
  85. if (!key) return
  86. let fun = this.notificationFun[key]
  87. if (fun) await fun(res)
  88. })
  89. /* #endif */
  90. /* #ifndef H5 */
  91. // 断线监听
  92. uni.onBLEConnectionStateChange(async (res) => {
  93. // 该方法回调中可以用于处理连接意外断开等异常情况
  94. if (res.connected) return
  95. let key = this.connectList[res.deviceId]
  96. logs(`掉线 deviceId:${res.deviceId} key:${key}`)
  97. if (!key) return
  98. let fun = this.connectionStateFun[key]
  99. if (fun) await fun(res)
  100. })
  101. /* #endif */
  102. this.isRunCreate = true
  103. return true
  104. }
  105. bleDeviceFound(callback = null) {
  106. this.foundCallback = callback
  107. if (!this.foundRun) {
  108. this.foundRun = true
  109. uni.onBluetoothDeviceFound((devices) => {
  110. // console.log('onBluetoothDeviceFound', devices)
  111. if (this.foundCallback) this.foundCallback(devices)
  112. })
  113. }
  114. }
  115. addConnect(deviceId, key) {
  116. this.connectList[deviceId] = key
  117. }
  118. delConnect(deviceId) {
  119. if (this.connectList[deviceId]) delete this.connectList[deviceId]
  120. }
  121. queueAdd(task) {
  122. this.queueLists.push(task)
  123. if (!this.queueCreated) {
  124. this.queueCreated = true
  125. this.queueRunHandle()
  126. this.queueInterval = setInterval(() => {
  127. this.queueRunHandle()
  128. }, 350);
  129. }
  130. }
  131. queueRunHandle() {
  132. if (this.queueRun) return
  133. this.queueRun = true
  134. let l = this.queueLists.length || 0
  135. if (l > 0) {
  136. this.queueOutNum = 0
  137. // 执行队列中的任务
  138. const {mT, p, t = 1, o = {}} = this.queueLists.shift();
  139. // console.log('queueRunHandle', mT, p, t)
  140. // 执行任务的逻辑
  141. let mKey = formatZero(mT, 2)
  142. let machineClass = this[`Machine${mKey}Class`]
  143. if (machineClass && machineClass.deviceId) {
  144. machineClass.sentOrder(p, t, o)
  145. }
  146. } else {
  147. this.queueOutNum++
  148. if (this.queueOutNum >= 400) this.clear()
  149. }
  150. this.queueRun = false
  151. }
  152. // 清空队列
  153. clear() {
  154. this.queueLists = [];
  155. if (this.queueInterval) {
  156. clearInterval(this.queueInterval);
  157. this.queueInterval = null;
  158. this.queueCreated = false
  159. this.queueRun = false
  160. }
  161. }
  162. }