123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- import {Machine03Class} from "./machine03Class";
- import * as tools from "@/utils/ble/tools";
- import {formatZero, logs, sleep} from "@/utils/util";
- import {showError, showToast} from "@/utils/app";
- import {isBluetoothEnabled} from "@/utils/uniFunction";
- import {isH5} from "@/utils/platform";
- import {audioClass} from "@/utils/machine/audioClass";
- import {ab2hex} from "@/utils/ble/hex";
- export class Machine {
- constructor() {
- logs('machineV2 类加载')
- this.isRunCreate = false
- this.open = false
- this.connectList = {};
- this.foundRun = !1
- this.foundCallback = null
- this.notificationFun = {}
- this.connectionStateFun = {}
- this.audioClass = null
- this.queueCreated = false
- this.queueRun = false
- this.queueInterval = null
- this.queueLists = []
- this.queueOutNum = 0
- this.Machine03Class = null
- /* #ifdef H5 */
- window.$machine = this
- /* #endif */
- }
- /**
- * @returns {Machine03Class}
- */
- machine03() {
- if (!this.Machine03Class) {
- this.Machine03Class = new Machine03Class(this)
- }
- return this.Machine03Class
- }
- /**
- *
- * @returns {audioClass}
- */
- audioHelper(obj = null) {
- if (!this.audioClass) this.audioClass = new audioClass(this)
- if (obj) this.audioClass.setOption(obj)
- return this.audioClass
- }
- async init() {
- if (this.isRunCreate) return true;
- logs('machineV2 init')
- if (isH5) {
- showToast('H5不支持蓝牙功能')
- return false
- }
- await tools._openAdapter();
- await sleep(1000)
- this.open = await isBluetoothEnabled()
- let that = this
- this.bleDeviceFound(null)
- /* #ifndef H5 */
- uni.onBluetoothAdapterStateChange(({available}) => {
- if (available == false && that.open == true) {
- showError('请检查手机蓝牙是否已开启')
- if (that.connectionStateFun.length > 0) {
- for (const k in that.connectionStateFun) {
- that.connectionStateFun[k]()
- }
- }
- } else if (available == true && that.open == false) {
- // 是否需要再次init
- tools._openAdapter();
- }
- if (that.open != available) {
- console.log(`蓝牙状态被更新 原:${that.open}`)
- that.open = available
- console.log(`蓝牙状态被更新 现:${available}`)
- }
- })
- /* #endif */
- // 内容监听
- /* #ifndef H5 */
- uni.onBLECharacteristicValueChange(async (res) => {
- res.str = ab2hex(res.value)
- let key = this.connectList[res.deviceId]
- if (!key) return
- let fun = this.notificationFun[key]
- if (fun) await fun(res)
- })
- /* #endif */
- /* #ifndef H5 */
- // 断线监听
- uni.onBLEConnectionStateChange(async (res) => {
- // 该方法回调中可以用于处理连接意外断开等异常情况
- if (res.connected) return
- let key = this.connectList[res.deviceId]
- logs(`掉线 deviceId:${res.deviceId} key:${key}`)
- if (!key) return
- let fun = this.connectionStateFun[key]
- if (fun) await fun(res)
- })
- /* #endif */
- this.isRunCreate = true
- return true
- }
- bleDeviceFound(callback = null) {
- this.foundCallback = callback
- if (!this.foundRun) {
- this.foundRun = true
- uni.onBluetoothDeviceFound((devices) => {
- // console.log('onBluetoothDeviceFound', devices)
- if (this.foundCallback) this.foundCallback(devices)
- })
- }
- }
- addConnect(deviceId, key) {
- this.connectList[deviceId] = key
- }
- delConnect(deviceId) {
- if (this.connectList[deviceId]) delete this.connectList[deviceId]
- }
- queueAdd(task) {
- this.queueLists.push(task)
- if (!this.queueCreated) {
- this.queueCreated = true
- this.queueRunHandle()
- this.queueInterval = setInterval(() => {
- this.queueRunHandle()
- }, 350);
- }
- }
- queueRunHandle() {
- if (this.queueRun) return
- this.queueRun = true
- let l = this.queueLists.length || 0
- if (l > 0) {
- this.queueOutNum = 0
- // 执行队列中的任务
- const {mT, p, t = 1, o = {}} = this.queueLists.shift();
- // console.log('queueRunHandle', mT, p, t)
- // 执行任务的逻辑
- let mKey = formatZero(mT, 2)
- let machineClass = this[`Machine${mKey}Class`]
- if (machineClass && machineClass.deviceId) {
- machineClass.sentOrder(p, t, o)
- }
- } else {
- this.queueOutNum++
- if (this.queueOutNum >= 400) this.clear()
- }
- this.queueRun = false
- }
- // 清空队列
- clear() {
- this.queueLists = [];
- if (this.queueInterval) {
- clearInterval(this.queueInterval);
- this.queueInterval = null;
- this.queueCreated = false
- this.queueRun = false
- }
- }
- }
|