CloudDataService.php 4.7 KB

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