LFBMS.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const readServiceID = '0000FFE0-0000-1000-8000-00805F9B34FB';
  2. const readID = '0000FFE1-0000-1000-8000-00805F9B34FB';
  3. const writeServiceID = '0000FFE0-0000-1000-8000-00805F9B34FB';
  4. const writeID = '0000FFE1-0000-1000-8000-00805F9B34FB';
  5. const readDataMethod = {
  6. 255: {
  7. length: 6,
  8. read: readDataFF
  9. }
  10. };
  11. var readState = {
  12. value: null,
  13. index: 0
  14. };
  15. function acceptDevice(device) {
  16. return device.btid ? true : false;
  17. }
  18. function isDevice(device, data) {
  19. const advertisData = new Uint8Array(data.advertisData);
  20. const mac = device.btid
  21. .split('')
  22. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  23. .filter((p, i) => i % 2 == 0);
  24. if (advertisData.slice(2, 4).toString() == [136, 160].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
  25. return true;
  26. }
  27. return false;
  28. }
  29. function readDataFF(value, index, data) {
  30. switch (index) {
  31. case 0:
  32. // B0 - B19
  33. data.count = value[1];
  34. if (!data.voltageList) {
  35. data.voltageList = [];
  36. }
  37. for (var i = 0; i < 9; i++) {
  38. data.voltageList[i] = Math.round(value[(i + 1) * 2] + value[(i + 1) * 2 + 1] * 256) / 1000;
  39. }
  40. break;
  41. case 1:
  42. // B20 - B39
  43. if (!data.voltageList) {
  44. data.voltageList = [];
  45. }
  46. for (var i = 9; i < 19; i++) {
  47. data.voltageList[i] = Math.round(value[(i - 9) * 2] + value[(i - 9) * 2 + 1] * 256) / 1000;
  48. }
  49. break;
  50. case 2:
  51. // B40 - B59
  52. if (!data.voltageList) {
  53. data.voltageList = [];
  54. }
  55. for (var i = 19; i < 24; i++) {
  56. data.voltageList[i] = Math.round(value[(i - 19) * 2] + value[(i - 19) * 2 + 1] * 256) / 1000;
  57. }
  58. data.chargeState = value[10] + 1;
  59. data.current = Math.round(value[11] + value[12] * 256) / 10;
  60. data.voltage = Math.round(value[13] + value[14] * 256) / 100;
  61. data.quantity = value[15];
  62. data.chargeCurrentCountState = (value[18] >> 0) & 1;
  63. data.dischargeProtectCharacteristic = (value[18] >> 1) & 1;
  64. data.equilibrium = (value[19] >> 0) & 1;
  65. data.disconnectState = (value[19] >> 1) & 1;
  66. data.dischargeProtectState = (value[19] >> 2) & 1;
  67. data.chargeProtectState = (value[19] >> 3) & 1;
  68. data.highTempProtectState = (value[19] >> 4) & 3;
  69. data.lowTempProtectState = (value[19] >> 6) & 1;
  70. data.highVoltageDifferenceState = (value[19] >> 7) & 1;
  71. break;
  72. case 3:
  73. // B60 - B79
  74. data.alarmState = [];
  75. if (((value[0] >> 0) & 1) == 1) {
  76. data.alarmState.push('低容量报警');
  77. }
  78. if (((value[0] >> 1) & 1) == 1) {
  79. data.alarmState.push('功率板超温报警');
  80. }
  81. if (((value[0] >> 2) & 1) == 1) {
  82. data.alarmState.push('充电过流报警');
  83. }
  84. if (((value[0] >> 3) & 1) == 1) {
  85. data.alarmState.push('放电过流报警');
  86. }
  87. data.faultState = [];
  88. if (((value[0] >> 4) & 1) == 1) {
  89. data.alarmState.push('充电MOS管故障');
  90. }
  91. if (((value[0] >> 5) & 1) == 1) {
  92. data.alarmState.push('放电MOS管故障');
  93. }
  94. data.chargingMOS = (value[0] >> 6) & 1;
  95. data.dischargeMOS = (value[0] >> 7) & 1;
  96. data.cycle = value[1] + value[2] * 256;
  97. data.tempCount = value[10];
  98. data.powerBoardTemp = value[11] - 40;
  99. data.equilibriumBoardTemp = value[12] - 40;
  100. if (!data.tempList) {
  101. data.tempList = [];
  102. }
  103. for (var i = 0; i < 7; i++) {
  104. data.tempList[i] = value[13 + i] - 40;
  105. }
  106. break;
  107. case 4:
  108. // B80 - B99
  109. data.capacity = Math.round(value[0] + value[1] * 256) / 100;
  110. data.dischargeCapacity = Math.round(value[2] + value[3] * 256 + value[4] * 65536 + value[5] * 16777216) / 100;
  111. break;
  112. case 5:
  113. // B100 - B120
  114. if (!data.voltageList) {
  115. data.voltageList = [];
  116. }
  117. for (var i = 24; i < 32; i++) {
  118. data.voltageList[i] = Math.round(value[(i - 24) * 2] + value[(i - 24) * 2 + 1] * 256) / 1000;
  119. }
  120. break;
  121. }
  122. console.log(data);
  123. return data;
  124. }
  125. function readData(device, value, data) {
  126. var value = Array.from(new Uint8Array(value));
  127. console.log(value);
  128. if (readState.value == null) {
  129. if (readDataMethod[value[0]]) {
  130. readState = {
  131. value: value[0],
  132. index: 1
  133. };
  134. return readDataMethod[value[0]].read(value, 0, data);
  135. }
  136. } else {
  137. const readStateValue = readState.value;
  138. const readStateIndex = readState.index;
  139. readState.index++;
  140. if (readState.index >= readDataMethod[readState.value].length) {
  141. readState = {
  142. value: null,
  143. index: 0
  144. };
  145. }
  146. return readDataMethod[readStateValue].read(value, readStateIndex, data);
  147. }
  148. return false;
  149. }
  150. function stateUpdate(device, deviceId) {
  151. var data = [
  152. [178, 233, 209, 175, 202, 253, 190, 221],
  153. [187, 241, 200, 161, 197, 228, 214, 195]
  154. ];
  155. return data;
  156. }
  157. function turnOn(device, deviceId) {
  158. var data = [
  159. [195, 220, 194, 235, 209, 233, 214, 164, 163, 186, 2, 0, 0, 4, 1, 3, 237, 167],
  160. [1, 6, 0, 216, 0, 1, 200, 49]
  161. ];
  162. return data;
  163. }
  164. function turnOff(device, deviceId) {
  165. var data = [
  166. [195, 220, 194, 235, 209, 233, 214, 164, 163, 186, 2, 0, 0, 4, 1, 3, 237, 167],
  167. [1, 6, 0, 216, 0, 0, 9, 241]
  168. ];
  169. return data;
  170. }
  171. function bmsInfo(device, deviceId, info) {
  172. return false;
  173. }
  174. function bmsSet(device) {
  175. return false;
  176. }
  177. module.exports = {
  178. readServiceID: readServiceID,
  179. readID: readID,
  180. writeServiceID: writeServiceID,
  181. writeID: writeID,
  182. acceptDevice: acceptDevice,
  183. isDevice: isDevice,
  184. readData: readData,
  185. stateUpdate: stateUpdate,
  186. turnOn: turnOn,
  187. turnOff: turnOff,
  188. bmsInfo: bmsInfo,
  189. bmsSet: bmsSet
  190. };