You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JLDeviceWechat+33Protocol.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. var TAG = "JLDeviceWechar+33Protocol";
  2. var onfire = require("./onfire");
  3. var request33 = require("./JLObuWechat33Pack.js");
  4. var recive33 = require("./JLObuWecha33UnPackt.js");
  5. var config = require("./JLZJConfig");
  6. var ble = require("./JLBleManagerTool.js");
  7. var dataTool = require("./dataTool");
  8. var sendTimeOut = 1e4;
  9. var connectTimeOut = 3e4;
  10. var wechat_arr = [];
  11. var dataArray = [];
  12. var isConnect = false;
  13. var frame_total = 1;
  14. var frame_count = 1;
  15. var fireFlag = "";
  16. var timeId;
  17. var receive_data = "";
  18. var receive_data_total_len = "";
  19. var wechat1 = false;
  20. var wechat2 = false;
  21. var timeId;
  22. var cmd_count = 0;
  23. var all_cmdArray = [];
  24. var flag = 0;
  25. var result_arr = [];
  26. var errMsg_arr = [];
  27. var current_device = null;
  28. export function scanDevice(nameFlag, timeOut, callBack) {
  29. timeId = setTimeout(res => {
  30. ble.scan_stopScanBleDevice(res => {
  31. callBack.call(this, {
  32. code: config.timeOutCode(),
  33. msg: "扫描超时",
  34. data: null
  35. })
  36. })
  37. }, timeOut);
  38. ble.scan_StartScanBleDevice(nameFlag, res => {
  39. clearTimeout(timeId);
  40. console.log("返回上层数据:" + JSON.stringify(res));
  41. callBack.call(this, res)
  42. })
  43. };
  44. export function stopScanDevice(callBack) {
  45. ble.scan_stopScanBleDevice(res => {
  46. callBack.call(this, res)
  47. })
  48. };
  49. export function connect(device, callBack) {
  50. console.log("==========connectDevice_Start==========:" + JSON.stringify(device));
  51. receive_data = "";
  52. if (current_device != null) {
  53. callBack.call(this, {
  54. code: 10004,
  55. msg: "已连接其他蓝牙设备",
  56. data: current_device
  57. });
  58. return
  59. }
  60. if (device == null || device == "undefined") {
  61. callBack.call(this, {
  62. code: 10003,
  63. msg: "参数错误",
  64. data: null
  65. });
  66. return
  67. }
  68. timeId = setTimeout(res => {
  69. disConnectDevice(res => {});
  70. callBack.call(this, {
  71. code: config.timeOutCode(),
  72. msg: "连接超时",
  73. data: null
  74. })
  75. }, connectTimeOut);
  76. ble.scan_stopScanBleDevice(res => {
  77. console.log(JSON.stringify(res)+"******************************************ldd");
  78. if (res.code == config.successCode()) {
  79. ble.connectDevice(device, res => {
  80. console.log(JSON.stringify(res)+"******************************************ldd");
  81. if (res.code == config.successCode()) {
  82. ble.findDeviceServices(res.data, res => {
  83. console.info(JSON.stringify(res));
  84. ble.setParams(res.data.id);
  85. if (res.code != config.successCode()) {
  86. disConnectDevice(res => {});
  87. console.log("返回上层数据:" + JSON.stringify(res));
  88. callBack.call(this, {
  89. code: config.bleErrorCode(),
  90. msg: "连接失败",
  91. data: null
  92. })
  93. }
  94. }, res1 => {
  95. dealReceiveData(res1.data.value, res => {
  96. clearTimeout(timeId);
  97. console.log("返回上层数据:" + JSON.stringify(res));
  98. ICCReset_Protocol(res => {
  99. if (res.code == 0) {
  100. current_device = device;
  101. callBack.call(this, {
  102. code: config.bleSuccessCode(),
  103. msg: "连接成功",
  104. data: null
  105. })
  106. } else {
  107. disConnectDevice(res => {});
  108. callBack.call(this, {
  109. code: config.bleErrorCode(),
  110. msg: "连接失败,卡复位失败",
  111. data: null
  112. })
  113. }
  114. })
  115. })
  116. })
  117. } else {
  118. console.log("返回上层数据:" + JSON.stringify(res));
  119. callBack.call(this, res)
  120. }
  121. })
  122. } else {
  123. console.log("返回上层数据:" + JSON.stringify(res));
  124. callBack.call(this, res)
  125. }
  126. })
  127. };
  128. export function onDisconnet(callBack) {
  129. ble.onDeviceConnectStateChange(res => {
  130. isConnect = false;
  131. dataArray = [];
  132. frame_count = 0;
  133. frame_total = 0;
  134. onfire.clear();
  135. receive_data = "";
  136. clearTimeout(timeId);
  137. current_device = null;
  138. callBack.call(this, res)
  139. })
  140. };
  141. export function disConnectDevice(callBack) {
  142. receive_data = "";
  143. ble.disConnectDevice(res => {
  144. isConnect = false;
  145. dataArray = [];
  146. frame_count = 0;
  147. frame_total = 0;
  148. onfire.clear();
  149. clearTimeout(timeId);
  150. current_device = null;
  151. callBack.call(this, res)
  152. })
  153. };
  154. export function ESAMReset_Protocol(callBack) {
  155. console.log("==========ESAMReset_Protocol_Start==========");
  156. receive_data = "";
  157. timeId = setTimeout(res => {
  158. receive_data = "";
  159. errMsg_arr = [];
  160. callBack.call(this, {
  161. code: config.timeOutCode(),
  162. msg: "ICC复位超时",
  163. data: null
  164. })
  165. }, sendTimeOut);
  166. var cmd_arr = request33.ESAMResetReq();
  167. ble.sendMessageToDevice(cmd_arr, res => {
  168. fireFlag = config.ResetFlag();
  169. recive33.receiveReset(res => {
  170. clearTimeout(timeId);
  171. console.log("返回上层数据:" + JSON.stringify(res));
  172. callBack.call(this, res)
  173. })
  174. })
  175. };
  176. export function ESAMChannel_CmdArray(cmdArray, callBack) {
  177. if (current_device == null) {
  178. callBack.call(this, {
  179. code: config.errorCode(),
  180. msg: "未连接设备",
  181. data: null
  182. });
  183. return
  184. }
  185. cmd_count = cmdArray.length;
  186. all_cmdArray = cmdArray;
  187. ESAMTransfro(callBack)
  188. };
  189. export function ICCReset_Protocol(callBack) {
  190. console.log("==========ICCReset_Protocol_Start==========");
  191. receive_data = "";
  192. timeId = setTimeout(res => {
  193. receive_data = "";
  194. callBack.call(this, {
  195. code: config.timeOutCode(),
  196. msg: "ICC复位超时",
  197. data: null
  198. })
  199. }, sendTimeOut);
  200. var cmd_arr = request33.ICCResetReq();
  201. ble.sendMessageToDevice(cmd_arr, res => {
  202. fireFlag = config.ResetFlag();
  203. recive33.receiveReset(res => {
  204. clearTimeout(timeId);
  205. console.log("返回上层数据:" + JSON.stringify(res));
  206. callBack.call(this, res)
  207. })
  208. })
  209. };
  210. export function ICCChannel_CmdArray(cmdArray, callBack) {
  211. if (current_device == null) {
  212. callBack.call(this, {
  213. code: config.errorCode(),
  214. msg: "未连接设备",
  215. data: null
  216. });
  217. return
  218. }
  219. cmd_count = cmdArray.length;
  220. all_cmdArray = cmdArray;
  221. ICCTransfro(callBack)
  222. };
  223. function ESAMChannel_Protocol(cmd, callBack) {
  224. console.log("==========ESAMChannel_Protocol_Start==========:" + cmd);
  225. var cmd_arr = request33.ESAMChannelTransmission("00", [cmd]);
  226. timeId = setTimeout(res => {
  227. receive_data = "";
  228. errMsg_arr = [];
  229. callBack.call(this, {
  230. code: config.timeOutCode(),
  231. msg: "ESAM透传超时",
  232. data: null
  233. })
  234. }, sendTimeOut);
  235. ble.sendMessageToDevice(cmd_arr, res => {
  236. fireFlag = config.ChannelFlag();
  237. recive33.receiveChannel(res => {
  238. clearTimeout(timeId);
  239. console.log("返回上层数据:" + JSON.stringify(res));
  240. callBack.call(this, res)
  241. })
  242. })
  243. }
  244. function ESAMTransfro(callBack) {
  245. var cmd = all_cmdArray[flag];
  246. var that = this;
  247. ESAMChannel_Protocol(cmd, res => {
  248. if (res.code == 0) {
  249. flag++;
  250. if (errMsg_arr.length == 0) {
  251. result_arr.push(res.data)
  252. }
  253. if (flag < cmd_count) {
  254. ESAMTransfro(callBack)
  255. } else {
  256. cmd_count = 0;
  257. all_cmdArray = [];
  258. flag = 0;
  259. if (errMsg_arr.length == 0) {
  260. res.data = result_arr
  261. }
  262. result_arr = [];
  263. callBack.call(this, res)
  264. }
  265. } else {
  266. if (errMsg_arr.length > 0) {
  267. res.msg = errMsg_arr[flag]
  268. }
  269. errMsg_arr = [];
  270. cmd_count = 0;
  271. all_cmdArray = [];
  272. flag = 0;
  273. callBack.call(this, res)
  274. }
  275. })
  276. }
  277. function ICCChannel_Protocol(cmd, callBack) {
  278. console.log("==========ICCChannel_Protocol_Start==========:" + cmd);
  279. var cmd_arr = request33.ICCChannelTransmission("00", [cmd]);
  280. timeId = setTimeout(res => {
  281. receive_data = "";
  282. callBack.call(this, {
  283. code: config.timeOutCode(),
  284. msg: "ICC透传超时",
  285. data: null
  286. })
  287. }, sendTimeOut);
  288. ble.sendMessageToDevice(cmd_arr, res => {
  289. fireFlag = config.ChannelFlag();
  290. recive33.receiveChannel(res => {
  291. clearTimeout(timeId);
  292. console.log("返回上层数据:" + JSON.stringify(res));
  293. callBack.call(this, res)
  294. })
  295. })
  296. }
  297. function ICCTransfro(callBack) {
  298. var cmd = all_cmdArray[flag];
  299. var that = this;
  300. ICCChannel_Protocol(cmd, res => {
  301. if (res.code == 0) {
  302. flag++;
  303. if (errMsg_arr.length == 0) {
  304. result_arr.push(res.data)
  305. }
  306. if (flag < cmd_count) {
  307. ICCTransfro(callBack)
  308. } else {
  309. cmd_count = 0;
  310. all_cmdArray = [];
  311. flag = 0;
  312. if (errMsg_arr.length == 0) {
  313. res.data = result_arr
  314. }
  315. result_arr = [];
  316. callBack.call(this, res)
  317. }
  318. } else {
  319. if (errMsg_arr.length > 0) {
  320. res.msg = errMsg_arr[flag]
  321. }
  322. cmd_count = 0;
  323. all_cmdArray = [];
  324. result_arr = [];
  325. flag = 0;
  326. callBack.call(this, res)
  327. }
  328. })
  329. }
  330. function dealReceiveData(data, callBack) {
  331. if (receive_data.length > 0) {
  332. if (receive_data.indexOf("FE01") == -1) {
  333. receive_data = ""
  334. }
  335. }
  336. receive_data += data;
  337. var blg = parseInt(receive_data.substring(4, 8), 16) * 2 == receive_data.length ? true : false;
  338. if (blg) {
  339. var flag_protocol = receive_data.substr(24, 2);
  340. var flag_wechat = receive_data.substr(8, 4);
  341. if (flag_protocol == "33") {
  342. console.log(TAG + "----fireFlag:" + fireFlag);
  343. var pack_count = receive_data.substr(28, 2);
  344. wechat_arr.push(receive_data);
  345. receive_data = "";
  346. if (pack_count == "80" || pack_count == "00") {
  347. onfire.fire(fireFlag, wechat_arr);
  348. wechat_arr = []
  349. }
  350. } else if (flag_wechat == "2711" || flag_wechat == "2713") {
  351. if (flag_wechat == "2711") {
  352. var mac = receive_data.substring(40, 44);
  353. var cmd_arr = request33.wechatInit1(mac);
  354. receive_data = "";
  355. ble.sendMessageToDevice(cmd_arr, res => {})
  356. } else if (flag_wechat == "2713") {
  357. var cmd_arr = request33.wechatInit2(mac);
  358. receive_data = "";
  359. ble.sendMessageToDevice(cmd_arr, res => {
  360. receive_data = "";
  361. callBack.call(this, {
  362. code: config.bleSuccessCode(),
  363. msg: "连接成功",
  364. data: null
  365. })
  366. })
  367. } else {
  368. callBack.call(this, {
  369. code: config.bleErrorCode,
  370. msg: "非法连接",
  371. data: null
  372. })
  373. }
  374. } else {
  375. callBack.call(this, {
  376. code: config.bleErrorCode,
  377. msg: "非法连接",
  378. data: null
  379. })
  380. }
  381. }
  382. }