Browse Source

BACnet api

peterguo 3 months ago
parent
commit
e094fde115

+ 3 - 3
app/Http/Controllers/API/ProjectController.php

@@ -728,8 +728,8 @@ class ProjectController extends Controller
         ]);
     }
 
-    public function iotDevice(){
-        $data = (new ProjectBACnetService())->fetchDeviceObjects(['deviceId' => '100.AV1']);
-        return $this->success($data ?? []);
+    public function fetchDeviceObjects(Request $request, string $siteId, string $deviceId){
+        $data = (new ProjectBACnetService())->fetchDeviceObjects($siteId, $deviceId);
+        return $this->success(['data' => $data], [],JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
     }
 }

+ 54 - 14
app/Services/Project/ProjectBACnetService.php

@@ -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;
     }
 }

+ 2 - 2
routes/api.php

@@ -200,8 +200,8 @@ Route::middleware(['auth:sanctum','account.limit'])->group(function () {
             ->name("project.4s");
         Route::get("project-iot", [API\ProjectController::class, "iot"])
             ->name("project.iot");
-        Route::get("project-iot/{siteName}/{deviceNumber}", [API\ProjectController::class, "iotDevice"])
-            ->name("project.iotDevice");
+        Route::get("project-iot/{siteName}/{deviceNumber}", [API\ProjectController::class, "fetchDeviceObjects"])
+            ->name("project.fetchDeviceObjects");
 
 //        Route::get("project/{project}/not-link-asset-requirement", [API\ProjectController::class, "notLinkAssetRequirement"])
 //            ->name("project.not-link-asset-requirement"); //项目未关联的资产需求