ShOneanalysis.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. namespace app\common\server;
  3. use app\common\model\FacilityModel;
  4. use app\common\model\ShoneReceiveInfoModel;
  5. use app\common\model\ShoneReceiveInfoRecode;
  6. use app\common\model\ShoneReceiveQueueModel;
  7. use app\common\model\ShoneSendListModel;
  8. use app\common\model\ShoneSendQueueModel;
  9. use think\facade\Log;
  10. /**
  11. * 手环字符串解析
  12. *
  13. * @author wj
  14. * @date 2023-08-11
  15. */
  16. class ShOneanalysis
  17. {
  18. private $num_scale = 16; //16进制
  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. return false;
  31. }
  32. return true;
  33. }
  34. /**
  35. * 获取信息
  36. *
  37. * @return void
  38. * @author wj
  39. * @date 2023-08-11
  40. */
  41. public function getline($str)
  42. {
  43. //[厂商*设备 ID*内容长度*内容]
  44. $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
  45. preg_match($preg, $str, $match);
  46. if (empty($match)) {
  47. throw new \Exception('解析失败:' . $str);
  48. }
  49. unset($match[0]);
  50. $data = array_values($match);
  51. $sandom_string = $data[0]; //厂商 随机字符串
  52. $device_id_code = $data[1]; //设置id编码
  53. $content_len = $data[2]; //内容长度
  54. $content_str = $data[3]; //内容字符串
  55. $content_len = $this->analysislen($content_len);
  56. $result = $this->checklen($content_len, $content_str);
  57. if (empty($result)) {
  58. throw new \Exception('内容位数错误');
  59. }
  60. $data = [
  61. 'device_id_code' => $device_id_code,
  62. 'content_len' => $content_len,
  63. 'content_str' => $content_str,
  64. ];
  65. return $data;
  66. }
  67. //KA,230814,0,0,96
  68. //WT,130823,160054,V,22.601242,N,113.8302765,E,0.00,0.0,29.2,0,70,96,0,0,00000000,1,1,460,0,8593,265807240,136,0,9999.0
  69. //ICCID,02203002080045057806
  70. public function getcommandcontent($content_str)
  71. {
  72. $content = explode(',', $content_str);
  73. return $content;
  74. }
  75. /**
  76. * 保存手环设备one信息
  77. *
  78. * @return void
  79. * @author wj
  80. * @date 2023-08-11
  81. */
  82. public function saveshouhuanoneinfo()
  83. {
  84. //保存时时
  85. //保存历史
  86. }
  87. //内容解析
  88. //解析长度
  89. public function analysislen($len)
  90. {
  91. //低位数在前
  92. $len_arr = array_reverse(str_split($len));
  93. $len_num = [];
  94. foreach ($len_arr as $key => $value) {
  95. if (is_numeric($value) && $value <= 9) {
  96. $num = $value;
  97. } elseif (ord($value) <= 65 || ord($value) >= 90) {
  98. $ord = ord($value);
  99. $num = bcadd(bcsub($ord, 65), 10);
  100. }
  101. $pow = pow($this->num_scale, $key); //16^0 16^1
  102. $len_num[] = bcmul($pow, $num);
  103. }
  104. $len = array_sum($len_num);
  105. return $len;
  106. }
  107. /**
  108. * 获取命令信息
  109. *
  110. * @param [type] $command
  111. * @return void
  112. * @author wj
  113. * @date 2023-08-11
  114. */
  115. public function getcommandinfo($command)
  116. {
  117. $data = [
  118. 'LK' => [
  119. 'content' => [
  120. 'command' => 'LK',
  121. ],
  122. 'kind' => 'back',
  123. ],
  124. 'PING' => [
  125. 'content' => [
  126. 'command' => 'PING',
  127. 'isbind' => 1,
  128. ],
  129. 'kind' => 'back',
  130. ],
  131. 'KA' => [
  132. 'content' => [
  133. 'command' => 'KA',
  134. ],
  135. 'kind' => 'back',
  136. ],
  137. 'MONITOR' => [
  138. 'content' => [
  139. 'command' => 'MONITOR',
  140. 'telno' => '',
  141. ],
  142. 'kind' => 'send',
  143. ],
  144. 'SOS' => [
  145. 'content' => [
  146. 'command' => 'SOS',
  147. 'telno1' => '',
  148. 'telno2' => '',
  149. 'telno3' => '',
  150. ],
  151. 'kind' => 'send',
  152. ],
  153. 'VERNO' => [
  154. 'content' => [
  155. 'command' => 'VERNO',
  156. ],
  157. 'kind' => 'send',
  158. ],
  159. 'ZONE' => [
  160. 'content' => [
  161. 'command' => 'ZONE',
  162. 'zone' => '',
  163. ],
  164. 'kind' => 'send',
  165. ],
  166. 'POWEROFF' => [
  167. 'content' => [
  168. 'command' => 'POWEROFF',
  169. ],
  170. 'kind' => 'send',
  171. ],
  172. ];
  173. if (!isset($data[$command])) {
  174. return false;
  175. }
  176. return $data[$command];
  177. }
  178. public function gettsendcommandinfo($command)
  179. {
  180. $data = [
  181. 'WT' => [
  182. 'content' => [
  183. 'command' => 'WT',
  184. 'date' => '',
  185. 'time' => '',
  186. 'is_location' => '',
  187. 'gps_long' => '',
  188. 'gps_long_tag' => '',
  189. 'gps_lat' => '',
  190. 'gps_lat_tag' => '',
  191. 'speed' => '',
  192. 'direction' => '',
  193. 'poster' => '',
  194. 'direction' => '',
  195. 'gps_satellite_num' => '',
  196. 'gsm_signal_strength' => '',
  197. 'electric_quantity' => '',
  198. 'step_number' => '',
  199. 'roll_number' => '',
  200. 'terminal_status' => '',
  201. ],
  202. 'kind' => 'tsend',
  203. 'need_bacl' => 0,
  204. ],
  205. 'KA' => [
  206. 'content' => [
  207. 'command' => 'KA',
  208. 'date' => '',
  209. 'step_number' => '',
  210. 'roll_number' => '',
  211. 'electric_quantity' => '',
  212. ],
  213. 'kind' => 'tsend',
  214. 'need_back' => 1,
  215. ],
  216. 'LK' => [
  217. 'content' => [
  218. 'command' => 'LK',
  219. ],
  220. 'kind' => 'tsend',
  221. 'need_back' => 1,
  222. ],
  223. 'MONITOR' => [
  224. 'content' => [
  225. 'command' => 'MONITOR',
  226. ],
  227. 'kind' => 'tback',
  228. ],
  229. 'SOS3' => [
  230. 'content' => [
  231. 'command' => 'SOS',
  232. ],
  233. 'kind' => 'tback',
  234. ],
  235. 'VERNO' => [
  236. 'content' => [
  237. 'command' => 'VERNO',
  238. 'version' => '',
  239. ],
  240. 'kind' => 'tback',
  241. ],
  242. ];
  243. if (!isset($data[$command])) {
  244. return false;
  245. }
  246. return $data[$command];
  247. }
  248. /**
  249. * 获取回复权重
  250. *
  251. * @param [type] $command
  252. * @return void
  253. * @author wj
  254. * @date 2023-08-11
  255. */
  256. public function getsendweight($command)
  257. {
  258. $weight = -1;
  259. switch ($command) {
  260. case 'POWEROFF':
  261. //关机
  262. $weight = 99;
  263. break;
  264. case 'RESET':
  265. //重启
  266. $weight = 98;
  267. break;
  268. case 'LK':
  269. case 'PING':
  270. case 'KA':
  271. case 'AL':
  272. case 'UD2';
  273. $weight = 1;
  274. break;
  275. case 'UPLOAD':
  276. case 'MONITOR':
  277. case 'SOS':
  278. case 'LANG':
  279. case 'ZONE':
  280. case 'CENTER':
  281. case 'SOSSMS':
  282. case 'REMOVE':
  283. case 'REMOVESMS':
  284. case 'VERNO':
  285. case 'CR':
  286. case 'SILENCETIME2':
  287. case 'WALKTIME':
  288. case 'SLEEPTIME':
  289. case 'FIND':
  290. case 'REMIND':
  291. case 'SCHEDULE':
  292. case 'PHB':
  293. case 'PHBX':
  294. case 'DPHBX':
  295. case 'MESSAGE':
  296. case 'FACTORY':
  297. case 'PROFILE':
  298. case 'BTEMP2':
  299. case 'BODYTEMP':
  300. case 'BTWARNSET':
  301. case 'APPLOCK':
  302. case 'HRTSTART':
  303. case 'BPHRT':
  304. case 'LSSET':
  305. $weight = 0;
  306. break;
  307. default:
  308. $weight = -1;
  309. break;
  310. }
  311. if ($weight < 0) {
  312. //不需设置 发送信息
  313. return false;
  314. }
  315. return $weight;
  316. }
  317. /**
  318. * 获取设别表id
  319. *
  320. * @return void
  321. * @author wj
  322. * @date 2023-08-11
  323. */
  324. public function getfacilityid($device_id_code)
  325. {
  326. $m_f = new FacilityModel();
  327. $where = ['code' => $device_id_code, 'status' => 1];
  328. $finfo = $m_f->getInfo($where, ['id']);
  329. if (empty($finfo)) {
  330. throw new \Exception("设备信息错误:" . $device_id_code);
  331. }
  332. $id = $finfo['id'];
  333. return $id;
  334. }
  335. /**
  336. * 创建发送信息
  337. *
  338. * @param [type] $data
  339. * @param [type] $command
  340. * @return void
  341. * @author wj
  342. * @date 2023-08-14
  343. */
  344. public function createbacksendmsg($facility_id, $device_id_code, $data, $command)
  345. {
  346. $infoarr = $this->gettsendcommandinfo($command);
  347. if (!$infoarr) {
  348. throw new \Exception("无对应命令数据:" . $command);
  349. }
  350. $data = $this->getdata($data, $command, $infoarr);
  351. $info_content = $data['info_content'];
  352. $need_back = $data['need_back'];
  353. if ($need_back) {
  354. $infoarr = $this->getcommandinfo($command);
  355. $data = $this->getdata($data, $command, $infoarr);
  356. $info_content = $data['info_content'];
  357. $weight = $this->getsendweight($command);
  358. $content = implode(',', $info_content);
  359. $msgdata = [
  360. 0 => 'DW',
  361. 'device_id_code' => $device_id_code,
  362. 'len' => str_pad(dechex(mb_strlen($content)), 4, "0", STR_PAD_LEFT),
  363. 'content' => $content,
  364. ];
  365. $msg = '[' . implode('*', $msgdata) . ']';
  366. $sslInsertData = [
  367. 'facility_id' => $facility_id,
  368. 'device_id_code' => $device_id_code,
  369. 'send_msg' => $msg,
  370. 'send_time' => date('Y-m-d H:i:s'),
  371. 'send_weight' => $weight,
  372. ];
  373. $m_ssl = new ShoneSendListModel();
  374. $sslid = $m_ssl->insertData($sslInsertData);
  375. if (empty($sslid)) {
  376. throw new \Exception("发送数据添加失败");
  377. }
  378. $data = [
  379. 'msg' => $msg,
  380. 'weight' => $weight,
  381. 'createtime' => date('Y-m-d H:i:s'),
  382. 'device_id_code' => $device_id_code,
  383. 'list_id' => $sslid,
  384. ];
  385. $m_ssq = new ShoneSendQueueModel();
  386. $id = $m_ssq->insertData($data);
  387. if (empty($id)) {
  388. throw new \Exception("发送队列数据添加失败");
  389. }
  390. return $data;
  391. }
  392. return false;
  393. }
  394. /**
  395. * 获取数据值
  396. *
  397. * @param [type] $data
  398. * @param [type] $command
  399. * @return void
  400. * @author wj
  401. * @date 2023-08-14
  402. */
  403. public function getdata($data, $command, $infoarr)
  404. {
  405. $info_content = $infoarr['content'];
  406. foreach (array_keys($info_content) as $key => $value) {
  407. if (empty($info_content[$value])) {
  408. if (isset($data[$key])) {
  409. $info_content[$value] = $data[$key];
  410. }
  411. }
  412. }
  413. $infoarr['info_content'] = $info_content;
  414. return $infoarr;
  415. }
  416. /**
  417. * 保存接收信息队列
  418. *
  419. * @param [type] $data
  420. * @return void
  421. * @author wj
  422. * @date 2023-08-14
  423. */
  424. public function savereceivequeue($data)
  425. {
  426. $m_srq = new ShoneReceiveQueueModel();
  427. $data = [
  428. 'msg' => $data,
  429. 'createtime' => date('Y-m-d H:i:s'),
  430. ];
  431. $id = $m_srq->insertData($data);
  432. Log::write('id:' . $id, 'shouhuan');
  433. if (empty($id)) {
  434. return false;
  435. }
  436. return true;
  437. }
  438. /**
  439. * 保存手环信息
  440. *
  441. * @return void
  442. * @author wj
  443. * @date 2023-08-14
  444. */
  445. public function saveshinfo($facility_id, $device_id_code, $data, $original_str)
  446. {
  447. $command = $data[0];
  448. $infoarr = $this->gettsendcommandinfo($command);
  449. if (!$infoarr) {
  450. throw new \Exception("无对应命令数据:" . $command);
  451. }
  452. $data = $this->getdata($data, $data[0], $infoarr);
  453. $m_sri = new ShoneReceiveInfoModel();
  454. $m_srir = new ShoneReceiveInfoRecode();
  455. $data = $data['info_content'];
  456. $data['facility_id'] = $facility_id;
  457. $data['device_id_code'] = $device_id_code;
  458. //$m_sri处理
  459. $sriwhere = [
  460. 'facility_id' => $facility_id,
  461. 'device_id_code' => $device_id_code,
  462. ];
  463. $sriinfo = $m_sri->getInfo($sriwhere);
  464. unset($data['command']);
  465. if (empty($sriinfo)) {
  466. $sriinsertData = $data;
  467. $sriinsertData['createtime'] = date('Y-m-d H:i:s');
  468. $sriid = $m_sri->insertData($sriinsertData);
  469. if (empty($sriid)) {
  470. throw new \Exception("设备信息添加失败:" . $device_id_code);
  471. }
  472. } else {
  473. unset($data['device_id_code']);
  474. unset($data['facility_id']);
  475. $sriid = $sriinfo['id'];
  476. $sriupdateData = $data;
  477. $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
  478. $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
  479. if (empty($row)) {
  480. throw new \Exception("设备信息修改失败:" . $device_id_code);
  481. }
  482. }
  483. //m_srir处理
  484. $data['original_str'] = $original_str;
  485. $sririnsertData = $data;
  486. $sririnsertData['createtime'] = date('Y-m-d H:i:s');
  487. $sririd = $m_srir->insertData($sririnsertData);
  488. if (empty($sririd)) {
  489. throw new \Exception("设备历史信息添加失败:" . $device_id_code);
  490. }
  491. }
  492. /**
  493. * 获取接收信息队列列表
  494. *
  495. * @param [type] $page
  496. * @param [type] $size
  497. * @return void
  498. * @author wj
  499. * @date 2023-08-14
  500. */
  501. public function getreceivequeuelist($page = null, $size = null)
  502. {
  503. $m_srq = new ShoneReceiveQueueModel();
  504. $where = ['status' => 0];
  505. $size = empty($size) ? 20 : $size;
  506. if (empty($page)) {
  507. $count = $m_srq->getList($where, 'count');
  508. $totalpage = ceil($count / $size);
  509. $data = ['totalpage' => $totalpage, 'size' => $size];
  510. return $data;
  511. } else {
  512. $list = $m_srq->getList($where, '*', $page, $size, 'id asc');
  513. if (empty($list)) {
  514. Log::write("手环接收信息队列-无数据", 'shouhuan');
  515. return false;
  516. }
  517. $list = $list->toArray();
  518. $ids = array_column($list, 'id');
  519. $updatecount = $m_srq->updateinfo(['id' => ['in', $ids], 'status' => 0], ['status' => 1]);
  520. if ($updatecount != count($ids)) {
  521. Log::write("手环接收信息队列-部分未修改", 'shouhuan');
  522. Log::write("count:" . count($ids) . " update count:" . $updatecount, 'shouhuan');
  523. }
  524. if (empty($updatecount)) {
  525. Log::write("手环接收信息队列-无修改数据", 'shouhuan');
  526. return false;
  527. }
  528. return $list;
  529. }
  530. }
  531. /**
  532. * 获取发送信息队列列表
  533. *
  534. * @param [type] $page
  535. * @param [type] $size
  536. * @return void
  537. * @author wj
  538. * @date 2023-08-14
  539. */
  540. public function getsendqueuelist($device_id_code, $page = null, $size = null)
  541. {
  542. $m_ssq = new ShoneSendQueueModel();
  543. $where = ['status' => 0, 'device_id_code' => $device_id_code];
  544. $size = empty($size) ? 20 : $size;
  545. if (empty($page)) {
  546. $count = $m_ssq->getList($where, 'count');
  547. $totalpage = ceil($count / $size);
  548. $data = ['totalpage' => $totalpage, 'size' => $size];
  549. return $data;
  550. } else {
  551. $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc');
  552. if (empty($list)) {
  553. Log::write("手环发送信息队列-无数据", 'shouhuan');
  554. return false;
  555. }
  556. $list = $list->toArray();
  557. $ids = array_column($list, 'id');
  558. $updatecount = $m_ssq->updateinfo(['id' => ['in', $ids], 'status' => 0], ['status' => 1]);
  559. if ($updatecount != count($ids)) {
  560. Log::write("手环发送信息队列-部分未修改", 'shouhuan');
  561. Log::write("count:" . count($ids) . " update count:" . $updatecount, 'shouhuan');
  562. }
  563. if (empty($updatecount)) {
  564. Log::write("手环发送信息队列-无修改数据", 'shouhuan');
  565. return false;
  566. }
  567. return $list;
  568. }
  569. }
  570. public function sendmsgsuccess($item)
  571. {
  572. $m_ssq = new ShoneSendQueueModel();
  573. $m_ssl = new ShoneSendListModel();
  574. $ssqid = $item['id'];
  575. $list_id = $item['list_id'];
  576. $sslwhere = ['id' => $list_id, 'is_send' => 0];
  577. $sslinfo = $m_ssl->getInfo($sslwhere);
  578. if ($sslinfo) {
  579. $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
  580. }
  581. $ssqwhere = ['id' => $ssqid];
  582. $ssqinfo = $m_ssq->getInfo($ssqwhere);
  583. if ($ssqinfo) {
  584. $m_ssq->deleteinfobyid($ssqid);
  585. }
  586. }
  587. }