1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681 |
- <?php
- namespace app\common\server;
- use app\common\model\FacilityModel;
- use app\common\model\ShalarmlistModel;
- use app\common\model\ShinfoModel;
- use app\common\model\ShreceivequeueModel;
- use app\common\model\ShreceiveredocdeModel;
- use app\common\model\ShsendlistModel;
- use app\common\model\ShsendqueueModel;
- use think\facade\Log;
- /**
- * 手环字符串解析
- *
- * @author wj
- * @date 2023-08-11
- */
- class SouhuanAnalysis
- {
- /**
- * 校验长度
- *
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function checklen($len, $str)
- {
- $strlen = mb_strlen($str);
- if ($len != $strlen) {
- Log::write("长度校验失败: len:" . $len . " str:" . $str, 'shouhuan');
- return false;
- }
- return true;
- }
- /**
- * 获取信息 整个信息解析
- * 并验证是否可解析 无解析命令配置 不报错
- *
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function getline($str, $field = null, $checkconfig = true)
- {
- //[厂商*设备 ID*内容长度*内容]
- $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
- preg_match($preg, $str, $match);
- if (empty($match)) {
- Log::write("解析失败:" . $str, 'shouhuan');
- throw new \Exception('解析失败:' . $str);
- }
- unset($match[0]);
- $data = array_values($match);
- $sandom_string = $data[0]; //厂商 随机字符串
- $device_id_code = $data[1]; //设置id编码
- $content_len = $data[2]; //内容长度
- $content_str = $data[3]; //内容字符串
- $content_len = $this->analysislen($content_len);
- $result = $this->checklen($content_len, $content_str);
- if (empty($result)) {
- $errmsg = '内容位数错误';
- Log::write($errmsg . ":" . $str, 'shouhuan');
- throw new \Exception($errmsg);
- }
- $data = [
- 'device_id_code' => $device_id_code,
- 'content_len' => $content_len,
- 'content_str' => $content_str,
- ];
- $contentarr = $this->getcommandcontent($content_str);
- $command = $contentarr['command'];
- $data['command'] = $command;
- $data['content_arr'] = $contentarr['content'];
- if ($checkconfig) {
- $config = $this->getreceivecommandinfo($command);
- $data['config'] = $config;
- if (!$config) {
- $errmsg = '命令无解析配置 command:' . $command;
- Log::write($errmsg, 'shouhuan');
- return false;
- }
- }
- if (isset($field)) {
- return $data[$field];
- }
- return $data;
- }
- /**
- * 解析UD命令
- *
- * @param [type] $str
- * @return void
- * @author wj
- * @date 2023-08-23
- */
- public function getmacforUD($str, $device_id_code)
- {
- $data = explode(",", $str);
- $is_gps = $data[3];
- $is_success = false;
- $accesstype = -1;
- $apikey = "67019de565fb4aa2a0e101b70ae3fd91";
- if ('V' == $is_gps) {
- $jzcount = $data['17'];
- $jzlist = [];
- $mcc = $data['19'];
- $mnc = $data['20'];
- $jzindex = 21;
- for ($i = 0; $i < $jzcount; $i++) {
- $item = [
- 'area_code' => $data[$jzindex],
- 'jz_code' => $data[$jzindex + 1],
- 'signal_strength' => $data[$jzindex + 2],
- ];
- $jzindex += 3;
- $jzlist[] = $item;
- }
- $wificount = $data[$jzindex];
- $wifiindex = $jzindex + 1;
- $wifilist = [];
- for ($i = 0; $i < $wificount; $i++) {
- $item = [
- 'name' => $data[$wifiindex],
- 'mac' => $data[$wifiindex + 1],
- 'signal_strength' => $data[$wifiindex + 2],
- ];
- $wifiindex += 3;
- $wifilist[] = $item;
- }
- $bts = "";
- $nearbts_arr = [];
- $nearbts = "";
- $macs_arr = [];
- $macs = "";
- foreach ($jzlist as $key => $value) {
- if ($key === 0) {
- $bts = $mcc . "," . $mnc . "," . $value['area_code'] . "," . $value['jz_code'] . "," . $value['signal_strength'];
- } else {
- $nearbts_arr[] = $mcc . "," . $mnc . "," . $value['area_code'] . "," . $value['jz_code'] . "," . $value['signal_strength'];
- }
- }
- $nearbts = join("|", $nearbts_arr);
- foreach ($wifilist as $key => $value) {
- $macs_arr[] = $value['mac'] . "," . $value['signal_strength'];
- }
- $macs = join("|", $macs_arr);
- $accesstype = 1;
- $url = "http://apilocate.amap.com/position?macs=" . $macs . "&bts=" . $bts . "&nearbts=" . $nearbts . "&network=GSM&cdma=0&accesstype=" . $accesstype . "&output=json&key=" . $apikey;
- $result = curl_request($url);
- if ('10000' === $result['infocode']) {
- $resultdata = $result['result'];
- $locations = explode(",", $resultdata['location']);
- $use_gps_long = $locations[0];
- $use_gps_lat = $locations[1];
- $address_desc = $resultdata['desc'];
- $accesstype = 2;
- $is_success = true;
- } else {
- Log::write($url, 'shouhuan');
- Log::write($result, 'shouhuan');
- $accesstype = -2;
- }
- }
- if (!$is_success) {
- $use_gps_long = $data[6];
- $use_gps_lat = $data[4];
- $gps_str = $use_gps_long . "," . $use_gps_lat;
- $url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=" . $gps_str . "&coordsys=gps&output=JSON&key=" . $apikey;
- $result = curl_request($url);
- if ('10000' == $result['infocode']) {
- $locations = $result['locations'];
- $url = "https://restapi.amap.com/v3/geocode/regeo?location=" . $locations . "&key=" . $apikey;
- $result = curl_request($url);
- if ('10000' == $result['infocode']) {
- $address_desc = $result['regeocode']['formatted_address'];
- $is_success = true;
- $accesstype = 0; //未接入智能硬件定位
- } else {
- Log::write($url, 'shouhuan');
- Log::write($result, 'shouhuan');
- }
- } else {
- Log::write($url, 'shouhuan');
- Log::write($result, 'shouhuan');
- }
- }
- if (!$is_success) {
- $accesstype = -2;
- $use_gps_long = $data[6];
- $use_gps_lat = $data[4];
- $address_desc = "";
- }
- $data = [
- 'is_gps' => $is_gps == "A" ? 1 : 0,
- 'address_desc' => $address_desc,
- 'accesstype' => $accesstype,
- 'use_gps_long' => $use_gps_long,
- 'use_gps_lat' => $use_gps_lat,
- ];
- return $data;
- }
- /**
- * 内容解析
- *
- * @param [type] $content_str
- * @return void
- * @author wj
- * @date 2023-08-19
- */
- public function getcommandcontent($content_str)
- {
- $content = explode(',', $content_str);
- $command = $content[0];
- unset($content[0]);
- $data = [
- 'command' => $command,
- 'content' => $content,
- ];
- return $data;
- }
- /**
- * 解析终端发送命令长度
- *
- * @param [type] $len
- * @return void
- * @author wj
- * @date 2023-08-18
- */
- public function analysislen($len)
- {
- $len = base_convert($len, 16, 10);
- return $len;
- }
- /**
- * 获取平台发送命令信息
- *
- * @param [type] $command
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function getsendcommandinfo($command)
- {
- $command = strtoupper($command);
- $data = [
- //平台发送
- 'ZONE' => [
- 'content' => [
- 'command' => 'ZONE',
- 'zone' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'UPLOAD' => [
- 'content' => [
- 'command' => 'UPLOAD',
- 'interval' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'CR' => [
- 'content' => [
- 'command' => 'CR',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'SLEEPTIME' => [
- 'content' => [
- 'command' => 'CR',
- ],
- 'kind' => 'send',
- 'content_custom' => true,
- 'need_back' => 1,
- ],
- 'FALLDOWN' => [
- 'content' => [
- 'command' => 'FALLDOWN',
- 'is_open' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'FACTORY' => [
- 'content' => [
- 'command' => 'FACTORY',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'SOS' => [
- 'content' => [
- 'command' => 'SOS',
- ],
- 'kind' => 'send',
- 'content_custom' => true,
- 'need_back' => 1,
- ],
- 'VERNO' => [
- 'content' => [
- 'command' => 'VERNO',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'LOWBAT' => [
- 'content' => [
- 'command' => 'LOWBAT',
- 'is_open' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'POWEROFF' => [
- 'content' => [
- 'command' => 'POWEROFF',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'LSSET' => [
- 'content' => [
- 'command' => 'LSSET',
- 'content_custom' => true,
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'PEDO' => [
- 'content' => [
- 'command' => 'PEDO',
- 'is_open' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'PHL' => [
- 'content' => [
- 'command' => 'PEDO',
- ],
- 'kind' => 'send',
- 'content_custom' => true,
- 'need_back' => 1,
- ],
- 'REMIND' => [
- 'content' => [
- 'command' => 'REMIND',
- ],
- 'kind' => 'send',
- 'content_custom' => true,
- 'need_back' => 1,
- ],
- 'SILENCETIME2' => [
- 'content' => [
- 'command' => 'SILENCETIME2',
- ],
- 'kind' => 'send',
- 'content_custom' => true,
- 'need_back' => 1,
- ],
- 'BOOTOFF' => [
- 'content' => [
- 'command' => 'BOOTOFF',
- 'is_open' => '',
- 'bootup_time' => '',
- 'shutdown_time' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- 'REMOVE' => [
- 'content' => [
- 'command' => 'REMOVE',
- 'is_open' => '',
- ],
- 'kind' => 'send',
- 'need_back' => 1,
- ],
- //平台回复
- 'LK' => [
- 'content' => [
- 'command' => 'LK',
- ],
- 'kind' => 'back',
- ],
- 'PING' => [
- 'content' => [
- 'command' => 'PING',
- 'isbind' => 1,
- ],
- 'kind' => 'back',
- ],
- 'KA' => [
- 'content' => [
- 'command' => 'KA',
- ],
- 'kind' => 'back',
- ],
- 'TEMP' => [
- 'content' => [
- 'command' => 'temp',
- ],
- 'kind' => 'back',
- ],
- 'BPHRT' => [
- 'content' => [
- 'command' => 'bphrt',
- ],
- 'kind' => 'back',
- ],
- 'HEART' => [
- 'content' => [
- 'command' => 'heart',
- ],
- 'kind' => 'back',
- ],
- 'BLOOD' => [
- 'content' => [
- 'command' => 'blood',
- ],
- 'kind' => 'back',
- ],
- 'OXYGEN' => [
- 'content' => [
- 'command' => 'oxygen',
- ],
- 'kind' => 'back',
- ],
- 'AL' => [
- 'content' => [
- 'command' => 'AL',
- ],
- 'kind' => 'back',
- ],
- ];
- if (!isset($data[$command])) {
- return false;
- }
- return $data[$command];
- }
- /**
- * 设置可解析数据 终端发送信息解析
- *
- * @param [type] $command
- * @return void
- * @author wj
- * @date 2023-08-18
- * analysis_digit 解析位数
- * content 解析对应字段
- * kind 类型 tsend 平台发送 tback平台回复
- * need_back 是否需回复 1是 0否 没设置为0
- * command 未设置则和键值相同
- */
- public function getreceivecommandinfo($command)
- {
- $command = strtoupper($command);
- $data = [
- 'UD' => [
- 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
- 'content' => [
- 'date',
- 'time',
- 'gps_lat',
- 'gps_long',
- 'speed',
- 'direction',
- 'poster',
- 'electric_quantity',
- 'step_number',
- 'roll_number',
- 'terminal_status',
- ],
- 'kind' => 'tsend',
- 'need_back' => 0,
- ],
- 'AL' => [
- 'analysis_digit' => [1, 2, 4, 6, 8, 9, 10, 13, 14, 15, 16],
- 'content' => [
- 'date',
- 'time',
- 'gps_lat',
- 'gps_long',
- 'speed',
- 'direction',
- 'poster',
- 'electric_quantity',
- 'step_number',
- 'roll_number',
- 'terminal_status',
- ],
- 'kind' => 'tsend',
- 'need_back' => 0,
- ],
- 'KA' => [
- 'analysis_digit' => [1, 2, 3, 4],
- 'content' => [
- 'date',
- 'step_number',
- 'roll_number',
- 'electric_quantity',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- ],
- 'LK' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tsend',
- 'need_back' => 1,
- ],
- 'BPHRT' => [
- 'analysis_digit' => [1, 2, 3],
- 'content' => [
- 'blood_height_pressure',
- 'blood_low_pressure',
- 'heart_rate',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- 'command' => 'bphrt',
- ],
- 'TEMP' => [
- 'analysis_digit' => [1],
- 'content' => [
- 'temp',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- 'command' => 'temp',
- ],
- 'HEART' => [
- 'analysis_digit' => [1],
- 'content' => [
- 'heart_rate',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- 'command' => 'heart',
- ],
- 'BLOOD' => [
- 'analysis_digit' => [1, 2],
- 'content' => [
- 'blood_height_pressure',
- 'blood_low_pressure',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- 'command' => 'blood',
- ],
- 'OXYGEN' => [
- 'analysis_digit' => [1],
- 'content' => [
- 'oxygen',
- ],
- 'kind' => 'tsend',
- 'need_back' => 1,
- 'command' => 'oxygen',
- ],
- //终端回复
- //ZONE UPLOAD CR SLEEPTIME FALLDOWN FACTORY SOS VERNO LOWBAT
- //POWEROFF LSSET PEDO PHL REMIND SILENCETIME2 BOOTOFF REMOVE
- 'ZONE' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'UPLOAD' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'CR' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'SLEEPTIME' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'FALLDOWN' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'SOS' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'VERNO' => [
- 'analysis_digit' => [1],
- 'content' => ['version'],
- 'kind' => 'tback',
- ],
- 'LOWBAT' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'POWEROFF' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'LSSET' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'PEDO' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'PHL' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'REMIND' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'SILENCETIME2' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'BOOTOFF' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'REMOVE' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- ];
- if (!isset($data[$command])) {
- return false;
- }
- return $data[$command];
- }
- /**
- * 获取回复权重
- *
- * @param [type] $command
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function getsendweight($command)
- {
- $weight = -1;
- switch ($command) {
- case 'POWEROFF':
- //关机
- $weight = 99;
- break;
- case 'RESET':
- //重启
- $weight = 98; //硬件未测试
- break;
- //平台回复命令
- case 'AL':
- case 'KA':
- case 'LK':
- case 'PING':
- case 'BPHRT':
- case 'TEMP':
- case 'HEART':
- case 'BLOOD':
- case 'OXYGEN':
- $weight = 1;
- break;
- //平台发送命令
- case 'ZONE':
- case 'UPLOAD':
- case 'CR':
- case 'SLEEPTIME':
- case 'FALLDOWN':
- case 'FACTORY':
- case 'SOS':
- case 'VERNO':
- case 'LOWBAT':
- case 'POWEROFF':
- case 'LSSET':
- case 'PEDO':
- case 'PHL':
- case 'REMIND':
- case 'SILENCETIME2':
- case 'BOOTOFF':
- case 'REMOVE':
- $weight = 0;
- break;
- default:
- $weight = -1;
- break;
- }
- if ($weight < 0) {
- //不需设置 发送信息
- return false;
- }
- return $weight;
- }
- /**
- * 获取数据值
- *
- * @param [type] $data
- * @param [type] $command
- * @return void
- * @author wj
- * @date 2023-08-14
- */
- public function getdata($config, $content_arr)
- {
- $analysis_digit = $config['analysis_digit'];
- $config_content = $config['content'];
- $data = [];
- if (is_array($analysis_digit)) {
- foreach ($analysis_digit as $key => $value) {
- if (isset($content_arr[$value])) {
- if (isset($config_content[$key])) {
- $item_key = $config_content[$key];
- $item_value = $content_arr[$value];
- $data[$item_key] = $item_value;
- }
- }
- }
- }
- if (isset($data['time'])) {
- $time = $data['time'];
- $data['time'] = $this->gettime($time);
- }
- if (isset($data['date'])) {
- $date = $data['date'];
- $data['date'] = $this->getdate($date);
- }
- if (isset($data['terminal_status'])) {
- $terminalstatus = $data['terminal_status'];
- $terminal_status_arr = $this->getterminalstatus($terminalstatus);
- //unset($data['terminal_status']);
- $data['status_type'] = $terminal_status_arr['status_type'];
- $data['alarm_type'] = $terminal_status_arr['alarm_type'];
- }
- return $data;
- }
- /**
- * 时间解析
- *
- * @param [type] $time
- * @return void
- * @author wj
- * @date 2023-08-19
- */
- public function gettime($time)
- {
- $hour = substr($time, 0, 2);
- $minute = substr($time, 2, 2);
- $second = substr($time, 4, 2);
- $time = $hour . ':' . $minute . ':' . $second;
- return $time;
- }
- /**
- * 日期解析
- *
- * @return void
- * @author wj
- * @date 2023-08-15
- */
- public function getdate($date)
- {
- $day = substr($date, 0, 2);
- $month = substr($date, 2, 2);
- $year = substr($date, 4, 2);
- $year1 = date('Y');
- $year2 = substr($year1, 0, 2);
- $year = $year2 . $year;
- if ($year != $year1) {
- $day = date('Y-m-d');
- } else {
- $day = $year . '-' . $month . '-' . $day;
- }
- return $day;
- }
- /**
- * 解析终端状态
- *
- * @param [type] $terminal_status
- * @return void
- * @author wj
- * @date 2023-08-19
- */
- public function getterminalstatus($terminal_status)
- {
- $terminal_status = str_split($terminal_status);
- foreach ($terminal_status as $key => $value) {
- $value = base_convert($value, 16, 2);
- $value = str_pad($value, 4, "0", STR_PAD_LEFT);
- $terminal_status[$key] = $value;
- }
- $terminal_status_str = implode("", $terminal_status);
- $mid = mb_strlen($terminal_status_str) / 2;
- $alarm = substr($terminal_status_str, 0, $mid);
- $first_num = strpos($alarm, '1');
- $end_num = strrpos($alarm, '1');
- if ($first_num != $end_num) {
- Log::write("状态解析错误:报警:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
- }
- if ($first_num) {
- //16:SOS报警;17:低电报警;20:手环拆除报警;21:跌倒报警;22:心率异常报警
- $first_num = $mid - $first_num + ($mid - 1);
- $alarm_type = $first_num;
- }
- $status = substr($terminal_status_str, $mid);
- $first_num = strpos($status, '1');
- $end_num = strrpos($status, '1');
- if ($first_num != $end_num) {
- Log::write("状态解析错误:状态:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
- }
- if ($first_num) {
- $first_num = $mid - 1 - $first_num;
- //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
- $status_type = $first_num + 1;
- }
- $data = [
- 'alarm_type' => isset($alarm_type) ? $alarm_type : false,
- 'status_type' => isset($status_type) ? $status_type : false,
- ];
- return $data;
- }
- // /**
- // * 解析报警
- // *
- // * @param [type] $first_num
- // * @return void
- // * @author wj
- // * @date 2023-08-19
- // */
- // public function getalarm($first_num)
- // {
- // $data = [
- // 16 => 'SOS报警',
- // 17 => '低电报警',
- // 18 => '出围栏报警',
- // 19 => '进围栏报警',
- // 20 => '手环拆除报警',
- // 21 => '跌倒报警',
- // 22 => '心率异常报警',
- // ];
- // if (isset($data[$first_num])) {
- // return $data[$first_num];
- // }
- // return false;
- // }
- // /**
- // * 解析状态
- // *
- // * @param [type] $first_num
- // * @return void
- // * @author wj
- // * @date 2023-08-19
- // */
- // public function getstatus($first_num)
- // {
- // $data = [
- // 0 => '低电状态',
- // 1 => '出围栏状态',
- // 2 => '进围栏状态',
- // 3 => '手环戴上取下状态',
- // 4 => '手表运行静止状态',
- // ];
- // if (isset($data[$first_num])) {
- // return $data[$first_num];
- // }
- // return false;
- // }
- // ̄へ ̄-----------内容解析结束---------------- ̄へ ̄
- // ̄へ ̄-----------业务处理开始---------------- ̄へ ̄
- /**
- * 获取设别表id
- *
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function getfacilityid($device_id_code)
- {
- $m_f = new FacilityModel();
- $where = ['code' => $device_id_code, 'status' => 1];
- $finfo = $m_f->getInfo($where, ['id']);
- if (empty($finfo)) {
- throw new \Exception("设备信息错误:" . $device_id_code);
- return false;
- }
- $id = $finfo['id'];
- return $id;
- }
- public function geshinfoid($device_id_code)
- {
- $m_si = new ShinfoModel();
- $where = ['device_id_code' => $device_id_code];
- $siinfo = $m_si->getInfo($where, ['id']);
- if (empty($siinfo)) {
- throw new \Exception("设备信息错误:" . $device_id_code);
- return false;
- }
- $id = $siinfo['id'];
- return $id;
- }
- /**
- * 保存接收信息队列
- *
- * @param [type] $data
- * @return void
- * @author wj
- * @date 2023-08-14
- */
- public function savereceivequeue($data, $device_id_code)
- {
- $m_srq = new ShreceivequeueModel();
- $facility_id = $this->getfacilityid($device_id_code);
- $data = [
- 'msg' => $data,
- 'createtime' => date('Y-m-d H:i:s'),
- 'device_id_code' => $device_id_code,
- 'facility_id' => $facility_id,
- ];
- $id = $m_srq->insertData($data);
- if (empty($id)) {
- return false;
- }
- return true;
- }
- /**
- * 获取接收信息队列列表
- *
- * @param [type] $page
- * @param [type] $size
- * @return void
- * @author wj
- * @date 2023-08-14
- */
- public function getreceivequeuelist($page = null, $size = null)
- {
- $m_srq = new ShreceivequeueModel();
- $where = ['status' => 0];
- $size = empty($size) ? 20 : $size;
- if (empty($page)) {
- $count = $m_srq->getList($where, 'count');
- $totalpage = ceil($count / $size);
- $data = ['totalpage' => $totalpage, 'size' => $size];
- return $data;
- } else {
- $list = $m_srq->getList($where, '*', $page, $size, 'id asc');
- if (empty($list)) {
- //Log::write("手环接收信息队列-无数据", 'shouhuan');
- return false;
- }
- $list = $list->toArray();
- foreach ($list as $key => $value) {
- $id = $value['id'];
- if (1 != $value['status']) {
- $row = $m_srq->updateinfo(['id' => $id], ['status' => 1]);
- if (1 != $row) {
- $msg = "Shreceivequeue id:" . $id . " 修改失败";
- Log::write($msg, 'shouhuan');
- unset($list[$key]);
- }
- }
- }
- return $list;
- }
- }
- /**
- * 获取发送信息队列列表
- *
- * @param [type] $page
- * @param [type] $size
- * @return void
- * @author wj
- * @date 2023-08-14
- */
- public function getsendqueuelist($device_id_code, $page = null, $size = null)
- {
- $m_ssq = new ShsendqueueModel();
- $where = ['status' => 0, 'device_id_code' => $device_id_code];
- $size = empty($size) ? 20 : $size;
- if (empty($page)) {
- $count = $m_ssq->getList($where, 'count');
- $totalpage = ceil($count / $size);
- $data = ['totalpage' => $totalpage, 'size' => $size];
- return $data;
- } else {
- $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc');
- if (empty($list)) {
- Log::write("手环发送信息队列-无数据", 'shouhuan');
- return false;
- }
- $list = $list->toArray();
- foreach ($list as $key => $value) {
- $id = $value['id'];
- if (1 != $value['status']) {
- $row = $m_ssq->updateinfo(['id' => $id], ['status' => 1]);
- if (1 != $row) {
- $msg = "Shsendqueue id:" . $id . " 修改失败";
- Log::write($msg, 'shouhuan');
- unset($list[$key]);
- }
- }
- }
- return $list;
- }
- }
- /**
- * 保存手环信息
- *
- * @return void
- * @author wj
- * @date 2023-08-14
- */
- public function saveshinfo($facility_id, $device_id_code, $data, $queue_data)
- {
- $original_str = $queue_data['msg'];
- $command = $data['command'];
- $content_arr = $data['content_arr'];
- $config = $data['config'];
- $data = $this->getdata($config, $content_arr);
- $m_sri = new ShinfoModel();
- $m_srir = new ShreceiveredocdeModel();
- $m_sa = new ShalarmlistModel();
- if ('AL' == $command) {
- //手环警报信息
- $data['alarm_from_type'] = 1;
- $data['device_id_code'] = $device_id_code;
- $sshinfo_id = $this->geshinfoid($device_id_code);
- $data['shinfo_id'] = $sshinfo_id;
- $data['facility_id'] = $facility_id;
- $data['createtime'] = date('Y-m-d H:i:s');
- $said = $m_sa->insertData($data);
- if (empty($said)) {
- $msg = "报警信息添加失败:" . $original_str;
- Log::write($msg, 'shouhuan');
- throw new \Exception($msg);
- }
- } else {
- //手环基础信息修改
- $sriData = $data;
- if ($command == "UD") {
- $uddata = $this->getmacforUD($original_str, $device_id_code);
- if ($uddata) {
- $sriData = array_merge($sriData, $uddata);
- }
- }
- $sriwhere = [
- 'facility_id' => $facility_id,
- 'device_id_code' => $device_id_code,
- ];
- $sriinfo = $m_sri->getInfo($sriwhere);
- if (empty($sriinfo)) {
- $sriData['device_id_code'] = $device_id_code;
- $sriData['facility_id'] = $facility_id;
- $sriData['online_statis'] = 1;
- $sriinsertData = $sriData;
- $sriinsertData['createtime'] = date('Y-m-d H:i:s');
- $sriinsertData['updatetime'] = date('Y-m-d H:i:s');
- $sriinsertData = $m_sri->formatinfo($sriinsertData);
- $sriid = $m_sri->insertData($sriinsertData);
- if (empty($sriid)) {
- throw new \Exception("设备信息添加失败:" . $device_id_code);
- }
- } else {
- $sriinfo = $sriinfo->toArray();
- $sriid = $sriinfo['id'];
- $sriupdateData = (array) $sriData;
- foreach ($sriupdateData as $key => $value) {
- if (in_array($key, array_keys($sriinfo))) {
- if ($sriinfo[$key] == $value) {
- unset($sriupdateData[$key]);
- }
- } else {
- unset($sriupdateData[$key]);
- }
- }
- if (count($sriupdateData) > 0) {
- $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
- $sriupdateData = $m_sri->formatinfo($sriupdateData);
- $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
- if (empty($row)) {
- $errmsg = "设备信息修改失败:" . $device_id_code;
- Log::write($errmsg);
- Log::write($sriupdateData, 'shouhuan');
- throw new \Exception($errmsg);
- }
- }
- }
- }
- //接收信息历史添加
- $sririnsertData['facility_id'] = $facility_id;
- $sririnsertData['device_id_code'] = $device_id_code;
- $sririnsertData['original_str'] = $original_str;
- $sririnsertData['content'] = json_encode($data);
- if (isset($data['date'])) {
- $sririnsertData['date'] = $data['date'];
- }
- if (isset($data['time'])) {
- $sririnsertData['time'] = $data['time'];
- }
- $sririnsertData['command'] = $command;
- $sririnsertData['createtime'] = date('Y-m-d H:i:s');
- $sririnsertData['receive_time'] = $queue_data['createtime'];
- if ('tback' == $config['kind']) {
- $is_back_msg = true;
- $sririnsertData['is_back_msg'] = 0;
- } else {
- $is_back_msg = false;
- $sririnsertData['is_back_msg'] = 1;
- }
- $sririd = $m_srir->insertData($sririnsertData);
- if (empty($sririd)) {
- throw new \Exception("设备历史信息添加失败:" . $device_id_code);
- }
- if ($is_back_msg) {
- //处理回复信息
- $m_ssl = new ShsendlistModel();
- $where = [
- 'is_success' => 0,
- 'is_send' => 1,
- 'command' => $command,
- ];
- $sslinfo = $m_ssl->getInfo($where);
- if ($sslinfo) {
- //发送成功状态回写
- $sslid = $sslinfo['id'];
- $sslupdateData = ['is_success' => 1];
- $m_ssl->updateinfo(['id' => $sslid], $sslupdateData);
- $srirupdateData = ['send_list_id' => $sslid];
- $m_srir->updateinfo(['id' => $sririd], $srirupdateData);
- }
- }
- return true;
- }
- /**
- * 创建发送信息
- *
- * @param [type] $data
- * @param [type] $command
- * @return void
- * @author wj
- * @date 2023-08-14
- * $data =['command'=>'','content_arr'=>[],]
- */
- public function createsendmsg($facility_id, $device_id_code, $data, $param = [])
- {
- $command = $data['command'];
- $infoarr = $this->getsendcommandinfo($command);
- if (!$infoarr) {
- // Log::write("无对应命令数据1:" . $command, 'shouhuan');
- // Log::write($data, 'shouhuan');
- return false;
- }
- $content_arr = $data['content_arr'];
- $config = $infoarr;
- $m_ssl = new ShsendlistModel();
- $commandxy = "3G";
- //仅创建
- if ($command == 'AL') {
- $msg_type = 1;
- } else {
- $msg_type = 0;
- }
- if ('back' == $config['kind']) {
- $content = $config['content'];
- $content = implode(',', $content);
- $send_type = 0;
- $is_success = 1;
- }
- if ('send' == $config['kind']) {
- $content = $config['content'];
- $useconmmand = $content['command'];
- $content_custom = isset($config['content_custom']) ? $config['content_custom'] : false;
- $need_back = $config['need_back'];
- if ($content_custom) {
- $functionname = 'get' . $command . "Commandinfo";
- if (method_exists($this, $functionname)) {
- $content = $useconmmand . "," . $this->$functionname($param);
- } else {
- throw new \Exception($command . "自定义方法不存在");
- }
- } else {
- $content = implode(',', $content_arr);
- }
- $send_type = 1;
- if ($need_back) {
- $is_success = 0;
- } else {
- $is_success = 1;
- }
- }
- $weight = $this->getsendweight($command);
- $msgdata = [
- 0 => $commandxy,
- 'device_id_code' => $device_id_code,
- 'len' => str_pad(strtoupper(dechex(mb_strlen($content))), 4, "0", STR_PAD_LEFT),
- 'content' => $content,
- ];
- $msg = '[' . implode('*', $msgdata) . ']';
- $sslInsertData = [
- 'facility_id' => $facility_id,
- 'device_id_code' => $device_id_code,
- 'command' => $command,
- 'send_msg' => $msg,
- 'send_weight' => $weight,
- 'send_time' => date('Y-m-d H:i:s'),
- 'is_success' => $is_success,
- 'send_type' => $send_type,
- ];
- $sslid = $m_ssl->insertData($sslInsertData);
- if (empty($sslid)) {
- throw new \Exception("发送数据添加失败");
- }
- $data = [
- 'msg' => $msg,
- 'weight' => $weight,
- 'createtime' => date('Y-m-d H:i:s'),
- 'facility_id' => $facility_id,
- 'device_id_code' => $device_id_code,
- 'send_list_id' => $sslid,
- 'msg_type' => $msg_type,
- ];
- $m_ssq = new ShsendqueueModel();
- $id = $m_ssq->insertData($data);
- if (empty($id)) {
- throw new \Exception("发送队列数据添加失败");
- }
- return true;
- }
- /**
- * 创建sos电话设置命令
- * 最多三个电话
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function getSOSCommandinfo($param)
- {
- if (count($param) <= 0) {
- throw new \Exception("未设置电话");
- }
- if (count($param) > 3) {
- throw new \Exception("设置电话数量过多");
- }
- foreach ($param as $key => $value) {
- //判断是否为手机号
- //暂不处理
- }
- $info = implode(",", $param);
- return $info;
- }
- /**
- * 设置跌倒灵敏度命令
- * [3G*358800006072996*0009*LSSET,3+6]
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function getLSSETCommandinfo($param)
- {
- if (2 != count($param)) {
- throw new \Exception("LSSET参数错误");
- }
- foreach ($param as $key => $value) {
- if (!is_numeric($value)) {
- throw new \Exception("LSSET参数错误:" . $value);
- }
- }
- $info = $param[0] . "+" . $param[1];
- return $info;
- }
- /**
- * 设置电话本
- * 最多10条
- *[{telno=>'',name=>''}]
- * @param [type] $param
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function getPHLCommaninfo($param)
- {
- if (50 < count($param)) {
- throw new \Exception("PHL参数错误");
- }
- if (0 != count($param) % 2) {
- throw new \Exception("PHL参数长度错误");
- }
- $usedata = [];
- foreach ($param as $key => $value) {
- $telno = $value['telno'];
- $name = $value['name'];
- //电话号码
- if (20 < mb_strlen($telno)) {
- throw new \Exception("PHL电话号码错误:" . $telno);
- }
- $name = $this->srtingToUnicode($name);
- $usedata[] = $telno;
- $usedata[] = $name;
- }
- $info = implode(",", $usedata);
- return $info;
- }
- /**
- * 设置电话本
- * 最多10条
- *{[time=>'',isopen=>'','type'=>'','weeks'=>[]]}
- * weeks 数组 每个1到7 周日到周六
- * 最多设置三个
- * @param [type] $param
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- /*
- 08:10-1-1:闹钟时间 8:10,打开,响铃一次
- 08:10-1-2:闹钟时间 8:10,打开,每天响铃
- 08:10-1-3-0111110:闹钟时间 8:10,打开,自定义周一至周五打开
- 最左边是周日,最右边是周六,即 0111110 的顺序是:周日,周一,周二,周三,周四,周五,周
- 六(1 为打开,0 为关闭),其中周一至周五打开闹钟
- */
- public function getREMINDCommandinfo($param)
- {
- if (3 > count($param)) {
- throw new \Exception("PHL参数错误");
- }
- $usedata = [];
- foreach ($param as $key => $value) {
- $useweeks = ["0", "0", "0", "0", "0", "0", "0"];
- $time = $this->formattime($value['time'], 'H:i');
- $isopen = $value['isopen'];
- $type = $value['type'];
- $weeks = $value['weeks'];
- if (3 == $type) {
- foreach ($weeks as $wkey => $wvalue) {
- if ($wvalue >= 1 && $wvalue <= 7) {
- $useweeks[$wvalue - 1] = '1';
- }
- }
- $useweeks = implode("", $useweeks);
- $type = $useweeks;
- }
- $usedata[] = $time . "-" . $isopen . "-" . $type;
- }
- $usedata = implode(",", $usedata);
- return $usedata;
- }
- /**
- * 静默时间段测试
- *{[starttime=>'',endtime=>'',isopen=>'',weeks=>[]]}
- * weeks 数组 每个1到7 周日到周六
- * 最多设置三个
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- /*
- [DW*334588000000156*0038*SILENCETIME2,7:30-21:10-1-0111110,7:30-21:10-1-0111110,7:
- 30-21:10-0-0111110]
- 设置上课禁用时间段范围,拦截终端的任何来电。格式为“起始时间-结束时间-自定义星期几”, 比
- 如上面的 7:30-21:00-0111110,则表示周一到周五 7:30 到 21:00 之间上课禁用生效,起始时间
- 默认大于结束时间。
- 总开关:0 为关,1 为开
- 关于自定义星期几的规则:最左边是周日,最右边是周六,即 0111110 的顺序是:周日,周一,周
- 二,周三,周四,周五,周六(1 为打开,0 为关闭),所以上面的意思是周一至周五打开上课禁
- 用。
- */
- public function getSILENCETIME2Commandinfo($param)
- {
- if (3 <= count($param)) {
- throw new \Exception("SILENCETIME2参数错误");
- }
- $usedata = [];
- foreach ($param as $key => $value) {
- $useweeks = ["0", "0", "0", "0", "0", "0", "0"];
- //未比较时间大小
- $starttime = $this->formattime($value['starttime'], 'H:i');
- $endtime = $this->formattime($value['endtime'], 'H:i');
- $isopen = $value['isopen'];
- $weeks = $value['weeks'];
- foreach ($weeks as $wkey => $wvalue) {
- if ($wvalue >= 1 && $wvalue <= 7) {
- $useweeks[$wvalue - 1] = '1';
- }
- }
- $useweeks = implode("", $useweeks);
- $usedata[] = $starttime . "-" . $endtime . "-" . $isopen . "-" . $useweeks;
- }
- $usedata = implode(",", $usedata);
- return $usedata;
- }
- /**
- * 睡眠时间段设置
- *{starttime=>'',endtime=>''}
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function getSLEEPTIMECommandinfo($param)
- {
- if (2 > count($param)) {
- throw new \Exception("SLEEPTIME参数错误");
- }
- $starttime = $this->formattime($param['starttime'], 'H:i');
- $endtime = $this->formattime($param['endtime'], 'H:i');
- $usedata = $starttime . "-" . $endtime;
- return $usedata;
- }
- //发送完处理
- /**
- * 发送成功 删除发送数据
- *
- * @param [type] $item
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function sendmsgsuccess($item)
- {
- $m_ssq = new ShsendqueueModel();
- $m_ssl = new ShsendlistModel();
- $m_sal = new ShalarmlistModel();
- $ssqid = $item['id'];
- if (0 === $item['msg_type']) {
- //普通信息
- if ($item['send_list_id']) {
- $list_id = $item['send_list_id'];
- $sslwhere = ['id' => $list_id, 'is_send' => 0];
- $sslinfo = $m_ssl->getInfo($sslwhere);
- if ($sslinfo) {
- $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
- }
- }
- }
- if (1 === $item['msg_type']) {
- //报警信息
- if ($item['send_list_id']) {
- $list_id = $item['send_list_id'];
- $salwhere = ['id' => $list_id, 'is_send' => 0];
- $salinfo = $m_sal->getInfo($salwhere);
- if ($salinfo) {
- $row = $m_sal->updateinfo(['id' => $salinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]);
- }
- }
- }
- $ssqwhere = ['id' => $ssqid];
- $ssqinfo = $m_ssq->getInfo($ssqwhere);
- if ($ssqinfo) {
- $m_ssq->deleteinfobyid($ssqid);
- }
- }
- /**
- * 解析成功 删除接收数据
- *
- * @param [type] $item
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function receivemsgsuccess($item)
- {
- $m_srq = new ShreceivequeueModel();
- $srqid = $item['id'];
- $srqwhere = ['id' => $srqid, 'status' => 1];
- $srqinfo = $m_srq->getInfo($srqwhere);
- if ($srqinfo) {
- $m_srq->deleteinfobyid($srqid);
- }
- }
- /**
- * 初始化手环接收信息解析
- *
- * @return void
- * @author wj
- * @date 2023-08-15
- */
- public function initanalysisShoneReceiveMsg()
- {
- $m_srq = new ShreceivequeueModel();
- $where = ['status' => 1];
- $m_srq->updateinfo($where, ['status' => 0]);
- }
- /**
- * 测试使用
- *
- * @param [type] $data
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function showfortest($data)
- {
- $strdata = $this->getline($data, null, false);
- if (!is_array($strdata)) {
- Log::write($data, 'shouhuan');
- return false;
- }
- $command = strtoupper($strdata['command']);
- $showcommands = [
- 'SOS', 'CR', 'UPLOAD', 'SLEEPTIME', 'ZONE', 'FALLDOWN',
- 'APPLOCK', 'FACTORY', 'VERNO', 'LOWBAT', 'UD', 'POWEROFF', 'MESSAGE',
- 'WALKTIME', 'PEDO', 'PHB', 'PHL', 'REMIND', 'SILENCETIME2', 'BOOTOFF',
- 'REMOVE', 'INFO', 'AL', 'LSSET', 'BTWARNSET',
- ];
- if (in_array($command, $showcommands)) {
- var_dump(date('Y-m-d H:i:s'));
- var_dump($data);
- }
- }
- /**
- * 设置设备上线
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function setonlineinfo($device_id_code)
- {
- $m_si = new ShinfoModel();
- $where = ['device_id_code' => $device_id_code, 'online_statis' => 0];
- $finfo = $m_si->getInfo($where, ['id', 'online_statis']);
- if ($finfo) {
- $facility_id = $finfo['id'];
- $m_si->setonlineinfobyid($facility_id);
- }
- }
- /**
- * 设置设备下线
- *
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function setofflineinfo()
- {
- $m_si = new ShinfoModel();
- $list = $m_si->getcheckupdatetimelist();
- $list = (array) $list;
- if (!empty($list)) {
- $facility_ids = array_column($list, 'id');
- $m_si->setofflineinfobyids($facility_ids);
- }
- }
- //工具方法
- /**
- * unicode 编码
- *
- * @param [type] $str
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function srtingToUnicode($str)
- {
- $unicode = '';
- $arr = mb_str_split($str); //适配中文
- foreach ($arr as $key => $value) {
- $char = bin2hex(iconv('UTF-8', 'UCS-2BE', $value));
- $char = str_pad($char, 4, '0', STR_PAD_LEFT);
- $arr[$key] = $char;
- }
- $unicode = implode("", $arr);
- return $unicode;
- }
- /**
- * unicode 解码
- *
- * @param [type] $str
- * @return void
- * @author wj
- * @date 2023-08-21
- */
- public function unicodeToChn($str)
- {
- $arr = str_split($str);
- $items = [];
- $item = "";
- foreach ($arr as $key => $value) {
- $index = $key + 1;
- if (0 != $index % 4) {
- $item .= $value;
- } else {
- $item = "";
- $items[] = $item;
- }
- }
- foreach ($items as $key => $value) {
- $char = hex2bin($value);
- $char = iconv('UCS-2BE', 'UTF-8', $char);
- $items[$key] = $char;
- }
- $info = implode("", $items);
- return $info;
- }
- /**
- * 拆包
- *
- * @return void
- * @author wj
- * @date 2023-08-15
- */
- public function unpack($data)
- {
- $arr = explode("][", $data);
- foreach ($arr as $key => $value) {
- if ($value[0] !== "[") {
- $arr[$key] = '[' . $value;
- }
- if ($value[mb_strlen($value) - 1] !== "]") {
- $arr[$key] .= "]";
- }
- }
- return $arr;
- }
- public function formattime($time, $format)
- {
- $time = strtotime($time);
- $time = date($format, $time);
- return $time;
- }
- //设备初始化
- public function facilityinit($facility_id, $device_id_code)
- {
- $m_f = new FacilityModel();
- $fwhere = [
- 'id' => $facility_id,
- 'code' => $device_id_code,
- 'kind' => 1,
- 'status' => 1,
- ];
- $finfo = $m_f->getInfo($fwhere);
- if (!$finfo) {
- throw new \Exception("无对应信息:" . $facility_id . "-" . $device_id_code);
- }
- $commandlist = [
- ['command' => 'ZONE', 'content_arr' => ['command' => 'ZONE', 'zone' => 8]],
- ['command' => 'UPLOAD', 'content_arr' => ['command' => 'UPLOAD', 'interval' => 60]],
- ['command' => 'CR', 'content_arr' => ['command' => 'CR']],
- ['command' => 'FALLDOWN', 'content_arr' => ['command' => 'FALLDOWN', 'is_open' => 1]],
- ['command' => 'VERNO', 'content_arr' => ['command' => 'VERNO']],
- ['command' => 'LOWBAT', 'content_arr' => ['command' => 'LOWBAT', 'is_open' => 1]],
- ['command' => 'PEDO', 'content_arr' => ['command' => 'PEDO', 'is_open' => 1]],
- ['command' => 'REMOVE', 'content_arr' => ['command' => 'REMOVE', 'is_open' => 1]],
- ];
- foreach ($commandlist as $key => $value) {
- $this->createsendmsg($facility_id, $device_id_code, $value);
- }
- }
- }
|