wang jun 1 year ago
parent
commit
55721dece8

+ 1 - 1
application/common/model/ShinfoModel.php

@@ -108,7 +108,7 @@ class ShinfoModel extends Model
             $data['temp_update_time'] = date('Y-m-d H:i:s');
         }
         //gps
-        if (isset($data['gps_long']) || isset($data['gps_lat'])) {
+        if (isset($data['gps_long']) || isset($data['gps_lat']) || isset($data['use_gps_long']) || isset($data['use_gps_lat'])) {
             $data['gps_update_time'] = date('Y-m-d H:i:s');
         }
         //电量

+ 225 - 85
application/common/server/SouhuanAnalysis.php

@@ -43,7 +43,7 @@ class SouhuanAnalysis
      * @author wj
      * @date 2023-08-11
      */
-    public function getline($str, $field = null)
+    public function getline($str, $field = null, $checkconfig = true)
     {
         //[厂商*设备 ID*内容长度*内容]
         $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
@@ -74,18 +74,60 @@ class SouhuanAnalysis
         $command = $contentarr['command'];
         $data['command'] = $command;
         $data['content_arr'] = $contentarr['content'];
-        $config = $this->getreceivecommandinfo($command);
-        $data['config'] = $config;
-        if (!$config) {
-            $errmsg = '命令无解析配置 command:' . $command;
-            Log::write($errmsg, 'shouhuan');
-            return false;
+
+        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)
+    {
+        $data = [];
+        $preg = "/(?<=\*)([a-zA-Z]){2,}(?=,)|(?<=,)([AV]{1})(?=,)|(?<=,)([0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2})(?=,)/";
+        $data = preg_match_all($preg, $str, $match);
+        $data = $match[0];
+        $command = $data[0];
+        if ('UD' == $command) {
+            $is_gps = $data[1];
+            if ('V' == $is_gps) {
+                //异步请求
+                $mac = $data[2];
+                $url = "http://api.cellocation.com:84/wifi/?mac=" . $mac . "&output=json";
+                $result = curl_request($url);
+                Log::write($match, 'shouhuan');
+                $data = [];
+                if (0 === $result['errcode']) {
+                    $data['use_gps_lat'] = $result['lat'];
+                    $data['use_gps_long'] = $result['lon'];
+                    $data['use_mac'] = $mac;
+                    $address = $result['address'];
+                    $address = explode(";", $address);
+                    $data['address'] = $address[0];
+                    $data['address1'] = $address[1];
+                    return $data;
+                }
+            }
+        }
+        return false;
+    }
     /**
      * 内容解析
      *
@@ -142,6 +184,7 @@ class SouhuanAnalysis
             'UPLOAD' => [
                 'content' => [
                     'command' => 'UPLOAD',
+                    'interval' => '',
                 ],
                 'kind' => 'send',
                 'need_back' => 1,
@@ -348,8 +391,8 @@ class SouhuanAnalysis
                 'content' => [
                     'date',
                     'time',
-                    'gps_long',
                     'gps_lat',
+                    'gps_long',
                     'speed',
                     'direction',
                     'poster',
@@ -366,8 +409,8 @@ class SouhuanAnalysis
                 'content' => [
                     'date',
                     'time',
-                    'gps_long',
                     'gps_lat',
+                    'gps_long',
                     'speed',
                     'direction',
                     'poster',
@@ -631,7 +674,7 @@ class SouhuanAnalysis
         if (isset($data['terminal_status'])) {
             $terminalstatus = $data['terminal_status'];
             $terminal_status_arr = $this->getterminalstatus($terminalstatus);
-            unset($data['terminal_status']);
+            //unset($data['terminal_status']);
             $data['status_type'] = $terminal_status_arr['status_type'];
             $data['alarm_type'] = $terminal_status_arr['alarm_type'];
         }
@@ -699,21 +742,25 @@ class SouhuanAnalysis
         if ($first_num != $end_num) {
             Log::write("状态解析错误:报警:" . $terminal_status . " " . $terminal_status_str, 'shouhuan');
         }
-        //16:SOS报警;17:低电报警;20:手环拆除报警;21:跌倒报警;22:心率异常报警
-        $first_num = $mid - $first_num + ($mid - 1);
-        $alarm_type = $first_num;
+        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');
         }
-        $first_num = $mid - 1 - $first_num;
-        //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
-        $status_type = $first_num + 1;
+        if ($first_num) {
+            $first_num = $mid - 1 - $first_num;
+            //1:低电状态;2:出围栏状态;3:进围栏状态;4:手环戴上取下状态;5:手表运行静止状态
+            $status_type = $first_num + 1;
+        }
         $data = [
-            'alarm_type' => $alarm_type,
-            'status_type' => $status_type,
+            'alarm_type' => isset($alarm_type) ? $alarm_type : false,
+            'status_type' => isset($status_type) ? $status_type : false,
         ];
         return $data;
     }
@@ -784,6 +831,18 @@ class SouhuanAnalysis
         $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;
+    }
     /**
      * 保存接收信息队列
      *
@@ -792,12 +851,16 @@ class SouhuanAnalysis
      * @author wj
      * @date 2023-08-14
      */
-    public function savereceivequeue($data)
+    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)) {
@@ -892,8 +955,9 @@ class SouhuanAnalysis
      * @author wj
      * @date 2023-08-14
      */
-    public function saveshinfo($facility_id, $device_id_code, $data, $original_str)
+    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'];
@@ -904,6 +968,11 @@ class SouhuanAnalysis
         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;
@@ -913,16 +982,19 @@ class SouhuanAnalysis
         } else {
             //手环基础信息修改
             $sriData = $data;
+            $uddata = $this->getmacforUD($original_str);
+            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,
-                    'facility_id' => $facility_id,
-                ];
+                $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');
@@ -932,59 +1004,75 @@ class SouhuanAnalysis
                     throw new \Exception("设备信息添加失败:" . $device_id_code);
                 }
             } else {
+                $sriinfo = $sriinfo->toArray();
                 $sriid = $sriinfo['id'];
-                $sriupdateData = $sriData;
-                $sriupdateData['updatetime'] = date('Y-m-d H:i:s');
-                $sriupdateData = $m_sri->formatinfo($sriupdateData);
-                $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData);
-                if (empty($row)) {
-                    throw new \Exception("设备信息修改失败:" . $device_id_code);
+                $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]);
+                    }
                 }
