CloudDataService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Service;
  3. use App\Model\SystemL;
  4. use Illuminate\Support\Facades\Redis;
  5. /**
  6. * 获取机器数据
  7. * Class CloudDataService
  8. * @package App\Service
  9. */
  10. class CloudDataService extends Service
  11. {
  12. //密钥
  13. private $token_key = '';
  14. //appSecret
  15. private $app_secret = 'ziou5spsyi9c947rasqajhwoejee1oq3';
  16. //appKey
  17. private $app_key = '7k8iwOdL';
  18. //设备id
  19. private $device = [
  20. // "01401422100800008703",
  21. "01401422100800000103",
  22. "01401422100800008976",
  23. "01401422100800000342",
  24. "01401422102400001960"
  25. ];
  26. public function cloudData(){
  27. $this->getAllDevice();
  28. }
  29. //获取token
  30. public function getToken(){
  31. $token = Redis::get($this->token_key);
  32. if(! empty($token)) return $token;
  33. //接口地址
  34. $url = 'https://openapi.mp.usr.cn/usrCloud/user/getAuthToken';
  35. //参数
  36. $app_message = [
  37. 'appSecret' => $this->app_secret,
  38. 'appKey' => $this->app_key,
  39. ];
  40. //发送请求
  41. $result = $this->curlOpen($url,['header'=>['Content-Type:application/json'],'post'=>json_encode($app_message)]);
  42. if(empty($result)) die('err');
  43. $res = json_decode($result,true);
  44. //设置token缓存
  45. $token = $res['data']['X-Access-Token'];
  46. Redis::setex($this->token_key,(3600*1.5),$token);
  47. return $token;
  48. }
  49. //获取数据
  50. public function getAllDevice(){
  51. $url = 'https://openapi.mp.usr.cn/usrCloud/datadic/getDataPointInfoByDevice';
  52. $res = $this->xlCurl($url,['deviceIds' => $this->device]);
  53. // $getUrl = $this->xlCurl('https://openapi.mp.usr.cn/usrCloud/vn/ucloudSdk/getHistoryServerAddress',[]);
  54. // if(isset(json_decode($getUrl,true)['data']['historyServerAddr'])) $getUrl = json_decode($getUrl,true)['data']['historyServerAddr'];
  55. // else $getUrl = 'https://history.usr.cn:7002';
  56. $getUrl = 'https://history.usr.cn:7002';
  57. $list = json_decode($res,true);
  58. $now = strtotime(date('Y-m-d')) * 1000;
  59. $start = $now;
  60. //目前设置的今天的开始时间戳 for循环一次
  61. for ($i = $start; $i <= $now; $i = $i + 86400 * 1000){
  62. $end = $i + 86400 * 1000;
  63. SystemL::where('push_time','>=',$i)->where('push_time','<',$end)->delete();
  64. foreach ($list['data'] as $v){
  65. $deviceNo = $v['deviceId'];
  66. foreach ($v['slaves'] as $vv){
  67. $slaveName = $vv['slaveName'];
  68. foreach ($vv['iotDataDescription'] as $vvv){
  69. $name = $vvv['name'];
  70. $id = $vvv['id'];
  71. $url = $getUrl.'/history/datapoint';
  72. $res = $this->xlCurl($url,[
  73. 'pageNo' => 1,
  74. 'start' => $i,
  75. 'end' => $end,
  76. 'pageSize' => 1000,
  77. 'devDatapoints' => [[
  78. 'deviceNo' => $deviceNo,
  79. 'slaveIndex' => 1,
  80. 'itemId' => 1,
  81. 'dataPointId' => $id,
  82. ]],
  83. ]);
  84. // file_put_contents('record_one.txt',$res,8);
  85. // dump($res);
  86. $res = json_decode($res,true);
  87. $insert = [];
  88. if(! empty($res['data']['list'])){
  89. foreach ($res['data']['list'] as $rv){
  90. foreach ($rv['list'] as $rvv){
  91. $insert[] = [
  92. 'device_name' => $rv['deviceName'],
  93. 'time' => $rvv['time'],
  94. 'value' => $rvv['value'],
  95. 'data_point_name' => $rv['dataPointName'],
  96. 'device_no' => $deviceNo,
  97. 'data_point_id' => $id,
  98. 'push_time' => $rvv['time'],
  99. 'slave_name' => $slaveName,
  100. ];
  101. }
  102. }
  103. //dd($insert);
  104. //写入数据
  105. SystemL::insert($insert);
  106. }
  107. sleep(10);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. //发送请求
  114. public function xlCurl($url,$data){
  115. $token = $this->getToken();
  116. return $this->curlOpen($url,['header'=>['Content-Type:application/json','X-Access-Token:'.$token,],'post'=>json_encode($data)]);
  117. }
  118. }