123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161 |
- <?php
- namespace app\common\server;
- use app\common\model\Devicelistmodel;
- 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);
- }
- //验证设备状态
- $fid = $this->getfacilityid($device_id_code);
- if ($fid) {
- //改设备接收信息时间
- $time = date('Y-m-d H:i:s');
- $shupdateData = ['receive_time' => $time];
- $shupdateWhere = ['facility_id' => $fid];
- $m_sh = new ShinfoModel();
- $row = $m_sh->updateinfo($shupdateWhere, $shupdateData);
- if (empty($row)) {
- $errmsg = '设备:' . $device_id_code . " receive_time 修改失败";
- Log::write($errmsg, 'shouhuan');
- }
- }
- $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;
- }
- //else {
- // $data['config'] = [];
- // }
- }
- 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)
- {
- Log::write($str, 'shouhuan');
- $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;
- Log::write($url, 'shouhuan');
- $result = curl_request($url);
- Log::write($result, 'shouhuan');
- if ('10000' === $result['infocode']) {
- $resultdata = $result['result'];
- if (isset($resultdata['location'])) {
- $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;
- }
- } 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);
- Log::write($url, 'shouhuan');
- Log::write($result, 'shouhuan');
- if ('10000' == $result['infocode']) {
- $locations = $result['locations'];
- $url = "https://restapi.amap.com/v3/geocode/regeo?location=" . $locations . "&key=" . $apikey;
- $result = curl_request($url);
- Log::write($url, 'shouhuan');
- Log::write($result, 'shouhuan');
- 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-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',
- ],
- 'TKQ' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tsend',
- 'need_back' => 1,
- ],
- 'TKQ' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tsend',
- 'need_back' => 1,
- ],
- 'WG' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tsend',
- 'need_back' => 1,
- ],
- //终端回复
- //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',
- ],
- 'HRTSART' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'WDSTART' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'BLDSTART' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'OXSTART' => [
- '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',
- ],
- 'CALL' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- 'KEYBRD' => [
- 'analysis_digit' => null,
- 'content' => null,
- 'kind' => 'tback',
- ],
- ];
- if (!isset($data[$command])) {
- return false;
- }
- return $data[$command];
- }
- /**
- * 获取数据值
- *
- * @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');
- $result = $this->getmacforUD($original_str, $device_id_code);
- $data['use_gps_long'] = $result['use_gps_long'];
- $data['use_gps_lat'] = $result['use_gps_lat'];
- $data['location'] = $result['address_desc'];
- $m_dl = new Devicelistmodel();
- $uid = $m_dl->getuidbydeviceid($device_id_code);
- $uid = empty($uid) ? 0 : $uid;
- $data['user_id'] = $uid;
- $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] $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', 'KEYBRD',
- ];
- 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 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;
- }
- }
|