ZQDcsApiService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 'IDs' => $pointIds,
  59. 'StartTime' => $startTime,
  60. 'EndTime' => $endTime
  61. ]
  62. ]);
  63. // 获取 API 响应的 JSON 数据
  64. $res = json_decode($response->getBody(), true);
  65. if($res['IsSuccessful'] == 0) {
  66. return $res;
  67. }
  68. $data = isset($res['Data'])? $res['Data']: null;
  69. return $data;
  70. }
  71. public function postApiData($apiUrl, $postData = '')
  72. {
  73. // 发送 HTTP POST 请求
  74. $response = $this->client->post($apiUrl, $postData);
  75. // 发送 HTTP POST 请求
  76. $response = $this->client->post($apiUrl, [
  77. 'headers' => [
  78. 'Authorization' => 'Bearer appkey_100100',
  79. 'Content-Type'=>'application/json; charset=utf-8',
  80. // 其他头部信息
  81. ],
  82. 'json' =>[
  83. 'PageNum'=>"-1"
  84. ]
  85. ]);
  86. return $response;
  87. // 获取 API 响应的 JSON 数据
  88. $data = json_decode($response->getBody(), true);
  89. // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  90. $transformedData = $this->transformData($data);
  91. return $transformedData;
  92. }
  93. protected function transformData($data)
  94. {
  95. $transformedData=[];
  96. for ($i = 0; $i < count($data); $i++) {
  97. $dic = $data[$i];
  98. $pid = $dic['ID'];
  99. $val = $dic['V'];
  100. $transformedData[$pid]=$val;
  101. }
  102. return $transformedData;
  103. }
  104. }