123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Services\Project;
- use GuzzleHttp\Client;
- use GuzzleHttp\Promise;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class ProjectBACnetService {
- protected string $baseProxyUrl;
- protected string $username;
- protected string $password;
- protected array $siteDataMap = [];
- public function __construct()
- {
- $this->baseProxyUrl = config('bacnet.base_proxy_url');
- $this->username = config('bacnet.auth.username');
- $this->password = config('bacnet.auth.password');
- $this->siteDataMap = config('bacnet.site_data_map');
- }
- private function getBasicAuth(): array
- {
- return [
- 'auth' => [
- $this->username,
- $this->password
- ]
- ];
- }
- public function fetchDeviceObjects($siteId, $deviceId): array
- {
- $objectList = $this->siteDataMap[$siteId]['objects'][$deviceId] ?? [];
- if (empty($objectList)) {
- return [];
- }
- $resData = [];
- $client = new Client();
- // 创建一个包含所有请求的数组
- $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());
- }
- try {
- // 发送并发请求并等待所有响应
- $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'],
- ];
- }
- }
- } catch (\Throwable $e) {
- Log::error('[fetchDeviceObjects] One of the batch requests failed: ' . $e->getMessage());
- }
- return $resData;
- }
- public function fetchObjectsValueMulti($siteId, $dataKey): array
- {
- $targetData = $this->siteDataMap[$siteId][$dataKey] ?? [];
- if (empty($targetData)) {
- return [];
- }
- $responseData = [];
- $data = [
- '$base' => 'Object',
- 'lifetime' => [
- '$base' => 'Unsigned',
- 'value' => 120,
- ],
- 'values' => [
- '$base' => 'List',
- ],
- ];
- foreach ($targetData as $dataRef) {
- $nameKey = "$dataRef/object-name";
- $valueKey = "$dataRef/present-value";
- $unitsKey = "$dataRef/units";
- $data['values'][$nameKey] = [
- '$base' => 'Any',
- 'via' => "/.bacnet/$siteId/$nameKey",
- ];
- $data['values'][$valueKey] = [
- '$base' => 'Any',
- 'via' => "/.bacnet/$siteId/$valueKey",
- ];
- $data['values'][$unitsKey] = [
- '$base' => 'Any',
- 'via' => "/.bacnet/$siteId/$unitsKey",
- ];
- }
- $url = $this->baseProxyUrl."/.multi?alt=json";
- $presentValueMap = $this->siteDataMap[$siteId]["{$dataKey}-present-value-map"] ?? [];
- $response = Http::withBasicAuth($this->username, $this->password)->post($url, $data);
- if ($response->successful()) {
- $json = json_decode($response->body(), true);
- foreach ($targetData as $dataRef) {
- $nameKey = "$dataRef/object-name";
- $valueKey = "$dataRef/present-value";
- $unitsKey = "$dataRef/units";
- $objectName = $json['values'][$nameKey]['value'] ?? '';
- $presentValue = $json['values'][$valueKey]['value'] ?? '';
- $units = $json['values'][$unitsKey]['value'] ?? '';
- $responseData[] = [
- 'displayName' => $objectName,
- 'present-value' => $presentValueMap[$presentValue] ?? $presentValue,
- 'units' => $units,
- ];
- }
- }
- return $responseData;
- }
- }
|