SouhuanAnalysis.php 36 KB

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