SouhuanAnalysis.php 36 KB

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