const util = require("../../../utils/util"); const datagramHelp = require("../../../utils/datagramHelp") var app = getApp(); var task_timer; Page({ data: { devices: { deviceId: '', serviceId: '', characteristicId: '', connectedBluetoothName: '' }, rtuInfo: { rtu_code: '', ip1: '', ip2: '', ip3: '', ip4: '', rtu_work_model: '', rtu_kind: '', collect_interval: '', water_level_base_point: '', water_level_correct_point: '', datagram_idx: '', collect_time: '', acc_rain: '', fiveMinute_rain: '', hour_rain: '', weater_level: '', power_v: '', rtu_ver: '', rtu_uuid: '', bt_v: '', rtu_rssi: '', rtu_temperature: '', lua_ver: '' }, datagramBuff: '', datagramCache: [], trySendCount: 0, datagramIndex: 0, showSettingResult: false, settingResultText: '操作已完成!' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log('bluebooth info page load ', '**********************************************') let deviceId = options.deviceId; let serviceId = options.serviceId; let characteristicId = options.characteristicId; let connectedBluetoothName = options.connectedBluetoothName; console.log('name' + connectedBluetoothName) let devices = {} devices['deviceId'] = deviceId; devices['serviceId'] = serviceId; devices['characteristicId'] = characteristicId; devices['connectedBluetoothName'] = connectedBluetoothName; this.setData({ devices: devices, trySendCount: 0, datagramIndex: 0 }) task_timer = undefined; this.getRtuStatus(); if (this.data.datagramCache.length > 0) { let datagram = this.data.datagramCache[this.data.datagramIndex]; this.sendDatagramToRtu(datagram); let index = this.data.datagramIndex + 1; if (this.data.datagramCache.length == index) { index = 0; } let count = this.data.trySendCount + 1; this.setData({ datagramIndex: index, trySendCount: count }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { console.log('bluebooth info page onHide ', '**********************************************'); this.cancelTime(); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { console.log('bluebooth info page onUnload ', '**********************************************'); this.cancelTime(); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log('bluebooth info page onShow ', '**********************************************'); this.timeCallback(); }, settingCompletTabEvent(e) { this.setData({ showSettingResult: false }) const btn = e.detail.index; if (btn == 0) { wx.navigateBack(); } }, resetMemryBtnTap: function () { this.cancelTime(); this.formatRtuMemory(); }, resetRtuBtnTap: function () { this.cancelTime(); this.resetRtu(); }, rebootBtnTap: function () { this.cancelTime(); this.rebootRtu(); }, /** * 发送查询RTU状态信息的指令 */ getRtuStatus() { var datagram = datagramHelp.datagram_f7(); let cache = this.data.datagramCache; cache.push(datagram); }, rebootRtu() { let datagram = datagramHelp.datagram_f3(); this.sendDatagramToRtu(datagram); }, /** * 恢复RTU出厂设置 */ resetRtu() { let datagram = datagramHelp.datagram_48(); this.sendDatagramToRtu(datagram); }, /** * 格式化RTU存储器 */ formatRtuMemory() { let datagram = datagramHelp.datagram_47(); this.sendDatagramToRtu(datagram); }, pushDatagramBuff(datagram) { let tmpdatagramBuff = this.data.datagramBuff; if (tmpdatagramBuff.length > 0) { tmpdatagramBuff = tmpdatagramBuff + datagram; } else { tmpdatagramBuff = datagram; } console.log(" datagram buff ==========", tmpdatagramBuff) this.setData({ datagramBuff: tmpdatagramBuff }) this.processDatagram(); }, processDatagram() { let datagram = this.data.datagramBuff; if (datagram.length > 0) { for (let i = 0; i < datagram.length; i++) { if (datagram.indexOf("7e7e") == 0) { break; } else { datagram = datagram.substring(i, datagram.length); } } if (null != datagram && datagram.length > 0 && datagram.indexOf("7e7e") >= 0) { if (datagram.length >= 26) { var lenBuff = util.HexStr2Bytes(datagram.substring(22, 22 + 4)); var len = util.byteToUint16(lenBuff); // 报文长度(int) var datagramlength = 11 + len + 6; if (datagram.length >= datagramlength * 2) { var onedatagram = datagram.substring(0, datagramlength * 2); if (!datagramHelp.crcCheck(onedatagram)) { wx.showToast({ title: '数据CRC校验错误,请重试!', icon: 'none', duration: 2000 }) this.setData({ datagramBuff: '' }) return; } var count = 20; var funcode = datagram.substring(count, count + 2); count += 2; if (funcode == "f6") { } else if (funcode == "f7") { let info = datagramHelp.datagram_f7_analyse(onedatagram) console.log(info) let rtuInfo = this.data.rtuInfo; rtuInfo['collect_time'] = info['collect_time']; rtuInfo['acc_rain'] = info['acc_rain']; rtuInfo['fiveMinute_rain'] = info['fiveMinute_rain']; rtuInfo['hour_rain'] = info['hour_rain']; rtuInfo['fiveMinute_rain'] = info['fiveMinute_rain']; rtuInfo['hour_rain'] = info['hour_rain']; rtuInfo['weater_level'] = info['weater_level']; rtuInfo['power_v'] = info['power_v']; rtuInfo['rtu_uuid'] = info['rtu_uuid']; rtuInfo['rtu_ver'] = info['rtu_ver']; rtuInfo['bt_v'] = info['bt_v']; rtuInfo['rtu_rssi'] = info['rtu_rssi']; rtuInfo['rtu_temperature'] = info['rtu_temperature']; rtuInfo['lua_ver'] = info['lua_ver']; this.setData({ rtuInfo: rtuInfo }); let index = this.data.datagramIndex + 1; if (this.data.datagramCache.length == index) { index = 0; } this.setData({ datagramIndex: index, trySendCount: 0 }) this.cancelTime(); } else if (funcode == '45') { } else if (funcode == '47') { this.setData({ showSettingResult: true, settingResultText: "设备存储重置操作已完成!" }) // wx.showToast({ // title: '操作已完成!', // icon: 'ok', // duration: 2000 //持续的时间 // }) } else if (funcode == '48') { this.setData({ showSettingResult: true, settingResultText: "恢复出厂设置操作已完成!" }) // wx.showToast({ // title: '操作已完成!', // icon: 'ok', // duration: 2000 //持续的时间 // }) } else if (funcode == '41') { } else if (funcode == '40') { } else if (funcode == '43') { } else if (funcode == '42') { } else if (funcode == '4a') { } else if (funcode == '51') { } else if (funcode == 'f3') { this.setData({ showSettingResult: true, settingResultText: "已重启RTU,请重新连接蓝牙!" }) // wx.showToast({ // title: '操作已完成!', // icon: 'ok', // duration: 2000 //持续的时间 // }) } if (datagram.length - datagramlength * 2 > 0) { let tmp = datagram.substring(datagramlength * 2, datagram.length); this.setData({ datagramBuff: tmp }) } } else { this.setData({ datagramBuff: datagram }) } } else { this.setData({ datagramBuff: datagram }) } } else { if (null != datagram && datagram.length > 0) { this.setData({ datagramBuff: datagram }) } else { this.setData({ datagramBuff: '' }) } } } }, sendDatagramToRtu(datagram) { console.log("will send datagram " + datagram); let buffer = util.HexStr2Bytes(datagram); var that = this; let count = 0; let inView = new DataView(buffer); while (true) { if (buffer.byteLength - count > 20) { let tmpBuff = new ArrayBuffer(20); let outView = new DataView(tmpBuff); for (let i = 0; i < 20; i++) { outView.setUint8(i, inView.getUint8(count + i)) } wx.writeBLECharacteristicValue({ deviceId: that.data.devices['deviceId'], serviceId: that.data.devices['serviceId'], characteristicId: that.data.devices['characteristicId'], value: tmpBuff, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) wx.onBLECharacteristicValueChange((characteristic) => { console.log("read datagram==============", util.ab2hex(characteristic.value)) var datagram = util.ab2hex(characteristic.value); that.pushDatagramBuff(datagram); }) wx.readBLECharacteristicValue({ deviceId: that.data.devices['deviceId'], serviceId: that.data.devices['serviceId'], characteristicId: that.data.devices['characteristicId'], success: function (res) { console.log('readBLECharacteristicValue') } }) }, fail(res) { console.log('writeBLECharacteristicValue success', res.errMsg) } }) count += 20; } else { let len = buffer.byteLength - count; let tmpBuff = new ArrayBuffer(len); let outView = new DataView(tmpBuff); for (let i = 0; i < len; i++) { outView.setUint8(i, inView.getUint8(count + i)) } wx.writeBLECharacteristicValue({ deviceId: that.data.devices['deviceId'], serviceId: that.data.devices['serviceId'], characteristicId: that.data.devices['characteristicId'], value: tmpBuff, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) wx.onBLECharacteristicValueChange((characteristic) => { console.log("read datagram==============", util.ab2hex(characteristic.value)) var datagram = util.ab2hex(characteristic.value); that.pushDatagramBuff(datagram); }) wx.readBLECharacteristicValue({ deviceId: that.data.devices['deviceId'], serviceId: that.data.devices['serviceId'], characteristicId: that.data.devices['characteristicId'], success: function (res) { console.log('readBLECharacteristicValue') } }) }, fail(res) { console.log('writeBLECharacteristicValue success', res.errMsg) } }) count += len; } if (count >= buffer.byteLength) { break; } } }, timeCallback() { var that = this; task_timer = setTimeout(function () { console.log("------------------------------------Time CallBack----------------------------"); if (that.data.trySendCount == 0) { let datagram = that.data.datagramCache[that.data.datagramIndex]; that.sendDatagramToRtu(datagram); let index = that.data.datagramIndex + 1; if (that.data.datagramCache.length == index) { index = 0; } let count = that.data.trySendCount + 1; that.setData({ datagramIndex: index, trySendCount: count }) } else { let count = that.data.trySendCount + 1; if (count >= 10) { count = 0; } that.setData({ trySendCount: count }) } that.timeCallback(); }, 3000); }, cancelTime() { if (task_timer != undefined) { clearTimeout(task_timer); task_timer = undefined; } this.setData({ datagramIndex: 0, trySendCount: 0 }) } })