bluetoothRunSetting.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. const util = require("../../../utils/util");
  2. const datagramHelp = require("../../../utils/datagramHelp")
  3. var app = getApp();
  4. var task_timer;
  5. Page({
  6. data: {
  7. devices: {
  8. deviceId: '',
  9. serviceId: '',
  10. characteristicId: '',
  11. connectedBluetoothName: ''
  12. },
  13. rtuInfo: {
  14. rtu_code: '',
  15. ip1: '',
  16. ip2: '',
  17. ip3: '',
  18. ip4: '',
  19. rtu_work_model: '',
  20. rtu_kind: '',
  21. collect_interval: '',
  22. water_level_base_point: '',
  23. water_level_correct_point: '',
  24. rain_precision: '',
  25. rain_extra_datagram_threshold: '',
  26. water_se_agreement: '',
  27. water_se_id: '',
  28. water_se_address: '',
  29. water_se_format: '',
  30. water_se_adjust: '',
  31. water_se_symbol: '',
  32. addIntervalTime: '',
  33. intervalTime: '',
  34. datagram_idx: '',
  35. collect_time: '',
  36. acc_rain: '',
  37. fiveMinute_rain: '',
  38. hour_rain: '',
  39. weater_level: '',
  40. power_v: '',
  41. riverDeep: '',
  42. riverWide: '',
  43. riverAngle: '',
  44. radarHigh: '',
  45. faceCoe: '',
  46. speedCoe: '',
  47. flowModelCoe: '',
  48. rtu_ver: '',
  49. rtu_uuid: '',
  50. bt_v: '',
  51. rtu_rssi: '',
  52. rtu_temperature: '',
  53. lua_ver: '',
  54. agreement: 'sw2014',
  55. datagramTimes: [{
  56. value: '5',
  57. name: '5分钟',
  58. },
  59. {
  60. value: '60',
  61. name: '1小时',
  62. checked: true
  63. },
  64. {
  65. value: '120',
  66. name: '2小时'
  67. },
  68. {
  69. value: '180',
  70. name: '3小时'
  71. },
  72. {
  73. value: '240',
  74. name: '4小时'
  75. },
  76. {
  77. value: '360',
  78. name: '6小时'
  79. },
  80. {
  81. value: '480',
  82. name: '8小时'
  83. },
  84. {
  85. value: '720',
  86. name: '12小时'
  87. },
  88. {
  89. value: '1440',
  90. name: '24小时'
  91. }
  92. ],
  93. waterLevelSensorKind: [{
  94. value: '0',
  95. name: '未连接',
  96. checked: true
  97. },
  98. {
  99. value: '1',
  100. name: '数字量'
  101. },
  102. {
  103. value: '2',
  104. name: '模似量'
  105. }
  106. ],
  107. flowRateSensorKind: [{
  108. value: '0',
  109. name: '未连接',
  110. checked: true
  111. },
  112. {
  113. value: '1',
  114. name: '数字量'
  115. },
  116. {
  117. value: '2',
  118. name: '模似量'
  119. }
  120. ],
  121. flowRateSensorProtocolType: [{
  122. value: '0',
  123. name: 'modbus',
  124. checked: true
  125. },
  126. {
  127. value: '1',
  128. name: '老百特流量计'
  129. },
  130. {
  131. value: '2',
  132. name: '插入式超声波流量计'
  133. },
  134. {
  135. value: '3',
  136. name: 'LMAG流量计'
  137. },
  138. {
  139. value: '4',
  140. name: '京仪流速计'
  141. },
  142. {
  143. value: '5',
  144. name: '91雷达流量计'
  145. },
  146. {
  147. value: '6',
  148. name: '华聚流速计'
  149. },
  150. {
  151. value: '7',
  152. name: '模拟流量计'
  153. },
  154. {
  155. value: '8',
  156. name: '华儒流速计'
  157. },
  158. {
  159. value: '9',
  160. name: '鹰格流速计'
  161. },
  162. {
  163. value: '10',
  164. name: '协议10'
  165. },
  166. {
  167. value: '11',
  168. name: '协议11'
  169. },
  170. {
  171. value: '12',
  172. name: '协议12'
  173. },
  174. {
  175. value: '13',
  176. name: '协议13'
  177. },
  178. {
  179. value: '14',
  180. name: '协议14'
  181. }
  182. ],
  183. flowRateSensorId:'',
  184. },
  185. datagramBuff: '',
  186. datagramCache: [],
  187. trySendCount: 0,
  188. datagramIndex: 0,
  189. showSettingResult: false,
  190. settingResultText: '操作已完成!'
  191. },
  192. /**
  193. * 生命周期函数--监听页面加载
  194. */
  195. onLoad: function (options) {
  196. console.log('bluebooth info page load ', '**********************************************')
  197. let deviceId = options.deviceId;
  198. let serviceId = options.serviceId;
  199. let characteristicId = options.characteristicId;
  200. let connectedBluetoothName = options.connectedBluetoothName;
  201. console.log('name' + connectedBluetoothName)
  202. let devices = {}
  203. devices['deviceId'] = deviceId;
  204. devices['serviceId'] = serviceId;
  205. devices['characteristicId'] = characteristicId;
  206. devices['connectedBluetoothName'] = connectedBluetoothName;
  207. this.setData({
  208. devices: devices,
  209. trySendCount: 0,
  210. datagramIndex: 0
  211. })
  212. task_timer = undefined;
  213. this.getRtuRunSetting();
  214. if (this.data.datagramCache.length > 0) {
  215. let datagram = this.data.datagramCache[this.data.datagramIndex];
  216. this.sendDatagramToRtu(datagram);
  217. let index = this.data.datagramIndex + 1;
  218. if (this.data.datagramCache.length == index) {
  219. index = 0;
  220. }
  221. let count = this.data.trySendCount + 1;
  222. this.setData({
  223. datagramIndex: index,
  224. trySendCount: count
  225. })
  226. }
  227. },
  228. /**
  229. * 生命周期函数--监听页面隐藏
  230. */
  231. onHide: function () {
  232. console.log('bluebooth runSetting page onHide ', '**********************************************')
  233. this.cancelTime();
  234. },
  235. /**
  236. * 生命周期函数--监听页面卸载
  237. */
  238. onUnload: function () {
  239. console.log('bluebooth runSetting page onUnload ', '**********************************************')
  240. this.cancelTime();
  241. },
  242. /**
  243. * 生命周期函数--监听页面显示
  244. */
  245. onShow: function () {
  246. console.log('bluebooth runSetting page onShow ', '**********************************************')
  247. this.timeCallback();
  248. },
  249. settingCompletTabEvent(e) {
  250. this.setData({
  251. showSettingResult: false
  252. })
  253. const btn = e.detail.index;
  254. if (btn == 0) {
  255. wx.navigateBack();
  256. }
  257. },
  258. radioChange: function (e) {
  259. let intervalTime = e.detail.value;
  260. let radioItems = this.data.rtuInfo.datagramTimes;
  261. for (let i = 0, len = radioItems.length; i < len; ++i) {
  262. radioItems[i].checked = radioItems[i].value == intervalTime;
  263. }
  264. let rtuInfo = this.data.rtuInfo;
  265. rtuInfo.datagramTimes = radioItems;
  266. rtuInfo.intervalTime = intervalTime;
  267. this.setData({
  268. rtuInfo: rtuInfo
  269. });
  270. },
  271. waterLevelSensorKindRadioChange: function (e) {
  272. var rtuInfo = this.data.rtuInfo;
  273. var radioItems = this.data.rtuInfo.waterLevelSensorKind;
  274. for (var i = 0, len = radioItems.length; i < len; ++i) {
  275. radioItems[i].checked = radioItems[i].value == e.detail.value;
  276. }
  277. rtuInfo.waterLevelSensorKind = radioItems;
  278. this.setData({
  279. rtuInfo: rtuInfo
  280. });
  281. },
  282. flowRateSensorKindRadioChange: function (e) {
  283. var rtuInfo = this.data.rtuInfo;
  284. var radioItems = this.data.rtuInfo.flowRateSensorKind;
  285. for (var i = 0, len = radioItems.length; i < len; ++i) {
  286. radioItems[i].checked = radioItems[i].value == e.detail.value;
  287. }
  288. rtuInfo.flowRateSensorKind = radioItems;
  289. this.setData({
  290. rtuInfo: rtuInfo
  291. });
  292. },
  293. flowRateProtocolRadioChange: function (e) {
  294. var rtuInfo = this.data.rtuInfo;
  295. var radioItems = this.data.rtuInfo.flowRateSensorProtocolType;
  296. for (var i = 0, len = radioItems.length; i < len; ++i) {
  297. radioItems[i].checked = radioItems[i].value == e.detail.value;
  298. }
  299. rtuInfo.flowRateSensorProtocolType = radioItems;
  300. this.setData({
  301. rtuInfo: rtuInfo
  302. });
  303. },
  304. addDatagramInputHandle: function (e) {
  305. let rtuInfo = this.data.rtuInfo;
  306. rtuInfo.addIntervalTime = e.detail.value;
  307. this.setData({
  308. rtuInfo: rtuInfo
  309. })
  310. },
  311. waterBaseValueInputHandle: function (e) {
  312. let rtuInfo = this.data.rtuInfo;
  313. rtuInfo.water_level_base_point = e.detail.value;
  314. this.setData({
  315. rtuInfo: rtuInfo
  316. })
  317. },
  318. waterCorrectValueInputHandle: function (e) {
  319. let rtuInfo = this.data.rtuInfo;
  320. rtuInfo.water_level_correct_point = e.detail.value;
  321. this.setData({
  322. rtuInfo: rtuInfo
  323. })
  324. },
  325. rainPrecisionInputHandle: function (e) {
  326. let rtuInfo = this.data.rtuInfo;
  327. rtuInfo.rain_precision = e.detail.value;
  328. this.setData({
  329. rtuInfo: rtuInfo
  330. })
  331. },
  332. rainExtraDatagramThresholdInputHandle: function (e) {
  333. let rtuInfo = this.data.rtuInfo;
  334. rtuInfo.rain_extra_datagram_threshold = e.detail.value;
  335. this.setData({
  336. rtuInfo: rtuInfo
  337. })
  338. },
  339. waterSeAgreementInputHandle: function (e) {
  340. let rtuInfo = this.data.rtuInfo;
  341. rtuInfo.water_se_agreement = e.detail.value;
  342. this.setData({
  343. rtuInfo: rtuInfo
  344. })
  345. },
  346. waterSeIdInputHandle: function (e) {
  347. let rtuInfo = this.data.rtuInfo;
  348. rtuInfo.water_se_id = e.detail.value;
  349. this.setData({
  350. rtuInfo: rtuInfo
  351. })
  352. },
  353. waterSeAddressInputHandle: function (e) {
  354. let rtuInfo = this.data.rtuInfo;
  355. rtuInfo.water_se_address = e.detail.value;
  356. this.setData({
  357. rtuInfo: rtuInfo
  358. })
  359. },
  360. waterSeFormatInputHandle: function (e) {
  361. let rtuInfo = this.data.rtuInfo;
  362. rtuInfo.water_se_format = e.detail.value;
  363. this.setData({
  364. rtuInfo: rtuInfo
  365. })
  366. },
  367. waterSeAdjustInputHandle: function (e) {
  368. let rtuInfo = this.data.rtuInfo;
  369. rtuInfo.water_se_adjust = e.detail.value;
  370. this.setData({
  371. rtuInfo: rtuInfo
  372. })
  373. },
  374. waterSeSymbolInputHandle: function (e) {
  375. let rtuInfo = this.data.rtuInfo;
  376. rtuInfo.water_se_symbol = e.detail.value;
  377. this.setData({
  378. rtuInfo: rtuInfo
  379. })
  380. },
  381. riverDeepInputHandle: function (e) {
  382. let rtuInfo = this.data.rtuInfo;
  383. rtuInfo.riverDeep = e.detail.value;
  384. this.setData({
  385. rtuInfo: rtuInfo
  386. })
  387. },
  388. riverWideInputHandle: function (e) {
  389. let rtuInfo = this.data.rtuInfo;
  390. rtuInfo.riverWide = e.detail.value;
  391. this.setData({
  392. rtuInfo: rtuInfo
  393. })
  394. },
  395. riverAngleInputHandle: function (e) {
  396. let rtuInfo = this.data.rtuInfo;
  397. rtuInfo.riverAngle = e.detail.value;
  398. this.setData({
  399. rtuInfo: rtuInfo
  400. })
  401. },
  402. radarHighInputHandle: function (e) {
  403. let rtuInfo = this.data.rtuInfo;
  404. rtuInfo.radarHigh = e.detail.value;
  405. this.setData({
  406. rtuInfo: rtuInfo
  407. })
  408. },
  409. faceCoeInputHandle: function (e) {
  410. let rtuInfo = this.data.rtuInfo;
  411. rtuInfo.faceCoe = e.detail.value;
  412. this.setData({
  413. rtuInfo: rtuInfo
  414. })
  415. },
  416. speedCoeInputHandle: function (e) {
  417. let rtuInfo = this.data.rtuInfo;
  418. rtuInfo.speedCoe = e.detail.value;
  419. this.setData({
  420. rtuInfo: rtuInfo
  421. })
  422. },
  423. flowModelCoeInputHandle: function (e) {
  424. let rtuInfo = this.data.rtuInfo;
  425. rtuInfo.flowModelCoe = e.detail.value;
  426. this.setData({
  427. rtuInfo: rtuInfo
  428. })
  429. },
  430. flowRateSensorIdInputHandle: function(e){
  431. let rtuInfo = this.data.rtuInfo;
  432. rtuInfo.flowRateSensorId = e.detail.value;
  433. this.setData({
  434. rtuInfo: rtuInfo
  435. })
  436. },
  437. submitRunsetting: function () {
  438. this.cancelTime();
  439. let datagram = datagramHelp.datagram_42(this.data.rtuInfo);
  440. this.sendDatagramToRtu(datagram);
  441. },
  442. getRtuRunSetting() {
  443. let datagram = datagramHelp.datagram_43();
  444. let cache = this.data.datagramCache;
  445. cache.push(datagram);
  446. },
  447. pushDatagramBuff(datagram) {
  448. let tmpdatagramBuff = this.data.datagramBuff;
  449. if (tmpdatagramBuff.length > 0) {
  450. tmpdatagramBuff = tmpdatagramBuff + datagram;
  451. } else {
  452. tmpdatagramBuff = datagram;
  453. }
  454. console.log(" datagram buff ==========", tmpdatagramBuff)
  455. this.setData({
  456. datagramBuff: tmpdatagramBuff
  457. })
  458. this.processDatagram();
  459. },
  460. processDatagram() {
  461. let datagram = this.data.datagramBuff;
  462. if (datagram.length > 0) {
  463. for (let i = 0; i < datagram.length; i++) {
  464. if (datagram.indexOf("7e7e") == 0) {
  465. break;
  466. } else {
  467. datagram = datagram.substring(i, datagram.length);
  468. }
  469. }
  470. if (null != datagram && datagram.length > 0 && datagram.indexOf("7e7e") >= 0) {
  471. if (datagram.length >= 26) {
  472. var lenBuff = util.HexStr2Bytes(datagram.substring(22, 22 + 4));
  473. var len = util.byteToUint16(lenBuff); // 报文长度(int)
  474. var datagramlength = 11 + len + 6;
  475. if (datagram.length >= datagramlength * 2) {
  476. var onedatagram = datagram.substring(0, datagramlength * 2);
  477. if (!datagramHelp.crcCheck(onedatagram)) {
  478. wx.showToast({
  479. title: '数据CRC校验错误,请重试!',
  480. icon: 'none',
  481. duration: 2000
  482. })
  483. this.setData({
  484. datagramBuff: ''
  485. })
  486. return;
  487. }
  488. var count = 20;
  489. var funcode = datagram.substring(count, count + 2);
  490. count += 2;
  491. if (funcode == "f6") {
  492. } else if (funcode == "f7") {
  493. } else if (funcode == '45') {
  494. } else if (funcode == '47') {
  495. } else if (funcode == '48') {
  496. } else if (funcode == '41') {
  497. } else if (funcode == '40') {
  498. } else if (funcode == '43') {
  499. var info = datagramHelp.datagram_43_analyse(onedatagram)
  500. console.log(info)
  501. let rtuInfo = this.data.rtuInfo;
  502. let intervalTime = info['intervalTime'];
  503. let datagramTimes = rtuInfo.datagramTimes;
  504. // let it = {};
  505. //it['name'] = "5分钟";
  506. // it['value'] = "5";
  507. if (intervalTime == "5") {
  508. datagramTimes[0]['checked'] = true;
  509. } else {
  510. datagramTimes[0]['checked'] = false;
  511. }
  512. // datagramTimes.push(it);
  513. // it = {};
  514. // it['name'] = "1小时";
  515. // it['value'] = "60";
  516. if (intervalTime == "60") {
  517. datagramTimes[1]['checked'] = true;
  518. } else {
  519. datagramTimes[1]['checked'] = false;
  520. }
  521. //datagramTimes.push(it);
  522. //it = {};
  523. //it['name'] = "2小时";
  524. //it['value'] = "120";
  525. if (intervalTime == "120") {
  526. datagramTimes[2]['checked'] = true;
  527. } else {
  528. datagramTimes[2]['checked'] = false;
  529. }
  530. //datagramTimes.push(it);
  531. //it = {};
  532. //it['name'] = "3小时";
  533. //it['value'] = "180";
  534. if (intervalTime == "180") {
  535. datagramTimes[3]['checked'] = true;
  536. } else {
  537. datagramTimes[3]['checked'] = false;
  538. }
  539. //datagramTimes.push(it);
  540. //it = {};
  541. //it['name'] = "4小时";
  542. //it['value'] = "240";
  543. if (intervalTime == "240") {
  544. datagramTimes[4]['checked'] = true;
  545. } else {
  546. datagramTimes[4]['checked'] = false;
  547. }
  548. //datagramTimes.push(it);
  549. //it = {};
  550. //it['name'] = "6小时";
  551. //it['value'] = "360";
  552. if (intervalTime == "360") {
  553. datagramTimes[5]['checked'] = true;
  554. } else {
  555. datagramTimes[5]['checked'] = false;
  556. }
  557. //datagramTimes.push(it);
  558. //it = {};
  559. //it['name'] = "8小时";
  560. //it['value'] = "480";
  561. if (intervalTime == "480") {
  562. datagramTimes[6]['checked'] = true;
  563. } else {
  564. datagramTimes[6]['checked'] = false;
  565. }
  566. //datagramTimes.push(it);
  567. //it = {};
  568. //it['name'] = "12小时";
  569. //it['value'] = "720";
  570. if (intervalTime == "720") {
  571. datagramTimes[7]['checked'] = true;
  572. } else {
  573. datagramTimes[7]['checked'] = false;
  574. }
  575. //datagramTimes.push(it);
  576. //it = {};
  577. //it['name'] = "24小时";
  578. //it['value'] = "1440";
  579. if (intervalTime == "1440") {
  580. datagramTimes[8]['checked'] = true;
  581. } else {
  582. datagramTimes[8]['checked'] = false;
  583. }
  584. //datagramTimes.push(it);
  585. rtuInfo['intervalTime'] = intervalTime;
  586. rtuInfo['datagramTimes'] = datagramTimes;
  587. var v = info['addIntervalTime'];
  588. if (null != v && v.length > 0) {
  589. rtuInfo['addIntervalTime'] = info['addIntervalTime'];
  590. }
  591. v = info['water_level_base_point'];
  592. if (null != v && v.length > 0) {
  593. rtuInfo['water_level_base_point'] = info['water_level_base_point'];
  594. }
  595. v = info['water_level_correct_point'];
  596. if (null != v && v.length > 0) {
  597. rtuInfo['water_level_correct_point'] = info['water_level_correct_point'];
  598. }
  599. v = info['rain_precision'];
  600. if (null != v && v.length > 0) {
  601. rtuInfo['rain_precision'] = info['rain_precision'];
  602. }
  603. v = info['rain_extra_datagram_threshold'];
  604. if (null != v && v.length > 0) {
  605. rtuInfo['rain_extra_datagram_threshold'] = info['rain_extra_datagram_threshold'];
  606. }
  607. v = info['water_se_agreement'];
  608. if (null != v && v.length > 0) {
  609. rtuInfo['water_se_agreement'] = info['water_se_agreement'];
  610. }
  611. v = info['water_se_id'];
  612. if (null != v && v.length > 0) {
  613. rtuInfo['water_se_id'] = info['water_se_id'];
  614. }
  615. v = info['water_se_address'];
  616. if (null != v && v.length > 0) {
  617. rtuInfo['water_se_address'] = info['water_se_address'];
  618. }
  619. v = info['water_se_format'];
  620. if (null != v && v.length > 0) {
  621. rtuInfo['water_se_format'] = info['water_se_format'];
  622. }
  623. v = info['water_se_adjust'];
  624. if (null != v && v.length > 0) {
  625. rtuInfo['water_se_adjust'] = info['water_se_adjust'];
  626. }
  627. v = info['water_se_symbol'];
  628. if (null != v && v.length > 0) {
  629. rtuInfo['water_se_symbol'] = info['water_se_symbol'];
  630. }
  631. v = info['riverDeep'];
  632. if (null != v && v.length > 0) {
  633. rtuInfo['riverDeep'] = info['riverDeep'];
  634. }
  635. v = info['riverWide'];
  636. if (null != v && v.length > 0) {
  637. rtuInfo['riverWide'] = info['riverWide'];
  638. }
  639. v = info['riverAngle'];
  640. if (null != v && v.length > 0) {
  641. rtuInfo['riverAngle'] = info['riverAngle'];
  642. }
  643. v = info['radarHigh'];
  644. if (null != v && v.length > 0) {
  645. rtuInfo['radarHigh'] = info['radarHigh'];
  646. }
  647. v = info['faceCoe'];
  648. if (null != v && v.length > 0) {
  649. rtuInfo['faceCoe'] = info['faceCoe'];
  650. }
  651. v = info['speedCoe'];
  652. if (null != v && v.length > 0) {
  653. rtuInfo['speedCoe'] = info['speedCoe'];
  654. }
  655. v = info['flowModelCoe'];
  656. if (null != v && v.length > 0) {
  657. rtuInfo['flowModelCoe'] = info['flowModelCoe'];
  658. }
  659. let waterLevelSensorKind = rtuInfo.waterLevelSensorKind;
  660. let waterLevelSensorKindValue = info['waterLevelSensorKind'];
  661. for(let i=0;i<3;i++){
  662. if (waterLevelSensorKindValue == i) {
  663. waterLevelSensorKind[i]['checked'] = true;
  664. } else {
  665. waterLevelSensorKind[i]['checked'] = false;
  666. }
  667. }
  668. let flowRateSensorKind = rtuInfo.flowRateSensorKind;
  669. let flowRateSensorKindValue = info['flowRateSensorKind'];
  670. for(let i=0;i<3;i++){
  671. if (flowRateSensorKindValue == i) {
  672. flowRateSensorKind[i]['checked'] = true;
  673. } else {
  674. flowRateSensorKind[i]['checked'] = false;
  675. }
  676. }
  677. let flowRateSensorProtocolType = rtuInfo.flowRateSensorProtocolType;
  678. let flowRateSensorProtocolTypeValue = info['flowRateSensorProtocolType'];
  679. for(let i=0;i<15;i++){
  680. if (flowRateSensorProtocolTypeValue == i) {
  681. flowRateSensorProtocolType[i]['checked'] = true;
  682. } else {
  683. flowRateSensorProtocolType[i]['checked'] = false;
  684. }
  685. }
  686. let flowRateSensorId = info['flowRateSensorId'];
  687. if (null != flowRateSensorId && flowRateSensorId.length > 0) {
  688. rtuInfo['flowRateSensorId'] = flowRateSensorId;
  689. }
  690. this.setData({
  691. rtuInfo: rtuInfo
  692. });
  693. this.cancelTime();
  694. } else if (funcode == '42') {
  695. this.setData({
  696. showSettingResult: true,
  697. settingResultText: "运行参数配置已完成!"
  698. })
  699. // wx.showToast({
  700. // title: 'RTU运行参数设置已完成!',
  701. // icon: 'ok',
  702. // duration: 2000 //持续的时间
  703. // })
  704. } else if (funcode == '4A') {
  705. } else if (funcode == '51') {
  706. } else if (funcode == 'f3') {
  707. }
  708. if (datagram.length - datagramlength * 2 > 0) {
  709. let tmp = datagram.substring(datagramlength * 2, datagram.length);
  710. this.setData({
  711. datagramBuff: tmp
  712. })
  713. }
  714. } else {
  715. this.setData({
  716. datagramBuff: datagram
  717. })
  718. }
  719. } else {
  720. this.setData({
  721. datagramBuff: datagram
  722. })
  723. }
  724. } else {
  725. if (null != datagram && datagram.length > 0) {
  726. this.setData({
  727. datagramBuff: datagram
  728. })
  729. } else {
  730. this.setData({
  731. datagramBuff: ''
  732. })
  733. }
  734. }
  735. }
  736. },
  737. sendDatagramToRtu(datagram) {
  738. console.log("will send datagram " + datagram);
  739. let buffer = util.HexStr2Bytes(datagram);
  740. var that = this;
  741. let count = 0;
  742. let inView = new DataView(buffer);
  743. while (true) {
  744. if (buffer.byteLength - count > 20) {
  745. let tmpBuff = new ArrayBuffer(20);
  746. let outView = new DataView(tmpBuff);
  747. for (let i = 0; i < 20; i++) {
  748. outView.setUint8(i, inView.getUint8(count + i))
  749. }
  750. wx.writeBLECharacteristicValue({
  751. deviceId: that.data.devices['deviceId'],
  752. serviceId: that.data.devices['serviceId'],
  753. characteristicId: that.data.devices['characteristicId'],
  754. value: tmpBuff,
  755. success(res) {
  756. console.log('writeBLECharacteristicValue success', res.errMsg)
  757. wx.onBLECharacteristicValueChange((characteristic) => {
  758. console.log("read datagram==============", util.ab2hex(characteristic.value))
  759. var datagram = util.ab2hex(characteristic.value);
  760. that.pushDatagramBuff(datagram);
  761. })
  762. wx.readBLECharacteristicValue({
  763. deviceId: that.data.devices['deviceId'],
  764. serviceId: that.data.devices['serviceId'],
  765. characteristicId: that.data.devices['characteristicId'],
  766. success: function (res) {
  767. console.log('readBLECharacteristicValue')
  768. }
  769. })
  770. },
  771. fail(res) {
  772. console.log('writeBLECharacteristicValue success', res.errMsg)
  773. }
  774. })
  775. count += 20;
  776. } else {
  777. let len = buffer.byteLength - count;
  778. let tmpBuff = new ArrayBuffer(len);
  779. let outView = new DataView(tmpBuff);
  780. for (let i = 0; i < len; i++) {
  781. outView.setUint8(i, inView.getUint8(count + i))
  782. }
  783. wx.writeBLECharacteristicValue({
  784. deviceId: that.data.devices['deviceId'],
  785. serviceId: that.data.devices['serviceId'],
  786. characteristicId: that.data.devices['characteristicId'],
  787. value: tmpBuff,
  788. success(res) {
  789. console.log('writeBLECharacteristicValue success', res.errMsg)
  790. wx.onBLECharacteristicValueChange((characteristic) => {
  791. console.log("read datagram==============", util.ab2hex(characteristic.value))
  792. var datagram = util.ab2hex(characteristic.value);
  793. that.pushDatagramBuff(datagram);
  794. })
  795. wx.readBLECharacteristicValue({
  796. deviceId: that.data.devices['deviceId'],
  797. serviceId: that.data.devices['serviceId'],
  798. characteristicId: that.data.devices['characteristicId'],
  799. success: function (res) {
  800. console.log('readBLECharacteristicValue')
  801. }
  802. })
  803. },
  804. fail(res) {
  805. console.log('writeBLECharacteristicValue success', res.errMsg)
  806. }
  807. })
  808. count += len;
  809. }
  810. if (count >= buffer.byteLength) {
  811. break;
  812. }
  813. }
  814. },
  815. timeCallback() {
  816. var that = this;
  817. task_timer = setTimeout(function () {
  818. console.log("------------------------------------Time CallBack----------------------------");
  819. if (that.data.trySendCount == 0) {
  820. let datagram = that.data.datagramCache[that.data.datagramIndex];
  821. that.sendDatagramToRtu(datagram);
  822. let index = that.data.datagramIndex + 1;
  823. if (that.data.datagramCache.length == index) {
  824. index = 0;
  825. }
  826. let count = that.data.trySendCount + 1;
  827. that.setData({
  828. datagramIndex: index,
  829. trySendCount: count
  830. })
  831. } else {
  832. let count = that.data.trySendCount + 1;
  833. if (count >= 10) {
  834. count = 0;
  835. }
  836. that.setData({
  837. trySendCount: count
  838. })
  839. }
  840. that.timeCallback();
  841. }, 3000);
  842. },
  843. cancelTime() {
  844. if (task_timer != undefined) {
  845. clearTimeout(task_timer);
  846. task_timer = undefined;
  847. }
  848. this.setData({
  849. datagramIndex: 0,
  850. trySendCount: 0
  851. })
  852. }
  853. })