SouhuanAnalysis.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. * @author wj
  15. * @date 2023-08-11
  16. */
  17. class SouhuanAnalysis
  18. {
  19. /**
  20. * 校验长度
  21. *
  22. * @return void
  23. * @author wj
  24. * @date 2023-08-11
  25. */
  26. public function checklen($len, $str)
  27. {
  28. $strlen = mb_strlen($str);
  29. if ($len != $strlen) {
  30. Log::write("长度校验失败: len:" . $len . " str:" . $str, 'shouhuan');
  31. return false;
  32. }
  33. return true;
  34. }
  35. /**
  36. * 获取信息 整个信息解析
  37. * 并验证是否可解析 无解析命令配置 不报错
  38. *
  39. * @return void
  40. * @author wj
  41. * @date 2023-08-11
  42. */
  43. public function getline($str, $field = null)
  44. {
  45. //[厂商*设备 ID*内容长度*内容]
  46. $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
  47. preg_match($preg, $str, $match);
  48. if (empty($match)) {
  49. Log::write("解析失败:" . $str, 'shouhuan');
  50. throw new \Exception('解析失败:' . $str);
  51. }
  52. unset($match[0]);
  53. $data = array_values($match);
  54. $sandom_string = $data[0]; //厂商 随机字符串
  55. $device_id_code = $data[1]; //设置id编码
  56. $content_len = $data[2]; //内容长度
  57. $content_str = $data[3]; //内容字符串
  58. $content_len = $this->analysislen($content_len);
  59. $result = $this->checklen($content_len, $content_str);
  60. if (empty($result)) {
  61. $errmsg = '内容位数错误';
  62. Log::write($errmsg . ":" . $str, 'shouhuan');
  63. throw new \Exception($errmsg);
  64. }
  65. $data = [
  66. 'device_id_code' => $device_id_code,
  67. 'content_len' => $content_len,
  68. 'content_str' => $content_str,
  69. ];
  70. $contentarr = $this->getcommandcontent($content_str);
  71. $command = $contentarr['command'];
  72. $data['command'] = $command;
  73. $data['content_arr'] = $contentarr['content'];
  74. $config = $this->getreceivecommandinfo($command);
  75. $data['config'] = $config;
  76. if (!$config) {
  77. $errmsg = '命令无解析配置 command:' . $command;
  78. Log::write($errmsg, 'shouhuan');
  79. return false;
  80. }
  81. if (isset($field)) {
  82. return $data[$field];
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 内容解析
  88. *
  89. * @param [type] $content_str
  90. * @return void
  91. * @author wj
  92. * @date 2023-08-19
  93. */
  94. public function getcommandcontent($content_str)
  95. {
  96. $content = explode(',', $content_str);
  97. $command = $content[0];
  98. unset($content[0]);
  99. $data = [
  100. 'command' => $command,
  101. 'content' => $content,
  102. ];
  103. return $data;
  104. }
  105. /**
  106. * 解析终端发送命令长度
  107. *
  108. * @param [type] $len
  109. * @return void
  110. * @author wj
  111. * @date 2023-08-18
  112. */
  113. public function analysislen($len)
  114. {
  115. $len = base_convert($len, 16, 10);
  116. return $len;
  117. }
  118. /**
  119. * 获取平台发送命令信息
  120. *
  121. * @param [type] $command
  122. * @return void
  123. * @author wj
  124. * @date 2023-08-11
  125. */
  126. public function getsendcommandinfo($command)
  127. {
  128. $command = strtoupper($command);
  129. $data = [
  130. //平台发送
  131. 'ZONE' => [
  132. 'content' => [
  133. 'command' => 'ZONE',
  134. 'zone' => '',
  135. ],
  136. 'kind' => 'send',
  137. 'need_back' => 1,
  138. ],
  139. 'UPLOAD' => [
  140. 'content' => [
  141. 'command' => 'UPLOAD',
  142. ],
  143. 'kind' => 'send',
  144. 'need_back' => 1,
  145. ],
  146. 'CR' => [
  147. 'content' => [
  148. 'command' => 'CR',
  149. ],
  150. 'kind' => 'send',
  151. 'need_back' => 1,
  152. ],
  153. 'SLEEPTIME' => [
  154. 'content' => [
  155. 'command' => 'CR',
  156. ],
  157. 'kind' => 'send',
  158. 'content_custom' => true,
  159. 'need_back' => 1,
  160. ],
  161. 'FALLDOWN' => [
  162. 'content' => [
  163. 'command' => 'FALLDOWN',
  164. 'is_open' => '',
  165. ],
  166. 'kind' => 'send',
  167. 'need_back' => 1,
  168. ],
  169. 'FACTORY' => [
  170. 'content' => [
  171. 'command' => 'FACTORY',
  172. ],
  173. 'kind' => 'send',
  174. 'need_back' => 1,
  175. ],
  176. 'SOS' => [
  177. 'content' => [
  178. 'command' => 'SOS',
  179. ],
  180. 'kind' => 'send',
  181. 'content_custom' => true,
  182. 'need_back' => 1,
  183. ],
  184. 'VERNO' => [
  185. 'content' => [
  186. 'command' => 'VERNO',
  187. ],
  188. 'kind' => 'send',
  189. 'need_back' => 1,
  190. ],
  191. 'LOWBAT' => [
  192. 'content' => [
  193. 'command' => 'LOWBAT',
  194. 'is_open' => '',
  195. ],
  196. 'kind' => 'send',
  197. 'need_back' => 1,
  198. ],
  199. 'POWEROFF' => [
  200. 'content' => [
  201. 'command' => 'POWEROFF',
  202. ],
  203. 'kind' => 'send',
  204. 'need_back' => 1,
  205. ],
  206. 'LSSET' => [
  207. 'content' => [
  208. 'command' => 'LSSET',
  209. 'content_custom' => true,
  210. ],
  211. 'kind' => 'send',
  212. 'need_back' => 1,
  213. ],
  214. 'PEDO' => [
  215. 'content' => [
  216. 'command' => 'PEDO',
  217. 'is_open' => '',
  218. ],
  219. 'kind' => 'send',
  220. 'need_back' => 1,
  221. ],
  222. 'PHL' => [
  223. 'content' => [
  224. 'command' => 'PEDO',
  225. ],
  226. 'kind' => 'send',
  227. 'content_custom' => true,
  228. 'need_back' => 1,
  229. ],
  230. 'REMIND' => [
  231. 'content' => [
  232. 'command' => 'REMIND',
  233. ],
  234. 'kind' => 'send',
  235. 'content_custom' => true,
  236. 'need_back' => 1,
  237. ],
  238. 'SILENCETIME2' => [
  239. 'content' => [
  240. 'command' => 'SILENCETIME2',
  241. ],
  242. 'kind' => 'send',
  243. 'content_custom' => true,
  244. 'need_back' => 1,
  245. ],
  246. 'BOOTOFF' => [
  247. 'content' => [
  248. 'command' => 'BOOTOFF',
  249. 'is_open' => '',
  250. 'bootup_time' => '',
  251. 'shutdown_time' => '',
  252. ],
  253. 'kind' => 'send',
  254. 'need_back' => 1,
  255. ],
  256. 'REMOVE' => [
  257. 'content' => [
  258. 'command' => 'REMOVE',
  259. 'is_open' => '',
  260. ],
  261. 'kind' => 'send',
  262. 'need_back' => 1,
  263. ],
  264. //平台回复
  265. 'LK' => [
  266. 'content' => [
  267. 'command' => 'LK',
  268. ],
  269. 'kind' => 'back',
  270. ],
  271. 'PING' => [
  272. 'content' => [
  273. 'command' => 'PING',
  274. 'isbind' => 1,
  275. ],
  276. 'kind' => 'back',
  277. ],
  278. 'KA' => [
  279. 'content' => [
  280. 'command' => 'KA',
  281. ],
  282. 'kind' => 'back',
  283. ],
  284. 'TEMP' => [
  285. 'content' => [
  286. 'command' => 'temp',
  287. ],
  288. 'kind' => 'back',
  289. ],
  290. 'BPHRT' => [
  291. 'content' => [
  292. 'command' => 'bphrt',
  293. ],
  294. 'kind' => 'back',
  295. ],
  296. 'HEART' => [
  297. 'content' => [
  298. 'command' => 'heart',
  299. ],
  300. 'kind' => 'back',
  301. ],
  302. 'BLOOD' => [
  303. 'content' => [
  304. 'command' => 'blood',
  305. ],
  306. 'kind' => 'back',
  307. ],
  308. 'OXYGEN' => [
  309. 'content' => [
  310. 'command' => 'oxygen',
  311. ],
  312. 'kind' => 'back',
  313. ],
  314. 'AL' => [
  315. 'content' => [
  316. 'command' => 'AL',
  317. ],
  318. 'kind' => 'back',
  319. ],
  320. ];
  321. if (!isset($data[$command])) {
  322. return false;
  323. }
  324. return $data[$command];
  325. }
  326. /**
  327. * 设置可解析数据 终端发送信息解析
  328. *
  329. * @param [type] $command
  330. * @return void
  331. * @author wj
  332. * @date 2023-08-18
  333. * analysis_digit 解析位数
  334. * content 解析对应字段
  335. * kind 类型 tsend 平台发送 tback平台回复
  336. * need_back 是否需回复 1是 0否 没设置为0
  337. * command 未设置则和键值相同
  338. */
  339. public function getreceivecommandinfo($command)
  340. {
  341. $command = strtoupper($command);
  342. $data = [
  343. 'UD' => [
  344. 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
  345. 'content' => [
  346. 'date',
  347. 'time',
  348. 'gps_long',
  349. 'gps_lat',
  350. 'speed',
  351. 'direction',
  352. 'poster',
  353. 'electric_quantity',
  354. 'step_number',
  355. 'roll_number',
  356. 'terminal_status',
  357. ],
  358. 'kind' => 'tsend',
  359. 'need_back' => 0,
  360. ],
  361. 'AL' => [
  362. 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
  363. 'content' => [
  364. 'date',
  365. 'time',
  366. 'gps_long',
  367. 'gps_lat',
  368. 'speed',
  369. 'direction',
  370. 'poster',
  371. 'electric_quantity',
  372. 'step_number',
  373. 'roll_number',
  374. 'terminal_status',
  375. ],
  376. 'kind' => 'tsend',
  377. 'need_back' => 0,
  378. ],
  379. 'KA' => [
  380. 'analysis_digit' => [1, 2, 3, 4],
  381. 'content' => [
  382. 'date',
  383. 'step_number',
  384. 'roll_number',
  385. 'electric_quantity',
  386. ],
  387. 'kind' => 'tsend',
  388. 'need_back' => 1,
  389. ],
  390. 'LK' => [
  391. 'analysis_digit' => null,
  392. 'content' => null,
  393. 'kind' => 'tsend',
  394. 'need_back' => 1,
  395. ],
  396. 'BPHRT' => [
  397. 'analysis_digit' => [1, 2, 3],
  398. 'content' => [
  399. 'blood_height_pressure',
  400. 'blood_low_pressure',
  401. 'heart_rate',
  402. ],
  403. 'kind' => 'tsend',
  404. 'need_back' => 1,
  405. 'command' => 'bphrt',
  406. ],
  407. 'TEMP' => [
  408. 'analysis_digit' => [1],
  409. 'content' => [
  410. 'temp',
  411. ],
  412. 'kind' => 'tsend',
  413. 'need_back' => 1,
  414. 'command' => 'temp',
  415. ],
  416. 'HEART' => [
  417. 'analysis_digit' => [1],
  418. 'content' => [
  419. 'heart_rate',
  420. ],
  421. 'kind' => 'tsend',
  422. 'need_back' => 1,
  423. 'command' => 'heart',
  424. ],
  425. 'BLOOD' => [
  426. 'analysis_digit' => [1, 2],
  427. 'content' => [
  428. 'blood_height_pressure',
  429. 'blood_low_pressure',
  430. ],
  431. 'kind' => 'tsend',
  432. 'need_back' => 1,
  433. 'command' => 'blood',
  434. ],
  435. 'OXYGEN' => [
  436. 'analysis_digit' => [1],
  437. 'content' => [
  438. 'oxygen',
  439. ],
  440. 'kind' => 'tsend',
  441. 'need_back' => 1,
  442. 'command' => 'oxygen',
  443. ],
  444. //终端回复
  445. //ZONE UPLOAD CR SLEEPTIME FALLDOWN FACTORY SOS VERNO LOWBAT
  446. //POWEROFF LSSET PEDO PHL REMIND SILENCETIME2 BOOTOFF REMOVE
  447. 'ZONE' => [
  448. 'analysis_digit' => null,
  449. 'content' => null,
  450. 'kind' => 'tback',
  451. ],
  452. 'UPLOAD' => [
  453. 'analysis_digit' => null,
  454. 'content' => null,
  455. 'kind' => 'tback',
  456. ],
  457. 'CR' => [
  458. 'analysis_digit' => null,
  459. 'content' => null,
  460. 'kind' => 'tback',
  461. ],
  462. 'SLEEPTIME' => [
  463. 'analysis_digit' => null,
  464. 'content' => null,
  465. 'kind' => 'tback',
  466. ],
  467. 'FALLDOWN' => [
  468. 'analysis_digit' => null,
  469. 'content' => null,
  470. 'kind' => 'tback',
  471. ],
  472. 'SOS' => [
  473. 'analysis_digit' => null,
  474. 'content' => null,
  475. 'kind' => 'tback',
  476. ],
  477. 'VERNO' => [
  478. 'analysis_digit' => [1],
  479. 'content' => ['version'],
  480. 'kind' => 'tback',
  481. ],
  482. 'LOWBAT' => [
  483. 'analysis_digit' => null,
  484. 'content' => null,
  485. 'kind' => 'tback',
  486. ],
  487. 'POWEROFF' => [
  488. 'analysis_digit' => null,
  489. 'content' => null,
  490. 'kind' => 'tback',
  491. ],
  492. 'LSSET' => [
  493. 'analysis_digit' => null,
  494. 'content' => null,
  495. 'kind' => 'tback',
  496. ],
  497. 'PEDO' => [
  498. 'analysis_digit' => null,
  499. 'content' => null,
  500. 'kind' => 'tback',
  501. ],
  502. 'PHL' => [
  503. 'analysis_digit' => null,
  504. 'content' => null,
  505. 'kind' => 'tback',
  506. ],
  507. 'REMIND' => [
  508. 'analysis_digit' => null,
  509. 'content' => null,
  510. 'kind' => 'tback',
  511. ],
  512. 'SILENCETIME2' => [
  513. 'analysis_digit' => null,
  514. 'content' => null,
  515. 'kind' => 'tback',
  516. ],
  517. 'BOOTOFF' => [
  518. 'analysis_digit' => null,
  519. 'content' => null,
  520. 'kind' => 'tback',
  521. ],
  522. 'REMOVE' => [
  523. 'analysis_digit' => null,
  524. 'content' => null,
  525. 'kind' => 'tback',
  526. ],
  527. ];
  528. if (!isset($data[$command])) {
  529. return false;
  530. }
  531. return $data[$command];
  532. }
  533. /**
  534. * 获取回复权重
  535. *
  536. * @param [type] $command
  537. * @return void
  538. * @author wj
  539. * @date 2023-08-11
  540. */
  541. public function getsendweight($command)
  542. {
  543. $weight = -1;
  544. switch ($command) {
  545. case 'POWEROFF':
  546. //关机
  547. $weight = 99;
  548. break;
  549. case 'RESET':
  550. //重启
  551. $weight = 98; //硬件未测试
  552. break;
  553. //平台回复命令
  554. case 'AL':
  555. case 'KA':
  556. case 'LK':
  557. case 'PING':
  558. case 'BPHRT':
  559. case 'TEMP':
  560. case 'HEART':
  561. case 'BLOOD':
  562. case 'OXYGEN':
  563. $weight = 1;
  564. break;
  565. //平台发送命令
  566. case 'ZONE':
  567. case 'UPLOAD':
  568. case 'CR':
  569. case 'SLEEPTIME':
  570. case 'FALLDOWN':
  571. case 'FACTORY':
  572. case 'SOS':
  573. case 'VERNO':
  574. case 'LOWBAT':
  575. case 'POWEROFF':
  576. case 'LSSET':
  577. case 'PEDO':
  578. case 'PHL':
  579. case 'REMIND':
  580. case 'SILENCETIME2':
  581. case 'BOOTOFF':
  582. case 'REMOVE':
  583. $weight = 0;
  584. break;
  585. default:
  586. $weight = -1;
  587. break;
  588. }
  589. if ($weight < 0) {
  590. //不需设置 发送信息
  591. return false;
  592. }
  593. return $weight;
  594. }
  595. /**
  596. * 获取数据值
  597. *
  598. * @param [type] $data
  599. * @param [type] $command
  600. * @return void
  601. * @author wj
  602. * @date 2023-08-14
  603. */
  604. public function getdata($config, $content_arr)
  605. {
  606. $analysis_digit = $config['analysis_digit'];
  607. $config_content = $config['content'];
  608. $data = [];
  609. if (is_array($analysis_digit)) {
  610. foreach ($analysis_digit as $key => $value) {
  611. if (isset($content_arr[$value])) {
  612. if (isset($config_content[$key])) {
  613. $item_key = $config_content[$key];
  614. $item_value = $content_arr[$value];
  615. $data[$item_key] = $item_value;
  616. }
  617. }
  618. }
  619. }
  620. if (isset($data['time'])) {
  621. $time = $data['time'];
  622. $data['time'] = $this->gettime($time);
  623. }
  624. if (isset($data['date'])) {
  625. $date = $data['date'];
  626. $data['date'] = $this->getdate($date);
  627. }
  628. if (isset($data['terminal_status'])) {
  629. $terminalstatus = $data['terminal_status'];
  630. $terminal_status_arr = $this->getterminalstatus($terminalstatus);
  631. unset($data['terminal_status']);
  632. $data['status_type'] = $terminal_status_arr['status_type'];
  633. $data['alarm_type'] = $terminal_status_arr['alarm_type'];
  634. }
  635. return $data;
  636. }
  637. /**
  638. * 时间解析
  639. *
  640. * @param [type] $time
  641. * @return void
  642. * @author wj
  643. * @date 2023-08-19
  644. */
  645. public function gettime($time)
  646. {
  647. $hour = substr($time, 0, 2);
  648. $minute = substr($time, 2, 2);
  649. $second = substr($time, 4, 2);
  650. $time = $hour . ':' . $minute . ':' . $second;
  651. return $time;
  652. }
  653. /**
  654. * 日期解析
  655. *
  656. * @return void
  657. * @author wj
  658. * @date 2023-08-15
  659. */
  660. public function getdate($date)
  661. {
  662. $day = substr($date, 0, 2);
  663. $month = substr($date, 2, 2);
  664. $year = substr($date, 4, 2);
  665. $year1 = date('Y');
  666. $year2 = substr($year1, 0, 2);
  667. $year = $year2 . $year;
  668. if ($year != $year1) {
  669. $day = date('Y-m-d');
  670. } else {
  671. $day = $year . '-' . $month . '-' . $day;
  672. }
  673. return $day;
  674. }
  675. /**
  676. * 解析终端状态
  677. *
  678. * @param [type] $terminal_status
  679. * @return void
  680. * @author wj
  681. * @date 2023-08-19
  682. */
  683. public function getterminalstatus($terminal_status)
  684. {
  685. $terminal_status = str_split($terminal_status);
  686. foreach ($terminal_status as $key => $value) {
  687. $value = base_convert($value, 16, 2);
  688. $value = str_pad($value, 4, "0", STR_PAD_LEFT);
  689. $terminal_status[$key] = $value;
  690. }
  691. $terminal_status_str = implode("", $terminal_status);
  692. $mid = mb_strlen($terminal_status_str) / 2;
  693. $alarm = substr($terminal_status_str, 0, $mid);
  694. $first_num = strpos($alarm, 1);
  695. $end_num = strrpos($alarm, 1);
  696. if ($first_num != $end_num) {
  697. Log::write("状态解析错误:报警:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  698. }
  699. //16:SOS报警;17:低电报警;20:手环拆除报警;21:跌倒报警;22:心率异常报警
  700. $alarm_type = $first_num;
  701. $status = substr($terminal_status_str, $mid);
  702. $first_num = strpos($status, 1);
  703. $end_num = strrpos($status, 1);
  704. if ($first_num != $end_num) {
  705. Log::write("状态解析错误:状态:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
  706. }
  707. //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
  708. $status_type = $first_num + 1;
  709. $data = [
  710. 'alarm_type' => $alarm_type,
  711. 'status_type' => $status_type,
  712. ];
  713. return $data;
  714. }
  715. // /**
  716. // * 解析报警
  717. // *
  718. // * @param [type] $first_num
  719. // * @return void
  720. // * @author wj
  721. // * @date 2023-08-19
  722. // */
  723. // public function getalarm($first_num)
  724. // {
  725. // $data = [
  726. // 16 => 'SOS报警',
  727. // 17 => '低电报警',
  728. // 18 => '出围栏报警',
  729. // 19 => '进围栏报警',
  730. // 20 => '手环拆除报警',
  731. // 21 => '跌倒报警',
  732. // 22 => '心率异常报警',
  733. // ];
  734. // if (isset($data[$first_num])) {
  735. // return $data[$first_num];
  736. // }
  737. // return false;
  738. // }
  739. // /**
  740. // * 解析状态
  741. // *
  742. // * @param [type] $first_num
  743. // * @return void
  744. // * @author wj
  745. // * @date 2023-08-19
  746. // */
  747. // public function getstatus($first_num)
  748. // {
  749. // $data = [
  750. // 0 => '低电状态',
  751. // 1 => '出围栏状态',
  752. // 2 => '进围栏状态',
  753. // 3 => '手环戴上取下状态',
  754. // 4 => '手表运行静止状态',
  755. // ];
  756. // if (isset($data[$first_num])) {
  757. // return $data[$first_num];
  758. // }
  759. // return false;
  760. // }
  761. // ̄へ ̄-----------内容解析结束---------------- ̄へ ̄
  762. // ̄へ ̄-----------业务处理开始---------------- ̄へ ̄
  763. /**
  764. * 获取设别表id
  765. *
  766. * @return void
  767. * @author wj
  768. * @date 2023-08-11
  769. */
  770. public function getfacilityid($device_id_code)
  771. {
  772. $m_f = new FacilityModel();
  773. $where = ['code' => $device_id_code, 'status' => 1];
  774. $finfo = $m_f->getInfo($where, ['id']);
  775. if (empty($finfo)) {
  776. throw new \Exception("设备信息错误:" . $device_id_code);
  777. return false;
  778. }
  779. $id = $finfo['id'];
  780. return $id;
  781. }
  782. /**
  783. * 保存接收信息队列
  784. *
  785. * @param [type] $data
  786. * @return void
  787. * @author wj
  788. * @date 2023-08-14
  789. */
  790. public function savereceivequeue($data)
  791. {
  792. $m_srq = new ShreceivequeueModel();
  793. $data = [
  794. 'msg' => $data,
  795. 'createtime' => date('Y-m-d H:i:s'),
  796. ];
  797. $id = $m_srq->insertData($data);
  798. if (empty($id)) {
  799. return false;
  800. }
  801. return true;
  802. }
  803. /**
  804. * 获取接收信息队列列表
  805. *
  806. * @param [type] $page
  807. * @param [type] $size
  808. * @return void
  809. * @author wj
  810. * @date 2023-08-14
  811. */
  812. public function getreceivequeuelist($page = null, $size = null)
  813. {
  814. $m_srq = new ShreceivequeueModel();
  815. $where = ['status' => 0];
  816. $size = empty($size) ? 20 : $size;
  817. if (empty($page)) {
  818. $count = $m_srq->getList($where, 'count');
  819. $totalpage = ceil($count / $size);
  820. $data = ['totalpage' => $totalpage, 'size' => $size];
  821. return $data;
  822. } else {
  823. $list = $m_srq->getList($where, '*', $page, $size, 'id asc');
  824. if (empty($list)) {
  825. //Log::write("手环接收信息队列-无数据", 'shouhuan');
  826. return false;
  827. }
  828. $list = $list->toArray();
  829. foreach ($list as $key => $value) {
  830. $id = $value['id'];
  831. if (1 != $value['status']) {
  832. $row = $m_srq->updateinfo(['id' => $id], ['status' => 1]);
  833. if (1 != $row) {
  834. $msg = "Shreceivequeue id:" . $id . " 修改失败";
  835. Log::write($msg, 'shouhuan');
  836. unset($list[$key]);
  837. }
  838. }
  839. }
  840. return $list;
  841. }
  842. }
  843. /**
  844. * 获取发送信息队列列表
  845. *
  846. * @param [type] $page
  847. * @param [type] $size
  848. * @return void
  849. * @author wj
  850. * @date 2023-08-14
  851. */
  852. public function getsendqueuelist($device_id_code, $page = null, $size = null)
  853. {
  854. $m_ssq = new ShsendqueueModel();
  855. $where = ['status' => 0, 'device_id_code' => $device_id_code];
  856. $size = empty($size) ? 20 : $size;
  857. if (empty($page)) {
  858. $count = $m_ssq->getList($where, 'count');
  859. $totalpage = ceil($count / $size);
  860. $data = ['totalpage' => $totalpage, 'size' => $size];
  861. return $data;
  862. } else {
  863. $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc');
  864. if (empty($list)) {
  865. Log::write("手环发送信息队列-无数据", 'shouhuan');
  866. return false;
  867. }
  868. $list = $list->toArray();
  869. foreach ($list as $key => $value) {
  870. $id = $value['id'];
  871. if (1 != $value['status']) {
  872. $row = $m_ssq->updateinfo(['id' => $id], ['status' => 1]);
  873. if (1 != $row) {
  874. $msg = "Shsendqueue id:" . $id . " 修改失败";
  875. Log::write($msg, 'shouhuan');
  876. unset($list[$key]);
  877. }
  878. }
  879. }
  880. return $list;
  881. }
  882. }
  883. /**
  884. * 保存手环信息
  885. *
  886. * @return void
  887. * @author wj
  888. * @date 2023-08-14
  889. */
  890. public function saveshinfo($facility_id, $device_id_code, $data, $original_str)
  891. {
  892. $command = $data['command'];
  893. $content_arr = $data['content_arr'];
  894. $config = $data['config'];
  895. $data = $this->getdata($config, $content_arr);
  896. $m_sri = new ShinfoModel();
  897. $m_srir = new ShreceiveredocdeModel();
  898. $m_sa = new ShalarmlistModel();
  899. if ('AL' == $command) {
  900. //手环警报信息
  901. $data['alarm_from_type'] = 1;
  902. $said = $m_sa->insertData($data);
  903. if (empty($said)) {
  904. $msg = "报警信息添加失败:" . $original_str;
  905. Log::write($msg, 'shouhuan');
  906. throw new \Exception($msg);
  907. }
  908. } else {
  909. //手环基础信息修改
  910. $sriData = $data;
  911. $sriwhere = [
  912. 'facility_id' => $facility_id,
  913. 'device_id_code' => $device_id_code,
  914. ];
  915. $sriinfo = $m_sri->getInfo($sriwhere);
  916. if (empty($sriinfo)) {
  917. $sriData = [
  918. 'device_id_code' => $device_id_code,
  919. 'facility_id' => $facility_id,
  920. ];
  921. $sriinsertData = $sriData;
  922. $sriinsertData['createtime'] = date('Y-m-d H:i:s');
  923. $sriinsertData['updatetime'] = date('Y-m-d H:i:s');
  924. $sriinsertData = $m_sri->formatinfo($sriinsertData);
  925. $sriid = $m_sri->insertData($sriinsertData);
  926. if (empty($sriid)) {
  927. throw new \Exception("设备信息添加失败:" . $device_id_code);
  928. }
  929. } else {
  930. $sriid = $sriinfo['id'];
  931. $sriupdateData = $sriData;
  932. $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
  933. $sriupdateData = $m_sri->formatinfo($sriupdateData);
  934. $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
  935. if (empty($row)) {
  936. throw new \Exception("设备信息修改失败:" . $device_id_code);
  937. }
  938. }
  939. //接收信息历史添加
  940. $sririnsertData['facility_id'] = $facility_id;
  941. $sririnsertData['device_id_code'] = $device_id_code;
  942. $sririnsertData['original_str'] = $original_str;
  943. $sririnsertData['content'] = json_encode($sriData);
  944. $sririnsertData['command'] = $command;
  945. $sririnsertData['createtime'] = date('Y-m-d H:i:s');
  946. if ('tback' == $config['kind']) {
  947. $is_back_msg = true;
  948. $sririnsertData['is_back_msg'] = 0;
  949. } else {
  950. $is_back_msg = false;
  951. $sririnsertData['is_back_msg'] = 1;
  952. }
  953. $sririd = $m_srir->insertData($sririnsertData);
  954. if (empty($sririd)) {
  955. throw new \Exception("设备历史信息添加失败:" . $device_id_code);
  956. }
  957. if ($is_back_msg) {
  958. //处理回复信息
  959. $m_ssl = new ShsendlistModel();
  960. $where = [
  961. 'is_success' => 0,
  962. 'is_send' => 1,
  963. 'command' => $command,
  964. ];
  965. $sslinfo = $m_ssl->getInfo($where);
  966. if ($sslinfo) {
  967. //发送成功状态回写
  968. $sslid = $sslinfo['id'];
  969. $sslupdateData = ['is_success' => 1];
  970. $m_ssl->updateinfo(['id' => $sslid], $sslupdateData);
  971. $srirupdateData = ['send_list_id' => $sslid];
  972. $m_srir->updateinfo(['id' => $sririd], $srirupdateData);
  973. }
  974. }
  975. }
  976. }
  977. /**
  978. * 创建发送信息
  979. *
  980. * @param [type] $data
  981. * @param [type] $command
  982. * @return void
  983. * @author wj
  984. * @date 2023-08-14
  985. */
  986. public function createsendmsg($facility_id, $device_id_code, $data, $command, $param = [])
  987. {
  988. $infoarr = $this->getsendcommandinfo($command);
  989. if (!$infoarr) {
  990. Log::write("无对应命令数据1:" . $command, 'shouhuan');
  991. Log::write($data, 'shouhuan');
  992. return false;
  993. }
  994. $command = $data['command'];
  995. $content_arr = $data['content_arr'];
  996. $config = $data['config'];
  997. $m_ssl = new ShsendlistModel();
  998. $commandxy = "3G";
  999. //仅创建
  1000. if ('back' == $config['kind']) {
  1001. $content = $config['content'];
  1002. $content = implode(',', $content);
  1003. $send_type = 0;
  1004. $is_success = 1;
  1005. }
  1006. if ('send' == $config['kind']) {
  1007. $content = $config['content'];
  1008. $useconmmand = $content['command'];
  1009. $content_custom = $config['content_custom'];
  1010. $need_back = $config['need_back'];
  1011. if ($content_custom) {
  1012. $functionname = 'get' . $command . "Commandinfo";
  1013. if (method_exists($this, $functionname)) {
  1014. $content = $useconmmand . "," . $this->$functionname($param);
  1015. } else {
  1016. throw new \Exception($command . "自定义方法不存在");
  1017. }
  1018. } else {
  1019. $content = implode(',', $content_arr);
  1020. }
  1021. $send_type = 1;
  1022. if ($need_back) {
  1023. $is_success = 0;
  1024. } else {
  1025. $is_success = 1;
  1026. }
  1027. }
  1028. $weight = $this->getsendweight($command);
  1029. $msgdata = [
  1030. 0 => $commandxy,
  1031. 'device_id_code' => $device_id_code,
  1032. 'len' => str_pad(dechex(mb_strlen($content)), 4, "0", STR_PAD_LEFT),
  1033. 'content' => $content,
  1034. ];
  1035. $msg = '[' . implode('*', $msgdata) . ']';
  1036. $sslInsertData = [
  1037. 'facility_id' => $facility_id,
  1038. 'device_id_code' => $device_id_code,
  1039. 'command' => $command,
  1040. 'send_msg' => $msg,
  1041. 'send_weight' => $weight,
  1042. 'send_time' => date('Y-m-d H:i:s'),
  1043. 'is_success' => $is_success,
  1044. 'send_type' => $send_type,
  1045. ];
  1046. $sslid = $m_ssl->insertData($sslInsertData);
  1047. if (empty($sslid)) {
  1048. throw new \Exception("发送数据添加失败");
  1049. }
  1050. $data = [
  1051. 'msg' => $msg,
  1052. 'weight' => $weight,
  1053. 'createtime' => date('Y-m-d H:i:s'),
  1054. 'facility_id' => $facility_id,
  1055. 'device_id_code' => $device_id_code,
  1056. 'send_list_id' => $sslid,
  1057. ];
  1058. $m_ssq = new ShsendqueueModel();
  1059. $id = $m_ssq->insertData($data);
  1060. if (empty($id)) {
  1061. throw new \Exception("发送队列数据添加失败");
  1062. }
  1063. return true;
  1064. }
  1065. /**
  1066. * 创建sos电话设置命令
  1067. * 最多三个电话
  1068. *
  1069. * @return void
  1070. * @author wj
  1071. * @date 2023-08-21
  1072. */
  1073. public function getSOSCommandinfo($param)
  1074. {
  1075. if (count($param) <= 3) {
  1076. throw new \Exception("未设置电话");
  1077. }
  1078. if (count($param) > 3) {
  1079. throw new \Exception("设置电话数量过多");
  1080. }
  1081. foreach ($param as $key => $value) {
  1082. //判断是否为手机号
  1083. //暂不处理
  1084. }
  1085. $info = implode(",", $param);
  1086. return $info;
  1087. }
  1088. /**
  1089. * 设置跌倒灵敏度命令
  1090. * [3G*358800006072996*0009*LSSET,3+6]
  1091. *
  1092. * @return void
  1093. * @author wj
  1094. * @date 2023-08-21
  1095. */
  1096. public function getLSSETCommandinfo($param)
  1097. {
  1098. if (2 != count($param)) {
  1099. throw new \Exception("LSSET参数错误");
  1100. }
  1101. foreach ($param as $key => $value) {
  1102. if (!is_numeric($value)) {
  1103. throw new \Exception("LSSET参数错误:" . $value);
  1104. }
  1105. }
  1106. $info = $param[0] . "+" . $param[1];
  1107. return $info;
  1108. }
  1109. /**
  1110. * 设置电话本
  1111. * 最多10条
  1112. *{telno=>'',name=>''}
  1113. * @param [type] $param
  1114. * @return void
  1115. * @author wj
  1116. * @date 2023-08-21
  1117. */
  1118. public function getPHLCommaninfo($param)
  1119. {
  1120. if (50 > count($param)) {
  1121. throw new \Exception("PHL参数错误");
  1122. }
  1123. if (0 != count($param) % 2) {
  1124. throw new \Exception("PHL参数长度错误");
  1125. }
  1126. $usedata = [];
  1127. foreach ($param as $key => $value) {
  1128. $telno = $value['telno'];
  1129. $name = $value['name'];
  1130. //电话号码
  1131. if (20 > mb_strlen($telno)) {
  1132. throw new \Exception("PHL电话号码错误:" . $telno);
  1133. }
  1134. $name = $this->srtingToUnicode($name);
  1135. $usedata[] = $telno;
  1136. $usedata[] = $name;
  1137. }
  1138. $info = implode(",", $usedata);
  1139. return $info;
  1140. }
  1141. /**
  1142. * 设置电话本
  1143. * 最多10条
  1144. *{[time=>'',isopen=>'','type'=>'','weeks'=>[]]}
  1145. * weeks 数组 每个1到7 周日到周六
  1146. * 最多设置三个
  1147. * @param [type] $param
  1148. * @return void
  1149. * @author wj
  1150. * @date 2023-08-21
  1151. */
  1152. /*
  1153. 08:10-1-1:闹钟时间 8:10,打开,响铃一次
  1154. 08:10-1-2:闹钟时间 8:10,打开,每天响铃
  1155. 08:10-1-3-0111110:闹钟时间 8:10,打开,自定义周一至周五打开
  1156. 最左边是周日,最右边是周六,即 0111110 的顺序是:周日,周一,周二,周三,周四,周五,周
  1157. 六(1 为打开,0 为关闭),其中周一至周五打开闹钟
  1158. */
  1159. public function getREMINDCommandinfo($param)
  1160. {
  1161. if (3 > count($param)) {
  1162. throw new \Exception("PHL参数错误");
  1163. }
  1164. $usedata = [];
  1165. $useweeks = ["0", "0", "0", "0", "0", "0", "0"];
  1166. foreach ($param as $key => $value) {
  1167. $time = date('H:i', $value['time']);
  1168. $isopen = $value['isopen'];
  1169. $type = $value['type'];
  1170. $weeks = $param['weeks'];
  1171. if (3 == $type) {
  1172. foreach ($weeks as $wkey => $wvalue) {
  1173. $useweeks[$wvalue - 1] = 1;
  1174. }
  1175. $useweeks = implode("", $useweeks);
  1176. $type = $useweeks;
  1177. }
  1178. $usedata[] = $time . "-" . $isopen . "-" . $type;
  1179. }
  1180. $usedata = implode(",", $usedata);
  1181. return $usedata;
  1182. }
  1183. /**
  1184. * 静默时间段测试
  1185. *{[starttime=>'',endtime=>'',isopen=>'',weeks=>[]]}
  1186. * weeks 数组 每个1到7 周日到周六
  1187. * 最多设置三个
  1188. *
  1189. * @return void
  1190. * @author wj
  1191. * @date 2023-08-21
  1192. */
  1193. /*
  1194. [DW*334588000000156*0038*SILENCETIME2,7:30-21:10-1-0111110,7:30-21:10-1-0111110,7:
  1195. 30-21:10-0-0111110]
  1196. 设置上课禁用时间段范围,拦截终端的任何来电。格式为“起始时间-结束时间-自定义星期几”, 比
  1197. 如上面的 7:30-21:00-0111110,则表示周一到周五 7:30 到 21:00 之间上课禁用生效,起始时间
  1198. 默认大于结束时间。
  1199. 总开关:0 为关,1 为开
  1200. 关于自定义星期几的规则:最左边是周日,最右边是周六,即 0111110 的顺序是:周日,周一,周
  1201. 二,周三,周四,周五,周六(1 为打开,0 为关闭),所以上面的意思是周一至周五打开上课禁
  1202. 用。
  1203. */
  1204. public function getSILENCETIME2Commandinfo($param)
  1205. {
  1206. if (3 > count($param)) {
  1207. throw new \Exception("SILENCETIME2参数错误");
  1208. }
  1209. $usedata = [];
  1210. $useweeks = ["0", "0", "0", "0", "0", "0", "0"];
  1211. foreach ($param as $key => $value) {
  1212. //未比较时间大小
  1213. $starttime = date('H:i', $value['starttime']);
  1214. $endtime = date('H:i', $value['endtime']);
  1215. $isopen = $value['isopen'];
  1216. $weeks = $param['weeks'];
  1217. foreach ($weeks as $wkey => $wvalue) {
  1218. $useweeks[$wvalue - 1] = 1;
  1219. }
  1220. $useweeks = implode("", $useweeks);
  1221. $usedata[] = $starttime . "-" . $endtime . "-" . $isopen . "-" . $useweeks;
  1222. }
  1223. $usedata = implode(",", $usedata);
  1224. return $usedata;
  1225. }
  1226. /**
  1227. * 睡眠时间段设置
  1228. *{starttime=>'',endtime=>''}
  1229. *
  1230. * @return void
  1231. * @author wj
  1232. * @date 2023-08-21
  1233. */
  1234. public function getSLEEPTIMECommandinfo($param)
  1235. {
  1236. if (2 > count($param)) {
  1237. throw new \Exception("SLEEPTIME参数错误");
  1238. }
  1239. $starttime = date('H:i', $param['starttime']);
  1240. $endtime = date('H:i', $param['endtime']);
  1241. $usedata = $starttime . "-" . $endtime;
  1242. return $usedata;
  1243. }
  1244. /**
  1245. * 发送成功 删除发送数据
  1246. *
  1247. * @param [type] $item
  1248. * @return void
  1249. * @author wj
  1250. * @date 2023-08-21
  1251. */
  1252. public function sendmsgsuccess($item)
  1253. {
  1254. $m_ssq = new ShsendqueueModel();
  1255. $m_ssl = new ShsendlistModel();
  1256. $ssqid = $item['id'];
  1257. if ($item['list_id']) {
  1258. $list_id = $item['list_id'];
  1259. $sslwhere = ['id' => $list_id, 'is_send' => 0];
  1260. $sslinfo = $m_ssl->getInfo($sslwhere);
  1261. if ($sslinfo) {
  1262. $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  1263. }
  1264. }
  1265. $ssqwhere = ['id' => $ssqid];
  1266. $ssqinfo = $m_ssq->getInfo($ssqwhere);
  1267. if ($ssqinfo) {
  1268. $m_ssq->deleteinfobyid($ssqid);
  1269. }
  1270. }
  1271. /**
  1272. * 解析成功 删除接收数据
  1273. *
  1274. * @param [type] $item
  1275. * @return void
  1276. * @author wj
  1277. * @date 2023-08-21
  1278. */
  1279. public function receivemsgsuccess($item)
  1280. {
  1281. $m_srq = new ShreceivequeueModel();
  1282. $srqid = $item['id'];
  1283. $srqwhere = ['id' => $srqid, 'status' => 1];
  1284. $srqinfo = $m_srq->getInfo($srqwhere);
  1285. if ($srqinfo) {
  1286. $m_srq->deleteinfobyid($srqid);
  1287. }
  1288. }
  1289. /**
  1290. * 初始化手环接收信息解析
  1291. *
  1292. * @return void
  1293. * @author wj
  1294. * @date 2023-08-15
  1295. */
  1296. public function initanalysisShoneReceiveMsg()
  1297. {
  1298. $m_srq = new ShreceivequeueModel();
  1299. $where = ['status' => 1];
  1300. $m_srq->updateinfo($where, ['status' => 0]);
  1301. }
  1302. /**
  1303. * 测试使用
  1304. *
  1305. * @param [type] $data
  1306. * @return void
  1307. * @author wj
  1308. * @date 2023-08-21
  1309. */
  1310. public function showfortest($data)
  1311. {
  1312. $strdata = $this->getline($data);
  1313. $content = $strdata['content_str'];
  1314. $content = $this->getcommandcontent($content);
  1315. $command = $content[0];
  1316. $showcommands = [
  1317. 'SOS', 'CR', 'UPLOAD', 'SLEEPTIME', 'ZONE', 'FALLDOWN',
  1318. 'APPLOCK', 'FACTORY', 'VERNO', 'LOWBAT', 'UD', 'POWEROFF', 'MESSAGE',
  1319. 'WALKTIME', 'PEDO', 'PHB', 'PHL', 'REMIND', 'SILENCETIME2', 'BOOTOFF',
  1320. 'REMOVE', 'INFO', 'AL', 'LSSET',
  1321. ];
  1322. if (in_array($command, $showcommands)) {
  1323. var_dump(date('Y-m-d H:i:s'));
  1324. var_dump($data);
  1325. }
  1326. }
  1327. /**
  1328. * 设置设备上线
  1329. *
  1330. * @return void
  1331. * @author wj
  1332. * @date 2023-08-21
  1333. */
  1334. public function setonlineinfo($device_id_code)
  1335. {
  1336. $m_si = new ShinfoModel();
  1337. $where = ['device_id_code' => $device_id_code, 'online_statis' => 0];
  1338. $finfo = $m_si->getInfo($where, ['id', 'online_statis']);
  1339. if ($finfo) {
  1340. $facility_id = $finfo['id'];
  1341. $m_si->setonlineinfobyid($facility_id);
  1342. }
  1343. }
  1344. /**
  1345. * 设置设备下线
  1346. *
  1347. * @return void
  1348. * @author wj
  1349. * @date 2023-08-21
  1350. */
  1351. public function setofflineinfo()
  1352. {
  1353. $m_si = new ShinfoModel();
  1354. $list = $m_si->getcheckupdatetimelist();
  1355. $list = (array) $list;
  1356. if (!empty($list)) {
  1357. $facility_ids = array_column($list, 'id');
  1358. $m_si->setofflineinfobyids($facility_ids);
  1359. }
  1360. }
  1361. //工具方法
  1362. /**
  1363. * unicode 编码
  1364. *
  1365. * @param [type] $str
  1366. * @return void
  1367. * @author wj
  1368. * @date 2023-08-21
  1369. */
  1370. public function srtingToUnicode($str)
  1371. {
  1372. $unicode = '';
  1373. $arr = str_split($str);
  1374. foreach ($arr as $key => $value) {
  1375. $char = bin2hex(iconv('UTF-8', 'UCS-2BE', $value));
  1376. $char = str_pad($char, 4, '0', STR_PAD_LEFT);
  1377. $arr[$key] = $char;
  1378. }
  1379. $unicode = implode("", $arr);
  1380. return $unicode;
  1381. }
  1382. /**
  1383. * unicode 解码
  1384. *
  1385. * @param [type] $str
  1386. * @return void
  1387. * @author wj
  1388. * @date 2023-08-21
  1389. */
  1390. public function unicodeToChn($str)
  1391. {
  1392. $arr = str_split($str);
  1393. $items = [];
  1394. $item = "";
  1395. foreach ($arr as $key => $value) {
  1396. $index = $key + 1;
  1397. if (0 != $index % 4) {
  1398. $item .= $value;
  1399. } else {
  1400. $item = "";
  1401. $items[] = $item;
  1402. }
  1403. }
  1404. foreach ($items as $key => $value) {
  1405. $char = hex2bin($value);
  1406. $char = iconv('UCS-2BE', 'UTF-8', $char);
  1407. $items[$key] = $char;
  1408. }
  1409. $info = implode("", $items);
  1410. return $info;
  1411. }
  1412. /**
  1413. * 拆包
  1414. *
  1415. * @return void
  1416. * @author wj
  1417. * @date 2023-08-15
  1418. */
  1419. public function unpack($data)
  1420. {
  1421. $arr = explode("][", $data);
  1422. foreach ($arr as $key => $value) {
  1423. if ($value[0] !== "[") {
  1424. $arr[$key] = '[' . $value;
  1425. }
  1426. if ($value[mb_strlen($value) - 1] !== "]") {
  1427. $arr[$key] .= "]";
  1428. }
  1429. }
  1430. return $arr;
  1431. }
  1432. }