SouhuanAnalysis.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. namespace app\common\server;
  3. use app\common\model\FacilityModel;
  4. use app\common\model\ShalarmlistModel;
  5. use app\common\model\ShinfoModel;
  6. use app\common\model\ShreceivequeueModel;
  7. use app\common\model\ShreceiveredocdeModel;
  8. use app\common\model\ShsendlistModel;
  9. use app\common\model\ShsendqueueModel;
  10. use think\facade\Log;
  11. /**
  12. * 手环字符串解析
  13. * 解析命令 发送命令
  14. *
  15. * @author wj
  16. * @date 2023-08-11
  17. */
  18. class SouhuanAnalysis
  19. {
  20. /**
  21. * 校验长度
  22. *
  23. * @return void
  24. * @author wj
  25. * @date 2023-08-11
  26. */
  27. public function checklen($len, $str)
  28. {
  29. $strlen = mb_strlen($str);
  30. if ($len != $strlen) {
  31. Log::write("长度校验失败: len:" . $len . " str:" . $str, 'shouhuan');
  32. return false;
  33. }
  34. return true;
  35. }
  36. /**
  37. * 获取信息 整个信息解析
  38. * 并验证是否可解析 无解析命令配置 不报错
  39. *
  40. * @return void
  41. * @author wj
  42. * @date 2023-08-11
  43. */
  44. public function getline($str, $field = null, $checkconfig = true)
  45. {
  46. //[厂商*设备 ID*内容长度*内容]
  47. $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
  48. preg_match($preg, $str, $match);
  49. if (empty($match)) {
  50. Log::write("解析失败:" . $str, 'shouhuan');
  51. throw new \Exception('解析失败:' . $str);
  52. }
  53. unset($match[0]);
  54. $data = array_values($match);
  55. $sandom_string = $data[0]; //厂商 随机字符串
  56. $device_id_code = $data[1]; //设置id编码
  57. $content_len = $data[2]; //内容长度
  58. $content_str = $data[3]; //内容字符串
  59. $content_len = $this->analysislen($content_len);
  60. $result = $this->checklen($content_len, $content_str);
  61. if (empty($result)) {
  62. $errmsg = '内容位数错误';
  63. Log::write($errmsg . ":" . $str, 'shouhuan');
  64. throw new \Exception($errmsg);
  65. }
  66. //验证设备状态
  67. $fid = $this->getfacilityid($device_id_code);
  68. if ($fid) {
  69. //改设备接收信息时间
  70. $time = date('Y-m-d H:i:s');
  71. $shupdateData = ['receive_time' => $time];
  72. $shupdateWhere = ['facility_id' => $fid];
  73. $m_sh = new ShinfoModel();
  74. $row = $m_sh->updateinfo($shupdateWhere, $shupdateData);
  75. if (empty($row)) {
  76. $errmsg = '设备:' . $device_id_code . " receive_time 修改失败";
  77. Log::write($errmsg, 'shouhuan');
  78. }
  79. }
  80. $data = [
  81. 'device_id_code' => $device_id_code,
  82. 'content_len' => $content_len,
  83. 'content_str' => $content_str,
  84. ];
  85. $contentarr = $this->getcommandcontent($content_str);
  86. $command = $contentarr['command'];
  87. $data['command'] = $command;
  88. $data['content_arr'] = $contentarr['content'];
  89. if ($checkconfig) {
  90. $config = $this->getreceivecommandinfo($command);
  91. $data['config'] = $config;
  92. if (!$config) {
  93. $errmsg = '命令无解析配置 command:' . $command;
  94. Log::write($errmsg, 'shouhuan');
  95. return false;
  96. }
  97. }
  98. if (isset($field)) {
  99. return $data[$field];
  100. }
  101. return $data;
  102. }
  103. /**
  104. * 解析UD命令
  105. *
  106. * @param [type] $str
  107. * @return void
  108. * @author wj
  109. * @date 2023-08-23
  110. */
  111. public function getmacforUD($str, $device_id_code)
  112. {
  113. $data = explode(",", $str);
  114. $is_gps = $data[3];
  115. $is_success = false;
  116. $accesstype = -1;
  117. $apikey = "67019de565fb4aa2a0e101b70ae3fd91";
  118. if ('V' == $is_gps) {
  119. $jzcount = $data['17'];
  120. $jzlist = [];
  121. $mcc = $data['19'];
  122. $mnc = $data['20'];
  123. $jzindex = 21;
  124. for ($i = 0; $i < $jzcount; $i++) {
  125. $item = [
  126. 'area_code' => $data[$jzindex],
  127. 'jz_code' => $data[$jzindex + 1],
  128. 'signal_strength' => $data[$jzindex + 2],
  129. ];
  130. $jzindex += 3;
  131. $jzlist[] = $item;
  132. }
  133. $wificount = $data[$jzindex];
  134. $wifiindex = $jzindex + 1;
  135. $wifilist = [];
  136. for ($i = 0; $i < $wificount; $i++) {
  137. $item = [
  138. 'name' => $data[$wifiindex],
  139. 'mac' => $data[$wifiindex + 1],
  140. 'signal_strength' => $data[$wifiindex + 2],
  141. ];
  142. $wifiindex += 3;
  143. $wifilist[] = $item;
  144. }
  145. $bts = "";
  146. $nearbts_arr = [];
  147. $nearbts = "";
  148. $macs_arr = [];
  149. $macs = "";
  150. foreach ($jzlist as $key => $value) {
  151. if ($key === 0) {
  152. $bts = $mcc . "," . $mnc . "," . $value['area_code'] . "," . $value['jz_code'] . "," . $value['signal_strength'];
  153. } else {
  154. $nearbts_arr[] = $mcc . "," . $mnc . "," . $value['area_code'] . "," . $value['jz_code'] . "," . $value['signal_strength'];
  155. }
  156. }
  157. $nearbts = join("|", $nearbts_arr);
  158. foreach ($wifilist as $key => $value) {
  159. $macs_arr[] = $value['mac'] . "," . $value['signal_strength'];
  160. }
  161. $macs = join("|", $macs_arr);
  162. $accesstype = 1;
  163. $url = "http://apilocate.amap.com/position?macs=" . $macs . "&bts=" . $bts . "&nearbts=" . $nearbts . "&network=GSM&cdma=0&accesstype=" . $accesstype . "&output=json&key=" . $apikey;
  164. $result = curl_request($url);
  165. if ('10000' === $result['infocode']) {
  166. $resultdata = $result['result'];
  167. $locations = explode(",", $resultdata['location']);
  168. $use_gps_long = $locations[0];
  169. $use_gps_lat = $locations[1];
  170. $address_desc = $resultdata['desc'];
  171. $accesstype = 2;
  172. $is_success = true;
  173. } else {
  174. Log::write($url, 'shouhuan');
  175. Log::write($result, 'shouhuan');
  176. $accesstype = -2;
  177. }
  178. }
  179. if (!$is_success) {
  180. $use_gps_long = $data[6];
  181. $use_gps_lat = $data[4];
  182. $gps_str = $use_gps_long . "," . $use_gps_lat;
  183. $url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=" . $gps_str . "&coordsys=gps&output=JSON&key=" . $apikey;
  184. $result = curl_request($url);
  185. if ('10000' == $result['infocode']) {
  186. $locations = $result['locations'];
  187. $url = "https://restapi.amap.com/v3/geocode/regeo?location=" . $locations . "&key=" . $apikey;
  188. $result = curl_request($url);
  189. if ('10000' == $result['infocode']) {
  190. $address_desc = $result['regeocode']['formatted_address'];
  191. $is_success = true;
  192. $accesstype = 0; //未接入智能硬件定位
  193. } else {
  194. Log::write($url, 'shouhuan');
  195. Log::write($result, 'shouhuan');
  196. }
  197. } else {
  198. Log::write($url, 'shouhuan');
  199. Log::write($result, 'shouhuan');
  200. }
  201. }
  202. if (!$is_success) {
  203. $accesstype = -2;
  204. $use_gps_long = $data[6];
  205. $use_gps_lat = $data[4];
  206. $address_desc = "";
  207. }
  208. $data = [
  209. 'is_gps' => $is_gps == "A" ? 1 : 0,
  210. 'address_desc' => $address_desc,
  211. 'accesstype' => $accesstype,
  212. 'use_gps_long' => $use_gps_long,
  213. 'use_gps_lat' => $use_gps_lat,
  214. ];
  215. return $data;
  216. }
  217. /**
  218. * 内容解析
  219. *
  220. * @param [type] $content_str
  221. * @return void
  222. * @author wj
  223. * @date 2023-08-19
  224. */
  225. public function getcommandcontent($content_str)
  226. {
  227. $content = explode(',', $content_str);
  228. $command = $content[0];
  229. unset($content[0]);
  230. $data = [
  231. 'command' => $command,
  232. 'content' => $content,
  233. ];
  234. return $data;
  235. }
  236. /**
  237. * 解析终端发送命令长度
  238. *
  239. * @param [type] $len
  240. * @return void
  241. * @author wj
  242. * @date 2023-08-18
  243. */
  244. public function analysislen($len)
  245. {
  246. $len = base_convert($len, 16, 10);
  247. return $len;
  248. }
  249. /**
  250. * 设置可解析数据 终端发送信息解析
  251. *
  252. * @param [type] $command
  253. * @return void
  254. * @author wj
  255. * @date 2023-08-18
  256. * analysis_digit 解析位数
  257. * content 解析对应字段
  258. * kind 类型 tsend 平台发送 tback平台回复
  259. * need_back 是否需回复 1是 0否 没设置为0
  260. * command 未设置则和键值相同
  261. */
  262. public function getreceivecommandinfo($command)
  263. {
  264. $command = strtoupper($command);
  265. $data = [
  266. 'UD' => [
  267. 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
  268. 'content' => [
  269. 'date',
  270. 'time',
  271. 'gps_lat',
  272. 'gps_long',
  273. 'speed',
  274. 'direction',
  275. 'poster',
  276. 'electric_quantity',
  277. 'step_number',
  278. 'roll_number',
  279. 'terminal_status',
  280. ],
  281. 'kind' => 'tsend',
  282. 'need_back' => 0,
  283. ],
  284. 'AL' => [
  285. 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
  286. 'content' => [
  287. 'date',
  288. 'time',
  289. 'gps_lat',
  290. 'gps_long',
  291. 'speed',
  292. 'direction',
  293. 'poster',
  294. 'electric_quantity',
  295. 'step_number',
  296. 'roll_number',
  297. 'terminal_status',
  298. ],
  299. 'kind' => 'tsend',
  300. 'need_back' => 0,
  301. ],
  302. 'KA' => [
  303. 'analysis_digit' => [1, 2, 3, 4],
  304. 'content' => [
  305. 'date',
  306. 'step_number',
  307. 'roll_number',
  308. 'electric_quantity',
  309. ],
  310. 'kind' => 'tsend',
  311. 'need_back' => 1,
  312. ],
  313. 'LK' => [
  314. 'analysis_digit' => null,
  315. 'content' => null,
  316. 'kind' => 'tsend',
  317. 'need_back' => 1,
  318. ],
  319. 'BPHRT' => [
  320. 'analysis_digit' => [1, 2, 3],
  321. 'content' => [
  322. 'blood_height_pressure',
  323. 'blood_low_pressure',
  324. 'heart_rate',
  325. ],
  326. 'kind' => 'tsend',
  327. 'need_back' => 1,
  328. 'command' => 'bphrt',
  329. ],
  330. 'TEMP' => [
  331. 'analysis_digit' => [1],
  332. 'content' => [
  333. 'temp',
  334. ],
  335. 'kind' => 'tsend',
  336. 'need_back' => 1,
  337. 'command' => 'temp',
  338. ],
  339. 'HEART' => [
  340. 'analysis_digit' => [1],
  341. 'content' => [
  342. 'heart_rate',
  343. ],
  344. 'kind' => 'tsend',
  345. 'need_back' => 1,
  346. 'command' => 'heart',
  347. ],
  348. 'BLOOD' => [
  349. 'analysis_digit' => [1, 2],
  350. 'content' => [
  351. 'blood_height_pressure',
  352. 'blood_low_pressure',
  353. ],
  354. 'kind' => 'tsend',
  355. 'need_back' => 1,
  356. 'command' => 'blood',
  357. ],
  358. 'OXYGEN' => [
  359. 'analysis_digit' => [1],
  360. 'content' => [
  361. 'oxygen',
  362. ],
  363. 'kind' => 'tsend',
  364. 'need_back' => 1,
  365. 'command' => 'oxygen',
  366. ],
  367. //终端回复
  368. //ZONE UPLOAD CR SLEEPTIME FALLDOWN FACTORY SOS VERNO LOWBAT
  369. //POWEROFF LSSET PEDO PHL REMIND SILENCETIME2 BOOTOFF REMOVE
  370. 'ZONE' => [
  371. 'analysis_digit' => null,
  372. 'content' => null,
  373. 'kind' => 'tback',
  374. ],
  375. 'UPLOAD' => [
  376. 'analysis_digit' => null,
  377. 'content' => null,
  378. 'kind' => 'tback',
  379. ],
  380. 'HRTSART' => [
  381. 'analysis_digit' => null,
  382. 'content' => null,
  383. 'kind' => 'tback',
  384. ],
  385. 'WDSTART' => [
  386. 'analysis_digit' => null,
  387. 'content' => null,
  388. 'kind' => 'tback',
  389. ],
  390. 'BLDSTART' => [
  391. 'analysis_digit' => null,
  392. 'content' => null,
  393. 'kind' => 'tback',
  394. ],
  395. 'OXSTART' => [
  396. 'analysis_digit' => null,
  397. 'content' => null,
  398. 'kind' => 'tback',
  399. ],
  400. 'CR' => [
  401. 'analysis_digit' => null,
  402. 'content' => null,
  403. 'kind' => 'tback',
  404. ],
  405. 'SLEEPTIME' => [
  406. 'analysis_digit' => null,
  407. 'content' => null,
  408. 'kind' => 'tback',
  409. ],
  410. 'FALLDOWN' => [
  411. 'analysis_digit' => null,
  412. 'content' => null,
  413. 'kind' => 'tback',
  414. ],
  415. 'SOS' => [
  416. 'analysis_digit' => null,
  417. 'content' => null,
  418. 'kind' => 'tback',
  419. ],
  420. 'VERNO' => [
  421. 'analysis_digit' => [1],
  422. 'content' => ['version'],
  423. 'kind' => 'tback',
  424. ],
  425. 'LOWBAT' => [
  426. 'analysis_digit' => null,
  427. 'content' => null,
  428. 'kind' => 'tback',
  429. ],
  430. 'POWEROFF' => [
  431. 'analysis_digit' => null,
  432. 'content' => null,
  433. 'kind' => 'tback',
  434. ],
  435. 'LSSET' => [
  436. 'analysis_digit' => null,
  437. 'content' => null,
  438. 'kind' => 'tback',
  439. ],
  440. 'PEDO' => [
  441. 'analysis_digit' => null,
  442. 'content' => null,
  443. 'kind' => 'tback',
  444. ],
  445. 'PHL' => [
  446. 'analysis_digit' => null,
  447. 'content' => null,
  448. 'kind' => 'tback',
  449. ],
  450. 'REMIND' => [
  451. 'analysis_digit' => null,
  452. 'content' => null,
  453. 'kind' => 'tback',
  454. ],
  455. 'SILENCETIME2' => [
  456. 'analysis_digit' => null,
  457. 'content' => null,
  458. 'kind' => 'tback',
  459. ],
  460. 'BOOTOFF' => [
  461. 'analysis_digit' => null,
  462. 'content' => null,
  463. 'kind' => 'tback',
  464. ],
  465. 'REMOVE' => [
  466. 'analysis_digit' => null,
  467. 'content' => null,
  468. 'kind' => 'tback',
  469. ],
  470. 'CALL' => [
  471. 'analysis_digit' => null,
  472. 'content' => null,
  473. 'kind' => 'tback',
  474. ],
  475. 'KEYBRD' => [
  476. 'analysis_digit' => null,
  477. 'content' => null,
  478. 'kind' => 'tback',
  479. ],
  480. ];
  481. if (!isset($data[$command])) {
  482. return false;
  483. }
  484. return $data[$command];
  485. }
  486. /**
  487. * 获取数据值
  488. *
  489. * @param [type] $data
  490. * @param [type] $command
  491. * @return void
  492. * @author wj
  493. * @date 2023-08-14
  494. */
  495. public function getdata($config, $content_arr)
  496. {
  497. $analysis_digit = $config['analysis_digit'];
  498. $config_content = $config['content'];
  499. $data = [];
  500. if (is_array($analysis_digit)) {
  501. foreach ($analysis_digit as $key => $value) {
  502. if (isset($content_arr[$value])) {
  503. if (isset($config_content[$key])) {
  504. $item_key = $config_content[$key];
  505. $item_value = $content_arr[$value];
  506. $data[$item_key] = $item_value;
  507. }
  508. }
  509. }
  510. }
  511. if (isset($data['time'])) {
  512. $time = $data['time'];
  513. $data['time'] = $this->gettime($time);
  514. }
  515. if (isset($data['date'])) {
  516. $date = $data['date'];
  517. $data['date'] = $this->getdate($date);
  518. }
  519. if (isset($data['terminal_status'])) {
  520. $terminalstatus = $data['terminal_status'];
  521. $terminal_status_arr = $this->getterminalstatus($terminalstatus);
  522. //unset($data['terminal_status']);
  523. $data['status_type'] = $terminal_status_arr['status_type'];
  524. $data['alarm_type'] = $terminal_status_arr['alarm_type'];
  525. }
  526. return $data;
  527. }
  528. /**
  529. * 时间解析
  530. *
  531. * @param [type] $time
  532. * @return void
  533. * @author wj
  534. * @date 2023-08-19
  535. */
  536. public function gettime($time)
  537. {
  538. $hour = substr($time, 0, 2);
  539. $minute = substr($time, 2, 2);
  540. $second = substr($time, 4, 2);
  541. $time = $hour . ':' . $minute . ':' . $second;
  542. return $time;
  543. }
  544. /**
  545. * 日期解析
  546. *
  547. * @return void
  548. * @author wj
  549. * @date 2023-08-15
  550. */
  551. public function getdate($date)
  552. {
  553. $day = substr($date, 0, 2);
  554. $month = substr($date, 2, 2);
  555. $year = substr($date, 4, 2);
  556. $year1 = date('Y');
  557. $year2 = substr($year1, 0, 2);
  558. $year = $year2 . $year;
  559. if ($year != $year1) {
  560. $day = date('Y-m-d');
  561. } else {
  562. $day = $year . '-' . $month . '-' . $day;
  563. }
  564. return $day;
  565. }
  566. /**
  567. * 解析终端状态
  568. *
  569. * @param [type] $terminal_status
  570. * @return void
  571. * @author wj
  572. * @date 2023-08-19
  573. */
  574. public function getterminalstatus($terminal_status)
  575. {
  576. $terminal_status = str_split($terminal_status);
  577. foreach ($terminal_status as $key => $value) {
  578. $value = base_convert($value, 16, 2);
  579. $value = str_pad($value, 4, "0", STR_PAD_LEFT);
  580. $terminal_status[$key] = $value;
  581. }
  582. $terminal_status_str = implode("", $terminal_status);
  583. $mid = mb_strlen($terminal_status_str) / 2;
  584. $alarm = substr($terminal_status_str, 0, $mid);
  585. $first_num = strpos($alarm, '1');
  586. $end_num = strrpos($alarm, '1');
  587. if ($first_num != $end_num) {
  588. Log::write("状态解析错误:报警:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  589. }
  590. if ($first_num) {
  591. //16:SOS报警;17:低电报警;20:手环拆除报警;21:跌倒报警;22:心率异常报警
  592. $first_num = $mid - $first_num + ($mid - 1);
  593. $alarm_type = $first_num;
  594. }
  595. $status = substr($terminal_status_str, $mid);
  596. $first_num = strpos($status, '1');
  597. $end_num = strrpos($status, '1');
  598. if ($first_num != $end_num) {
  599. Log::write("状态解析错误:状态:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  600. }
  601. if ($first_num) {
  602. $first_num = $mid - 1 - $first_num;
  603. //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
  604. $status_type = $first_num + 1;
  605. }
  606. $data = [
  607. 'alarm_type' => isset($alarm_type) ? $alarm_type : false,
  608. 'status_type' => isset($status_type) ? $status_type : false,
  609. ];
  610. return $data;
  611. }
  612. // /**
  613. // * 解析报警
  614. // *
  615. // * @param [type] $first_num
  616. // * @return void
  617. // * @author wj
  618. // * @date 2023-08-19
  619. // */
  620. // public function getalarm($first_num)
  621. // {
  622. // $data = [
  623. // 16 => 'SOS报警',
  624. // 17 => '低电报警',
  625. // 18 => '出围栏报警',
  626. // 19 => '进围栏报警',
  627. // 20 => '手环拆除报警',
  628. // 21 => '跌倒报警',
  629. // 22 => '心率异常报警',
  630. // ];
  631. // if (isset($data[$first_num])) {
  632. // return $data[$first_num];
  633. // }
  634. // return false;
  635. // }
  636. // /**
  637. // * 解析状态
  638. // *
  639. // * @param [type] $first_num
  640. // * @return void
  641. // * @author wj
  642. // * @date 2023-08-19
  643. // */
  644. // public function getstatus($first_num)
  645. // {
  646. // $data = [
  647. // 0 => '低电状态',
  648. // 1 => '出围栏状态',
  649. // 2 => '进围栏状态',
  650. // 3 => '手环戴上取下状态',
  651. // 4 => '手表运行静止状态',
  652. // ];
  653. // if (isset($data[$first_num])) {
  654. // return $data[$first_num];
  655. // }
  656. // return false;
  657. // }
  658. // ̄へ ̄-----------内容解析结束---------------- ̄へ ̄
  659. // ̄へ ̄-----------业务处理开始---------------- ̄へ ̄
  660. /**
  661. * 获取设别表id
  662. *
  663. * @return void
  664. * @author wj
  665. * @date 2023-08-11
  666. */
  667. public function getfacilityid($device_id_code)
  668. {
  669. $m_f = new FacilityModel();
  670. $where = ['code' => $device_id_code, 'status' => 1];
  671. $finfo = $m_f->getInfo($where, ['id']);
  672. if (empty($finfo)) {
  673. throw new \Exception("设备信息错误:" . $device_id_code);
  674. return false;
  675. }
  676. $id = $finfo['id'];
  677. return $id;
  678. }
  679. public function geshinfoid($device_id_code)
  680. {
  681. $m_si = new ShinfoModel();
  682. $where = ['device_id_code' => $device_id_code];
  683. $siinfo = $m_si->getInfo($where, ['id']);
  684. if (empty($siinfo)) {
  685. throw new \Exception("设备信息错误:" . $device_id_code);
  686. return false;
  687. }
  688. $id = $siinfo['id'];
  689. return $id;
  690. }
  691. /**
  692. * 保存接收信息队列
  693. *
  694. * @param [type] $data
  695. * @return void
  696. * @author wj
  697. * @date 2023-08-14
  698. */
  699. public function savereceivequeue($data, $device_id_code)
  700. {
  701. $m_srq = new ShreceivequeueModel();
  702. $facility_id = $this->getfacilityid($device_id_code);
  703. $data = [
  704. 'msg' => $data,
  705. 'createtime' => date('Y-m-d H:i:s'),
  706. 'device_id_code' => $device_id_code,
  707. 'facility_id' => $facility_id,
  708. ];
  709. $id = $m_srq->insertData($data);
  710. if (empty($id)) {
  711. return false;
  712. }
  713. return true;
  714. }
  715. /**
  716. * 获取接收信息队列列表
  717. *
  718. * @param [type] $page
  719. * @param [type] $size
  720. * @return void
  721. * @author wj
  722. * @date 2023-08-14
  723. */
  724. public function getreceivequeuelist($page = null, $size = null)
  725. {
  726. $m_srq = new ShreceivequeueModel();
  727. $where = ['status' => 0];
  728. $size = empty($size) ? 20 : $size;
  729. if (empty($page)) {
  730. $count = $m_srq->getList($where, 'count');
  731. $totalpage = ceil($count / $size);
  732. $data = ['totalpage' => $totalpage, 'size' => $size];
  733. return $data;
  734. } else {
  735. $list = $m_srq->getList($where, '*', $page, $size, 'id asc');
  736. if (empty($list)) {
  737. //Log::write("手环接收信息队列-无数据", 'shouhuan');
  738. return false;
  739. }
  740. $list = $list->toArray();
  741. foreach ($list as $key => $value) {
  742. $id = $value['id'];
  743. if (1 != $value['status']) {
  744. $row = $m_srq->updateinfo(['id' => $id], ['status' => 1]);
  745. if (1 != $row) {
  746. $msg = "Shreceivequeue id:" . $id . " 修改失败";
  747. Log::write($msg, 'shouhuan');
  748. unset($list[$key]);
  749. }
  750. }
  751. }
  752. return $list;
  753. }
  754. }
  755. /**
  756. * 获取发送信息队列列表
  757. *
  758. * @param [type] $page
  759. * @param [type] $size
  760. * @return void
  761. * @author wj
  762. * @date 2023-08-14
  763. */
  764. public function getsendqueuelist($device_id_code, $page = null, $size = null)
  765. {
  766. $m_ssq = new ShsendqueueModel();
  767. $where = ['status' => 0, 'device_id_code' => $device_id_code];
  768. $size = empty($size) ? 20 : $size;
  769. if (empty($page)) {
  770. $count = $m_ssq->getList($where, 'count');
  771. $totalpage = ceil($count / $size);
  772. $data = ['totalpage' => $totalpage, 'size' => $size];
  773. return $data;
  774. } else {
  775. $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc');
  776. if (empty($list)) {
  777. Log::write("手环发送信息队列-无数据", 'shouhuan');
  778. return false;
  779. }
  780. $list = $list->toArray();
  781. foreach ($list as $key => $value) {
  782. $id = $value['id'];
  783. if (1 != $value['status']) {
  784. $row = $m_ssq->updateinfo(['id' => $id], ['status' => 1]);
  785. if (1 != $row) {
  786. $msg = "Shsendqueue id:" . $id . " 修改失败";
  787. Log::write($msg, 'shouhuan');
  788. unset($list[$key]);
  789. }
  790. }
  791. }
  792. return $list;
  793. }
  794. }
  795. /**
  796. * 保存手环信息
  797. *
  798. * @return void
  799. * @author wj
  800. * @date 2023-08-14
  801. */
  802. public function saveshinfo($facility_id, $device_id_code, $data, $queue_data)
  803. {
  804. $original_str = $queue_data['msg'];
  805. $command = $data['command'];
  806. $content_arr = $data['content_arr'];
  807. $config = $data['config'];
  808. $data = $this->getdata($config, $content_arr);
  809. $m_sri = new ShinfoModel();
  810. $m_srir = new ShreceiveredocdeModel();
  811. $m_sa = new ShalarmlistModel();
  812. if ('AL' == $command) {
  813. //手环警报信息
  814. $data['alarm_from_type'] = 1;
  815. $data['device_id_code'] = $device_id_code;
  816. $sshinfo_id = $this->geshinfoid($device_id_code);
  817. $data['shinfo_id'] = $sshinfo_id;
  818. $data['facility_id'] = $facility_id;
  819. $data['createtime'] = date('Y-m-d H:i:s');
  820. $said = $m_sa->insertData($data);
  821. if (empty($said)) {
  822. $msg = "报警信息添加失败:" . $original_str;
  823. Log::write($msg, 'shouhuan');
  824. throw new \Exception($msg);
  825. }
  826. } else {
  827. //手环基础信息修改
  828. $sriData = $data;
  829. if ($command == "UD") {
  830. $uddata = $this->getmacforUD($original_str, $device_id_code);
  831. if ($uddata) {
  832. $sriData = array_merge($sriData, $uddata);
  833. }
  834. }
  835. $sriwhere = [
  836. 'facility_id' => $facility_id,
  837. 'device_id_code' => $device_id_code,
  838. ];
  839. $sriinfo = $m_sri->getInfo($sriwhere);
  840. if (empty($sriinfo)) {
  841. $sriData['device_id_code'] = $device_id_code;
  842. $sriData['facility_id'] = $facility_id;
  843. $sriData['online_statis'] = 1;
  844. $sriinsertData = $sriData;
  845. $sriinsertData['createtime'] = date('Y-m-d H:i:s');
  846. $sriinsertData['updatetime'] = date('Y-m-d H:i:s');
  847. $sriinsertData = $m_sri->formatinfo($sriinsertData);
  848. $sriid = $m_sri->insertData($sriinsertData);
  849. if (empty($sriid)) {
  850. throw new \Exception("设备信息添加失败:" . $device_id_code);
  851. }
  852. } else {
  853. $sriinfo = $sriinfo->toArray();
  854. $sriid = $sriinfo['id'];
  855. $sriupdateData = (array) $sriData;
  856. foreach ($sriupdateData as $key => $value) {
  857. if (in_array($key, array_keys($sriinfo))) {
  858. if ($sriinfo[$key] == $value) {
  859. unset($sriupdateData[$key]);
  860. }
  861. } else {
  862. unset($sriupdateData[$key]);
  863. }
  864. }
  865. if (count($sriupdateData) > 0) {
  866. $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
  867. $sriupdateData = $m_sri->formatinfo($sriupdateData);
  868. $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
  869. if (empty($row)) {
  870. $errmsg = "设备信息修改失败:" . $device_id_code;
  871. Log::write($errmsg);
  872. Log::write($sriupdateData, 'shouhuan');
  873. throw new \Exception($errmsg);
  874. }
  875. }
  876. }
  877. }
  878. //接收信息历史添加
  879. $sririnsertData['facility_id'] = $facility_id;
  880. $sririnsertData['device_id_code'] = $device_id_code;
  881. $sririnsertData['original_str'] = $original_str;
  882. $sririnsertData['content'] = json_encode($data);
  883. if (isset($data['date'])) {
  884. $sririnsertData['date'] = $data['date'];
  885. }
  886. if (isset($data['time'])) {
  887. $sririnsertData['time'] = $data['time'];
  888. }
  889. $sririnsertData['command'] = $command;
  890. $sririnsertData['createtime'] = date('Y-m-d H:i:s');
  891. $sririnsertData['receive_time'] = $queue_data['createtime'];
  892. if ('tback' == $config['kind']) {
  893. $is_back_msg = true;
  894. $sririnsertData['is_back_msg'] = 0;
  895. } else {
  896. $is_back_msg = false;
  897. $sririnsertData['is_back_msg'] = 1;
  898. }
  899. $sririd = $m_srir->insertData($sririnsertData);
  900. if (empty($sririd)) {
  901. throw new \Exception("设备历史信息添加失败:" . $device_id_code);
  902. }
  903. if ($is_back_msg) {
  904. //处理回复信息
  905. $m_ssl = new ShsendlistModel();
  906. $where = [
  907. 'is_success' => 0,
  908. 'is_send' => 1,
  909. 'command' => $command,
  910. ];
  911. $sslinfo = $m_ssl->getInfo($where);
  912. if ($sslinfo) {
  913. //发送成功状态回写
  914. $sslid = $sslinfo['id'];
  915. $sslupdateData = ['is_success' => 1];
  916. $m_ssl->updateinfo(['id' => $sslid], $sslupdateData);
  917. $srirupdateData = ['send_list_id' => $sslid];
  918. $m_srir->updateinfo(['id' => $sririd], $srirupdateData);
  919. }
  920. }
  921. return true;
  922. }
  923. //发送完处理
  924. /**
  925. * 发送成功 删除发送数据
  926. *
  927. * @param [type] $item
  928. * @return void
  929. * @author wj
  930. * @date 2023-08-21
  931. */
  932. public function sendmsgsuccess($item)
  933. {
  934. $m_ssq = new ShsendqueueModel();
  935. $m_ssl = new ShsendlistModel();
  936. $m_sal = new ShalarmlistModel();
  937. $ssqid = $item['id'];
  938. if (0 === $item['msg_type']) {
  939. //普通信息
  940. if ($item['send_list_id']) {
  941. $list_id = $item['send_list_id'];
  942. $sslwhere = ['id' => $list_id, 'is_send' => 0];
  943. $sslinfo = $m_ssl->getInfo($sslwhere);
  944. if ($sslinfo) {
  945. $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  946. }
  947. }
  948. }
  949. if (1 === $item['msg_type']) {
  950. //报警信息
  951. if ($item['send_list_id']) {
  952. $list_id = $item['send_list_id'];
  953. $salwhere = ['id' => $list_id, 'is_send' => 0];
  954. $salinfo = $m_sal->getInfo($salwhere);
  955. if ($salinfo) {
  956. $row = $m_sal->updateinfo(['id' => $salinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  957. }
  958. }
  959. }
  960. $ssqwhere = ['id' => $ssqid];
  961. $ssqinfo = $m_ssq->getInfo($ssqwhere);
  962. if ($ssqinfo) {
  963. $m_ssq->deleteinfobyid($ssqid);
  964. }
  965. }
  966. /**
  967. * 解析成功 删除接收数据
  968. *
  969. * @param [type] $item
  970. * @return void
  971. * @author wj
  972. * @date 2023-08-21
  973. */
  974. public function receivemsgsuccess($item)
  975. {
  976. $m_srq = new ShreceivequeueModel();
  977. $srqid = $item['id'];
  978. $srqwhere = ['id' => $srqid, 'status' => 1];
  979. $srqinfo = $m_srq->getInfo($srqwhere);
  980. if ($srqinfo) {
  981. $m_srq->deleteinfobyid($srqid);
  982. }
  983. }
  984. /**
  985. * 初始化手环接收信息解析
  986. *
  987. * @return void
  988. * @author wj
  989. * @date 2023-08-15
  990. */
  991. public function initanalysisShoneReceiveMsg()
  992. {
  993. $m_srq = new ShreceivequeueModel();
  994. $where = ['status' => 1];
  995. $m_srq->updateinfo($where, ['status' => 0]);
  996. }
  997. /**
  998. * 测试使用
  999. *
  1000. * @param [type] $data
  1001. * @return void
  1002. * @author wj
  1003. * @date 2023-08-21
  1004. */
  1005. public function showfortest($data)
  1006. {
  1007. $strdata = $this->getline($data, null, false);
  1008. if (!is_array($strdata)) {
  1009. Log::write($data, 'shouhuan');
  1010. return false;
  1011. }
  1012. $command = strtoupper($strdata['command']);
  1013. $showcommands = [
  1014. 'SOS', 'CR', 'UPLOAD', 'SLEEPTIME', 'ZONE', 'FALLDOWN',
  1015. 'APPLOCK', 'FACTORY', 'VERNO', 'LOWBAT', 'UD', 'POWEROFF', 'MESSAGE',
  1016. 'WALKTIME', 'PEDO', 'PHB', 'PHL', 'REMIND', 'SILENCETIME2', 'BOOTOFF',
  1017. 'REMOVE', 'INFO', 'AL', 'LSSET', 'BTWARNSET',
  1018. ];
  1019. if (in_array($command, $showcommands)) {
  1020. var_dump(date('Y-m-d H:i:s'));
  1021. var_dump($data);
  1022. }
  1023. }
  1024. /**
  1025. * 设置设备上线
  1026. *
  1027. * @return void
  1028. * @author wj
  1029. * @date 2023-08-21
  1030. */
  1031. public function setonlineinfo($device_id_code)
  1032. {
  1033. $m_si = new ShinfoModel();
  1034. $where = ['device_id_code' => $device_id_code, 'online_statis' => 0];
  1035. $finfo = $m_si->getInfo($where, ['id', 'online_statis']);
  1036. if ($finfo) {
  1037. $facility_id = $finfo['id'];
  1038. $m_si->setonlineinfobyid($facility_id);
  1039. }
  1040. }
  1041. /**
  1042. * 设置设备下线
  1043. *
  1044. * @return void
  1045. * @author wj
  1046. * @date 2023-08-21
  1047. */
  1048. public function setofflineinfo()
  1049. {
  1050. $m_si = new ShinfoModel();
  1051. $list = $m_si->getcheckupdatetimelist();
  1052. $list = (array) $list;
  1053. if (!empty($list)) {
  1054. $facility_ids = array_column($list, 'id');
  1055. $m_si->setofflineinfobyids($facility_ids);
  1056. }
  1057. }
  1058. //工具方法
  1059. /**
  1060. * unicode 解码
  1061. *
  1062. * @param [type] $str
  1063. * @return void
  1064. * @author wj
  1065. * @date 2023-08-21
  1066. */
  1067. public function unicodeToChn($str)
  1068. {
  1069. $arr = str_split($str);
  1070. $items = [];
  1071. $item = "";
  1072. foreach ($arr as $key => $value) {
  1073. $index = $key + 1;
  1074. if (0 != $index % 4) {
  1075. $item .= $value;
  1076. } else {
  1077. $item = "";
  1078. $items[] = $item;
  1079. }
  1080. }
  1081. foreach ($items as $key => $value) {
  1082. $char = hex2bin($value);
  1083. $char = iconv('UCS-2BE', 'UTF-8', $char);
  1084. $items[$key] = $char;
  1085. }
  1086. $info = implode("", $items);
  1087. return $info;
  1088. }
  1089. /**
  1090. * 拆包
  1091. *
  1092. * @return void
  1093. * @author wj
  1094. * @date 2023-08-15
  1095. */
  1096. public function unpack($data)
  1097. {
  1098. $arr = explode("][", $data);
  1099. foreach ($arr as $key => $value) {
  1100. if ($value[0] !== "[") {
  1101. $arr[$key] = '[' . $value;
  1102. }
  1103. if ($value[mb_strlen($value) - 1] !== "]") {
  1104. $arr[$key] .= "]";
  1105. }
  1106. }
  1107. return $arr;
  1108. }
  1109. }