瀏覽代碼

测试数据 展示实时数据伪代码写完

cqpCow 1 年之前
父節點
當前提交
6f475ea3bb

+ 13 - 0
app/Http/Controllers/Api/ScreenController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers\Api;
 
 use App\Service\ScreenService;
+use Illuminate\Http\Request;
 
 class ScreenController extends BaseController
 {
@@ -96,4 +97,16 @@ class ScreenController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function screenDeviceDataRealTimeList(Request $request){
+        $service = new ScreenService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->screenDeviceDataRealTimeList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 19 - 0
app/Model/SysMenu.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * 菜单管理
+ * Class Unit
+ * @package App\Models
+ */
+class SysMenu extends Model
+{
+    protected $table = "sys_menu"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+}

+ 26 - 0
app/Model/SystemL.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class SystemL extends Model
+{
+    protected $table = "system_L"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+
+    const run = '压机上升'; //  运行时间/次数
+    const work = '小车前进'; // 工作时间/次数  不用了
+    const stop = '急停'; // 故障时间/次数
+    const standBy = '压机下降';// 待机时间/次数
+
+    public static $device = [
+        "2号热压机" => "01401422102400001960",
+        "1号热压机" => "01401422100800000103",
+        "6号热压机" => "01401422100800008976",
+        "5号热压机" => "01401422100800000342"
+    ];
+
+}

+ 133 - 0
app/Service/CloudDataService.php

@@ -0,0 +1,133 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\SystemL;
+use Illuminate\Support\Facades\Redis;
+
+/**
+ * 获取机器数据
+ * Class CloudDataService
+ * @package App\Service
+ */
+class CloudDataService extends Service
+{
+    //密钥
+    private $token_key = '';
+    //appSecret
+    private $app_secret = 'ziou5spsyi9c947rasqajhwoejee1oq3';
+    //appKey
+    private $app_key = '7k8iwOdL';
+    //设备id
+    private $device = [
+//        "01401422100800008703",
+        "01401422100800000103",
+        "01401422100800008976",
+        "01401422100800000342",
+        "01401422102400001960"
+    ];
+
+    public function cloudData(){
+        $this->getAllDevice();
+    }
+
+    //获取token
+    public function getToken(){
+        $token = Redis::get($this->token_key);
+        if(! empty($token)) return $token;
+
+        //接口地址
+        $url = 'https://openapi.mp.usr.cn/usrCloud/user/getAuthToken';
+        //参数
+        $app_message = [
+            'appSecret' => $this->app_secret,
+            'appKey' => $this->app_key,
+        ];
+        //发送请求
+        $result = $this->curlOpen($url,['header'=>['Content-Type:application/json'],'post'=>json_encode($app_message)]);
+
+        if(empty($result)) die('err');
+        $res = json_decode($result,true);
+
+        //设置token缓存
+        $token = $res['data']['X-Access-Token'];
+        Redis::setex($this->token_key,(3600*1.5),$token);
+        return $token;
+    }
+
+    //获取数据
+    public function getAllDevice(){
+        $url = 'https://openapi.mp.usr.cn/usrCloud/datadic/getDataPointInfoByDevice';
+        $res = $this->xlCurl($url,['deviceIds' => $this->device]);
+
+//        $getUrl = $this->xlCurl('https://openapi.mp.usr.cn/usrCloud/vn/ucloudSdk/getHistoryServerAddress',[]);
+//        if(isset(json_decode($getUrl,true)['data']['historyServerAddr']))  $getUrl = json_decode($getUrl,true)['data']['historyServerAddr'];
+//        else $getUrl = 'https://history.usr.cn:7002';
+
+        $getUrl = 'https://history.usr.cn:7002';
+        $list = json_decode($res,true);
+
+        $now = strtotime(date('Y-m-d')) * 1000;
+        $start = $now;
+
+        //目前设置的今天的开始时间戳 for循环一次
+        for ($i = $start; $i <= $now; $i = $i + 86400 * 1000){
+            $end = $i + 86400 * 1000;
+            SystemL::where('push_time','>=',$i)->where('push_time','<',$end)->delete();
+
+            foreach ($list['data'] as $v){
+                $deviceNo = $v['deviceId'];
+                foreach ($v['slaves'] as $vv){
+                    $slaveName = $vv['slaveName'];
+                    foreach ($vv['iotDataDescription'] as $vvv){
+                        $name = $vvv['name'];
+                        $id = $vvv['id'];
+                        $url = $getUrl.'/history/datapoint';
+                        $res =  $this->xlCurl($url,[
+                            'pageNo' => 1,
+                            'start' => $i,
+                            'end' => $end,
+                            'pageSize' => 1000,
+                            'devDatapoints' => [[
+                                'deviceNo' => $deviceNo,
+                                'slaveIndex' => 1,
+                                'itemId' => 1,
+                                'dataPointId' => $id,
+                            ]],
+                        ]);
+//                        file_put_contents('record_one.txt',$res,8);
+//                        dump($res);
+                        $res = json_decode($res,true);
+                        $insert = [];
+                        if(! empty($res['data']['list'])){
+                            foreach ($res['data']['list'] as $rv){
+                                foreach ($rv['list'] as $rvv){
+                                    $insert[] =  [
+                                        'device_name' => $rv['deviceName'],
+                                        'time' => $rvv['time'],
+                                        'value' => $rvv['value'],
+                                        'data_point_name' => $rv['dataPointName'],
+                                        'device_no' => $deviceNo,
+                                        'data_point_id' => $id,
+                                        'push_time' => $rvv['time'],
+                                        'slave_name' => $slaveName,
+                                    ];
+                                }
+                            }
+                            //dd($insert);
+                            //写入数据
+                            SystemL::insert($insert);
+                        }
+                        sleep(10);
+                    }
+                }
+            }
+        }
+    }
+
+    //发送请求
+    public function xlCurl($url,$data){
+        $token = $this->getToken();
+        return $this->curlOpen($url,['header'=>['Content-Type:application/json','X-Access-Token:'.$token,],'post'=>json_encode($data)]);
+    }
+}

+ 2 - 2
app/Service/DeviceService.php

@@ -120,7 +120,7 @@ class DeviceService extends Service
         return [true,$list];
     }
 
-    `public function fillData($data){
+    public function fillData($data){
         if(empty($data['data'])) return $data;
         $gatewayMap = Gateway::whereIn('id',array_unique(array_column($data['data'],'gateway_id')))
             ->pluck('code','id')
@@ -135,7 +135,7 @@ class DeviceService extends Service
         }
 
         return $data;
-    }`
+    }
 
     public function equipmentRule($data,$is_add = true){
         if(empty($data['title'])) return [false,'设备名称不能为空'];

+ 45 - 0
app/Service/ScreenService.php

@@ -6,6 +6,7 @@ use App\Model\Device;
 use App\Model\Screen;
 use App\Model\ScreenDevice;
 use App\Model\ScreenGateway;
+use App\Model\SystemL;
 use Illuminate\Support\Facades\DB;
 
 class ScreenService extends Service
@@ -190,4 +191,48 @@ class ScreenService extends Service
 
         return [true,''];
     }
