AD3BTS.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 isBuzzer() {
  20. return true;
  21. }
  22. function haveBms() {
  23. console.log('是单蓝牙并且带bms');
  24. return false;
  25. }
  26. function isDevice(device, data) {
  27. const advertisData = new Uint8Array(data.advertisData);
  28. const mac = device.btid
  29. .split('')
  30. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  31. .filter((p, i) => i % 2 == 0);
  32. if (advertisData.slice(0, 4).toString() == [0x09, 0xff, 0x01, 0x02].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. function sendCommand(cmd, data = []) {
  38. data = [cmd, data.length].concat(data);
  39. return common.completArrayCRC(data, data.length);
  40. } // 鉴权
  41. function alterConnect(device, deviceId) {
  42. return [sendCommand(0x01, [0xab, 0xcd, 0xab, 0xcd])];
  43. }
  44. function readData(device, value, data) {
  45. var value = new Uint8Array(value);
  46. switch (value[0]) {
  47. case 0x48:
  48. if (value.length == 7) {
  49. data.voltage = value[2] * 0x100 + value[3] * 1;
  50. if (value[4] == 0x80) {
  51. data.temp = -value[5];
  52. } else {
  53. data.temp = value[5];
  54. }
  55. }
  56. break;
  57. case 0x42:
  58. break;
  59. }
  60. if (value[0] != 0x48 && value.length != 7) return;
  61. return data;
  62. }
  63. function sendQVCommand(data) {
  64. data = [0x06, data.length].concat(data);
  65. return common.completArrayCRC(data, data.length);
  66. }
  67. function sendDataCommand(data) {
  68. data = [0x02, data.length, (data.length & 0xff00) >> 8, data.length & 0x00ff, 0].concat(data);
  69. return common.completArrayCRC(data, data.length);
  70. }
  71. function mergeUint8Array(arr1, arr2) {
  72. let len1 = arr1 ? arr1.length : 0;
  73. let len2 = arr2.length;
  74. let arr = new Uint8Array(len1 + len2);
  75. for (let i = 0; i < len1; i++) {
  76. arr[i] = arr1[i];
  77. }
  78. for (let i = 0; i < len2; i++) {
  79. arr[len1 + i] = arr2[i];
  80. }
  81. return arr;
  82. }
  83. function voltageToEle(device, value) {
  84. var commandList = [];
  85. var list = hexToList(value);
  86. commandList.push(sendQVCommand(list));
  87. return commandList;
  88. }
  89. function stateUpdate(device, deviceId) {
  90. return [[0x40, 0x00, 0x40]];
  91. }
  92. function turnOn(device, deviceId) {
  93. return [sendCommand(0x30, [0xaa])];
  94. }
  95. function turnOff(device, deviceId) {
  96. return [sendCommand(0x30, [0xbb])];
  97. }
  98. function turnOnBuzzer(device, deviceId) {
  99. return [sendCommand(0x50, [0xaa])];
  100. }
  101. function turnOffBuzzer(device, deviceId) {
  102. return [sendCommand(0x50, [0xbb])];
  103. }
  104. function bmsDischargeMOS(value, device) {
  105. if (value == 1) {
  106. const bluetoothConfig = app.globalData.bluetoothConfig;
  107. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  108. var turnonCommand = bluetoothConfig[device.device_type].turnon;
  109. var commandList = [];
  110. var list = hexToList(turnonCommand);
  111. commandList.push(sendDataCommand(list));
  112. return commandList;
  113. }
  114. } else {
  115. const bluetoothConfig = app.globalData.bluetoothConfig;
  116. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  117. var turnoffCommand = bluetoothConfig[device.device_type].turnoff;
  118. var commandList = [];
  119. var list = hexToList(turnoffCommand);
  120. commandList.push(sendDataCommand(list));
  121. return commandList;
  122. }
  123. }
  124. }
  125. function bmsChargingMOS(value, device) {
  126. if (value == 1) {
  127. const bluetoothConfig = app.globalData.bluetoothConfig;
  128. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  129. var chargingMosOn = bluetoothConfig[device.device_type].chargingMosOn;
  130. var commandList = [];
  131. var list = hexToList(chargingMosOn);
  132. commandList.push(sendDataCommand(list));
  133. return commandList;
  134. }
  135. } else {
  136. const bluetoothConfig = app.globalData.bluetoothConfig;
  137. if (Object.keys(bluetoothConfig).includes(device.device_type)) {
  138. var chargingMosOff = bluetoothConfig[device.device_type].chargingMosOff;
  139. var commandList = [];
  140. var list = hexToList(chargingMosOff);
  141. commandList.push(sendDataCommand(list));
  142. return commandList;
  143. }
  144. }
  145. }
  146. function hexToList(str) {
  147. var val = [];
  148. for (var i = 0; i < str.length / 2; i++) {
  149. val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16));
  150. }
  151. return val;
  152. }
  153. module.exports = {
  154. readServiceID: readServiceID,
  155. readID: readID,
  156. writeServiceID: writeServiceID,
  157. writeID: writeID,
  158. MTU: MTU,
  159. acceptDevice: acceptDevice,
  160. isDevice: isDevice,
  161. alterConnect: alterConnect,
  162. readData: readData,
  163. stateUpdate: stateUpdate,
  164. turnOn: turnOn,
  165. turnOff: turnOff,
  166. turnOnBuzzer: turnOnBuzzer,
  167. turnOffBuzzer: turnOffBuzzer,
  168. isSingleBt: isSingleBt,
  169. haveBms: haveBms,
  170. bmsDischargeMOS: bmsDischargeMOS,
  171. bmsChargingMOS: bmsChargingMOS,
  172. voltageToEle: voltageToEle,
  173. isBuzzer: isBuzzer
  174. };