|
@@ -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)]);
|
|
|
+ }
|
|
|
+}
|