SouhuanAnalysis.php 50 KB

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