bluetoothBaseSetting.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. ip1_port: '',
  17. ip2: '',
  18. ip2_port: '',
  19. ip3: '',
  20. ip3_port: '',
  21. ip4: '',
  22. ip4_port: '',
  23. ipV6Checked1: false,
  24. ipV6Checked2: false,
  25. ipV6Checked3: false,
  26. ipV6Checked4: false,
  27. rtu_work_model: '',
  28. rtu_kind: '',
  29. collect_interval: '',
  30. water_level_base_point: '',
  31. water_level_correct_point: '',
  32. datagram_idx: '',
  33. collect_time: '',
  34. acc_rain: '',
  35. fiveMinute_rain: '',
  36. hour_rain: '',
  37. weater_level: '',
  38. power_v: '',
  39. rtu_ver: '',
  40. rtu_uuid: '',
  41. bt_v: '',
  42. rtu_rssi: '',
  43. rtu_temperature: '',
  44. lua_ver: '',
  45. netModel: '00',
  46. },
  47. netModels: [{
  48. value: '00',
  49. name: 'TCP',
  50. checked: true
  51. }, {
  52. value: '01',
  53. name: 'UDP',
  54. checked: false
  55. }, {
  56. value: '02',
  57. name: 'TCP-NDNS',
  58. checked: false
  59. }, {
  60. value: '03',
  61. name: 'UDP-NDNS',
  62. checked: false
  63. }, {
  64. value: '04',
  65. name: 'IPV6',
  66. checked: false
  67. }, {
  68. value: '05',
  69. name: 'DAPN',
  70. checked: false
  71. }],
  72. ip1Checkbox: [{
  73. value: 'IPV4',
  74. name: 'IPV4',
  75. checked: true
  76. },
  77. {
  78. value: 'IPV6',
  79. name: 'IPV6',
  80. checked: false
  81. }
  82. ],
  83. ip2Checkbox: [{
  84. value: 'IPV4',
  85. name: 'IPV4',
  86. checked: true
  87. },
  88. {
  89. value: 'IPV6',
  90. name: 'IPV6',
  91. checked: false
  92. }
  93. ],
  94. ip3Checkbox: [{
  95. value: 'IPV4',
  96. name: 'IPV4',
  97. checked: true
  98. },
  99. {
  100. value: 'IPV6',
  101. name: 'IPV6',
  102. checked: false
  103. }
  104. ],
  105. ip4Checkbox: [{
  106. value: 'IPV4',
  107. name: 'IPV4',
  108. checked: true
  109. },
  110. {
  111. value: 'IPV6',
  112. name: 'IPV6',
  113. checked: false
  114. }
  115. ],
  116. datagramBuff: '',
  117. datagramCache: [],
  118. trySendCount: 0,
  119. datagramIndex: 0,
  120. rtu_kind_code: ['P', 'H', 'K', 'Z', 'D', 'T', 'M', 'G', 'Q', 'I', 'O'],
  121. rtu_kind_name: ['P(降水)', 'H(河道)', 'K(水库/湖泊)', 'Z(闸坝)', 'D(泵站)', 'T(潮汐)', 'M(墒情)', 'G(地下水)', 'Q(水质)', 'I(取水口)', 'O(排水口)'],
  122. rtu_kind_index: 0,
  123. rtu_work_model_name: ['低功耗', '永在线'],
  124. rtu_work_model_index: 0,
  125. suspend: false,
  126. showSettingResult: false,
  127. settingResultText: '操作已完成!'
  128. },
  129. /**
  130. * 生命周期函数--监听页面加载
  131. */
  132. onLoad: function (options) {
  133. console.log('bluebooth home page load ', '**********************************************')
  134. let deviceId = options.deviceId;
  135. let serviceId = options.serviceId;
  136. let characteristicId = options.characteristicId;
  137. let connectedBluetoothName = options.connectedBluetoothName;
  138. console.log('name' + connectedBluetoothName)
  139. let devices = {}
  140. devices['deviceId'] = deviceId;
  141. devices['serviceId'] = serviceId;
  142. devices['characteristicId'] = characteristicId;
  143. devices['connectedBluetoothName'] = connectedBluetoothName;
  144. this.setData({
  145. devices: devices,
  146. trySendCount: 0,
  147. datagramIndex: 0,
  148. suspend: false
  149. })
  150. task_timer = undefined;
  151. this.getRtuBaseSetting();
  152. if (this.data.datagramCache.length > 0) {
  153. let datagram = this.data.datagramCache[this.data.datagramIndex];
  154. this.sendDatagramToRtu(datagram);
  155. let index = this.data.datagramIndex + 1;
  156. if (this.data.datagramCache.length == index) {
  157. index = 0;
  158. }
  159. let count = this.data.trySendCount + 1;
  160. this.setData({
  161. datagramIndex: index,
  162. trySendCount: count
  163. })
  164. }
  165. },
  166. /**
  167. * 生命周期函数--监听页面隐藏
  168. */
  169. onHide: function () {
  170. console.log('bluebooth baseSetting page onHide ', '**********************************************')
  171. this.cancelTime();
  172. },
  173. /**
  174. * 生命周期函数--监听页面卸载
  175. */
  176. onUnload: function () {
  177. console.log('bluebooth baseSetting page onUnload ', '**********************************************')
  178. this.cancelTime();
  179. },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow: function () {
  184. console.log('bluebooth baseSetting page onShow ', '**********************************************')
  185. // if (!this.data.suspend) {
  186. this.timeCallback();
  187. // }
  188. },
  189. settingCompletTabEvent(e) {
  190. this.setData({
  191. showSettingResult: false
  192. })
  193. const btn = e.detail.index;
  194. if (btn == 0) {
  195. wx.navigateBack({
  196. delta: 2,
  197. });
  198. }
  199. },
  200. bindNewRtuCodeInput: function (e) {
  201. let rtuInfo = this.data.rtuInfo;
  202. rtuInfo.rtu_code = e.detail.value;
  203. this.setData({
  204. rtuInfo: rtuInfo
  205. })
  206. },
  207. bindIp1Input: function (e) {
  208. let rtuInfo = this.data.rtuInfo;
  209. let oldip = rtuInfo.ip1;
  210. let newip = e.detail.value;
  211. let ipv6checked = rtuInfo.ipV6Checked1;
  212. let maxLen = false;
  213. if (ipv6checked) {
  214. let ipitems = oldip.split(":");
  215. if (newip.length > oldip.length) {
  216. if (ipitems.length == 8) {
  217. let lastText = ipitems[ipitems.length - 1];
  218. if (lastText != null && lastText.length == 4) {
  219. maxLen = true;
  220. }
  221. }
  222. if (maxLen) {
  223. newip = oldip;
  224. } else {
  225. let ipitems = newip.split(":");
  226. let lastText = ipitems[ipitems.length - 1];
  227. if (lastText != null && lastText.length == 4) {
  228. if (ipitems.length < 8) {
  229. newip += ":";
  230. }
  231. }
  232. }
  233. }
  234. } else {
  235. let ipitems = oldip.split(".");
  236. if (newip.length > oldip.length) {
  237. if (ipitems.length == 4) {
  238. let lastText = ipitems[ipitems.length - 1];
  239. if (lastText != null && lastText.length == 3) {
  240. maxLen = true;
  241. }
  242. }
  243. if (maxLen) {
  244. newip = oldip;
  245. } else {
  246. let ipitems = newip.split(".");
  247. let lastText = ipitems[ipitems.length - 1];
  248. if (lastText != null && lastText.length == 3) {
  249. if (ipitems.length < 4) {
  250. newip += ".";
  251. }
  252. }
  253. }
  254. }
  255. }
  256. rtuInfo.ip1 = newip;
  257. this.setData({
  258. rtuInfo: rtuInfo
  259. })
  260. },
  261. bindIp2Input: function (e) {
  262. let rtuInfo = this.data.rtuInfo;
  263. let oldip = rtuInfo.ip2;
  264. let newip = e.detail.value;
  265. let ipv6checked = rtuInfo.ipV6Checked2;
  266. let maxLen = false;
  267. if (ipv6checked) {
  268. let ipitems = oldip.split(":");
  269. if (newip.length > oldip.length) {
  270. if (ipitems.length == 8) {
  271. let lastText = ipitems[ipitems.length - 1];
  272. if (lastText != null && lastText.length == 4) {
  273. maxLen = true;
  274. }
  275. }
  276. if (maxLen) {
  277. newip = oldip;
  278. } else {
  279. let ipitems = newip.split(":");
  280. let lastText = ipitems[ipitems.length - 1];
  281. if (lastText != null && lastText.length == 4) {
  282. if (ipitems.length < 8) {
  283. newip += ":";
  284. }
  285. }
  286. }
  287. }
  288. } else {
  289. let ipitems = oldip.split(".");
  290. if (newip.length > oldip.length) {
  291. if (ipitems.length == 4) {
  292. let lastText = ipitems[ipitems.length - 1];
  293. if (lastText != null && lastText.length == 3) {
  294. maxLen = true;
  295. }
  296. }
  297. if (maxLen) {
  298. newip = oldip;
  299. } else {
  300. let ipitems = newip.split(".");
  301. let lastText = ipitems[ipitems.length - 1];
  302. if (lastText != null && lastText.length == 3) {
  303. if (ipitems.length < 4) {
  304. newip += ".";
  305. }
  306. }
  307. }
  308. }
  309. }
  310. rtuInfo.ip2 = newip;
  311. this.setData({
  312. rtuInfo: rtuInfo
  313. })
  314. },
  315. bindIp3Input: function (e) {
  316. let rtuInfo = this.data.rtuInfo;
  317. let oldip = rtuInfo.ip3;
  318. let newip = e.detail.value;
  319. let ipv6checked = rtuInfo.ipV6Checked3;
  320. let maxLen = false;
  321. if (ipv6checked) {
  322. let ipitems = oldip.split(":");
  323. if (newip.length > oldip.length) {
  324. if (ipitems.length == 8) {
  325. let lastText = ipitems[ipitems.length - 1];
  326. if (lastText != null && lastText.length == 4) {
  327. maxLen = true;
  328. }
  329. }
  330. if (maxLen) {
  331. newip = oldip;
  332. } else {
  333. let ipitems = newip.split(":");
  334. let lastText = ipitems[ipitems.length - 1];
  335. if (lastText != null && lastText.length == 4) {
  336. if (ipitems.length < 8) {
  337. newip += ":";
  338. }
  339. }
  340. }
  341. }
  342. } else {
  343. let ipitems = oldip.split(".");
  344. if (newip.length > oldip.length) {
  345. if (ipitems.length == 4) {
  346. let lastText = ipitems[ipitems.length - 1];
  347. if (lastText != null && lastText.length == 3) {
  348. maxLen = true;
  349. }
  350. }
  351. if (maxLen) {
  352. newip = oldip;
  353. } else {
  354. let ipitems = newip.split(".");
  355. let lastText = ipitems[ipitems.length - 1];
  356. if (lastText != null && lastText.length == 3) {
  357. if (ipitems.length < 4) {
  358. newip += ".";
  359. }
  360. }
  361. }
  362. }
  363. }
  364. rtuInfo.ip3 = newip;
  365. this.setData({
  366. rtuInfo: rtuInfo
  367. })
  368. },
  369. bindIp4Input: function (e) {
  370. let rtuInfo = this.data.rtuInfo;
  371. let oldip = rtuInfo.ip4;
  372. let newip = e.detail.value;
  373. let ipv6checked = rtuInfo.ipV6Checked4;
  374. let maxLen = false;
  375. if (ipv6checked) {
  376. let ipitems = oldip.split(":");
  377. if (newip.length > oldip.length) {
  378. if (ipitems.length == 8) {
  379. let lastText = ipitems[ipitems.length - 1];
  380. if (lastText != null && lastText.length == 4) {
  381. maxLen = true;
  382. }
  383. }
  384. if (maxLen) {
  385. newip = oldip;
  386. } else {
  387. let ipitems = newip.split(":");
  388. let lastText = ipitems[ipitems.length - 1];
  389. if (lastText != null && lastText.length == 4) {
  390. if (ipitems.length < 8) {
  391. newip += ":";
  392. }
  393. }
  394. }
  395. }
  396. } else {
  397. let ipitems = oldip.split(".");
  398. if (newip.length > oldip.length) {
  399. if (ipitems.length == 4) {
  400. let lastText = ipitems[ipitems.length - 1];
  401. if (lastText != null && lastText.length == 3) {
  402. maxLen = true;
  403. }
  404. }
  405. if (maxLen) {
  406. newip = oldip;
  407. } else {
  408. let ipitems = newip.split(".");
  409. let lastText = ipitems[ipitems.length - 1];
  410. if (lastText != null && lastText.length == 3) {
  411. if (ipitems.length < 4) {
  412. newip += ".";
  413. }
  414. }
  415. }
  416. }
  417. }
  418. rtuInfo.ip4 = newip;
  419. this.setData({
  420. rtuInfo: rtuInfo
  421. })
  422. },
  423. bindIp1portInput: function (e) {
  424. let rtuInfo = this.data.rtuInfo;
  425. rtuInfo.ip1_port = e.detail.value;
  426. this.setData({
  427. rtuInfo: rtuInfo
  428. })
  429. },
  430. bindIp2portInput: function (e) {
  431. let rtuInfo = this.data.rtuInfo;
  432. rtuInfo.ip2_port = e.detail.value;
  433. this.setData({
  434. rtuInfo: rtuInfo
  435. })
  436. },
  437. bindIp3portInput: function (e) {
  438. let rtuInfo = this.data.rtuInfo;
  439. rtuInfo.ip3_port = e.detail.value;
  440. this.setData({
  441. rtuInfo: rtuInfo
  442. })
  443. },
  444. bindIp4portInput: function (e) {
  445. let rtuInfo = this.data.rtuInfo;
  446. rtuInfo.ip4_port = e.detail.value;
  447. this.setData({
  448. rtuInfo: rtuInfo
  449. })
  450. },
  451. bindWorkModelPickerChange: function (e) {
  452. console.log('picker发送选择改变,携带值为', e.detail.value)
  453. let index = e.detail.value;
  454. let rtuInfo = this.data.rtuInfo;
  455. if (index == 0) {
  456. rtuInfo['rtu_work_model'] = '0';
  457. } else {
  458. rtuInfo['rtu_work_model'] = '1';
  459. }
  460. this.setData({
  461. rtu_work_model_index: index,
  462. rtuInfo: rtuInfo
  463. })
  464. },
  465. bindRtuKindPickerChange: function (e) {
  466. console.log('picker发送选择改变,携带值为', e.detail.value)
  467. let rtuInfo = this.data.rtuInfo;
  468. let index = e.detail.value;
  469. let v = "" + this.data.rtu_kind_code[index];
  470. rtuInfo['rtu_kind'] = v;
  471. this.setData({
  472. rtu_kind_index: index,
  473. rtuInfo: rtuInfo
  474. })
  475. },
  476. checkIp(ip, port, ipV6Checked) {
  477. let formatError = false;
  478. if (ip != undefined && ip.length > 0) {
  479. if (ipV6Checked) {
  480. let ipitems = ip.split(":");
  481. if (ipitems.length == 8) {
  482. for (let i = 0; i < ipitems.length; i++) {
  483. let text = ipitems[i];
  484. let len = text.length;
  485. if (len == 4) {
  486. for (let j = 0; j < 3; j++) {
  487. let subText = text.charCodeAt(j);
  488. if ((subText >= 48 && subText <= 57) || (subText >= 65 && subText <= 70) || (subText >= 97 && subText <= 102)) {
  489. formatError = false;
  490. } else {
  491. formatError = true;
  492. break;
  493. }
  494. }
  495. if (formatError) {
  496. break;
  497. }
  498. } else {
  499. formatError = true;
  500. break;
  501. }
  502. }
  503. } else {
  504. formatError = true;
  505. }
  506. } else {
  507. let ipitems = ip.split(".");
  508. if (ipitems.length == 4) {
  509. for (let i = 0; i < ipitems.length; i++) {
  510. let text = ipitems[i];
  511. let len = text.length;
  512. if (len > 0) {
  513. for (let j = 0; j < len; j++) {
  514. let subText = text.charCodeAt(j);
  515. if (subText >= 48 && subText <= 57) {
  516. formatError = false;
  517. } else {
  518. formatError = true;
  519. break;
  520. }
  521. }
  522. if (formatError) {
  523. break;
  524. }
  525. } else {
  526. formatError = true;
  527. break;
  528. }
  529. }
  530. } else {
  531. formatError = true;
  532. }
  533. }
  534. let len = port.length;
  535. if (len > 0) {
  536. if (isNaN(Number(port, 10))) {
  537. formatError = true;
  538. }
  539. } else {
  540. formatError = true;
  541. }
  542. }
  543. return formatError;
  544. },
  545. baseSettingBtn: function () {
  546. let ip = this.data.rtuInfo['ip1'];
  547. let port = this.data.rtuInfo['ip1_port'];
  548. let ipV6Checked = this.data.rtuInfo['ipV6Checked1'];
  549. let formatError = this.checkIp(ip, port, ipV6Checked);
  550. if (formatError) {
  551. wx.showToast({
  552. title: '第1路IP地址格式错误,请检查!',
  553. icon: 'none',
  554. duration: 2000
  555. })
  556. return;
  557. }
  558. ip = this.data.rtuInfo['ip2'];
  559. port = this.data.rtuInfo['ip2_port'];
  560. ipV6Checked = this.data.rtuInfo['ipV6Checked2'];
  561. formatError = this.checkIp(ip, port, ipV6Checked);
  562. if (formatError) {
  563. wx.showToast({
  564. title: '第2路IP地址格式错误,请检查!',
  565. icon: 'none',
  566. duration: 2000
  567. })
  568. return;
  569. }
  570. ip = this.data.rtuInfo['ip3'];
  571. port = this.data.rtuInfo['ip3_port'];
  572. ipV6Checked = this.data.rtuInfo['ipV6Checked3'];
  573. formatError = this.checkIp(ip, port, ipV6Checked);
  574. if (formatError) {
  575. wx.showToast({
  576. title: '第3路IP地址格式错误,请检查!',
  577. icon: 'none',
  578. duration: 2000
  579. })
  580. return;
  581. }
  582. ip = this.data.rtuInfo['ip4'];
  583. port = this.data.rtuInfo['ip4_port'];
  584. ipV6Checked = this.data.rtuInfo['ipV6Checked4'];
  585. formatError = this.checkIp(ip, port, ipV6Checked);
  586. if (formatError) {
  587. wx.showToast({
  588. title: '第4路IP地址格式错误,请检查!',
  589. icon: 'none',
  590. duration: 2000
  591. })
  592. return;
  593. }
  594. this.cancelTime();
  595. this.setRtuBaseSetting(this.data.rtuInfo);
  596. },
  597. setRtuBaseSetting(rtuInfo) {
  598. let datagram = datagramHelp.datagram_40(rtuInfo);
  599. this.sendDatagramToRtu(datagram);
  600. },
  601. /**
  602. * 查询测站基础参数信息
  603. */
  604. getRtuBaseSetting() {
  605. let datagram = datagramHelp.datagram_41();
  606. let cache = this.data.datagramCache;
  607. cache.push(datagram);
  608. },
  609. pushDatagramBuff(datagram) {
  610. let tmpdatagramBuff = this.data.datagramBuff;
  611. if (tmpdatagramBuff.length > 0) {
  612. tmpdatagramBuff = tmpdatagramBuff + datagram;
  613. } else {
  614. tmpdatagramBuff = datagram;
  615. }
  616. console.log(" datagram buff ==========", tmpdatagramBuff)
  617. this.setData({
  618. datagramBuff: tmpdatagramBuff
  619. })
  620. this.processDatagram();
  621. },
  622. processDatagram() {
  623. let datagram = this.data.datagramBuff;
  624. if (datagram.length > 0) {
  625. for (let i = 0; i < datagram.length; i++) {
  626. if (datagram.indexOf("7e7e") == 0) {
  627. break;
  628. } else {
  629. datagram = datagram.substring(i, datagram.length);
  630. }
  631. }
  632. if (null != datagram && datagram.length > 0 && datagram.indexOf("7e7e") >= 0) {
  633. if (datagram.length >= 26) {
  634. var lenBuff = util.HexStr2Bytes(datagram.substring(22, 22 + 4));
  635. var len = util.byteToUint16(lenBuff); // 报文长度(int)
  636. var datagramlength = 11 + len + 6;
  637. if (datagram.length >= datagramlength * 2) {
  638. var onedatagram = datagram.substring(0, datagramlength * 2);
  639. if (!datagramHelp.crcCheck(onedatagram)) {
  640. wx.showToast({
  641. title: '数据CRC校验错误,请重试!',
  642. icon: 'none',
  643. duration: 2000
  644. })
  645. this.setData({
  646. datagramBuff: ''
  647. })
  648. return;
  649. }
  650. var count = 20;
  651. var funcode = datagram.substring(count, count + 2);
  652. count += 2;
  653. if (funcode == "f6") {
  654. } else if (funcode == "f7") {
  655. } else if (funcode == '45') {
  656. } else if (funcode == '47') {
  657. } else if (funcode == '48') {
  658. } else if (funcode == '41') {
  659. var info = datagramHelp.datagram_41_analyse(onedatagram)
  660. console.log(info)
  661. let rtuCode = info['rtu_code'];
  662. let workModel = info['rtu_work_model'];
  663. let rtuInfo = this.data.rtuInfo;
  664. rtuInfo['rtu_code'] = rtuCode;
  665. rtuInfo['ip1'] = info['ip1'];
  666. rtuInfo['ip2'] = info['ip2'];
  667. rtuInfo['ip3'] = info['ip3'];
  668. rtuInfo['ip4'] = info['ip4'];
  669. rtuInfo['ip1_port'] = info['ip1_port'];
  670. rtuInfo['ip2_port'] = info['ip2_port'];
  671. rtuInfo['ip3_port'] = info['ip3_port'];
  672. rtuInfo['ip4_port'] = info['ip4_port'];
  673. rtuInfo['rtu_kind'] = info['rtu_kind'];
  674. rtuInfo['ipV6Checked1'] = info['ipV6Checked1'];
  675. rtuInfo['ipV6Checked2'] = info['ipV6Checked2'];
  676. rtuInfo['ipV6Checked3'] = info['ipV6Checked3'];
  677. rtuInfo['ipV6Checked4'] = info['ipV6Checked4'];
  678. rtuInfo['netModel'] = info['net_model'];
  679. console.log("netModel" + rtuInfo['netModel']);
  680. let netModels = this.data.netModels;
  681. for (let i = 0; i < netModels.length; i++) {
  682. console.log("netModel " + i + " " + netModels[i].value);
  683. if (netModels[i].value === rtuInfo['netModel']) {
  684. netModels[i].checked = true;
  685. } else {
  686. netModels[i].checked = false;
  687. }
  688. }
  689. console.log("ipv61 " + rtuInfo['ipV6Checked1']);
  690. console.log("ipv62 " + rtuInfo['ipV6Checked2']);
  691. console.log("ipv63 " + rtuInfo['ipV6Checked3']);
  692. console.log("ipv64 " + rtuInfo['ipV6Checked4']);
  693. let checkbox1 = this.data.ip1Checkbox;
  694. let checkbox2 = this.data.ip2Checkbox;
  695. let checkbox3 = this.data.ip3Checkbox;
  696. let checkbox4 = this.data.ip4Checkbox;
  697. if (rtuInfo['ipV6Checked1']) {
  698. checkbox1[0].checked = false;
  699. checkbox1[1].checked = true;
  700. } else {
  701. checkbox1[0].checked = true;
  702. checkbox1[1].checked = false;
  703. }
  704. if (rtuInfo['ipV6Checked2']) {
  705. checkbox2[0].checked = false;
  706. checkbox2[1].checked = true;
  707. } else {
  708. checkbox2[0].checked = true;
  709. checkbox2[1].checked = false;
  710. }
  711. if (rtuInfo['ipV6Checked3']) {
  712. checkbox3[0].checked = false;
  713. checkbox3[1].checked = true;
  714. } else {
  715. checkbox3[0].checked = true;
  716. checkbox3[1].checked = false;
  717. }
  718. if (rtuInfo['ipV6Checked4']) {
  719. checkbox4[0].checked = false;
  720. checkbox4[1].checked = true;
  721. } else {
  722. checkbox4[0].checked = true;
  723. checkbox4[1].checked = false;
  724. }
  725. rtuInfo['rtu_work_model'] = workModel;
  726. let work_model_index = 0;
  727. if (workModel == "0") {
  728. work_model_index = 0;
  729. } else {
  730. work_model_index = 1;
  731. }
  732. let rtu_kind_index = 0;
  733. for (let i = 0; i < this.data.rtu_kind_code.length; i++) {
  734. let code = this.data.rtu_kind_code[i];
  735. if (code == rtuInfo['rtu_kind']) {
  736. rtu_kind_index = i;
  737. break;
  738. }
  739. }
  740. this.setData({
  741. rtuInfo: rtuInfo,
  742. netModels: netModels,
  743. ip1Checkbox: checkbox1,
  744. ip2Checkbox: checkbox2,
  745. ip3Checkbox: checkbox3,
  746. ip4Checkbox: checkbox4,
  747. rtu_work_model_index: work_model_index,
  748. rtu_kind_index: rtu_kind_index
  749. });
  750. let index = this.data.datagramIndex + 1;
  751. if (this.data.datagramCache.length == index) {
  752. index = 0;
  753. }
  754. this.setData({
  755. datagramIndex: index,
  756. trySendCount: 0
  757. })
  758. this.cancelTime();
  759. } else if (funcode == '40') {
  760. this.setData({
  761. showSettingResult: true,
  762. settingResultText: "基本参数配置已完成,请重新连接蓝牙!"
  763. })
  764. // wx.showToast({
  765. // title: 'RTU基本参数操作已完成!',
  766. // icon: 'ok',
  767. // duration: 2000 //持续的时间
  768. // })
  769. } else if (funcode == '43') {
  770. } else if (funcode == '42') {
  771. } else if (funcode == '4A') {
  772. } else if (funcode == '51') {
  773. } else if (funcode == 'f3') {
  774. }
  775. if (datagram.length - datagramlength * 2 > 0) {
  776. let tmp = datagram.substring(datagramlength * 2, datagram.length);
  777. this.setData({
  778. datagramBuff: tmp
  779. })
  780. } else {
  781. this.setData({
  782. datagramBuff: ""
  783. })
  784. }
  785. } else {
  786. this.setData({
  787. datagramBuff: datagram
  788. })
  789. }
  790. } else {
  791. this.setData({
  792. datagramBuff: datagram
  793. })
  794. }
  795. } else {
  796. if (null != datagram && datagram.length > 0) {
  797. this.setData({
  798. datagramBuff: datagram
  799. })
  800. } else {
  801. this.setData({
  802. datagramBuff: ''
  803. })
  804. }
  805. }
  806. }
  807. },
  808. sendDatagramToRtu(datagram) {
  809. console.log("will send datagram " + datagram);
  810. let buffer = util.HexStr2Bytes(datagram);
  811. var that = this;
  812. let count = 0;
  813. let inView = new DataView(buffer);
  814. while (true) {
  815. if (buffer.byteLength - count > 20) {
  816. let tmpBuff = new ArrayBuffer(20);
  817. let outView = new DataView(tmpBuff);
  818. for (let i = 0; i < 20; i++) {
  819. outView.setUint8(i, inView.getUint8(count + i))
  820. }
  821. wx.writeBLECharacteristicValue({
  822. deviceId: that.data.devices['deviceId'],
  823. serviceId: that.data.devices['serviceId'],
  824. characteristicId: that.data.devices['characteristicId'],
  825. value: tmpBuff,
  826. success(res) {
  827. console.log('writeBLECharacteristicValue success', res.errMsg)
  828. wx.onBLECharacteristicValueChange((characteristic) => {
  829. console.log("read datagram==============", util.ab2hex(characteristic.value))
  830. var datagram = util.ab2hex(characteristic.value);
  831. that.pushDatagramBuff(datagram);
  832. })
  833. wx.readBLECharacteristicValue({
  834. deviceId: that.data.devices['deviceId'],
  835. serviceId: that.data.devices['serviceId'],
  836. characteristicId: that.data.devices['characteristicId'],
  837. success: function (res) {
  838. console.log('readBLECharacteristicValue')
  839. }
  840. })
  841. },
  842. fail(res) {
  843. console.log('writeBLECharacteristicValue success', res.errMsg)
  844. }
  845. })
  846. count += 20;
  847. } else {
  848. let len = buffer.byteLength - count;
  849. let tmpBuff = new ArrayBuffer(len);
  850. let outView = new DataView(tmpBuff);
  851. for (let i = 0; i < len; i++) {
  852. outView.setUint8(i, inView.getUint8(count + i))
  853. }
  854. wx.writeBLECharacteristicValue({
  855. deviceId: that.data.devices['deviceId'],
  856. serviceId: that.data.devices['serviceId'],
  857. characteristicId: that.data.devices['characteristicId'],
  858. value: tmpBuff,
  859. success(res) {
  860. console.log('writeBLECharacteristicValue success', res.errMsg)
  861. wx.onBLECharacteristicValueChange((characteristic) => {
  862. console.log("read datagram==============", util.ab2hex(characteristic.value))
  863. var datagram = util.ab2hex(characteristic.value);
  864. that.pushDatagramBuff(datagram);
  865. })
  866. wx.readBLECharacteristicValue({
  867. deviceId: that.data.devices['deviceId'],
  868. serviceId: that.data.devices['serviceId'],
  869. characteristicId: that.data.devices['characteristicId'],
  870. success: function (res) {
  871. console.log('readBLECharacteristicValue')
  872. }
  873. })
  874. },
  875. fail(res) {
  876. console.log('writeBLECharacteristicValue success', res.errMsg)
  877. }
  878. })
  879. count += len;
  880. }
  881. if (count >= buffer.byteLength) {
  882. break;
  883. }
  884. }
  885. },
  886. timeCallback() {
  887. var that = this;
  888. task_timer = setTimeout(function () {
  889. console.log("------------------------------------Time CallBack----------------------------");
  890. if (that.data.trySendCount == 0) {
  891. if (that.data.datagramCache.length > 0) {
  892. let datagram = that.data.datagramCache[that.data.datagramIndex];
  893. that.sendDatagramToRtu(datagram);
  894. let index = that.data.datagramIndex + 1;
  895. if (that.data.datagramCache.length == index) {
  896. index = 0;
  897. }
  898. let count = that.data.trySendCount + 1;
  899. that.setData({
  900. datagramIndex: index,
  901. trySendCount: count
  902. })
  903. }
  904. } else {
  905. let count = that.data.trySendCount + 1;
  906. if (count >= 10) {
  907. count = 0;
  908. }
  909. that.setData({
  910. trySendCount: count
  911. })
  912. }
  913. that.timeCallback();
  914. }, 3000);
  915. },
  916. cancelTime() {
  917. if (task_timer != undefined) {
  918. clearTimeout(task_timer);
  919. task_timer = undefined;
  920. }
  921. this.setData({
  922. datagramIndex: 0,
  923. trySendCount: 0,
  924. suspend: true
  925. })
  926. },
  927. /**
  928. * 第一路IPV6勾选项
  929. * @param {} e
  930. */
  931. netModelsCheckedChange: function (e) {
  932. const netModels = this.data.netModels;
  933. let netModel = e.detail.value;
  934. for (let i = 0, len = netModels.length; i < len; ++i) {
  935. netModels[i].checked = netModels[i].value === e.detail.value
  936. }
  937. const rtuInfo = this.data.rtuInfo;
  938. rtuInfo.netModel = netModel;
  939. this.setData({
  940. netModels,
  941. rtuInfo
  942. })
  943. },
  944. /**
  945. * 第一路IPV6勾选项
  946. * @param {} e
  947. */
  948. ip1CheckedChange: function (e) {
  949. const ip1Checkbox = this.data.ip1Checkbox;
  950. let ipV6Checked1 = false;
  951. for (let i = 0, len = ip1Checkbox.length; i < len; ++i) {
  952. ip1Checkbox[i].checked = ip1Checkbox[i].value === e.detail.value
  953. if (e.detail.value === 'IPV6') {
  954. ipV6Checked1 = true;
  955. }
  956. }
  957. const rtuInfo = this.data.rtuInfo;
  958. rtuInfo.ipV6Checked1 = ipV6Checked1;
  959. this.setData({
  960. ip1Checkbox,
  961. rtuInfo
  962. })
  963. },
  964. /**
  965. * 第二路IP勾选项
  966. * @param {} e
  967. */
  968. ip2CheckedChange: function (e) {
  969. const ip2Checkbox = this.data.ip2Checkbox;
  970. let ipV6Checked2 = false;
  971. for (let i = 0, len = ip2Checkbox.length; i < len; ++i) {
  972. ip2Checkbox[i].checked = ip2Checkbox[i].value === e.detail.value
  973. if (e.detail.value === 'IPV6') {
  974. ipV6Checked2 = true;
  975. }
  976. }
  977. const rtuInfo = this.data.rtuInfo;
  978. rtuInfo.ipV6Checked2 = ipV6Checked2;
  979. this.setData({
  980. ip2Checkbox,
  981. rtuInfo
  982. })
  983. },
  984. /**
  985. * 第三路IP勾选项
  986. * @param {} e
  987. */
  988. ip3CheckedChange: function (e) {
  989. const ip3Checkbox = this.data.ip3Checkbox;
  990. let ipV6Checked3 = false;
  991. for (let i = 0, len = ip3Checkbox.length; i < len; ++i) {
  992. ip3Checkbox[i].checked = ip3Checkbox[i].value === e.detail.value
  993. if (e.detail.value === 'IPV6') {
  994. ipV6Checked3 = true;
  995. }
  996. }
  997. const rtuInfo = this.data.rtuInfo;
  998. rtuInfo.ipV6Checked3 = ipV6Checked3;
  999. this.setData({
  1000. ip3Checkbox,
  1001. rtuInfo
  1002. })
  1003. },
  1004. /**
  1005. * 第四路IP勾选项
  1006. * @param {} e
  1007. */
  1008. ip4CheckedChange: function (e) {
  1009. const ip4Checkbox = this.data.ip4Checkbox;
  1010. let ipV6Checked4 = false;
  1011. for (let i = 0, len = ip4Checkbox.length; i < len; ++i) {
  1012. ip4Checkbox[i].checked = ip4Checkbox[i].value === e.detail.value
  1013. if (e.detail.value === 'IPV6') {
  1014. ipV6Checked4 = true;
  1015. }
  1016. }
  1017. const rtuInfo = this.data.rtuInfo;
  1018. rtuInfo.ipV6Checked4 = ipV6Checked4;
  1019. this.setData({
  1020. ip4Checkbox,
  1021. rtuInfo
  1022. })
  1023. }
  1024. })