bluetoothClock.js 12 KB

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