cqpCow 2 лет назад
Родитель
Сommit
8c6f5250cf

+ 16 - 0
app/Http/Controllers/Api/CloudDataController.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Jobs\CloudDataJob;
+use App\Jobs\ProcessDataJob;
+use App\Service\CloudDataService;
+use Illuminate\Http\Request;
+
+//有人云
+class CloudDataController extends BaseController
+{
+    public function getCloudData(Request $request){
+        CloudDataJob::dispatch([])->onQueue('cloud_has_man');
+    }
+}

+ 52 - 0
app/Jobs/CloudDataJob.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Service\CloudDataService;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Symfony\Component\Console\Output\ConsoleOutput;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class CloudDataJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $data;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($data)
+    {
+        $this->data = $data;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $this->settle();
+
+        //输出信息 测试
+        $this->echoMessage(new ConsoleOutput());
+    }
+
+    private function settle(){
+        $service = new CloudDataService();
+        $service->cloudData();
+    }
+
+    protected function echoMessage(OutputInterface $output)
+    {
+        $output->writeln($this->data);
+    }
+}

+ 14 - 0
app/Model/SystemL.php

@@ -0,0 +1,14 @@
+<?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';
+
+}

+ 129 - 0
app/Service/CloudDataService.php

@@ -0,0 +1,129 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\SystemL;
+use Illuminate\Support\Facades\Redis;
+
+class CloudDataService extends Service
+{
+    //密钥
+    private $token_key = '';
+    //appSecret
+    private $app_secret = 'ziou5spsyi9c947rasqajhwoejee1oq3';
+    //appKey
+    private $app_key = '7k8iwOdL';
+    //设备id
+    private $device = [
+        "01401422100800008703",
+        "01401422100800000103",
+        "01401422100800008976",
+        "01401422100800000342"
+    ];
+
+    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);
+
+        date_default_timezone_set("PRC");
+
+        $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)]);
+    }
+}