SouhuanAnalysis.php 35 KB

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