SouhuanAnalysis.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. ];
  476. if (!isset($data[$command])) {
  477. return false;
  478. }
  479. return $data[$command];
  480. }
  481. /**
  482. * 获取数据值
  483. *
  484. * @param [type] $data
  485. * @param [type] $command
  486. * @return void
  487. * @author wj
  488. * @date 2023-08-14
  489. */
  490. public function getdata($config, $content_arr)
  491. {
  492. $analysis_digit = $config['analysis_digit'];
  493. $config_content = $config['content'];
  494. $data = [];
  495. if (is_array($analysis_digit)) {
  496. foreach ($analysis_digit as $key => $value) {
  497. if (isset($content_arr[$value])) {
  498. if (isset($config_content[$key])) {
  499. $item_key = $config_content[$key];
  500. $item_value = $content_arr[$value];
  501. $data[$item_key] = $item_value;
  502. }
  503. }
  504. }
  505. }
  506. if (isset($data['time'])) {
  507. $time = $data['time'];
  508. $data['time'] = $this->gettime($time);
  509. }
  510. if (isset($data['date'])) {
  511. $date = $data['date'];
  512. $data['date'] = $this->getdate($date);
  513. }
  514. if (isset($data['terminal_status'])) {
  515. $terminalstatus = $data['terminal_status'];
  516. $terminal_status_arr = $this->getterminalstatus($terminalstatus);
  517. //unset($data['terminal_status']);
  518. $data['status_type'] = $terminal_status_arr['status_type'];
  519. $data['alarm_type'] = $terminal_status_arr['alarm_type'];
  520. }
  521. return $data;
  522. }
  523. /**
  524. * 时间解析
  525. *
  526. * @param [type] $time
  527. * @return void
  528. * @author wj
  529. * @date 2023-08-19
  530. */
  531. public function gettime($time)
  532. {
  533. $hour = substr($time, 0, 2);
  534. $minute = substr($time, 2, 2);
  535. $second = substr($time, 4, 2);
  536. $time = $hour . ':' . $minute . ':' . $second;
  537. return $time;
  538. }
  539. /**
  540. * 日期解析
  541. *
  542. * @return void
  543. * @author wj
  544. * @date 2023-08-15
  545. */
  546. public function getdate($date)
  547. {
  548. $day = substr($date, 0, 2);
  549. $month = substr($date, 2, 2);
  550. $year = substr($date, 4, 2);
  551. $year1 = date('Y');
  552. $year2 = substr($year1, 0, 2);
  553. $year = $year2 . $year;
  554. if ($year != $year1) {
  555. $day = date('Y-m-d');
  556. } else {
  557. $day = $year . '-' . $month . '-' . $day;
  558. }
  559. return $day;
  560. }
  561. /**
  562. * 解析终端状态
  563. *
  564. * @param [type] $terminal_status
  565. * @return void
  566. * @author wj
  567. * @date 2023-08-19
  568. */
  569. public function getterminalstatus($terminal_status)
  570. {
  571. $terminal_status = str_split($terminal_status);
  572. foreach ($terminal_status as $key => $value) {
  573. $value = base_convert($value, 16, 2);
  574. $value = str_pad($value, 4, "0", STR_PAD_LEFT);
  575. $terminal_status[$key] = $value;
  576. }
  577. $terminal_status_str = implode("", $terminal_status);
  578. $mid = mb_strlen($terminal_status_str) / 2;
  579. $alarm = substr($terminal_status_str, 0, $mid);
  580. $first_num = strpos($alarm, '1');
  581. $end_num = strrpos($alarm, '1');
  582. if ($first_num != $end_num) {
  583. Log::write("状态解析错误:报警:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  584. }
  585. if ($first_num) {
  586. //16:SOS报警;17:低电报警;20:手环拆除报警;21:跌倒报警;22:心率异常报警
  587. $first_num = $mid - $first_num + ($mid - 1);
  588. $alarm_type = $first_num;
  589. }
  590. $status = substr($terminal_status_str, $mid);
  591. $first_num = strpos($status, '1');
  592. $end_num = strrpos($status, '1');
  593. if ($first_num != $end_num) {
  594. Log::write("状态解析错误:状态:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  595. }
  596. if ($first_num) {
  597. $first_num = $mid - 1 - $first_num;
  598. //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
  599. $status_type = $first_num + 1;
  600. }
  601. $data = [
  602. 'alarm_type' => isset($alarm_type) ? $alarm_type : false,
  603. 'status_type' => isset($status_type) ? $status_type : false,
  604. ];
  605. return $data;
  606. }
  607. // /**
  608. // * 解析报警
  609. // *
  610. // * @param [type] $first_num
  611. // * @return void
  612. // * @author wj
  613. // * @date 2023-08-19
  614. // */
  615. // public function getalarm($first_num)
  616. // {
  617. // $data = [
  618. // 16 => 'SOS报警',
  619. // 17 => '低电报警',
  620. // 18 => '出围栏报警',
  621. // 19 => '进围栏报警',
  622. // 20 => '手环拆除报警',
  623. // 21 => '跌倒报警',
  624. // 22 => '心率异常报警',
  625. // ];
  626. // if (isset($data[$first_num])) {
  627. // return $data[$first_num];
  628. // }
  629. // return false;
  630. // }
  631. // /**
  632. // * 解析状态
  633. // *
  634. // * @param [type] $first_num
  635. // * @return void
  636. // * @author wj
  637. // * @date 2023-08-19
  638. // */
  639. // public function getstatus($first_num)
  640. // {
  641. // $data = [
  642. // 0 => '低电状态',
  643. // 1 => '出围栏状态',
  644. // 2 => '进围栏状态',
  645. // 3 => '手环戴上取下状态',
  646. // 4 => '手表运行静止状态',
  647. // ];
  648. // if (isset($data[$first_num])) {
  649. // return $data[$first_num];
  650. // }
  651. // return false;
  652. // }
  653. // ̄へ ̄-----------内容解析结束---------------- ̄へ ̄
  654. // ̄へ ̄-----------业务处理开始---------------- ̄へ ̄
  655. /**
  656. * 获取设别表id
  657. *
  658. * @return void
  659. * @author wj
  660. * @date 2023-08-11
  661. */
  662. public function getfacilityid($device_id_code)
  663. {
  664. $m_f = new FacilityModel();
  665. $where = ['code' => $device_id_code, 'status' => 1];
  666. $finfo = $m_f->getInfo($where, ['id']);
  667. if (empty($finfo)) {
  668. throw new \Exception("设备信息错误:" . $device_id_code);
  669. return false;
  670. }
  671. $id = $finfo['id'];
  672. return $id;
  673. }
  674. public function geshinfoid($device_id_code)
  675. {
  676. $m_si = new ShinfoModel();
  677. $where = ['device_id_code' => $device_id_code];
  678. $siinfo = $m_si->getInfo($where, ['id']);
  679. if (empty($siinfo)) {
  680. throw new \Exception("设备信息错误:" . $device_id_code);
  681. return false;
  682. }
  683. $id = $siinfo['id'];
  684. return $id;
  685. }
  686. /**
  687. * 保存接收信息队列
  688. *
  689. * @param [type] $data
  690. * @return void
  691. * @author wj
  692. * @date 2023-08-14
  693. */
  694. public function savereceivequeue($data, $device_id_code)
  695. {
  696. $m_srq = new ShreceivequeueModel();
  697. $facility_id = $this->getfacilityid($device_id_code);
  698. $data = [
  699. 'msg' => $data,
  700. 'createtime' => date('Y-m-d H:i:s'),
  701. 'device_id_code' => $device_id_code,
  702. 'facility_id' => $facility_id,
  703. ];
  704. $id = $m_srq->insertData($data);
  705. if (empty($id)) {
  706. return false;
  707. }
  708. return true;
  709. }
  710. /**
  711. * 获取接收信息队列列表
  712. *
  713. * @param [type] $page
  714. * @param [type] $size
  715. * @return void
  716. * @author wj
  717. * @date 2023-08-14
  718. */
  719. public function getreceivequeuelist($page = null, $size = null)
  720. {
  721. $m_srq = new ShreceivequeueModel();
  722. $where = ['status' => 0];
  723. $size = empty($size) ? 20 : $size;
  724. if (empty($page)) {
  725. $count = $m_srq->getList($where, 'count');
  726. $totalpage = ceil($count / $size);
  727. $data = ['totalpage' => $totalpage, 'size' => $size];
  728. return $data;
  729. } else {
  730. $list = $m_srq->getList($where, '*', $page, $size, 'id asc');
  731. if (empty($list)) {
  732. //Log::write("手环接收信息队列-无数据", 'shouhuan');
  733. return false;
  734. }
  735. $list = $list->toArray();
  736. foreach ($list as $key => $value) {
  737. $id = $value['id'];
  738. if (1 != $value['status']) {
  739. $row = $m_srq->updateinfo(['id' => $id], ['status' => 1]);
  740. if (1 != $row) {
  741. $msg = "Shreceivequeue id:" . $id . " 修改失败";
  742. Log::write($msg, 'shouhuan');
  743. unset($list[$key]);
  744. }
  745. }
  746. }
  747. return $list;
  748. }
  749. }
  750. /**
  751. * 获取发送信息队列列表
  752. *
  753. * @param [type] $page
  754. * @param [type] $size
  755. * @return void
  756. * @author wj
  757. * @date 2023-08-14
  758. */
  759. public function getsendqueuelist($device_id_code, $page = null, $size = null)
  760. {
  761. $m_ssq = new ShsendqueueModel();
  762. $where = ['status' => 0, 'device_id_code' => $device_id_code];
  763. $size = empty($size) ? 20 : $size;
  764. if (empty($page)) {
  765. $count = $m_ssq->getList($where, 'count');
  766. $totalpage = ceil($count / $size);
  767. $data = ['totalpage' => $totalpage, 'size' => $size];
  768. return $data;
  769. } else {
  770. $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc');
  771. if (empty($list)) {
  772. Log::write("手环发送信息队列-无数据", 'shouhuan');
  773. return false;
  774. }
  775. $list = $list->toArray();
  776. foreach ($list as $key => $value) {
  777. $id = $value['id'];
  778. if (1 != $value['status']) {
  779. $row = $m_ssq->updateinfo(['id' => $id], ['status' => 1]);
  780. if (1 != $row) {
  781. $msg = "Shsendqueue id:" . $id . " 修改失败";
  782. Log::write($msg, 'shouhuan');
  783. unset($list[$key]);
  784. }
  785. }
  786. }
  787. return $list;
  788. }
  789. }
  790. /**
  791. * 保存手环信息
  792. *
  793. * @return void
  794. * @author wj
  795. * @date 2023-08-14
  796. */
  797. public function saveshinfo($facility_id, $device_id_code, $data, $queue_data)
  798. {
  799. $original_str = $queue_data['msg'];
  800. $command = $data['command'];
  801. $content_arr = $data['content_arr'];
  802. $config = $data['config'];
  803. $data = $this->getdata($config, $content_arr);
  804. $m_sri = new ShinfoModel();
  805. $m_srir = new ShreceiveredocdeModel();
  806. $m_sa = new ShalarmlistModel();
  807. if ('AL' == $command) {
  808. //手环警报信息
  809. $data['alarm_from_type'] = 1;
  810. $data['device_id_code'] = $device_id_code;
  811. $sshinfo_id = $this->geshinfoid($device_id_code);
  812. $data['shinfo_id'] = $sshinfo_id;
  813. $data['facility_id'] = $facility_id;
  814. $data['createtime'] = date('Y-m-d H:i:s');
  815. $said = $m_sa->insertData($data);
  816. if (empty($said)) {
  817. $msg = "报警信息添加失败:" . $original_str;
  818. Log::write($msg, 'shouhuan');
  819. throw new \Exception($msg);
  820. }
  821. } else {
  822. //手环基础信息修改
  823. $sriData = $data;
  824. if ($command == "UD") {
  825. $uddata = $this->getmacforUD($original_str, $device_id_code);
  826. if ($uddata) {
  827. $sriData = array_merge($sriData, $uddata);
  828. }
  829. }
  830. $sriwhere = [
  831. 'facility_id' => $facility_id,
  832. 'device_id_code' => $device_id_code,
  833. ];
  834. $sriinfo = $m_sri->getInfo($sriwhere);
  835. if (empty($sriinfo)) {
  836. $sriData['device_id_code'] = $device_id_code;
  837. $sriData['facility_id'] = $facility_id;
  838. $sriData['online_statis'] = 1;
  839. $sriinsertData = $sriData;
  840. $sriinsertData['createtime'] = date('Y-m-d H:i:s');
  841. $sriinsertData['updatetime'] = date('Y-m-d H:i:s');
  842. $sriinsertData = $m_sri->formatinfo($sriinsertData);
  843. $sriid = $m_sri->insertData($sriinsertData);
  844. if (empty($sriid)) {
  845. throw new \Exception("设备信息添加失败:" . $device_id_code);
  846. }
  847. } else {
  848. $sriinfo = $sriinfo->toArray();
  849. $sriid = $sriinfo['id'];
  850. $sriupdateData = (array) $sriData;
  851. foreach ($sriupdateData as $key => $value) {
  852. if (in_array($key, array_keys($sriinfo))) {
  853. if ($sriinfo[$key] == $value) {
  854. unset($sriupdateData[$key]);
  855. }
  856. } else {
  857. unset($sriupdateData[$key]);
  858. }
  859. }
  860. if (count($sriupdateData) > 0) {
  861. $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
  862. $sriupdateData = $m_sri->formatinfo($sriupdateData);
  863. $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
  864. if (empty($row)) {
  865. $errmsg = "设备信息修改失败:" . $device_id_code;
  866. Log::write($errmsg);
  867. Log::write($sriupdateData, 'shouhuan');
  868. throw new \Exception($errmsg);
  869. }
  870. }
  871. }
  872. }
  873. //接收信息历史添加
  874. $sririnsertData['facility_id'] = $facility_id;
  875. $sririnsertData['device_id_code'] = $device_id_code;
  876. $sririnsertData['original_str'] = $original_str;
  877. $sririnsertData['content'] = json_encode($data);
  878. if (isset($data['date'])) {
  879. $sririnsertData['date'] = $data['date'];
  880. }
  881. if (isset($data['time'])) {
  882. $sririnsertData['time'] = $data['time'];
  883. }
  884. $sririnsertData['command'] = $command;
  885. $sririnsertData['createtime'] = date('Y-m-d H:i:s');
  886. $sririnsertData['receive_time'] = $queue_data['createtime'];
  887. if ('tback' == $config['kind']) {
  888. $is_back_msg = true;
  889. $sririnsertData['is_back_msg'] = 0;
  890. } else {
  891. $is_back_msg = false;
  892. $sririnsertData['is_back_msg'] = 1;
  893. }
  894. $sririd = $m_srir->insertData($sririnsertData);
  895. if (empty($sririd)) {
  896. throw new \Exception("设备历史信息添加失败:" . $device_id_code);
  897. }
  898. if ($is_back_msg) {
  899. //处理回复信息
  900. $m_ssl = new ShsendlistModel();
  901. $where = [
  902. 'is_success' => 0,
  903. 'is_send' => 1,
  904. 'command' => $command,
  905. ];
  906. $sslinfo = $m_ssl->getInfo($where);
  907. if ($sslinfo) {
  908. //发送成功状态回写
  909. $sslid = $sslinfo['id'];
  910. $sslupdateData = ['is_success' => 1];
  911. $m_ssl->updateinfo(['id' => $sslid], $sslupdateData);
  912. $srirupdateData = ['send_list_id' => $sslid];
  913. $m_srir->updateinfo(['id' => $sririd], $srirupdateData);
  914. }
  915. }
  916. return true;
  917. }
  918. //发送完处理
  919. /**
  920. * 发送成功 删除发送数据
  921. *
  922. * @param [type] $item
  923. * @return void
  924. * @author wj
  925. * @date 2023-08-21
  926. */
  927. public function sendmsgsuccess($item)
  928. {
  929. $m_ssq = new ShsendqueueModel();
  930. $m_ssl = new ShsendlistModel();
  931. $m_sal = new ShalarmlistModel();
  932. $ssqid = $item['id'];
  933. if (0 === $item['msg_type']) {
  934. //普通信息
  935. if ($item['send_list_id']) {
  936. $list_id = $item['send_list_id'];
  937. $sslwhere = ['id' => $list_id, 'is_send' => 0];
  938. $sslinfo = $m_ssl->getInfo($sslwhere);
  939. if ($sslinfo) {
  940. $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  941. }
  942. }
  943. }
  944. if (1 === $item['msg_type']) {
  945. //报警信息
  946. if ($item['send_list_id']) {
  947. $list_id = $item['send_list_id'];
  948. $salwhere = ['id' => $list_id, 'is_send' => 0];
  949. $salinfo = $m_sal->getInfo($salwhere);
  950. if ($salinfo) {
  951. $row = $m_sal->updateinfo(['id' => $salinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  952. }
  953. }
  954. }
  955. $ssqwhere = ['id' => $ssqid];
  956. $ssqinfo = $m_ssq->getInfo($ssqwhere);
  957. if ($ssqinfo) {
  958. $m_ssq->deleteinfobyid($ssqid);
  959. }
  960. }
  961. /**
  962. * 解析成功 删除接收数据
  963. *
  964. * @param [type] $item
  965. * @return void
  966. * @author wj
  967. * @date 2023-08-21
  968. */
  969. public function receivemsgsuccess($item)
  970. {
  971. $m_srq = new ShreceivequeueModel();
  972. $srqid = $item['id'];
  973. $srqwhere = ['id' => $srqid, 'status' => 1];
  974. $srqinfo = $m_srq->getInfo($srqwhere);
  975. if ($srqinfo) {
  976. $m_srq->deleteinfobyid($srqid);
  977. }
  978. }
  979. /**
  980. * 初始化手环接收信息解析
  981. *
  982. * @return void
  983. * @author wj
  984. * @date 2023-08-15
  985. */
  986. public function initanalysisShoneReceiveMsg()
  987. {
  988. $m_srq = new ShreceivequeueModel();
  989. $where = ['status' => 1];
  990. $m_srq->updateinfo($where, ['status' => 0]);
  991. }
  992. /**
  993. * 测试使用
  994. *
  995. * @param [type] $data
  996. * @return void
  997. * @author wj
  998. * @date 2023-08-21
  999. */
  1000. public function showfortest($data)
  1001. {
  1002. $strdata = $this->getline($data, null, false);
  1003. if (!is_array($strdata)) {
  1004. Log::write($data, 'shouhuan');
  1005. return false;
  1006. }
  1007. $command = strtoupper($strdata['command']);
  1008. $showcommands = [
  1009. 'SOS', 'CR', 'UPLOAD', 'SLEEPTIME', 'ZONE', 'FALLDOWN',
  1010. 'APPLOCK', 'FACTORY', 'VERNO', 'LOWBAT', 'UD', 'POWEROFF', 'MESSAGE',
  1011. 'WALKTIME', 'PEDO', 'PHB', 'PHL', 'REMIND', 'SILENCETIME2', 'BOOTOFF',
  1012. 'REMOVE', 'INFO', 'AL', 'LSSET', 'BTWARNSET',
  1013. ];
  1014. if (in_array($command, $showcommands)) {
  1015. var_dump(date('Y-m-d H:i:s'));
  1016. var_dump($data);
  1017. }
  1018. }
  1019. /**
  1020. * 设置设备上线
  1021. *
  1022. * @return void
  1023. * @author wj
  1024. * @date 2023-08-21
  1025. */
  1026. public function setonlineinfo($device_id_code)
  1027. {
  1028. $m_si = new ShinfoModel();
  1029. $where = ['device_id_code' => $device_id_code, 'online_statis' => 0];
  1030. $finfo = $m_si->getInfo($where, ['id', 'online_statis']);
  1031. if ($finfo) {
  1032. $facility_id = $finfo['id'];
  1033. $m_si->setonlineinfobyid($facility_id);
  1034. }
  1035. }
  1036. /**
  1037. * 设置设备下线
  1038. *
  1039. * @return void
  1040. * @author wj
  1041. * @date 2023-08-21
  1042. */
  1043. public function setofflineinfo()
  1044. {
  1045. $m_si = new ShinfoModel();
  1046. $list = $m_si->getcheckupdatetimelist();
  1047. $list = (array) $list;
  1048. if (!empty($list)) {
  1049. $facility_ids = array_column($list, 'id');
  1050. $m_si->setofflineinfobyids($facility_ids);
  1051. }
  1052. }
  1053. //工具方法
  1054. /**
  1055. * unicode 解码
  1056. *
  1057. * @param [type] $str
  1058. * @return void
  1059. * @author wj
  1060. * @date 2023-08-21
  1061. */
  1062. public function unicodeToChn($str)
  1063. {
  1064. $arr = str_split($str);
  1065. $items = [];
  1066. $item = "";
  1067. foreach ($arr as $key => $value) {
  1068. $index = $key + 1;
  1069. if (0 != $index % 4) {
  1070. $item .= $value;
  1071. } else {
  1072. $item = "";
  1073. $items[] = $item;
  1074. }
  1075. }
  1076. foreach ($items as $key => $value) {
  1077. $char = hex2bin($value);
  1078. $char = iconv('UCS-2BE', 'UTF-8', $char);
  1079. $items[$key] = $char;
  1080. }
  1081. $info = implode("", $items);
  1082. return $info;
  1083. }
  1084. /**
  1085. * 拆包
  1086. *
  1087. * @return void
  1088. * @author wj
  1089. * @date 2023-08-15
  1090. */
  1091. public function unpack($data)
  1092. {
  1093. $arr = explode("][", $data);
  1094. foreach ($arr as $key => $value) {
  1095. if ($value[0] !== "[") {
  1096. $arr[$key] = '[' . $value;
  1097. }
  1098. if ($value[mb_strlen($value) - 1] !== "]") {
  1099. $arr[$key] .= "]";
  1100. }
  1101. }
  1102. return $arr;
  1103. }
  1104. }