LFBMS.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. 0xff: {
  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() == [0x88, 0xa0].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] * 0x100) / 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] * 0x100) / 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] * 0x100) / 1000;
  57. }
  58. data.chargeState = value[10] + 1;
  59. data.current = Math.round(value[11] + value[12] * 0x100) / 10;
  60. data.voltage = Math.round(value[13] + value[14] * 0x100) / 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) data.alarmState.push('低容量报警');
  76. if (((value[0] >> 1) & 1) == 1) data.alarmState.push('功率板超温报警');
  77. if (((value[0] >> 2) & 1) == 1) data.alarmState.push('充电过流报警');
  78. if (((value[0] >> 3) & 1) == 1) data.alarmState.push('放电过流报警');
  79. data.faultState = [];
  80. if (((value[0] >> 4) & 1) == 1) data.alarmState.push('充电MOS管故障');
  81. if (((value[0] >> 5) & 1) == 1) data.alarmState.push('放电MOS管故障');
  82. data.chargingMOS = (value[0] >> 6) & 1;
  83. data.dischargeMOS = (value[0] >> 7) & 1;
  84. data.cycle = value[1] + value[2] * 0x100;
  85. data.tempCount = value[10];
  86. data.powerBoardTemp = value[11] - 40;
  87. data.equilibriumBoardTemp = value[12] - 40;
  88. if (!data.tempList) {
  89. data.tempList = [];
  90. }
  91. for (var i = 0; i < 7; i++) {
  92. data.tempList[i] = value[13 + i] - 40;
  93. }
  94. break;
  95. case 4:
  96. // B80 - B99
  97. data.capacity = Math.round(value[0] + value[1] * 0x100) / 100;
  98. data.dischargeCapacity = Math.round(value[2] + value[3] * 0x100 + value[4] * 0x10000 + value[5] * 0x1000000) / 100;
  99. break;
  100. case 5:
  101. // B100 - B120
  102. if (!data.voltageList) {
  103. data.voltageList = [];
  104. }
  105. for (var i = 24; i < 32; i++) {
  106. data.voltageList[i] = Math.round(value[(i - 24) * 2] + value[(i - 24) * 2 + 1] * 0x100) / 1000;
  107. }
  108. break;
  109. }
  110. console.log(data);
  111. return data;
  112. }
  113. function readData(device, value, data) {
  114. var value = Array.from(new Uint8Array(value));
  115. if (readState.value == null) {
  116. if (readDataMethod[value[0]]) {
  117. readState = {
  118. value: value[0],
  119. index: 1
  120. };
  121. return readDataMethod[value[0]].read(value, 0, data);
  122. }
  123. } else {
  124. const readStateValue = readState.value;
  125. const readStateIndex = readState.index;
  126. readState.index++;
  127. if (readState.index >= readDataMethod[readState.value].length) {
  128. readState = {
  129. value: null,
  130. index: 0
  131. };
  132. }
  133. return readDataMethod[readStateValue].read(value, readStateIndex, data);
  134. }
  135. return false;
  136. }
  137. function stateUpdate(device, deviceId) {
  138. var data = [
  139. [0xb2, 0xe9, 0xd1, 0xaf, 0xca, 0xfd, 0xbe, 0xdd],
  140. [0xbb, 0xf1, 0xc8, 0xa1, 0xc5, 0xe4, 0xd6, 0xc3]
  141. ];
  142. return data;
  143. }
  144. function turnOn(device, deviceId) {
  145. var data = [
  146. [0xc3, 0xdc, 0xc2, 0xeb, 0xd1, 0xe9, 0xd6, 0xa4, 0xa3, 0xba, 0x02, 0x00, 0x00, 0x04, 0x01, 0x03, 0xed, 0xa7],
  147. [0x01, 0x06, 0x00, 0xd8, 0x00, 0x01, 0xc8, 0x31]
  148. ];
  149. return data;
  150. }
  151. function turnOff(device, deviceId) {
  152. var data = [
  153. [0xc3, 0xdc, 0xc2, 0xeb, 0xd1, 0xe9, 0xd6, 0xa4, 0xa3, 0xba, 0x02, 0x00, 0x00, 0x04, 0x01, 0x03, 0xed, 0xa7],
  154. [0x01, 0x06, 0x00, 0xd8, 0x00, 0x00, 0x09, 0xf1]
  155. ];
  156. return data;
  157. }
  158. function bmsInfo(device, deviceId, info) {
  159. return false;
  160. }
  161. function bmsSet(device, deviceId, name, vars) {
  162. return false;
  163. }
  164. module.exports = {
  165. readServiceID: readServiceID,
  166. readID: readID,
  167. writeServiceID: writeServiceID,
  168. writeID: writeID,
  169. acceptDevice: acceptDevice,
  170. isDevice: isDevice,
  171. readData: readData,
  172. stateUpdate: stateUpdate,
  173. turnOn: turnOn,
  174. turnOff: turnOff,
  175. bmsInfo: bmsInfo,
  176. bmsSet: bmsSet
  177. };