-            }
-
-            //接收信息历史添加
-            $sririnsertData['facility_id'] = $facility_id;
-            $sririnsertData['device_id_code'] = $device_id_code;
-            $sririnsertData['original_str'] = $original_str;
-            $sririnsertData['content'] = json_encode($sriData);
-            if (isset($sriData['date'])) {
-                $sririnsertData['date'] = $sriData['date'];
-            }
-            if (isset($sriData['time'])) {
-                $sririnsertData['time'] = $sriData['time'];
-            }
-            $sririnsertData['command'] = $command;
-            $sririnsertData['createtime'] = date('Y-m-d H:i:s');
-            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);
+                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;
     }
 
     /**
@@ -995,21 +1083,27 @@ class SouhuanAnalysis
      * @return void
      * @author wj
      * @date 2023-08-14
+     * $data =['command'=>'','content_arr'=>[],]
      */
-    public function createsendmsg($facility_id, $device_id_code, $data, $command, $param = [])
+    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;
         }
-        $command = $data['command'];
         $content_arr = $data['content_arr'];
-        $config = $data['config'];
+        $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);
@@ -1019,7 +1113,7 @@ class SouhuanAnalysis
         if ('send' == $config['kind']) {
             $content = $config['content'];
             $useconmmand = $content['command'];
-            $content_custom = $config['content_custom'];
+            $content_custom = isset($config['content_custom']) ? $config['content_custom'] : false;
             $need_back = $config['need_back'];
             if ($content_custom) {
                 $functionname = 'get' . $command . "Commandinfo";
@@ -1043,7 +1137,7 @@ class SouhuanAnalysis
         $msgdata = [
             0 => $commandxy,
             'device_id_code' => $device_id_code,
-            'len' => str_pad(dechex(mb_strlen($content)), 4, "0", STR_PAD_LEFT),
+            'len' => str_pad(strtoupper(dechex(mb_strlen($content))), 4, "0", STR_PAD_LEFT),
             'content' => $content,
         ];
         $msg = '[' . implode('*', $msgdata) . ']';
@@ -1068,6 +1162,7 @@ class SouhuanAnalysis
             '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);
@@ -1274,13 +1369,28 @@ class SouhuanAnalysis
     {
         $m_ssq = new ShsendqueueModel();
         $m_ssl = new ShsendlistModel();
+        $m_sal = new ShalarmlistModel();
         $ssqid = $item['id'];
-        if ($item['list_id']) {
-            $list_id = $item['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 (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];
@@ -1288,6 +1398,7 @@ class SouhuanAnalysis
         if ($ssqinfo) {
             $m_ssq->deleteinfobyid($ssqid);
         }
+
     }
     /**
      * 解析成功 删除接收数据
@@ -1330,7 +1441,7 @@ class SouhuanAnalysis
      */
     public function showfortest($data)
     {
-        $strdata = $this->getline($data);
+        $strdata = $this->getline($data, null, false);
         if (!is_array($strdata)) {
             Log::write($data, 'shouhuan');
             return false;
@@ -1340,7 +1451,7 @@ class SouhuanAnalysis
             'SOS', 'CR', 'UPLOAD', 'SLEEPTIME', 'ZONE', 'FALLDOWN',
             'APPLOCK', 'FACTORY', 'VERNO', 'LOWBAT', 'UD', 'POWEROFF', 'MESSAGE',
             'WALKTIME', 'PEDO', 'PHB', 'PHL', 'REMIND', 'SILENCETIME2', 'BOOTOFF',
-            'REMOVE', 'INFO', 'AL', 'LSSET',
+            'REMOVE', 'INFO', 'AL', 'LSSET', 'BTWARNSET',
         ];
         if (in_array($command, $showcommands)) {
             var_dump(date('Y-m-d H:i:s'));
@@ -1461,4 +1572,33 @@ class SouhuanAnalysis
         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);
+        }
+    }
+
 }

+ 4 - 5
application/workerman/ShTcp.php

@@ -58,7 +58,6 @@ class ShTcp extends Server
                 foreach ($list as $key => $value) {
                     try {
                         $msg = $value['msg'];
-                        var_dump($msg);
                         $linedata = $server_sa->getline($msg);
                         if (!is_array($linedata)) {
                             Log::write("不可解析_onWorkerStart:" . $msg, 'shouhuan');
@@ -71,9 +70,9 @@ class ShTcp extends Server
                         $command = $linedata['command'];
                         $content = $linedata['content_arr'];
                         $fid = $server_sa->getfacilityid($device_id_code);
-                        $result = $server_sa->saveshinfo($fid, $device_id_code, $linedata, $msg);
+                        $result = $server_sa->saveshinfo($fid, $device_id_code, $linedata, $value);
                         if ($result) {
-                            $data = $server_sa->createsendmsg($fid, $device_id_code, $linedata, $command);
+                            $data = $server_sa->createsendmsg($fid, $device_id_code, $linedata);
                         }
                         $server_sa->receivemsgsuccess($value);
                     } catch (\Exception $e) {
@@ -107,7 +106,7 @@ class ShTcp extends Server
     }
     public function onMessage($connection, $data)
     {
-        //Log::write($data, 'shouhuan');
+        Log::write($data, 'shouhuan');
         $server_sa = new SouhuanAnalysis();
         $datas = (array) $server_sa->unpack($data);
         foreach ($datas as $key => $data) {
@@ -119,7 +118,7 @@ class ShTcp extends Server
                     Log::write('不可解析_onMessage:' . $data, 'shouhuan');
                     continue;
                 }
-                $result = $server_sa->savereceivequeue($data);
+                $result = $server_sa->savereceivequeue($data, $device_id_code);
                 if (empty($result)) {
                     Log::write("手环接收信息队列,数据保存失败", 'shouhuan');
                 }