Procházet zdrojové kódy

Merge remote-tracking branch 'origin/main'

# Conflicts:
#	app/Service/ThirdService.php
gogs před 2 roky
rodič
revize
42d83b365f

+ 2 - 2
app/Model/DeviceType.php

@@ -16,9 +16,9 @@ class DeviceType extends Model
     const type_three = 3;
     const type_four = 4;
 
-    const temperature_normal = '温度正常';
+    const temperature_normal = '温度';
     const temperature_abnormal = '温度异常';
-    const humidity_normal = '湿度正常';
+    const humidity_normal = '湿度';
     const humidity_abnormal = '湿度异常';
 
     public static $abnormal = [self::temperature_abnormal,self::humidity_abnormal];

+ 40 - 4
app/Service/DeviceService.php

@@ -109,6 +109,8 @@ class DeviceService extends Service
     }
 
     public function equipmentList($data){
+        $device_map = SystemlService::getLastData();
+
         $model = Device::where('del_time',0)
             ->select('*')
             ->orderBy('id','DESC');
@@ -128,14 +130,48 @@ class DeviceService extends Service
                 $model->whereNotIn('id',array_column($id,'device_id'));
             }
         }
+        if(isset($data['is_online'])){
+            $search = [];
+            foreach ($device_map as $device_no => $value){
+                if($data['is_online'] && ! empty($value)){
+                    $search[] = $device_no;
+                }elseif(! $data['is_online'] && empty($value)){
+                    $search[] = $device_no;
+                }
+            }
+            $model->whereIn('code',$search);
+        }
+        if(isset($data['is_exception'])){
+            $search = [];
+            foreach ($device_map as $device_no => $value){
+                if($data['is_exception'] && ! empty($value)){
+                    if(in_array($value['device_type_id'],DeviceType::$node_two)){
+                        //传感器类
+                        if(in_array($value['data_point_name'],DeviceType::$normal)) $search[] = $device_no;
+                    }else{
+                        $search[] = $device_no;
+                    }
+                }elseif(! $data['is_exception']){
+                    if(empty($value)){
+                        $search[] = $device_no;
+                    }else{
+                        if(in_array($value['device_type_id'],DeviceType::$node_two)){
+                            //传感器类
+                            if(in_array($value['data_point_name'],DeviceType::$abnormal)) $search[] = $device_no;
+                        }
+                    }
+                }
+            }
+            $model->whereIn('code',$search);
+        }
 
         $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list);
+        $list = $this->fillData($list,$device_map);
 
         return [true,$list];
     }
 
