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: '', rain_precision: '', rain_extra_datagram_threshold: '', water_se_agreement: '', water_se_id: '', water_se_address: '', water_se_format: '', water_se_adjust: '', water_se_symbol: '', addIntervalTime: '', intervalTime: '', datagram_idx: '', collect_time: '', acc_rain: '', fiveMinute_rain: '', hour_rain: '', weater_level: '', power_v: '', riverDeep: '', riverWide: '', riverAngle: '', radarHigh: '', faceCoe: '', speedCoe: '', flowModelCoe: '', rtu_ver: '', rtu_uuid: '', bt_v: '', rtu_rssi: '', rtu_temperature: '', lua_ver: '', agreement: 'sw2014', datagramTimes: [{ value: '5', name: '5分钟', }, { value: '60', name: '1小时', checked: true }, { value: '120', name: '2小时' }, { value: '180', name: '3小时' }, { value: '240', name: '4小时' }, { value: '360', name: '6小时' }, { value: '480', name: '8小时' }, { value: '720', name: '12小时' }, { value: '1440', name: '24小时' } ], waterLevelSensorKind: [{ value: '0', name: '未连接', checked: true }, { value: '1', name: '数字量' }, { value: '2', name: '模似量' } ], flowRateSensorKind: [{ value: '0', name: '未连接', checked: true }, { value: '1', name: '数字量' }, { value: '2', name: '模似量' } ], flowRateSensorProtocolType: [{ value: '0', name: 'modbus', checked: true }, { value: '1', name: '老百特流量计' }, { value: '2', name: '插入式超声波流量计' }, { value: '3', name: 'LMAG流量计' }, { value: '4', name: '京仪流速计' }, { value: '5', name: '91雷达流量计' }, { value: '6', name: '华聚流速计' }, { value: '7', name: '模拟流量计' }, { value: '8', name: '华儒流速计' }, { value: '9', name: '鹰格流速计' }, { value: '10', name: '协议10' }, { value: '11', name: '协议11' }, { value: '12', name: '协议12' }, { value: '13', name: '协议13' }, { value: '14', name: '协议14' } ], flowRateSensorId:'', }, 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.getRtuRunSetting(); 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 runSetting page onHide ', '**********************************************') this.cancelTime(); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { console.log('bluebooth runSetting page onUnload ', '**********************************************') this.cancelTime(); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log('bluebooth runSetting page onShow ', '**********************************************') this.timeCallback(); }, settingCompletTabEvent(e) { this.setData({ showSettingResult: false }) const btn = e.detail.index; if (btn == 0) { wx.navigateBack(); } }, radioChange: function (e) { let intervalTime = e.detail.value; let radioItems = this.data.rtuInfo.datagramTimes; for (let i = 0, len = radioItems.length; i < len; ++i) { radioItems[i].checked = radioItems[i].value == intervalTime; } let rtuInfo = this.data.rtuInfo; rtuInfo.datagramTimes = radioItems; rtuInfo.intervalTime = intervalTime; this.setData({ rtuInfo: rtuInfo }); }, waterLevelSensorKindRadioChange: function (e) { var rtuInfo = this.data.rtuInfo; var radioItems = this.data.rtuInfo.waterLevelSensorKind; for (var i = 0, len = radioItems.length; i < len; ++i) { radioItems[i].checked = radioItems[i].value == e.detail.value; } rtuInfo.waterLevelSensorKind = radioItems; this.setData({ rtuInfo: rtuInfo }); }, flowRateSensorKindRadioChange: function (e) { var rtuInfo = this.data.rtuInfo; var radioItems = this.data.rtuInfo.flowRateSensorKind; for (var i = 0, len = radioItems.length; i < len; ++i) { radioItems[i].checked = radioItems[i].value == e.detail.value; } rtuInfo.flowRateSensorKind = radioItems; this.setData({ rtuInfo: rtuInfo }); }, flowRateProtocolRadioChange: function (e) { var rtuInfo = this.data.rtuInfo; var radioItems = this.data.rtuInfo.flowRateSensorProtocolType; for (var i = 0, len = radioItems.length; i < len; ++i) { radioItems[i].checked = radioItems[i].value == e.detail.value; } rtuInfo.flowRateSensorProtocolType = radioItems; this.setData({ rtuInfo: rtuInfo }); }, addDatagramInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.addIntervalTime = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterBaseValueInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_level_base_point = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterCorrectValueInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_level_correct_point = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, rainPrecisionInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.rain_precision = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, rainExtraDatagramThresholdInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.rain_extra_datagram_threshold = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeAgreementInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_agreement = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeIdInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_id = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeAddressInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_address = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeFormatInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_format = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeAdjustInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_adjust = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, waterSeSymbolInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.water_se_symbol = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, riverDeepInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.riverDeep = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, riverWideInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.riverWide = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, riverAngleInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.riverAngle = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, radarHighInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.radarHigh = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, faceCoeInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.faceCoe = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, speedCoeInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.speedCoe = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, flowModelCoeInputHandle: function (e) { let rtuInfo = this.data.rtuInfo; rtuInfo.flowModelCoe = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, flowRateSensorIdInputHandle: function(e){ let rtuInfo = this.data.rtuInfo; rtuInfo.flowRateSensorId = e.detail.value; this.setData({ rtuInfo: rtuInfo }) }, submitRunsetting: function () { this.cancelTime(); let datagram = datagramHelp.datagram_42(this.data.rtuInfo); this.sendDatagramToRtu(datagram); }, getRtuRunSetting() { let datagram = datagramHelp.datagram_43(); let cache = this.data.datagramCache; cache.push(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') { var info = datagramHelp.datagram_43_analyse(onedatagram) console.log(info) let rtuInfo = this.data.rtuInfo; let intervalTime = info['intervalTime']; let datagramTimes = rtuInfo.datagramTimes; // let it = {}; //it['name'] = "5分钟"; // it['value'] = "5"; if (intervalTime == "5") { datagramTimes[0]['checked'] = true; } else { datagramTimes[0]['checked'] = false; } // datagramTimes.push(it); // it = {}; // it['name'] = "1小时"; // it['value'] = "60"; if (intervalTime == "60") { datagramTimes[1]['checked'] = true; } else { datagramTimes[1]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "2小时"; //it['value'] = "120"; if (intervalTime == "120") { datagramTimes[2]['checked'] = true; } else { datagramTimes[2]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "3小时"; //it['value'] = "180"; if (intervalTime == "180") { datagramTimes[3]['checked'] = true; } else { datagramTimes[3]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "4小时"; //it['value'] = "240"; if (intervalTime == "240") { datagramTimes[4]['checked'] = true; } else { datagramTimes[4]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "6小时"; //it['value'] = "360"; if (intervalTime == "360") { datagramTimes[5]['checked'] = true; } else { datagramTimes[5]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "8小时"; //it['value'] = "480"; if (intervalTime == "480") { datagramTimes[6]['checked'] = true; } else { datagramTimes[6]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "12小时"; //it['value'] = "720"; if (intervalTime == "720") { datagramTimes[7]['checked'] = true; } else { datagramTimes[7]['checked'] = false; } //datagramTimes.push(it); //it = {}; //it['name'] = "24小时"; //it['value'] = "1440"; if (intervalTime == "1440") { datagramTimes[8]['checked'] = true; } else { datagramTimes[8]['checked'] = false; } //datagramTimes.push(it); rtuInfo['intervalTime'] = intervalTime; rtuInfo['datagramTimes'] = datagramTimes; var v = info['addIntervalTime']; if (null != v && v.length > 0) { rtuInfo['addIntervalTime'] = info['addIntervalTime']; } v = info['water_level_base_point']; if (null != v && v.length > 0) { rtuInfo['water_level_base_point'] = info['water_level_base_point']; } v = info['water_level_correct_point']; if (null != v && v.length > 0) { rtuInfo['water_level_correct_point'] = info['water_level_correct_point']; } v = info['rain_precision']; if (null != v && v.length > 0) { rtuInfo['rain_precision'] = info['rain_precision']; } v = info['rain_extra_datagram_threshold']; if (null != v && v.length > 0) { rtuInfo['rain_extra_datagram_threshold'] = info['rain_extra_datagram_threshold']; } v = info['water_se_agreement']; if (null != v && v.length > 0) { rtuInfo['water_se_agreement'] = info['water_se_agreement']; } v = info['water_se_id']; if (null != v && v.length > 0) { rtuInfo['water_se_id'] = info['water_se_id']; } v = info['water_se_address']; if (null != v && v.length > 0) { rtuInfo['water_se_address'] = info['water_se_address']; } v = info['water_se_format']; if (null != v && v.length > 0) { rtuInfo['water_se_format'] = info['water_se_format']; } v = info['water_se_adjust']; if (null != v && v.length > 0) { rtuInfo['water_se_adjust'] = info['water_se_adjust']; } v = info['water_se_symbol']; if (null != v && v.length > 0) { rtuInfo['water_se_symbol'] = info['water_se_symbol']; } v = info['riverDeep']; if (null != v && v.length > 0) { rtuInfo['riverDeep'] = info['riverDeep']; } v = info['riverWide']; if (null != v && v.length > 0) { rtuInfo['riverWide'] = info['riverWide']; } v = info['riverAngle']; if (null != v && v.length > 0) { rtuInfo['riverAngle'] = info['riverAngle']; } v = info['radarHigh']; if (null != v && v.length > 0) { rtuInfo['radarHigh'] = info['radarHigh']; } v = info['faceCoe']; if (null != v && v.length > 0) { rtuInfo['faceCoe'] = info['faceCoe']; } v = info['speedCoe']; if (null != v && v.length > 0) { rtuInfo['speedCoe'] = info['speedCoe']; } v = info['flowModelCoe']; if (null != v && v.length > 0) { rtuInfo['flowModelCoe'] = info['flowModelCoe']; } let waterLevelSensorKind = rtuInfo.waterLevelSensorKind; let waterLevelSensorKindValue = info['waterLevelSensorKind']; for(let i=0;i<3;i++){ if (waterLevelSensorKindValue == i) { waterLevelSensorKind[i]['checked'] = true; } else { waterLevelSensorKind[i]['checked'] = false; } } let flowRateSensorKind = rtuInfo.flowRateSensorKind; let flowRateSensorKindValue = info['flowRateSensorKind']; for(let i=0;i<3;i++){ if (flowRateSensorKindValue == i) { flowRateSensorKind[i]['checked'] = true; } else { flowRateSensorKind[i]['checked'] = false; } } let flowRateSensorProtocolType = rtuInfo.flowRateSensorProtocolType; let flowRateSensorProtocolTypeValue = info['flowRateSensorProtocolType']; for(let i=0;i<15;i++){ if (flowRateSensorProtocolTypeValue == i) { flowRateSensorProtocolType[i]['checked'] = true; } else { flowRateSensorProtocolType[i]['checked'] = false; } } let flowRateSensorId = info['flowRateSensorId']; if (null != flowRateSensorId && flowRateSensorId.length > 0) { rtuInfo['flowRateSensorId'] = flowRateSensorId; } this.setData({ rtuInfo: rtuInfo }); this.cancelTime(); } else if (funcode == '42') { this.setData({ showSettingResult: true, settingResultText: "运行参数配置已完成!" }) // wx.showToast({ // title: 'RTU运行参数设置已完成!', // icon: 'ok', // duration: 2000 //持续的时间 // }) } else if (funcode == '4A') { } else if (funcode == '51') { } 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 }) } })