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, date: '', time: '', showSettingResult: false, settingResultText: '操作已完成!' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log('bluebooth home 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; let time = util.getTime().substring(0, 5) + ":00"; let date = util.getDate(); this.setData({ devices: devices, trySendCount: 0, datagramIndex: 0, time: time, date: date }) task_timer = undefined; this.getRtuClock(); 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 clock page onHide ', '**********************************************') this.cancelTime(); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { console.log('bluebooth clock onUnload ', '**********************************************') this.cancelTime(); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log('bluebooth clock onShow ', '**********************************************') this.timeCallback(); }, settingCompletTabEvent(e) { this.setData({ showSettingResult: false }) const btn = e.detail.index; if (btn == 0) { wx.navigateBack(); } }, bindDateChange: function (e) { console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ date: e.detail.value }) }, bindTimeChange: function (e) { console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ time: e.detail.value + ":00" }) }, syncClockBtnTap: function () { this.cancelTime(); let tm = util.getTimeStr(); console.log(tm) this.setRtuClock(tm); }, submitClockBtnTap: function () { this.cancelTime(); let tm = this.data.date.substring(0, 4) + this.data.date.substring(5, 7) + this.data.date.substring(8, 10); tm += this.data.time.substring(0, 2) + this.data.time.substring(3, 5) + this.data.time.substring(6, 8); console.log(tm) this.setRtuClock(tm); }, getRtuClock() { let datagram = datagramHelp.datagram_51(); let cache = this.data.datagramCache; cache.push(datagram); }, /** * 设置时钟 * @param {时钟格式 yyyymmddhhmmss} clock */ setRtuClock(clock) { let datagram = datagramHelp.datagram_4A(clock); 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") { } else if (funcode == '45') { } else if (funcode == '47') { } else if (funcode == '48') { } else if (funcode == '41') { } else if (funcode == '40') { } else if (funcode == '43') { } else if (funcode == '42') { } else if (funcode == '4a') { this.setData({ showSettingResult: true, settingResultText: "时钟同步操作已完成!" }) // wx.showToast({ // title: '操作已完成!', // icon: 'ok', // duration: 2000 //持续的时间 // }) } else if (funcode == '51') { var info = datagramHelp.datagram_51_analyse(onedatagram) console.log(info) let ct = info['collect_time']; if (ct != undefined && ct.length > 0) { let rtuInfo = this.data.rtuInfo; rtuInfo['collect_time'] = ct; 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 == 'f3') { } 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 }) } })