ZXBTS.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. const common = require('../common.js');
  2. const app = getApp();
  3. const readServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  4. const readID = '000036F6-0000-1000-8000-00805F9B34FB';
  5. const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  6. const writeID = '000036F5-0000-1000-8000-00805F9B34FB';
  7. const MTU = 128;
  8. var joinPack = [];
  9. var lastLength = 0;
  10. var curTime = 0;
  11. var stateRepeatTime = 0;
  12. var readRepeatTime = 0;
  13. function acceptDevice(device) {
  14. return device.btid ? true : false;
  15. }
  16. function isSingleBt() {
  17. return true;
  18. }
  19. function haveBms() {
  20. console.log('是单蓝牙并且带bms');
  21. return true;
  22. }
  23. function bmsSet(device) {
  24. // if (BMSSet.isBleBmsSet(device)) {
  25. var bmsSetCmd = BMSSet.bmsSet(device);
  26. var list = hexToList(bmsSetCmd);
  27. return [sendDataCommand(list)];
  28. // }
  29. // else return false
  30. }
  31. function isDevice(device, data) {
  32. const advertisData = new Uint8Array(data.advertisData);
  33. const mac = device.btid
  34. .split('')
  35. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  36. .filter((p, i) => i % 2 == 0);
  37. if (advertisData.slice(0, 4).toString() == [9, 255, 1, 2].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. function sendCommand(cmd, data = []) {
  43. data = [cmd, data.length].concat(data);
  44. return common.completArrayCRC(data, data.length);
  45. }
  46. // 鉴权
  47. function alterConnect(device, deviceId) {
  48. return [sendCommand(1, [171, 205, 171, 205])];
  49. }
  50. function readData(device, value, data) {
  51. console.log("读取到数据"+value)
  52. if (device.bms == 0) {
  53. var value = new Uint8Array(value);
  54. switch (value[0]) {
  55. case 68:
  56. if (value.length == 5) {
  57. data.voltage = (((value[2] << 8) | value[3]) / 100).toFixed(2);
  58. }
  59. break;
  60. case 66:
  61. break;
  62. }
  63. var endTime = new Date().getTime();
  64. if (endTime - curTime < 15000) {
  65. return;
  66. }
  67. if (value[0] != 68 && value.length != 5) {
  68. return;
  69. }
  70. if (endTime - curTime > 15000) {
  71. if (value[0] == 68) {
  72. curTime = new Date().getTime();
  73. }
  74. return data;
  75. }
  76. } else {
  77. var value = new Uint8Array(value);
  78. console.log(value);
  79. switch (value[0]) {
  80. case 66:
  81. data = packBmsData(device, value, data);
  82. return data;
  83. break;
  84. case 112:
  85. if (value.length === 4) {
  86. data.baud = value[2];
  87. return data;
  88. }
  89. }
  90. }
  91. }
  92. function packBmsData(device, value, data) {
  93. if (value) {
  94. if (value[1] === value.length - 6) {
  95. var totalPackLength = value[2] * 256 + value[3];
  96. if (totalPackLength !== lastLength) {
  97. joinPack = [];
  98. }
  99. lastLength = totalPackLength;
  100. if (totalPackLength > joinPack.length && totalPackLength > 30) {
  101. if (value.length > 4 && value[4] !== 0) {
  102. var pack = value.slice(5, -1);
  103. if (joinPack.length === 0) {
  104. joinPack = pack;
  105. } else {
  106. joinPack = mergeUint8Array(joinPack, pack);
  107. }
  108. }
  109. if (value.length > 4 && value[4] === 0) {
  110. var endTime = new Date().getTime();
  111. if (endTime - readRepeatTime > 15000) {
  112. //防止多条数据重发
  113. var pack = value.slice(5, -1);
  114. joinPack = mergeUint8Array(joinPack, pack);
  115. readRepeatTime = endTime;
  116. data = joinPack;
  117. console.log(data);
  118. joinPack = [];
  119. return data;
  120. } else {
  121. joinPack = [];
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. function sendDataCommand(data) {
  129. data = [2, data.length, (data.length & 65280) >> 8, data.length & 255, 0].concat(data);
  130. return common.completArrayCRC(data, data.length);
  131. }
  132. function mergeUint8Array(arr1, arr2) {
  133. let len1 = arr1 ? arr1.length : 0;
  134. let len2 = arr2.length;
  135. let arr = new Uint8Array(len1 + len2);
  136. for (let i = 0; i < len1; i++) {
  137. arr[i] = arr1[i];
  138. }
  139. for (let i = 0; i < len2; i++) {
  140. arr[len1 + i] = arr2[i];
  141. }
  142. return arr;
  143. }
  144. function stateUpdate(device, deviceId) {
  145. if (device.bms == 0) {
  146. curTime = 0;
  147. return [sendCommand(4)];
  148. } else {
  149. console.log("获取电池数据")
  150. readRepeatTime = 0;
  151. var endTime = new Date().getTime();
  152. if (endTime - stateRepeatTime > 3000) {
  153. //防止多条数据重发
  154. stateRepeatTime = endTime;
  155. const bluetoothConfig = app.globalData.bluetoothConfig;
  156. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  157. var readCommandList = bluetoothConfig[device.device_type].read;
  158. var commandList = [];
  159. for (var i = 0; readCommandList.length > i; i++) {
  160. var list = hexToList(readCommandList[i]);
  161. commandList.push(sendDataCommand(list));
  162. }
  163. console.log("commandList"+commandList)
  164. return commandList;
  165. }
  166. }
  167. }
  168. }
  169. function turnOn(device, deviceId) {
  170. if (device.bms == 0) {
  171. return [sendCommand(3, [170])];
  172. } else {
  173. readRepeatTime = 0;
  174. const bluetoothConfig = app.globalData.bluetoothConfig;
  175. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  176. var turnonCommand = bluetoothConfig[device.device_type].turnon;
  177. var commandList = [];
  178. var list = hexToList(turnonCommand);
  179. commandList.push(sendDataCommand(list));
  180. return commandList;
  181. }
  182. }
  183. }
  184. function turnOff(device, deviceId) {
  185. if (device.bms == 0) {
  186. return [sendCommand(3, [187])];
  187. } else {
  188. readRepeatTime = 0;
  189. const bluetoothConfig = app.globalData.bluetoothConfig;
  190. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  191. var turnoffCommand = bluetoothConfig[device.device_type].turnoff;
  192. var commandList = [];
  193. var list = hexToList(turnoffCommand);
  194. commandList.push(sendDataCommand(list));
  195. return commandList;
  196. }
  197. }
  198. }
  199. function bmsDischargeMOS(value, device) {
  200. if (value == 1) {
  201. const bluetoothConfig = app.globalData.bluetoothConfig;
  202. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  203. var turnonCommand = bluetoothConfig[device.device_type].turnon;
  204. var commandList = [];
  205. var list = hexToList(turnonCommand);
  206. commandList.push(sendDataCommand(list));
  207. return commandList;
  208. }
  209. } else {
  210. const bluetoothConfig = app.globalData.bluetoothConfig;
  211. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  212. var turnoffCommand = bluetoothConfig[device.device_type].turnoff;
  213. var commandList = [];
  214. var list = hexToList(turnoffCommand);
  215. //this.getConsoleData(sendDataCommand(list))
  216. commandList.push(sendDataCommand(list));
  217. return commandList;
  218. }
  219. }
  220. }
  221. function bmsChargingMOS(value, device) {
  222. if (value == 1) {
  223. const bluetoothConfig = app.globalData.bluetoothConfig;
  224. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  225. var chargingMosOn = bluetoothConfig[device.device_type].chargingMosOn;
  226. var commandList = [];
  227. var list = hexToList(chargingMosOn);
  228. commandList.push(sendDataCommand(list));
  229. return commandList;
  230. }
  231. } else {
  232. const bluetoothConfig = app.globalData.bluetoothConfig;
  233. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  234. var chargingMosOff = bluetoothConfig[device.device_type].chargingMosOff;
  235. var commandList = [];
  236. var list = hexToList(chargingMosOff);
  237. //this.getConsoleData(sendDataCommand(list))
  238. commandList.push(sendDataCommand(list));
  239. return commandList;
  240. }
  241. }
  242. }
  243. function turnOnBuzzer(device, deviceId) {
  244. return [sendCommand(80, [170])];
  245. }
  246. function turnOffBuzzer(device, deviceId) {
  247. return [sendCommand(80, [187])];
  248. }
  249. function queryBaud(device, deviceId) {
  250. console.log('下发查询波特率');
  251. return [[7, 1, 8]];
  252. }
  253. function setBaud(value, device) {
  254. console.log('下发设置波特率');
  255. console.log('下发baud');
  256. console.log([sendCommand(8, [value])]);
  257. return [sendCommand(8, [value])];
  258. }
  259. function hexToList(str) {
  260. var val = [];
  261. for (var i = 0; i < str.length / 2; i++) {
  262. val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16));
  263. }
  264. return val;
  265. }
  266. module.exports = {
  267. readServiceID: readServiceID,
  268. readID: readID,
  269. writeServiceID: writeServiceID,
  270. writeID: writeID,
  271. MTU: MTU,
  272. acceptDevice: acceptDevice,
  273. isDevice: isDevice,
  274. alterConnect: alterConnect,
  275. readData: readData,
  276. stateUpdate: stateUpdate,
  277. turnOn: turnOn,
  278. turnOff: turnOff,
  279. isSingleBt: isSingleBt,
  280. haveBms: haveBms,
  281. bmsDischargeMOS: bmsDischargeMOS,
  282. bmsChargingMOS: bmsChargingMOS,
  283. turnOnBuzzer: turnOnBuzzer,
  284. turnOffBuzzer: turnOffBuzzer,
  285. queryBaud: queryBaud,
  286. setBaud: setBaud,
  287. bmsSet: bmsSet
  288. };