CloudDataService.php 4.7 KB

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