LSCabinet.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. const http = require('../../../http.js');
  2. const config = require('../../../config.js');
  3. const common = require('../../../config.js');
  4. const msg = require('./LSCabinetMsg.js');
  5. var functionList = [];
  6. const DF_CAB_INFO_LOAD = 10001; //机柜信息传输中
  7. const DF_CAB_INFO_DONE = 10000; //机柜信息传输完成
  8. function realData(value1) {
  9. //汇报数据
  10. var json = {};
  11. json['command'] = 0x00;
  12. json['response'] = 0;
  13. json['report'] = 0;
  14. console.log(value1);
  15. var value = dataBody(value1);
  16. if (value.length < 2) return;
  17. if (value[0] === 0x81 || value[0] === 0x82 || value[0] === 0x83) {
  18. //租电 换电 还电 响应数据
  19. json = exChangeHireReturnReponse(value, json); //换电 租电 还电汇报数据
  20. json = exChangeHireReturnReport(value, json);
  21. }
  22. if (value[0] === 0x84) {
  23. if (value[1] === 0x01) {
  24. //响应数据
  25. json['msg'] = '取消未确认成功';
  26. json['state'] = value[2];
  27. }
  28. } //电柜信息
  29. json = cabinetInfo(value, value1, json);
  30. return json;
  31. } //换电 租电 还电汇报数据
  32. function exChangeHireReturnReport(value, json) {
  33. if (value[0] === 0x81) {
  34. if (value[1] === 0x02) {
  35. json['msg'] = msg.reportMsg(value);
  36. json['report'] = 1; //汇报数据
  37. var toIndex = 4;
  38. json['state'] = value[2];
  39. json['emptyNo'] = value[3];
  40. json['emptyBatteryNo'] = asciiListToString(toIndex, value);
  41. json['emptyBatterySOC'] = value[toIndex + value[toIndex] + 1];
  42. toIndex = toIndex + value[toIndex] + 1 + 1;
  43. json['fullNo'] = value[toIndex];
  44. toIndex = toIndex + 1;
  45. json['fullBatteryNo'] = asciiListToString(toIndex, value);
  46. json['fullBatterySOC'] = value[toIndex + value[toIndex] + 1];
  47. toIndex = toIndex + value[toIndex] + 1 + 1;
  48. json['orderNo'] = asciiListToString(toIndex, value);
  49. toIndex = toIndex + value[toIndex] + 1;
  50. json['serialLen'] = value[toIndex];
  51. json['serialNo'] = asciiListToString(toIndex, value);
  52. }
  53. } else {
  54. if (value[1] === 0x02) {
  55. json['msg'] = msg.reportMsg(value);
  56. json['report'] = 1;
  57. var toIndex = 4;
  58. json['state'] = value[2];
  59. json['boxNo'] = value[3];
  60. json['batteryNo'] = asciiListToString(toIndex, value);
  61. json['batterySOC'] = value[toIndex + value[toIndex] + 1];
  62. toIndex = toIndex + value[toIndex] + 1 + 1;
  63. json['orderNo'] = asciiListToString(toIndex, value);
  64. toIndex = toIndex + value[toIndex] + 1;
  65. json['serialLen'] = value[toIndex];
  66. json['serialNo'] = asciiListToString(toIndex, value);
  67. }
  68. }
  69. console.log('蓝牙汇报操作的json数据');
  70. console.log(json);
  71. return json;
  72. } //换电 租电 还电响应数据
  73. function exChangeHireReturnReponse(value, json) {
  74. if (value[1] === 0x01) {
  75. //响应数据
  76. json['state'] = value[2];
  77. json['response'] = 1;
  78. json['msg'] = msg.reponseMsg(value);
  79. json['command'] = value[0];
  80. if (value[0] === 0x81) {
  81. json['batteryNo'] = asciiListToString(3, value);
  82. var toIndex = 3 + value[3] + 1;
  83. json['serialLen'] = value[toIndex];
  84. json['serialNo'] = asciiListToString(toIndex, value);
  85. } else {
  86. json['serialLen'] = value[3];
  87. json['serialNo'] = asciiListToString(3, value);
  88. }
  89. }
  90. console.log('蓝牙响应操作的json数据');
  91. console.log(json);
  92. return json;
  93. } //换电柜信息
  94. function cabinetInfo(value, value1, json) {
  95. if (value[0] === 0x86) {
  96. if (value[1] === 0x01) {
  97. functionList = [];
  98. }
  99. json['state'] = DF_CAB_INFO_LOAD; //信息传输中
  100. json['command'] = value[0];
  101. if (value.length > 3) {
  102. if (value[1] === 0x04) {
  103. if (functionList.length > 0 && functionList[0].length > 2 && functionList[0][2] === 0x86 && functionList[0][3] === 0x01) {
  104. json['commandList'] = functionList;
  105. json['state'] = DF_CAB_INFO_DONE; //信息传输结束
  106. functionList = [];
  107. }
  108. } else {
  109. functionList.push(value1);
  110. }
  111. }
  112. }
  113. return json;
  114. }
  115. function asciiListToString(index, value) {
  116. var len = value[index];
  117. index = index + 1;
  118. var str = '';
  119. if (len != 0) {
  120. for (var i = 0; len > i; i++) {
  121. str += String.fromCharCode(value[index + i]);
  122. }
  123. } else {
  124. str = '0';
  125. }
  126. return str;
  127. }
  128. function dataBody(hexList) {
  129. if (hexList.length > 4) {
  130. if (hexList[0] === 0x7a && hexList[hexList.length - 1] === 0x78) {
  131. return hexList.slice(2, -2);
  132. }
  133. } else {
  134. return hexList;
  135. }
  136. }
  137. function reportCabintHire(dev_id, sn, json) {
  138. const pData = {
  139. battery_ca: json.batteryNo,
  140. battery_sn: sn,
  141. box_sn: json.boxNo,
  142. dev_id: dev_id,
  143. order_sn: json.orderNo,
  144. status: json.state
  145. };
  146. http.postApi(config.API_CABINET_BLUETOOTH_HIRE, pData, function (response) {
  147. if (response.data.code === 200) {
  148. } else {
  149. common.simpleToast(response.data.msg);
  150. }
  151. });
  152. }
  153. function reportCabintExchange(dev_id, json) {
  154. const pData = {
  155. battery_sn: json.batteryNo,
  156. dev_id: dev_id,
  157. order_sn: json.orderNo,
  158. status: json.state,
  159. rtn_door_id: json.emptyNo,
  160. brw_door_id: json.fullNo,
  161. rtn_battery_ca: json.emptyBatteryNo,
  162. brw_battery_ca: json.fullBatteryNo
  163. };
  164. http.postApi(config.API_CABINET_BLUETOOTH_EXCHANGE_SUCCESS, pData, function (response) {
  165. if (response.data.code === 200) {
  166. } else {
  167. common.simpleToast(response.data.msg);
  168. }
  169. });
  170. }
  171. function reportCabintReturn(dev_id, sn, json) {
  172. const pData = {
  173. battery_sn: sn,
  174. rtn_battery_ca: json.batteryNo,
  175. dev_id: dev_id,
  176. order_sn: json.orderNo,
  177. box_sn: json.boxNo,
  178. status: json.state
  179. };
  180. http.postApi(config.API_CABINET_BLUETOOTH_RETURN_SUCCESS, pData, function (response) {
  181. if (response.data.code === 200) {
  182. } else {
  183. common.simpleToast(response.data.msg);
  184. }
  185. });
  186. }
  187. module.exports = {
  188. realData: realData,
  189. reportCabintExchange: reportCabintExchange,
  190. reportCabintHire: reportCabintHire,
  191. reportCabintReturn: reportCabintReturn
  192. };