LSBMS.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const bluetooth = require('../bluetooth.js');
  2. const parse = require('./blueParse/LSBMS.js');
  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 common = require('../common.js');
  8. const app = getApp();
  9. const MTU = 115;
  10. var curTime = 0;
  11. var isEndPackage = true;
  12. var total_length = 0;
  13. var functionCode = 0;
  14. var functionList = [0, 0, 0, 0];
  15. function acceptDevice(device) {
  16. return device.btid ? true : false;
  17. }
  18. function haveBms() {
  19. console.log('是单蓝牙并且带bms');
  20. return true;
  21. }
  22. function isSingleBt() {
  23. return true;
  24. }
  25. function isDevice(device, data) {
  26. const advertisData = new Uint8Array(data.advertisData);
  27. const mac = device.btid
  28. .split('')
  29. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  30. .filter((p, i) => i % 2 == 0);
  31. if (advertisData.slice(0, 4).toString() == [9, 255, 1, 2].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. function sendCommand(cmd, data = []) {
  37. data = [cmd, data.length].concat(data);
  38. return common.completArrayCRC(data, data.length);
  39. }
  40. // 鉴权
  41. function alterConnect(device, deviceId) {
  42. return [sendCommand(1, [171, 205, 171, 205])];
  43. }
  44. function readData(device, value, data) {
  45. var value = new Uint8Array(value);
  46. console.log(value);
  47. let macid = device.mac_id;
  48. if (!isEndPackage) {
  49. var endTime = new Date().getTime();
  50. if (endTime - curTime < 800) {
  51. curTime = endTime;
  52. var bms_data = mergeUint8Array(app.globalData.deviceBTBMSBuf[macid].bms_origin_data, value);
  53. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bms_data;
  54. if (bms_data.length === total_length) {
  55. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bms_data.slice(0, -1);
  56. if (functionCode === 30) {
  57. functionList.splice(0, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  58. }
  59. if (functionCode === 32) {
  60. functionList.splice(2, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  61. }
  62. if (functionCode === 9) {
  63. functionList.splice(3, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  64. console.log(functionList);
  65. }
  66. if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
  67. data = loadDoneBmsInfo(functionList, macid);
  68. }
  69. isEndPackage = true;
  70. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
  71. }
  72. } else {
  73. isEndPackage = true;
  74. }
  75. }
  76. switch (value[0]) {
  77. case 59:
  78. {
  79. if (value[4] === 30) {
  80. functionCode = 30;
  81. curTime = new Date().getTime();
  82. isEndPackage = false;
  83. total_length = value[5] + 1;
  84. var bmsData = value.slice(6);
  85. if (bmsData.length === total_length) {
  86. isEndPackage = true;
  87. }
  88. app.globalData.deviceBTBMSBuf[macid] = {
  89. bms_origin_data: null
  90. };
  91. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
  92. }
  93. if (value[4] === 31) {
  94. functionCode = 31;
  95. curTime = new Date().getTime();
  96. isEndPackage = false;
  97. total_length = value[5] + 1;
  98. var bmsData = value.slice(6);
  99. if (bmsData.length === total_length) {
  100. isEndPackage = true;
  101. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
  102. functionList.splice(1, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  103. if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
  104. data = loadDoneBmsInfo(functionList, macid);
  105. }
  106. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
  107. }
  108. app.globalData.deviceBTBMSBuf[macid] = {
  109. bms_origin_data: null
  110. };
  111. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
  112. }
  113. if (value[4] === 32) {
  114. functionCode = 32;
  115. curTime = new Date().getTime();
  116. isEndPackage = false;
  117. total_length = value[5] + 1;
  118. var bmsData = value.slice(6);
  119. if (bmsData.length === total_length) {
  120. isEndPackage = true;
  121. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
  122. functionList.splice(2, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  123. if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
  124. data = loadDoneBmsInfo(functionList, macid);
  125. }
  126. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
  127. }
  128. app.globalData.deviceBTBMSBuf[macid] = {
  129. bms_origin_data: null
  130. };
  131. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
  132. }
  133. if (value[4] === 9) {
  134. console.log('xxxxx');
  135. functionCode = 9;
  136. curTime = new Date().getTime();
  137. isEndPackage = false;
  138. total_length = value[5] + 1;
  139. var bmsData = value.slice(6);
  140. if (bmsData.length === total_length) {
  141. console.log('09功能进来了');
  142. isEndPackage = true;
  143. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
  144. functionList.splice(3, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
  145. console.log('functionList');
  146. console.log(functionList);
  147. if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
  148. data = loadDoneBmsInfo(functionList, macid);
  149. console.log(data);
  150. }
  151. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
  152. }
  153. console.log('bmsDatabmsDatabmsData');
  154. console.log(bmsData);
  155. app.globalData.deviceBTBMSBuf[macid] = {
  156. bms_origin_data: null
  157. };
  158. app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
  159. }
  160. }
  161. break;
  162. }
  163. return data;
  164. }
  165. function loadDoneBmsInfo(functionList, macid) {
  166. var data = parse.parseBmsData(functionList[0], functionList[1], functionList[2]);
  167. common.reportBluetooth(macid, functionList[0], functionList[1], functionList[2], functionList[3]);
  168. data.voltage = data.state.voltageAll;
  169. data.quantity = data.state.soc;
  170. data.soh = data.state.soh;
  171. data.alarm = data.state.alarm;
  172. data.temp = data.state.temp.length != 0 ? data.state.temp[0] : 0;
  173. console.log(data);
  174. return data;
  175. }
  176. function stateUpdate(device, deviceId) {
  177. console.log('读电压');
  178. return [
  179. [58, 126, 1, 1, 30, 0, 216],
  180. [58, 126, 1, 1, 31, 0, 217],
  181. [58, 126, 1, 1, 32, 0, 218],
  182. [58, 126, 1, 1, 9, 0, 195]
  183. ];
  184. }
  185. function turnOn(device, deviceId) {
  186. console.log('通电');
  187. //return [sendCommand(0x03, [0xaa])]
  188. return [[58, 126, 1, 0, 60, 4, 255, 1, 255, 255, 247]];
  189. }
  190. function turnOff(device, deviceId) {
  191. console.log('断电');
  192. //return [sendCommand(0x03, [0xbb])]
  193. return [[58, 126, 1, 0, 60, 4, 255, 0, 255, 255, 246]];
  194. }
  195. function bmsChargingMOS(value) {
  196. if (value == 1) {
  197. return [[58, 126, 1, 0, 60, 4, 255, 255, 1, 255, 247]];
  198. } else {
  199. return [[58, 126, 1, 0, 60, 4, 255, 255, 0, 255, 246]];
  200. }
  201. }
  202. function bmsDischargeMOS(value, device) {
  203. if (value == 1) {
  204. return [[58, 126, 1, 0, 60, 4, 255, 1, 255, 255, 247]];
  205. } else {
  206. console.log('xxxxxx44');
  207. return [[58, 126, 1, 0, 60, 4, 255, 0, 255, 255, 246]];
  208. }
  209. }
  210. function mergeUint8Array(arr1, arr2) {
  211. let len1 = arr1 ? arr1.length : 0;
  212. let len2 = arr2.length;
  213. let arr = new Uint8Array(len1 + len2);
  214. for (let i = 0; i < len1; i++) {
  215. arr[i] = arr1[i];
  216. }
  217. for (let i = 0; i < len2; i++) {
  218. arr[len1 + i] = arr2[i];
  219. }
  220. return arr;
  221. }
  222. // function bmsInfo(){
  223. // }
  224. module.exports = {
  225. readServiceID: readServiceID,
  226. readID: readID,
  227. writeServiceID: writeServiceID,
  228. writeID: writeID,
  229. MTU: MTU,
  230. acceptDevice: acceptDevice,
  231. // bmsInfo: bmsInfo,
  232. isDevice: isDevice,
  233. alterConnect: alterConnect,
  234. readData: readData,
  235. stateUpdate: stateUpdate,
  236. turnOn: turnOn,
  237. turnOff: turnOff,
  238. isSingleBt: isSingleBt,
  239. haveBms: haveBms,
  240. bmsChargingMOS: bmsChargingMOS,
  241. bmsDischargeMOS: bmsDischargeMOS
  242. };