ZQDcsApiService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Modules\OpcData\Services\zaoquan;
  3. use GuzzleHttp\Client;
  4. class ZQDcsApiService
  5. {
  6. protected $client;
  7. public function __construct()
  8. {
  9. $this->client = new Client();
  10. }
  11. public function getApiData($apiUrl, $queryParams='')
  12. {
  13. // 发送 HTTP GET 请求
  14. $response = $this->client->get($apiUrl, [
  15. 'query' => $queryParams,
  16. // 可以添加其他选项,如 headers、auth 等
  17. ]);
  18. // 获取 API 响应的 JSON 数据
  19. $data = json_decode($response->getBody(), true);
  20. return $data;
  21. }
  22. // 获取实时数据
  23. public function postPointRealData($apiUrl, $pointIds) {
  24. // 发送 HTTP POST 请求
  25. $response = $this->client->post($apiUrl, [
  26. 'headers' => [
  27. 'Authorization' => 'Bearer appkey_100100',
  28. 'Content-Type'=>'application/json; charset=utf-8',
  29. // 其他头部信息
  30. ],
  31. 'json' =>[
  32. 'PageNum'=>"-1",
  33. 'PointIds' => $pointIds
  34. ]
  35. ]);
  36. // 获取 API 响应的 JSON 数据
  37. $res = json_decode($response->getBody(), true);
  38. if($res['IsSuccessful'] == 0) {
  39. return $res;
  40. }
  41. $data = isset($res['Data'])? $res['Data']: null;
  42. $transformedData['data'] = $this->transformData($data);
  43. $transformedData['IsSuccessful'] =1;
  44. return $transformedData;
  45. }
  46. // 获取历史数据
  47. public function postDcsHisData($pointIds, $startTime, $endTime)
  48. {
  49. $apiUrl = "http://7.250.4.3:4200/v1/common/GetPointDataFromInfluxDB";
  50. // 发送 HTTP POST 请求
  51. $response = $this->client->post($apiUrl, [
  52. 'headers' => [
  53. 'Authorization' => 'Bearer appkey_100100',
  54. 'Content-Type'=>'application/json; charset=utf-8',
  55. // 其他头部信息
  56. ],
  57. 'json' =>[
  58. 'PointValueTypeID'=>"1",
  59. 'IDs' => $pointIds,
  60. 'StartTime' => $startTime,
  61. 'EndTime' => $endTime
  62. ]
  63. ]);
  64. // 获取 API 响应的 JSON 数据
  65. $res = json_decode($response->getBody(), true);
  66. if($res['IsSuccessful'] == 0) {
  67. return $res;
  68. }
  69. $data = isset($res['Data'])? $res['Data']: null;
  70. $transformedData['data'] = $this->transformData($data);
  71. $transformedData['IsSuccessful'] =1;
  72. return $transformedData;
  73. }
  74. public function postApiData($apiUrl, $postData = '')
  75. {
  76. // 发送 HTTP POST 请求
  77. $response = $this->client->post($apiUrl, $postData);
  78. // 发送 HTTP POST 请求
  79. $response = $this->client->post($apiUrl, [
  80. 'headers' => [
  81. 'Authorization' => 'Bearer appkey_100100',
  82. 'Content-Type'=>'application/json; charset=utf-8',
  83. // 其他头部信息
  84. ],
  85. 'json' =>[
  86. 'PageNum'=>"-1"
  87. ]
  88. ]);
  89. return $response;
  90. // 获取 API 响应的 JSON 数据
  91. $data = json_decode($response->getBody(), true);
  92. // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  93. $transformedData = $this->transformData($data);
  94. return $transformedData;
  95. }
  96. protected function transformData($data)
  97. {
  98. $transformedData=[];
  99. for ($i = 0; $i < count($data); $i++) {
  100. $dic = $data[$i];
  101. $pid = $dic['ID'];
  102. $val = $dic['V'];
  103. $transformedData[$pid]=$val;
  104. }
  105. return $transformedData;
  106. }
  107. }