-    public function fillData($data){
+    public function fillData($data,$device_map){
         if(empty($data['data'])) return $data;
         $gatewayMap = Gateway::whereIn('id',array_unique(array_column($data['data'],'gateway_id')))
             ->pluck('code','id')
@@ -144,12 +180,12 @@ class DeviceService extends Service
             ->pluck('title','id')
             ->toArray();
 
-        $message = SystemlService::getLastData(array_column($data['data'],'code'));
+        $message = $device_map;
         foreach ($data['data'] as $key => $value){
             $data['data'][$key]['gateway'] = $gatewayMap[$value['gateway_id']] ?? '';
             $data['data'][$key]['device_type'] = $deviceTypeMap[$value['device_type_id']] ?? '';
             $is_online = $is_exception = 0;
-            if(isset($message[$value['code']])){
+            if(! empty($message[$value['code']])){
                 $is_online = 1;
                 $tmp = $message[$value['code']];
                 if(in_array($value['device_type_id'],DeviceType::$node_two)){

+ 15 - 5
app/Service/GatewayService.php

@@ -60,15 +60,28 @@ class GatewayService extends Service
     }
 
     public function equipmentList($data){
+        $gateway_map = SystemlService::getIsOnlineStatusGateWay();
+
         $model = Gateway::where('del_time',0)
             ->select('*');
 
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
         if(! empty($data['ip'])) $model->where('ip', 'LIKE', '%'.$data['ip'].'%');
+        if(isset($data['is_online'])){
+            $search = [];
+            foreach ($gateway_map as $gateway => $value){
+                if($data['is_online'] && ! empty($value)){
+                    $search[] = $gateway;
+                }elseif(! $data['is_online'] && empty($value)){
+                    $search[] = $gateway;
+                }
+            }
+            $model->whereIn('id',$search);
+        }
 
         $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list);
+        $list = $this->fillData($list,$gateway_map);
 
         return [true,$list];
     }
@@ -95,12 +108,9 @@ class GatewayService extends Service
         return [true,''];
     }
 
-    public function fillData($data){
+    public function fillData($data,$online){
         if(empty($data['data'])) return $data;
 
-        //获取仓是否在线离线状态
-        $online = SystemlService::getIsOnlineStatusGateWay(array_column($data['data'],'id'));
-
         foreach ($data['data'] as $key => $value){
             $tmp_online = $online[$value['id']] ?? 0;
             $data['data'][$key]['is_online'] = $tmp_online;

+ 93 - 25
app/Service/ScreenService.php

@@ -212,7 +212,7 @@ class ScreenService extends Service
         $message = SystemlService::getLastData(array_column($data['data'],'code'));
         foreach ($data['data'] as $key => $value){
             $is_online = $is_exception = 0;
-            if(isset($message[$value['code']])){
+            if(! empty($message[$value['code']])){
                 $is_online = 1;
                 $tmp = $message[$value['code']];
                 if(in_array($value['device_type_id'],DeviceType::$node_two)){
@@ -437,11 +437,13 @@ class ScreenService extends Service
             'province' => '',
             'city' => '',
             'district' => '',
-            'location' => '',
+            'location' => [],
             'temperature' => [],
-            'humidity' => []
+            'humidity' => [],
+            'screen_num' => 1,
         ];
 
+        //选中仓的 传感器设备和定位设备信息
         $device = ScreenDevice::from('screen_device as a')
             ->leftJoin('device as b','b.id','a.device_id')
             ->whereIn('b.device_type_id',[DeviceType::type_one,DeviceType::type_four])
@@ -460,33 +462,28 @@ class ScreenService extends Service
         }
 
         //gps定位
-        $return_gps = SystemlService::getLastData($gps);
+        $time = strtotime('-60 min',time());
+        $time = $time * 1000;
+        $model = SystemL::whereIn('device_no', $gps)
+            ->select('value','time','device_no','data_point_name')
+            ->orderBy('id','desc');
+        $model->where('time','>=',$time);
+        $return_gps = $model->get()->toArray();
         if(! empty($return_gps)){
-            $first_position = "";
+            $first_postition = "";
+            $location = [];
             foreach ($return_gps as $value){
-                if(! empty ($first_postition)) continue;
-                $first_position = $value['value'];
-            }
-            $url = 'https://restapi.amap.com/v3/geocode/regeo?location='.$first_position.'&key='.SystemL::GaoDeMapKey;
-            try{
-                $return = $this->curlOpen($url);
-                $return = json_decode($return,true);
-                if(! empty($return['status'])){
-                    $position = $return['regeocode']['addressComponent'] ?? [];
-                    $result = [
-                        'province' => $position['province'] ?? '',
-                        'city' => $position['city'] ?? '',
-                        'district' => $position['district'] ?? '',
-                        'location' => $first_position
-                    ];
+                if(empty($first_postition)) {
+                    $first_postition = $value['value'];
                 }
-            }catch (\Exception $exception){
-//                return [false,$exception->getMessage()];
+                $location[] = $value['value'];
             }
+            $result['location'] = $location;
+            $this->getLocation($first_postition,$result);
         }
 
         //温度湿度
-        $time = strtotime('-2 min',time());
+        $time = strtotime('-1440 min',time());
         $time = $time * 1000;
         $model = SystemL::whereIn('device_no', $other)
             ->select('value','time','device_no','data_point_name')
@@ -497,6 +494,34 @@ class ScreenService extends Service
         $result['temperature'] = $chart['temperature'];
         $result['humidity'] = $chart['humidity'];
 
+        //同一区域有几个仓
+        $counts = ScreenDevice::from('screen_device as a')
+            ->leftJoin('device as b','b.id','a.device_id')
+            ->where('b.device_type_id',DeviceType::type_four)
+            ->where('a.del_time',0)
+            ->where('b.del_time',0)
+            ->where('a.screen_id','<>',$data['screen_id'])
+            ->select('b.code')
+            ->get()->toArray();
+        if(! empty($counts)){
+            $time = strtotime('-10 min',time());
+            $time = $time * 1000;
+            $model = SystemL::whereIn('device_no', $gps)
+                ->select('value','device_no')
+                ->orderBy('id','desc');
+            $model->where('time','>=',$time);
+            $return_other_gps = $model->get()->toArray();
+            if(! empty($return_other_gps)){
+                $code_p = [];
+                foreach ($return_other_gps as $value){
+                    if(! isset($code_p[$value['device_no']]) && $value['value']) $code_p[$value['device_no']] = $value['value'];
+                }
+                if(! empty($code_p)){
+                    $this->isSameLocation($code_p,$result);
+                }
+            }
+        }
+
         return [true, $result];
     }
 
@@ -507,12 +532,12 @@ class ScreenService extends Service
         foreach ($data as $value){
             if (strstr($value['data_point_name'],'温度')){
                 $return1[$value['device_no']][] = [
-                    'time' => date('Y-m-d H:i:s',$value['time'] / 1000),
+                    'time' => date('H:i:s',$value['time'] / 1000),
                     'value' => $value['value']
                 ];
             }elseif (strstr($value['data_point_name'],'湿度')){
                 $return2[$value['device_no']][] = [
-                    'time' => date('Y-m-d H:i:s',$value['time'] / 1000),
+                    'time' => date('H:i:s',$value['time'] / 1000),
                     'value' => $value['value']
                 ];
             }
@@ -587,6 +612,7 @@ class ScreenService extends Service
         foreach ($data as $key => $value){
             if(isset($map[$value['id']])){
                 foreach ($return as $code => $val){
+                    if(empty($val)) continue;
                     if(in_array($code,$map[$value['id']]) && isset($map2[$code])){
                         if(empty($data[$key]['temperature']) && $map2[$code] == DeviceType::type_one && strstr($val['data_point_name'],'温度')){
                             $data[$key]['temperature'] = $val['value'] . ($map3[$code] ?? '');
@@ -598,6 +624,16 @@ class ScreenService extends Service
                             $data[$key]['warning'] = $val['value'] ? '是' : '否';
                         }elseif(empty($data[$key]['gps']) && $map2[$code] == DeviceType::type_four){
                             $data[$key]['gps'] = $val['value'];
+                            $tmp = [
+                                'province' => '',
+                                'city' => '',
+                                'district' => '',
+                            ];
+                            $this->getLocation($val['value'],$tmp);
+                            $data[$key]['province'] = $tmp['province'];
+                            $data[$key]['city'] = $tmp['city'];
+                            $data[$key]['district'] = $tmp['district'];
+                            $data[$key]['township'] = $tmp['township'];
                         }
                     }
                 }
@@ -606,4 +642,36 @@ class ScreenService extends Service
 
         return $data;
     }
+
+    public function getLocation($position,&$result){
+        $url = 'https://restapi.amap.com/v3/geocode/regeo?location='.$position.'&key='.SystemL::GaoDeMapKey;
+        try{
+            $return = $this->curlOpen($url);
+            $return = json_decode($return,true);
+            if(! empty($return['status'])){
+                $position = $return['regeocode']['addressComponent'] ?? [];
+                $result['province'] = $position['province'] ?? '';
+                $result['city'] = $position['city'] ?? '';
+                $result['district'] = $position['district'] ?? '';
+                $result['township'] = $position['township'] ?? '';
+            }
+        }catch (\Exception $exception){
+        }
+    }
+
+    public function isSameLocation($position,&$result){
+        foreach ($position as $position_tmp){
+            $url = 'https://restapi.amap.com/v3/geocode/regeo?location='.$position_tmp.'&key='.SystemL::GaoDeMapKey;
+            try{
+                $return = $this->curlOpen($url);
+                $return = json_decode($return,true);
+                if(! empty($return['status'])){
+                    $position_arr = $return['regeocode']['addressComponent'] ?? [];
+                    $province = $position_arr['province'] ?? '';
+                    if($province == $result['province']) $result['screen_num'] += 1;
+                }
+            }catch (\Exception $exception){
+            }
+        }
+    }
 }

+ 24 - 9
app/Service/SystemlService.php

@@ -4,13 +4,24 @@ namespace App\Service;
 
 use App\Model\Device;
 use App\Model\DeviceType;
+use App\Model\Gateway;
 use App\Model\ScreenDevice;
 use App\Model\SystemL;
 
 class SystemlService extends Service
 {
-    public static function getLastData($code = [], $min = -1){
-        if(empty($code)) return [];
+    public static function getLastData($code = [], $min = -10){
+        if(empty($code)) {
+            $codes = Device::where('del_time',0)->select('code','device_type_id')->get()->toArray();
+            if(empty($codes)) return [];
+            $code = array_column($codes,'code');
+            $code_type_map = array_column($codes,'device_type_id','code');
+        }
+
+        $return = [];
+        foreach ($code as $value){
+            $return[$value] = [];
+        }
 
         $time = strtotime($min.' min',time());
         $time = $time * 1000;
@@ -21,15 +32,15 @@ class SystemlService extends Service
             ->orderBy('id','desc')
             ->get()->toArray();
 
-        $return = [];
         if(! empty($result)){
             foreach ($result as $value){
-                if(isset($return[$value['device_no']])) continue;
+                if(! empty($return[$value['device_no']])) continue;
                 //只取每个设备最新的一条数据
                 $return[$value['device_no']] = [
                     'data_point_name' => $value['data_point_name'],
                     'value' => $value['value']
                 ];
+                if(! empty($code_type_map[$value['device_no']])) $return[$value['device_no']]['device_type_id'] = $code_type_map[$value['device_no']];
             }
         }
 
@@ -100,7 +111,7 @@ class SystemlService extends Service
         //获取最新设备的数据
         $data = self::getLastData(array_column($screen,'code'));
         foreach ($data as $key => $value){
-            if(isset($screen_map[$key])){
+            if(isset($screen_map[$key]) && ! empty($value)){
                 $tmp = $screen_map[$key];
                 $tmp_coordinate = $tmp['coordinate'];
                 if(isset($result[$tmp['screen_id']]) && $tmp_coordinate){
@@ -167,7 +178,7 @@ class SystemlService extends Service
         //获取最新设备的数据
         $data = self::getLastData(array_column($screen,'code'));
         foreach ($data as $key => $value){
-            if(isset($screen_map[$key])){
+            if(isset($screen_map[$key]) && ! empty($value)){
                 $tmp = $screen_map[$key];
                 if(isset($result[$tmp['screen_id']]) && $value['value'] == 1){
                     $result[$tmp['screen_id']] = 1;
@@ -178,8 +189,12 @@ class SystemlService extends Service
         return $result;
     }
 
-    public static function getIsOnlineStatusGateWay($gateway){
-        if(empty($gateway)) return [];
+    public static function getIsOnlineStatusGateWay($gateway = []){
+        if(empty($gateway)) {
+            $gateway = Gateway::where('del_time',0)->select('id')->get()->toArray();
+            if(empty($gateway)) return [];
+            $gateway = array_column($gateway,'id');
+        }
 
         //网关是否在线
         $result = [];
@@ -201,7 +216,7 @@ class SystemlService extends Service
         foreach ($gateway_connect_device as $gateway => $value){
             if(! empty($result[$gateway])) continue;
             foreach ($value as $code){
-                if(isset($data[$code])){
+                if(! empty($data[$code])){
                     $result[$gateway] = 1;
                 }
             }

+ 2 - 2
app/Service/ThirdService.php

@@ -18,8 +18,8 @@ class ThirdService extends Service
         $list = Device::where('del_time',0)->select('*')->get()->toArray();
 
         while (true) {
-//            sleep(60);
-//            $insert = [];
+            sleep(600);
+            $insert = [];
             $jwkey = [];
             foreach ($list as $k=>$v){
                 $value = 0;