bluetoothInfo.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. datagram_idx: '',
  25. collect_time: '',
  26. acc_rain: '',
  27. fiveMinute_rain: '',
  28. hour_rain: '',
  29. weater_level: '',
  30. power_v: '',
  31. rtu_ver: '',
  32. rtu_uuid: '',
  33. bt_v: '',
  34. rtu_rssi: '',
  35. rtu_temperature: '',
  36. lua_ver: ''
  37. },
  38. datagramBuff: '',
  39. datagramCache: [],
  40. trySendCount: 0,
  41. datagramIndex: 0,
  42. showSettingResult: false,
  43. settingResultText: '操作已完成!'
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: function (options) {
  49. console.log('bluebooth info page load ', '**********************************************')
  50. let deviceId = options.deviceId;
  51. let serviceId = options.serviceId;
  52. let characteristicId = options.characteristicId;
  53. let connectedBluetoothName = options.connectedBluetoothName;
  54. console.log('name' + connectedBluetoothName)
  55. let devices = {}
  56. devices['deviceId'] = deviceId;
  57. devices['serviceId'] = serviceId;
  58. devices['characteristicId'] = characteristicId;
  59. devices['connectedBluetoothName'] = connectedBluetoothName;
  60. this.setData({
  61. devices: devices,
  62. trySendCount: 0,
  63. datagramIndex: 0
  64. })
  65. task_timer = undefined;
  66. this.getRtuStatus();
  67. if (this.data.datagramCache.length > 0) {
  68. let datagram = this.data.datagramCache[this.data.datagramIndex];
  69. this.sendDatagramToRtu(datagram);
  70. let index = this.data.datagramIndex + 1;
  71. if (this.data.datagramCache.length == index) {
  72. index = 0;
  73. }
  74. let count = this.data.trySendCount + 1;
  75. this.setData({
  76. datagramIndex: index,
  77. trySendCount: count
  78. })
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面隐藏
  83. */
  84. onHide: function () {
  85. console.log('bluebooth info page onHide ', '**********************************************');
  86. this.cancelTime();
  87. },
  88. /**
  89. * 生命周期函数--监听页面卸载
  90. */
  91. onUnload: function () {
  92. console.log('bluebooth info page onUnload ', '**********************************************');
  93. this.cancelTime();
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow: function () {
  99. console.log('bluebooth info page onShow ', '**********************************************');
  100. this.timeCallback();
  101. },
  102. settingCompletTabEvent(e) {
  103. this.setData({
  104. showSettingResult: false
  105. })
  106. const btn = e.detail.index;
  107. if (btn == 0) {
  108. wx.navigateBack();
  109. }
  110. },
  111. resetMemryBtnTap: function () {
  112. this.cancelTime();
  113. this.formatRtuMemory();
  114. },
  115. resetRtuBtnTap: function () {
  116. this.cancelTime();
  117. this.resetRtu();
  118. },
  119. rebootBtnTap: function () {
  120. this.cancelTime();
  121. this.rebootRtu();
  122. },
  123. /**
  124. * 发送查询RTU状态信息的指令
  125. */
  126. getRtuStatus() {
  127. var datagram = datagramHelp.datagram_f7();
  128. let cache = this.data.datagramCache;
  129. cache.push(datagram);
  130. },
  131. rebootRtu() {
  132. let datagram = datagramHelp.datagram_f3();
  133. this.sendDatagramToRtu(datagram);
  134. },
  135. /**
  136. * 恢复RTU出厂设置
  137. */
  138. resetRtu() {
  139. let datagram = datagramHelp.datagram_48();
  140. this.sendDatagramToRtu(datagram);
  141. },
  142. /**
  143. * 格式化RTU存储器
  144. */
  145. formatRtuMemory() {
  146. let datagram = datagramHelp.datagram_47();
  147. this.sendDatagramToRtu(datagram);
  148. },
  149. pushDatagramBuff(datagram) {
  150. let tmpdatagramBuff = this.data.datagramBuff;
  151. if (tmpdatagramBuff.length > 0) {
  152. tmpdatagramBuff = tmpdatagramBuff + datagram;
  153. } else {
  154. tmpdatagramBuff = datagram;
  155. }
  156. console.log(" datagram buff ==========", tmpdatagramBuff)
  157. this.setData({
  158. datagramBuff: tmpdatagramBuff
  159. })
  160. this.processDatagram();
  161. },
  162. processDatagram() {
  163. let datagram = this.data.datagramBuff;
  164. if (datagram.length > 0) {
  165. for (let i = 0; i < datagram.length; i++) {
  166. if (datagram.indexOf("7e7e") == 0) {
  167. break;
  168. } else {
  169. datagram = datagram.substring(i, datagram.length);
  170. }
  171. }
  172. if (null != datagram && datagram.length > 0 && datagram.indexOf("7e7e") >= 0) {
  173. if (datagram.length >= 26) {
  174. var lenBuff = util.HexStr2Bytes(datagram.substring(22, 22 + 4));
  175. var len = util.byteToUint16(lenBuff); // 报文长度(int)
  176. var datagramlength = 11 + len + 6;
  177. if (datagram.length >= datagramlength * 2) {
  178. var onedatagram = datagram.substring(0, datagramlength * 2);
  179. if (!datagramHelp.crcCheck(onedatagram)) {
  180. wx.showToast({
  181. title: '数据CRC校验错误,请重试!',
  182. icon: 'none',
  183. duration: 2000
  184. })
  185. this.setData({
  186. datagramBuff: ''
  187. })
  188. return;
  189. }
  190. var count = 20;
  191. var funcode = datagram.substring(count, count + 2);
  192. count += 2;
  193. if (funcode == "f6") {
  194. } else if (funcode == "f7") {
  195. let info = datagramHelp.datagram_f7_analyse(onedatagram)
  196. console.log(info)
  197. let rtuInfo = this.data.rtuInfo;
  198. rtuInfo['collect_time'] = info['collect_time'];
  199. rtuInfo['acc_rain'] = info['acc_rain'];
  200. rtuInfo['fiveMinute_rain'] = info['fiveMinute_rain'];
  201. rtuInfo['hour_rain'] = info['hour_rain'];
  202. rtuInfo['fiveMinute_rain'] = info['fiveMinute_rain'];
  203. rtuInfo['hour_rain'] = info['hour_rain'];
  204. rtuInfo['weater_level'] = info['weater_level'];
  205. rtuInfo['power_v'] = info['power_v'];
  206. rtuInfo['rtu_uuid'] = info['rtu_uuid'];
  207. rtuInfo['rtu_ver'] = info['rtu_ver'];
  208. rtuInfo['bt_v'] = info['bt_v'];
  209. rtuInfo['rtu_rssi'] = info['rtu_rssi'];
  210. rtuInfo['rtu_temperature'] = info['rtu_temperature'];
  211. rtuInfo['lua_ver'] = info['lua_ver'];
  212. this.setData({
  213. rtuInfo: rtuInfo
  214. });
  215. let index = this.data.datagramIndex + 1;
  216. if (this.data.datagramCache.length == index) {
  217. index = 0;
  218. }
  219. this.setData({
  220. datagramIndex: index,
  221. trySendCount: 0
  222. })
  223. this.cancelTime();
  224. } else if (funcode == '45') {
  225. } else if (funcode == '47') {
  226. this.setData({
  227. showSettingResult: true,
  228. settingResultText: "设备存储重置操作已完成!"
  229. })
  230. // wx.showToast({
  231. // title: '操作已完成!',
  232. // icon: 'ok',
  233. // duration: 2000 //持续的时间
  234. // })
  235. } else if (funcode == '48') {
  236. this.setData({
  237. showSettingResult: true,
  238. settingResultText: "恢复出厂设置操作已完成!"
  239. })
  240. // wx.showToast({
  241. // title: '操作已完成!',
  242. // icon: 'ok',
  243. // duration: 2000 //持续的时间
  244. // })
  245. } else if (funcode == '41') {
  246. } else if (funcode == '40') {
  247. } else if (funcode == '43') {
  248. } else if (funcode == '42') {
  249. } else if (funcode == '4a') {
  250. } else if (funcode == '51') {
  251. } else if (funcode == 'f3') {
  252. this.setData({
  253. showSettingResult: true,
  254. settingResultText: "已重启RTU,请重新连接蓝牙!"
  255. })
  256. // wx.showToast({
  257. // title: '操作已完成!',
  258. // icon: 'ok',
  259. // duration: 2000 //持续的时间
  260. // })
  261. }
  262. if (datagram.length - datagramlength * 2 > 0) {
  263. let tmp = datagram.substring(datagramlength * 2, datagram.length);
  264. this.setData({
  265. datagramBuff: tmp
  266. })
  267. }
  268. } else {
  269. this.setData({
  270. datagramBuff: datagram
  271. })
  272. }
  273. } else {
  274. this.setData({
  275. datagramBuff: datagram
  276. })
  277. }
  278. } else {
  279. if (null != datagram && datagram.length > 0) {
  280. this.setData({
  281. datagramBuff: datagram
  282. })
  283. } else {
  284. this.setData({
  285. datagramBuff: ''
  286. })
  287. }
  288. }
  289. }
  290. },
  291. sendDatagramToRtu(datagram) {
  292. console.log("will send datagram " + datagram);
  293. let buffer = util.HexStr2Bytes(datagram);
  294. var that = this;
  295. let count = 0;
  296. let inView = new DataView(buffer);
  297. while (true) {
  298. if (buffer.byteLength - count > 20) {
  299. let tmpBuff = new ArrayBuffer(20);
  300. let outView = new DataView(tmpBuff);
  301. for (let i = 0; i < 20; i++) {
  302. outView.setUint8(i, inView.getUint8(count + i))
  303. }
  304. wx.writeBLECharacteristicValue({
  305. deviceId: that.data.devices['deviceId'],
  306. serviceId: that.data.devices['serviceId'],
  307. characteristicId: that.data.devices['characteristicId'],
  308. value: tmpBuff,
  309. success(res) {
  310. console.log('writeBLECharacteristicValue success', res.errMsg)
  311. wx.onBLECharacteristicValueChange((characteristic) => {
  312. console.log("read datagram==============", util.ab2hex(characteristic.value))
  313. var datagram = util.ab2hex(characteristic.value);
  314. that.pushDatagramBuff(datagram);
  315. })
  316. wx.readBLECharacteristicValue({
  317. deviceId: that.data.devices['deviceId'],
  318. serviceId: that.data.devices['serviceId'],
  319. characteristicId: that.data.devices['characteristicId'],
  320. success: function (res) {
  321. console.log('readBLECharacteristicValue')
  322. }
  323. })
  324. },
  325. fail(res) {
  326. console.log('writeBLECharacteristicValue success', res.errMsg)
  327. }
  328. })
  329. count += 20;
  330. } else {
  331. let len = buffer.byteLength - count;
  332. let tmpBuff = new ArrayBuffer(len);
  333. let outView = new DataView(tmpBuff);
  334. for (let i = 0; i < len; i++) {
  335. outView.setUint8(i, inView.getUint8(count + i))
  336. }
  337. wx.writeBLECharacteristicValue({
  338. deviceId: that.data.devices['deviceId'],
  339. serviceId: that.data.devices['serviceId'],
  340. characteristicId: that.data.devices['characteristicId'],
  341. value: tmpBuff,
  342. success(res) {
  343. console.log('writeBLECharacteristicValue success', res.errMsg)
  344. wx.onBLECharacteristicValueChange((characteristic) => {
  345. console.log("read datagram==============", util.ab2hex(characteristic.value))
  346. var datagram = util.ab2hex(characteristic.value);
  347. that.pushDatagramBuff(datagram);
  348. })
  349. wx.readBLECharacteristicValue({
  350. deviceId: that.data.devices['deviceId'],
  351. serviceId: that.data.devices['serviceId'],
  352. characteristicId: that.data.devices['characteristicId'],
  353. success: function (res) {
  354. console.log('readBLECharacteristicValue')
  355. }
  356. })
  357. },
  358. fail(res) {
  359. console.log('writeBLECharacteristicValue success', res.errMsg)
  360. }
  361. })
  362. count += len;
  363. }
  364. if (count >= buffer.byteLength) {
  365. break;
  366. }
  367. }
  368. },
  369. timeCallback() {
  370. var that = this;
  371. task_timer = setTimeout(function () {
  372. console.log("------------------------------------Time CallBack----------------------------");
  373. if (that.data.trySendCount == 0) {
  374. let datagram = that.data.datagramCache[that.data.datagramIndex];
  375. that.sendDatagramToRtu(datagram);
  376. let index = that.data.datagramIndex + 1;
  377. if (that.data.datagramCache.length == index) {
  378. index = 0;
  379. }
  380. let count = that.data.trySendCount + 1;
  381. that.setData({
  382. datagramIndex: index,
  383. trySendCount: count
  384. })
  385. } else {
  386. let count = that.data.trySendCount + 1;
  387. if (count >= 10) {
  388. count = 0;
  389. }
  390. that.setData({
  391. trySendCount: count
  392. })
  393. }
  394. that.timeCallback();
  395. }, 3000);
  396. },
  397. cancelTime() {
  398. if (task_timer != undefined) {
  399. clearTimeout(task_timer);
  400. task_timer = undefined;
  401. }
  402. this.setData({
  403. datagramIndex: 0,
  404. trySendCount: 0
  405. })
  406. }
  407. })