SouhuanAnalysis.php 35 KB

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