SouhuanAnalysis.php 36 KB

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