|
@@ -1,37 +1,77 @@
|
|
|
<?php
|
|
|
namespace App\Services\Project;
|
|
|
-use Illuminate\Support\Facades\Http;
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use GuzzleHttp\Promise;
|
|
|
|
|
|
class ProjectBACnetService {
|
|
|
protected string $baseProxyUrl;
|
|
|
- protected string $siteId;
|
|
|
protected array $queryParams = [
|
|
|
- 'skip' => 0,
|
|
|
- 'max-results' => 15,
|
|
|
'alt' => 'json'
|
|
|
];
|
|
|
protected string $username;
|
|
|
protected string $password;
|
|
|
+ protected array $siteMap = [
|
|
|
+ /*site*/
|
|
|
+ 'IFC' => [
|
|
|
+ /*our device number*/
|
|
|
+ 'CH1' => [
|
|
|
+ /*object ref*/
|
|
|
+ '600/analog-value,6',
|
|
|
+ '600/analog-value,7',
|
|
|
+ '600/analog-value,8',
|
|
|
+ '600/analog-value,9',
|
|
|
+ '500/analog-value,1407',
|
|
|
+ '500/analog-value,1408',
|
|
|
+ ],
|
|
|
+ ]
|
|
|
+ ];
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->baseProxyUrl = config('bacnet.base_proxy_url');
|
|
|
- $this->siteId = config('bacnet.site_id');
|
|
|
$this->username = config('bacnet.auth.username');
|
|
|
$this->password = config('bacnet.auth.password');
|
|
|
}
|
|
|
|
|
|
- public function fetchDeviceObjects($params = [])
|
|
|
+ private function getBasicAuth() {
|
|
|
+ return [
|
|
|
+ 'auth' => [
|
|
|
+ $this->username,
|
|
|
+ $this->password
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fetchDeviceObjects($siteId, $deviceId): array
|
|
|
{
|
|
|
- $url = "{$this->baseProxyUrl}/{$this->siteId}/{$params['deviceId']}";
|
|
|
- $response = Http::withBasicAuth($this->username, $this->password)
|
|
|
- ->timeout(30)
|
|
|
- ->get($url, $this->queryParams);
|
|
|
+ $objectList = $this->siteMap[$siteId][$deviceId];
|
|
|
+ if (empty($objectList)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $resData = [];
|
|
|
+ $client = new Client();
|
|
|
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- } else {
|
|
|
- throw new \Exception('API request failed: ' . $response->body());
|
|
|
+ // 创建一个包含所有请求的数组
|
|
|
+ $promises = [];
|
|
|
+ for ($i = 0; $i < count($objectList); $i++) {
|
|
|
+ $objectRef = $objectList[$i];
|
|
|
+ $url = $this->baseProxyUrl."/.bacnet/$siteId/$objectRef?alt=json";
|
|
|
+ $promises[$objectRef] = $client->getAsync($url, $this->getBasicAuth());
|
|
|
}
|
|
|
+
|
|
|
+ // 发送并发请求并等待所有响应
|
|
|
+ $responses = Promise\Utils::settle($promises)->wait();
|
|
|
+ foreach ($responses as $response) {
|
|
|
+ if ($response['state'] == "fulfilled") {
|
|
|
+ $data = json_decode($response['value']->getBody()->getContents(), true);
|
|
|
+ $resData[] = [
|
|
|
+ 'displayName' => $data['displayName'],
|
|
|
+ 'present-value' => $data['present-value']['value'],
|
|
|
+ 'units' => $data['units']['value'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $resData;
|
|
|
}
|
|
|
}
|