123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import {dataToHexHandle, orderSplit, orderToData} from '../ble/hex';
- import {logs, sleep} from "../util";
- import {showToast} from "../app";
- import {bleBase} from "@/utils/ble";
- import {ProtocolFormat03} from "@/utils/machine/manufacturerConfig";
- import {equimentConnectConfig} from "@/utils/machine/config";
- export class Machine03Class extends bleBase {
- constructor(topClass) {
- super(topClass)
- /******************* 常规配置 *******************/
- this.mType = 3
- this.typeKey = 'machine03'
- this.option = equimentConnectConfig[3]
- /******************* 常规配置 *******************/
- this.machine = {} // 设备蓝牙信息
- this.machineInfo = {} // 网关信息
- this.notify = false
- this.notifyThis = null
- this.init()
- this.allMachine = {} // 所有设备地址
- this.config = {
- group: -1,
- status: 0,
- list: {
- // init 0 未上线;1 已上线;2 错误;
- // status -1 未上线;0 待检测;1 被遮挡;2 触发;3 未安装;
- },
- track: {},
- }
- this.interval = 0
- }
- async watch(str) {
- const {code, order} = orderSplit(str, ProtocolFormat03);
- if (code != 'D1') logs('watch str', str)
- // logs('code', code, order)
- let result = order;
- let acceptOrder = this.getAcceptOrder(code)
- if (acceptOrder) result = orderToData(order, acceptOrder)
- // logs('code', code)
- // logs('acceptOrder', acceptOrder)
- // logs('result', result)
- let runThat = this.notify ? this.notifyThis : null
- if (!runThat) return
- switch (code) {
- // 断线
- case '99':
- result = '设备断线了'
- this.closeBle()
- break
- default:
- console.log('未知内容', code, order)
- break
- }
- return !0;
- }
- init() {
- /* #ifdef H5 */
- window.machine03 = async (str) => {
- await this.watch(str)
- }
- /* #endif */
- this.topClass.notificationFun[this.typeKey] = async ({str}) => {
- await this.watch(str)
- }
- this.topClass.connectionStateFun[this.typeKey] = async () => {
- await this.closeBle()
- await this.watch('029900')
- }
- }
- /**
- * 连接机器
- */
- connectBle(machine) {
- return new Promise(async (resolve, reject) => {
- try {
- if (!machine) throw '请确保设备 deviceId 不为空'
- let {deviceId} = machine
- uni.showLoading({
- title: '正在连接',
- })
- await this.connectionBle(deviceId)
- this.machine = machine
- await sleep(1000)
- // 连接并监听
- logs('✔ 设备连接成功,可以开始监听')
- uni.hideLoading()
- return resolve()
- } catch (error) {
- console.error('connectBle', error)
- await this.closeBle() // 失败断开并清除连接信息
- uni.hideLoading()
- showToast(error, 4000)
- return reject()
- }
- })
- }
- async closeBle() {
- await this.closeConnection()
- }
- /**
- * @returns {string}
- */
- async sendCmd(cmd, data = null) {
- const cmdMap = {
- '07': '0207020000',
- '14': () => {
- return {
- p: `02140500${dataToHexHandle(data.t, 8)}`,
- o: data.o,
- }
- },
- }
- let result = cmdMap[cmd]
- if (!result) return
- if (typeof result === 'function') {
- result = result(data);
- }
- // console.log('result', result)
- if (!Array.isArray(result)) {
- result = [result]
- }
- // 如果返回的是数组,则逐个加入队列
- result.forEach(str => {
- let d = {
- t: 1,
- p: '',
- o: null,
- }
- if (typeof str == 'string') {
- d.p = str
- } else if (typeof str == 'object') {
- d = Object.assign(d, str)
- }
- // this.sentOrder(d.p, d.t, d.o)
- this.topClass.queueAdd({
- mT: this.mType,
- p: d.p,
- o: d.o,
- });
- });
- return true
- }
- getAcceptOrder(cmd) {
- let order = {
- '87': [
- {k: 'battery'},
- {k: 'k1'},
- {k: 'group', l: 8},
- {k: 'time', l: 8},
- ],
- '94': [
- {k: 'time', l: 8},
- ],
- '97': [
- {k: 'ok'},
- {k: 'k1', l: 8},
- ],
- 'D1': [
- {k: 'k1'},
- {k: 'time', l: 8},
- ],
- }
- return order[cmd] || null
- }
- }
|