SouhuanAnalysis.php 53 KB

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