| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | <?phpnamespace App\Service;use App\Model\SystemL;use Illuminate\Support\Facades\Redis;/** * 获取机器数据 * Class CloudDataService * @package App\Service */class CloudDataService extends Service{    //密钥    private $token_key = 'hcsy';    //appSecret    private $app_secret = 'ziou5spsyi9c947rasqajhwoejee1oq3';    //appKey    private $app_key = '7k8iwOdL';    //设备id    private $device = [        "01401424101800006540",        "01401424101800006626",        "01401424101800006231",        "01401424101800006489",    ];    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,                                    ];                                }                            }                            //写入数据                            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)]);    }}
 |