wang jun 1 year ago
parent
commit
81b0790dc3
2 changed files with 167 additions and 24 deletions
  1. 150 15
      application/common/server/ShOneanalysis.php
  2. 17 9
      application/workerman/ShTcp.php

+ 150 - 15
application/common/server/ShOneanalysis.php

@@ -93,19 +93,7 @@ class ShOneanalysis
     public function analysislen($len)
     {
         //低位数在前
-        $len_arr = array_reverse(str_split($len));
-        $len_num = [];
-        foreach ($len_arr as $key => $value) {
-            if (is_numeric($value) && $value <= 9) {
-                $num = $value;
-            } elseif (ord($value) <= 65 || ord($value) >= 90) {
-                $ord = ord($value);
-                $num = bcadd(bcsub($ord, 65), 10);
-            }
-            $pow = pow($this->num_scale, $key); //16^0 16^1
-            $len_num[] = bcmul($pow, $num);
-        }
-        $len = array_sum($len_num);
+        $len = base_convert($len, 16, 10);
         return $len;
     }
     /**
@@ -118,6 +106,7 @@ class ShOneanalysis
      */
     public function getcommandinfo($command)
     {
+        $command = strtoupper($command);
         $data = [
             'LK' => [
                 'content' => [
@@ -173,6 +162,24 @@ class ShOneanalysis
                 ],
                 'kind' => 'send',
             ],
+            'TEMP' => [
+                'content' => [
+                    'command' => 'temp',
+                ],
+                'kind' => 'back',
+            ],
+            'BPHRT' => [
+                'content' => [
+                    'command' => 'bphrt',
+                ],
+                'kind' => 'back',
+            ],
+            'HEART'=>[
+                'content' => [
+                    'command' => 'heart',
+                ],
+                'kind' => 'back',
+            ]
         ];
         if (!isset($data[$command])) {
             return false;
@@ -182,6 +189,7 @@ class ShOneanalysis
 
     public function gettsendcommandinfo($command)
     {
+        $command = strtoupper($command);
         $data = [
             'WT' => [
                 'content' => [
@@ -207,6 +215,30 @@ class ShOneanalysis
                 'kind' => 'tsend',
                 'need_bacl' => 0,
             ],
+            'WG' => [
+                'content' => [
+                    'command' => 'WT',
+                    'date' => '',
+                    'time' => '',
+                    'is_location' => '',
+                    'gps_long' => '',
+                    'gps_long_tag' => '',
+                    'gps_lat' => '',
+                    'gps_lat_tag' => '',
+                    'speed' => '',
+                    'direction' => '',
+                    'poster' => '',
+                    'direction' => '',
+                    'gps_satellite_num' => '',
+                    'gsm_signal_strength' => '',
+                    'electric_quantity' => '',
+                    'step_number' => '',
+                    'roll_number' => '',
+                    'terminal_status' => '',
+                ],
+                'kind' => 'tsend',
+                'need_bacl' => 0,
+            ],
             'KA' => [
                 'content' => [
                     'command' => 'KA',
@@ -244,6 +276,36 @@ class ShOneanalysis
                 ],
                 'kind' => 'tback',
             ],
+            'TEMP' => [
+                'content' => [
+                    'command' => 'temp',
+                    'temp' => '',
+                ],
+                'kind' => 'tsend',
+                'need_back' => 1,
+            ],
+            'BPHRT' => [
+                'content' => [
+                    'command' => 'bphrt',
+                    'blood_height_pressure' => '',
+                    'blood_low_pressure' => '',
+                    'heart_rate' => '',
+                    'height' => '',
+                    'gender' => '',
+                    'age' => '',
+                    'weight' => '',
+                ],
+                'kind' => 'tsend',
+                'need_back' => 1,
+            ],
+            'HEART' => [
+                'content' => [
+                    'command' => 'heart',
+                    'heart_rate' => '',
+                ],
+                'kind' => 'tsend',
+                'need_back' => 1,
+            ],
         ];
         if (!isset($data[$command])) {
             return false;
@@ -362,7 +424,7 @@ class ShOneanalysis
             $weight = $this->getsendweight($command);
             $content = implode(',', $info_content);
             $msgdata = [
-                0 => 'DW',
+                0 => '3G',
                 'device_id_code' => $device_id_code,
                 'len' => str_pad(dechex(mb_strlen($content)), 4, "0", STR_PAD_LEFT),
                 'content' => $content,
@@ -415,6 +477,14 @@ class ShOneanalysis
                 }
             }
         }
+        if (isset($info_content['time'])) {
+            $time = $info_content['time'];
+            $info_content['time'] = $this->gettime($time);
+        }
+        if (isset($info_content['date'])) {
+            $date = $info_content['date'];
+            $info_content['date'] = $this->getdate($date);
+        }
         $infoarr['info_content'] = $info_content;
         return $infoarr;
     }
@@ -490,6 +560,8 @@ class ShOneanalysis
         //m_srir处理
         $data['original_str'] = $original_str;
         $sririnsertData = $data;
+        $sririnsertData['facility_id'] = $facility_id;
+        $sririnsertData['device_id_code'] = $device_id_code;
         $sririnsertData['createtime'] = date('Y-m-d H:i:s');
         $sririd = $m_srir->insertData($sririnsertData);
         if (empty($sririd)) {
@@ -596,10 +668,73 @@ class ShOneanalysis
     {
         $m_srq = new ShoneReceiveQueueModel();
         $srqid = $item['id'];
-        $srqwhere = ['id' => $srqid, 'is_send' => 0];
+        $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 getdate($date)
+    {
+        $day = substr($date, 0, 2);
+        $month = substr($date, 2, 2);
+        $year = substr($date, 4, 2);
+        $year1 = date('Y');
+        $year1 = substr($year1, 0, 2);
+        $year = $year1 . $year;
+        $day = $year . '-' . $month . '-' . $day;
+        return $day;
+    }
+    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 initanalysisShoneReceiveMsg()
+    {
+        $m_srq = new ShoneReceiveQueueModel();
+        $where = ['status' => 1];
+        $m_srq->updateinfo($where, ['status' => 0]);
+    }
+    /**
+     * 拆包
+     *
+     * @return void
+     * @author wj
+     * @date 2023-08-15
+     */
+    public function unpack($data)
+    {
+        $arr = explode("][", $data);
+        foreach ($arr as $key => $value) {
+            $regex = "/^[^[].*$/";
+            preg_match($regex, $value, $mc0);
+            $regex = "/^.*[^]]$/";
+            preg_match($regex, $value, $mc1);
+            if ($mc0) {
+                $arr[$key] = '[' . $value;
+            }
+            if ($mc1) {
+                $arr[$key] = $value . "]";
+            }
+        }
+        return $arr;
+    }
 }

+ 17 - 9
application/workerman/ShTcp.php

@@ -36,8 +36,7 @@ class ShTcp extends Server
                         for ($page = 1; $page <= $totalpage; $page++) {
                             $list = $server_sa->getsendqueuelist($device_id_code, 1, $size);
                             foreach ($list as $key => $value) {
-                                var_dump('send sq:' . $value['id']);
-                                $connection->send(json_encode($value['msg']));
+                                $connection->send($value['msg']);
                                 $server_sa->sendmsgsuccess($value);
                             }
                         }
@@ -65,15 +64,24 @@ class ShTcp extends Server
     public function onMessage($connection, $data)
     {
         Log::write("ShTcp", 'shouhuan');
-        Log::write($data, 'shouhuan');
+
         $server_sa = new ShOneanalysis();
-        $result = $server_sa->savereceivequeue($data);
-        if (empty($result)) {
-            Log::write("手环接收信息队列,数据保存失败", 'shouhuan');
+        $datas = (array) $server_sa->unpack($data);
+        foreach ($datas as $key => $data) {
+            try {
+                $result = $server_sa->savereceivequeue($data);
+                if (empty($result)) {
+                    Log::write("手环接收信息队列,数据保存失败", 'shouhuan');
+                }
+                $line = $server_sa->getline($data);
+                $this->device_id_code = $line['device_id_code'];
+                //$this->device_id_code = "358800006072996";
+            } catch (\Exception $e) {
+                Log::write($data, 'shouhuan');
+                Log::write($e->getMessage(), 'shouhuan');
+                var_dump($e->getTraceAsString());
+            }
         }
-        $line = $server_sa->getline($data);
-        $this->device_id_code = $line['device_id_code'];
-        //$this->device_id_code = "358800006072996";
     }
 
     public function onClose($connection)