ZX16D.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. //\公司文件\项目\GPS服务\16D、信驰达低功耗蓝牙(BLE)模块及协议 v5.10u_(RS02A1-B)191230.pdf
  2. const common = require('../common.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. var readPart = '';
  8. const FMBMS = require('./FMBMS.js');
  9. var curTime = 0;
  10. function acceptDevice(device) {
  11. return device.btid ? true : false;
  12. }
  13. function isDevice(device, data) {
  14. const advertisData = new Uint8Array(data.advertisData);
  15. const mac = device.btid
  16. .split('')
  17. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  18. .filter((p, i) => i % 2 == 0);
  19. if (advertisData.slice(0, 4).toString() == [0x09, 0xff, 0x01, 0x02].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. function readData(device, value, data) {
  25. var value = new Uint8Array(value);
  26. switch (value[0]) {
  27. case 0x44:
  28. if (value.length == 5) {
  29. data.voltage = (((value[2] << 8) | value[3]) / 100).toFixed(2);
  30. }
  31. break;
  32. case 0x42:
  33. break;
  34. }
  35. var endTime = new Date().getTime();
  36. if (endTime - curTime < 15000) return;
  37. if (value[0] != 0x44 && value.length != 5) return;
  38. if (endTime - curTime > 15000) {
  39. if (value[0] == 0x44) {
  40. curTime = new Date().getTime();
  41. }
  42. return data;
  43. }
  44. } // function readData(device, value, data) {
  45. // value = Array.from(new Int8Array(value)).map(p => String.fromCharCode(p)).join('')
  46. // if (value[value.length - 1] == ')') {
  47. // value = readPart.concat(value)
  48. // readPart = ''
  49. // } else if (value[0] == '(') {
  50. // readPart = value
  51. // } else {
  52. // readPart = readPart.concat(value)
  53. // }
  54. // if (value[0] == '(' && value[value.length-1] == ')') {
  55. // value = value.slice(1, value.length-1)
  56. // if (value.slice(0, 12) == device.mac_id) {
  57. // console.log(value)
  58. // value = value.slice(12, value.length)
  59. // switch (value.slice(0, 4)) {
  60. // case 'BP00':
  61. // return readDataBP00(value.slice(4, value.length), data)
  62. // }
  63. // }
  64. // }
  65. // return false
  66. // }
  67. // function stateUpdate(device, deviceId) {
  68. // var data = '(' + device.mac_id + 'AU20)'
  69. // console.log(data)
  70. // return [data.split('').map(p => p.charCodeAt(0))]
  71. // }
  72. // function turnOn(device, deviceId) {
  73. // var data = '(' + device.mac_id + 'AV011)'
  74. // console.log(data)
  75. // console.log([data.split('').map(p => p.charCodeAt(0))])
  76. // return [data.split('').map(p => p.charCodeAt(0))]
  77. // }
  78. // function turnOff(device, deviceId) {
  79. // var data = '(' + device.mac_id + 'AV010)'
  80. // console.log(data)
  81. // return [data.split('').map(p => p.charCodeAt(0))]
  82. // }
  83. function stateUpdate(device, deviceId) {
  84. console.log('读电压');
  85. curTime = 0;
  86. return [sendCommand(0x04)];
  87. }
  88. function turnOn(device, deviceId) {
  89. console.log('通电');
  90. return [sendCommand(0x03, [0xaa])];
  91. }
  92. function turnOff(device, deviceId) {
  93. console.log('断电');
  94. return [sendCommand(0x03, [0xbb])];
  95. } // 鉴权
  96. function alterConnect(device, deviceId) {
  97. return [sendCommand(0x01, [0xab, 0xcd, 0xab, 0xcd])];
  98. }
  99. function sendCommand(cmd, data = []) {
  100. data = [cmd, data.length].concat(data);
  101. return common.completArrayCRC(data, data.length);
  102. }
  103. function bmsInfo(device, deviceId, info) {
  104. return {
  105. state: {
  106. soc: 0,
  107. voltageAll: info.voltage ? info.voltage : 0,
  108. tempCount: info.temp ? 1 : 0,
  109. temp: info.temp ? [info.temp] : []
  110. },
  111. state_v2: [
  112. {
  113. class: '',
  114. class_eng: '',
  115. icon: 0,
  116. vars: [
  117. {
  118. label: '总电压',
  119. label_eng: 'voltageAll',
  120. name: '',
  121. type: 'readonly',
  122. value: info.voltage || info.voltage == 0 ? info.voltage + 'V' : '未知'
  123. },
  124. {
  125. label: '充电保护',
  126. label_eng: '',
  127. name: '',
  128. type: 'readonly',
  129. value: info.chargeProtectState == 0 ? '正常' : info.chargeProtectState == 1 ? '异常' : '未知'
  130. },
  131. {
  132. label: '放电保护',
  133. label_eng: '',
  134. name: '',
  135. type: 'readonly',
  136. value: info.dischargeProtectState == 0 ? '正常' : info.dischargeProtectState == 1 ? '异常' : '未知'
  137. },
  138. {
  139. label: '电池超温',
  140. label_eng: '',
  141. name: '',
  142. type: 'readonly',
  143. value: info.highTempProtectState == 0 ? '正常' : info.highTempProtectState == 1 ? '异常' : '未知'
  144. },
  145. {
  146. label: '电池温度',
  147. label_eng: '',
  148. name: '',
  149. type: 'readonly',
  150. value: info.temp || info.temp == 0 ? info.temp + '°C' : '未知'
  151. },
  152. {
  153. label: '电池低温保护',
  154. label_eng: '',
  155. name: '',
  156. type: 'readonly',
  157. value: info.lowTempProtectState == 0 ? '正常' : info.lowTempProtectState == 1 ? '异常' : '未知'
  158. },
  159. {
  160. label: '充放电状态',
  161. label_eng: '',
  162. name: '',
  163. type: 'readonly',
  164. value: info.chargeState == 1 ? '充电' : info.chargeState == 2 ? '放电' : '静止'
  165. },
  166. {
  167. label: '循环次数',
  168. label_eng: '',
  169. name: '',
  170. type: 'readonly',
  171. value: info.cycle || info.cycle == 0 ? info.cycle + '次' : '未知'
  172. }
  173. ]
  174. }
  175. ],
  176. btsets: [
  177. {
  178. class: '高低温保护阀值',
  179. name: 'AU10',
  180. vars: [
  181. {
  182. label: '低温保护阀值',
  183. name: 'AU100',
  184. type: 'number',
  185. value: ''
  186. },
  187. {
  188. label: '高温保护阀值',
  189. name: 'AU101',
  190. type: 'number',
  191. value: ''
  192. }
  193. ]
  194. },
  195. {
  196. class: '充放电保护电压',
  197. name: 'AU12',
  198. vars: [
  199. {
  200. label: '充电保护电压',
  201. name: 'AU120',
  202. type: 'number',
  203. value: ''
  204. },
  205. {
  206. label: '放电保护电压',
  207. name: 'AU121',
  208. type: 'number',
  209. value: ''
  210. }
  211. ]
  212. },
  213. {
  214. class: '充放电保护开关',
  215. name: 'AU13',
  216. vars: [
  217. {
  218. label: '充电保护',
  219. name: 'AU130',
  220. type: 'radio',
  221. option: [
  222. {
  223. label: '关闭',
  224. value: '0'
  225. },
  226. {
  227. label: '开启',
  228. value: '1'
  229. }
  230. ],
  231. value: ''
  232. },
  233. {
  234. label: '放电保护',
  235. name: 'AU131',
  236. type: 'radio',
  237. option: [
  238. {
  239. label: '关闭',
  240. value: '0'
  241. },
  242. {
  243. label: '开启',
  244. value: '1'
  245. }
  246. ],
  247. value: ''
  248. }
  249. ]
  250. },
  251. {
  252. class: '充放电保护恢复电压',
  253. name: 'AU14',
  254. vars: [
  255. {
  256. label: '充电保护恢复电压',
  257. name: 'AU140',
  258. type: 'number',
  259. value: ''
  260. },
  261. {
  262. label: '放电保护恢复电压',
  263. name: 'AU141',
  264. type: 'number',
  265. value: ''
  266. }
  267. ]
  268. },
  269. {
  270. class: '高低温保护恢复值',
  271. name: 'AU15',
  272. vars: [
  273. {
  274. label: '低温保护恢复值',
  275. name: 'AU150',
  276. type: 'number',
  277. value: ''
  278. },
  279. {
  280. label: '高温保护恢复值',
  281. name: 'AU151',
  282. type: 'number',
  283. value: ''
  284. }
  285. ]
  286. },
  287. {
  288. class: '电池循环次数',
  289. name: 'AU16',
  290. vars: [
  291. {
  292. label: '循环次数',
  293. name: 'AU160',
  294. type: 'number',
  295. value: ''
  296. }
  297. ]
  298. },
  299. {
  300. class: 'BMS自动保护开关',
  301. name: 'AU17',
  302. vars: [
  303. {
  304. label: '自动保护',
  305. name: 'AU170',
  306. type: 'radio',
  307. option: [
  308. {
  309. label: '关闭',
  310. value: '0'
  311. },
  312. {
  313. label: '开启',
  314. value: '1'
  315. }
  316. ],
  317. value: ''
  318. }
  319. ]
  320. }
  321. ]
  322. };
  323. }
  324. function bmsSet(device, deviceId, name, vars) {
  325. var data = '';
  326. switch (name) {
  327. case 'AU10':
  328. if (
  329. vars['AU100'] &&
  330. !isNaN(parseInt(vars['AU100'])) &&
  331. Math.abs(parseInt(vars['AU100'])) < 1000 &&
  332. vars['AU101'] &&
  333. !isNaN(parseInt(vars['AU101'])) &&
  334. Math.abs(parseInt(vars['AU101'])) < 1000
  335. ) {
  336. data =
  337. (parseInt(vars['AU100']) < 0 ? '-' : '+') +
  338. ('000' + Math.abs(parseInt(vars['AU100']))).substr(String(Math.abs(parseInt(vars['AU100']))).length) +
  339. (parseInt(vars['AU101']) < 0 ? '-' : '+') +
  340. ('000' + Math.abs(parseInt(vars['AU101']))).substr(String(Math.abs(parseInt(vars['AU101']))).length);
  341. }
  342. break;
  343. case 'AU12':
  344. if (
  345. vars['AU120'] &&
  346. !isNaN(parseFloat(vars['AU120'])) &&
  347. parseFloat(vars['AU120']) >= 0 &&
  348. parseInt(parseFloat(vars['AU120']) * 100) < 0x10000 &&
  349. vars['AU121'] &&
  350. !isNaN(parseFloat(vars['AU121'])) &&
  351. parseFloat(vars['AU121']) >= 0 &&
  352. parseInt(parseFloat(vars['AU121']) * 100) < 0x10000
  353. ) {
  354. data = (
  355. ('0000' + parseInt(parseFloat(vars['AU120']) * 100).toString(16)).substr(parseInt(parseFloat(vars['AU120']) * 100).toString(16).length) +
  356. ('0000' + parseInt(parseFloat(vars['AU121']) * 100).toString(16)).substr(parseInt(parseFloat(vars['AU121']) * 100).toString(16).length)
  357. ).toUpperCase();
  358. }
  359. break;
  360. case 'AU13':
  361. if (
  362. vars['AU130'] &&
  363. (parseInt(vars['AU130']) == 0 || parseInt(vars['AU130']) == 1) &&
  364. vars['AU131'] &&
  365. (parseInt(vars['AU131']) == 0 || parseInt(vars['AU131']) == 1)
  366. ) {
  367. data = parseInt(vars['AU130']).toString() + parseInt(vars['AU131']).toString();
  368. }
  369. break;
  370. case 'AU14':
  371. if (
  372. vars['AU140'] &&
  373. !isNaN(parseFloat(vars['AU140'])) &&
  374. parseFloat(vars['AU140']) >= 0 &&
  375. parseInt(parseFloat(vars['AU140']) * 100) < 0x10000 &&
  376. vars['AU141'] &&
  377. !isNaN(parseFloat(vars['AU141'])) &&
  378. parseFloat(vars['AU141']) >= 0 &&
  379. parseInt(parseFloat(vars['AU141']) * 100) < 0x10000
  380. ) {
  381. data = (
  382. ('0000' + parseInt(parseFloat(vars['AU140']) * 100).toString(16)).substr(parseInt(parseFloat(vars['AU140']) * 100).toString(16).length) +
  383. ('0000' + parseInt(parseFloat(vars['AU141']) * 100).toString(16)).substr(parseInt(parseFloat(vars['AU141']) * 100).toString(16).length)
  384. ).toUpperCase();
  385. }
  386. break;
  387. case 'AU15':
  388. if (
  389. vars['AU150'] &&
  390. !isNaN(parseInt(vars['AU150'])) &&
  391. Math.abs(parseInt(vars['AU150'])) < 1000 &&
  392. vars['AU151'] &&
  393. !isNaN(parseInt(vars['AU151'])) &&
  394. Math.abs(parseInt(vars['AU151'])) < 1000
  395. ) {
  396. data =
  397. (parseInt(vars['AU150']) < 0 ? '-' : '+') +
  398. ('000' + Math.abs(parseInt(vars['AU150']))).substr(String(Math.abs(parseInt(vars['AU150']))).length) +
  399. (parseInt(vars['AU151']) < 0 ? '-' : '+') +
  400. ('000' + Math.abs(parseInt(vars['AU151']))).substr(String(Math.abs(parseInt(vars['AU151']))).length);
  401. }
  402. break;
  403. case 'AU16':
  404. if (vars['AU160'] && !isNaN(parseInt(vars['AU160'])) && parseInt(vars['AU160']) >= 0 && parseInt(vars['AU160']) < 0x10000) {
  405. data = ('0000' + parseInt(vars['AU160']).toString(16)).substr(parseInt(vars['AU160']).toString(16).length).toUpperCase();
  406. }
  407. break;
  408. case 'AU17':
  409. if (vars['AU170'] && (parseInt(vars['AU170']) == 0 || parseInt(vars['AU170']) == 1)) {
  410. data = parseInt(vars['AU170']).toString() + parseInt(vars['AU170']).toString();
  411. }
  412. break;
  413. }
  414. if (data) {
  415. data = '(' + device.mac_id + name + data + ')';
  416. console.log(data);
  417. return [data.split('').map((p) => p.charCodeAt(0))];
  418. }
  419. return false;
  420. }
  421. module.exports = {
  422. readServiceID: readServiceID,
  423. readID: readID,
  424. writeServiceID: writeServiceID,
  425. writeID: writeID,
  426. acceptDevice: acceptDevice,
  427. isDevice: isDevice,
  428. readData: readData,
  429. alterConnect: alterConnect,
  430. stateUpdate: stateUpdate,
  431. turnOn: turnOn,
  432. turnOff: turnOff,
  433. bmsInfo: bmsInfo,
  434. bmsSet: bmsSet
  435. };