+
+    public function screenDeviceDataRealTimeList($data){
+        if(empty($data['id'])) return [false,'请选择仓或台'];
+
+        //获取仓或台的设备ID
+        $device = ScreenDevice::where('del_time',0)
+            ->where('screen_id',$data['id'])
+            ->select('device_id')
+            ->get()->toArray();
+        $device = array_column($device,'device_id');
+
+        //设备的分组信息 按照设备类型
+        $device = Device::whereIn('id',$device)
+            ->select('code','device_type_id')
+            ->get()->toArray();
+        $nodeOne = $nodeTwo = $nodeThree = [];
+        foreach ($device as $value){
+            if($value['device_type_id'] == 3){
+                $nodeOne[] = $value['code'];
+            }elseif(in_array($value['device_type_id'],[1,2,7])){
+                $nodeTwo[] = $value['code'];
+            }else{
+                $nodeThree[] = $value['code'];
+            }
+        }
+
+        $posts_a = SystemL::whereIn('device_no', $nodeOne)
+            ->select('data_point_name','value')
+            ->orderBy('id','desc')
+            ->limit(10)
+            ->get()->toArray();
+        $posts_b = SystemL::whereIn('device_no', $nodeTwo)
+            ->select('data_point_name','value')
+            ->orderBy('id','desc')
+            ->limit(10)
+            ->get()->toArray();
+        $posts_c = SystemL::whereIn('device_no', $nodeThree)
+            ->select('data_point_name','value')
+            ->orderBy('id','desc')
+            ->limit(10)
+            ->get()->toArray();
+
+        return [true,['node_one' => $posts_a,'node_two'=>$posts_b,'node_three'=>$posts_c]];
+    }
 }

+ 1 - 0
routes/api.php

@@ -73,6 +73,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('screenDeviceList', 'Api\ScreenController@screenDeviceList');//仓绑定的设备列表
     $route->any('screenDevice', 'Api\ScreenController@screenDevice');//仓绑定设备
     $route->any('screenRemoveDevice', 'Api\ScreenController@screenRemoveDevice');//仓移除设备
+    $route->any('screenDeviceDataRealTimeList', 'Api\ScreenController@screenDeviceDataRealTimeList');//仓设备实时数据列表
 
     //暂时没用----------------------------
     //菜单