ZQDcsApiService.php 3.6 KB

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