From bece21c9c153e28c176f1938ec9142391e8ad608 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Fri, 3 Jul 2026 15:11:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E7=8A=B6?= =?UTF-8?q?=E6=80=81=EF=BC=8C=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/CheckEquipmentStatus.php | 30 +++- app/Console/Commands/SetCameraData.php | 82 +++------- app/Services/Api/BaseApiService.php | 38 +++++ app/Services/Api/CameraApiService.php | 48 ++++++ app/Services/BaseService.php | 48 ------ app/Services/CameraDeviceService.php | 77 ---------- app/Services/Device/BaseService.php | 63 ++++++++ app/Services/Device/CameraDeviceService.php | 144 ++++++++++++++++++ 8 files changed, 334 insertions(+), 196 deletions(-) create mode 100644 app/Services/Api/BaseApiService.php create mode 100644 app/Services/Api/CameraApiService.php delete mode 100644 app/Services/BaseService.php delete mode 100644 app/Services/CameraDeviceService.php create mode 100644 app/Services/Device/BaseService.php create mode 100644 app/Services/Device/CameraDeviceService.php diff --git a/app/Console/Commands/CheckEquipmentStatus.php b/app/Console/Commands/CheckEquipmentStatus.php index 9833f7c..1d7b4eb 100644 --- a/app/Console/Commands/CheckEquipmentStatus.php +++ b/app/Console/Commands/CheckEquipmentStatus.php @@ -2,7 +2,8 @@ namespace App\Console\Commands; -use App\Services\CameraDeviceService; +use App\Services\Api\CameraApiService; +use App\Services\Device\CameraDeviceService; use Illuminate\Console\Command; /** @@ -15,7 +16,7 @@ class CheckEquipmentStatus extends Command * * @var string */ - protected $signature = 'app:check-equipment-status'; + protected $signature = 'run:check-equipment-status'; /** * The console command description. @@ -37,11 +38,24 @@ class CheckEquipmentStatus extends Command public function handle() { // 获取所有设备信息 - $ip = '192.168.66.16'; - $this->service = new CameraDeviceService($ip); - - // 通过设备信息获取设备状态 - - // 返回告诉设备状态情况 + $data = CameraApiService::query()->getCameraStatus(); + if ($data && isset($data['body']['list'])) { + $list = $data['body']['list']; + // 通过设备信息获取设备状态 + foreach ($list as $key => $item) { + $ip = $item['camera_ip']; + try { + $statusBool = CameraDeviceService::query($ip)->getStatus(); + } catch (\Exception $e) { + $statusBool = false; + } + $list[$key]['status'] = $statusBool ? 1 : 0; + } + // 返回告诉设备状态情况 + CameraApiService::query()->putCameraStatus(['list' => $list]); + echo 'SUCCESS'; + } + echo 'empty'; + exit(); } } diff --git a/app/Console/Commands/SetCameraData.php b/app/Console/Commands/SetCameraData.php index 13e4dd9..40e685f 100644 --- a/app/Console/Commands/SetCameraData.php +++ b/app/Console/Commands/SetCameraData.php @@ -2,8 +2,8 @@ namespace App\Console\Commands; -use App\Services\CameraDeviceService; -use GuzzleHttp\Client; +use App\Services\Api\CameraApiService; +use App\Services\Device\CameraDeviceService; use Illuminate\Console\Command; /** @@ -23,79 +23,35 @@ class SetCameraData extends Command */ protected $description = 'Command description'; - protected string $sysUrl; - protected string $sysPost; - protected string $sysHttp; - - public function __construct() - { - parent::__construct(); - $this->sysUrl = env('SYS_URL', '192.168.66.16'); - $this->sysPost = env('SYS_POST', ''); - $this->sysHttp = env('SYS_HTTP', 'http'); - } - /** * Execute the console command. */ public function handle() { // 获取所有设备信息 - $ip = '192.168.66.64'; - $service = new CameraDeviceService($ip); - $url = $this->sysHttp . '://' . $this->sysUrl; - $Host = $this->sysUrl; - if ($this->sysPost) { - $url .= ':' . $this->sysPost; - $Host .= ':' . $this->sysPost; - } - - $curl = curl_init(); - - curl_setopt_array($curl, [ - CURLOPT_PORT => $this->sysPost, - CURLOPT_URL => $url . "/api/device/camera/getBody", - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => "GET", - CURLOPT_POSTFIELDS => "", - CURLOPT_HTTPHEADER => [ - "Accept: */*", - "Accept-Encoding: gzip, deflate, br", - "Cache-Control: no-cache", - "Connection: keep-alive", - "Host: {$Host}", - "User-Agent: PostmanRuntime-ApipostRuntime/1.1.0" - ], - ]); - - $response = curl_exec($curl); - $err = curl_error($curl); - - curl_close($curl); - - if ($err) { - echo "cURL Error #:" . $err; - exit; - } + $response = CameraApiService::query()->getCameraBody(); if ($response) { - $resData = json_decode($response, true); - if (isset($resData['error']) && $resData['error'] < 1) { - $body = $resData['body']; + if (isset($response['error']) && $response['error'] < 1) { + $body = $response['body']; + foreach ($body['list'] as $item) { + // 通过设备信息获取设备状态 + $ip = $item['ip']; + $color_occupy = $item['color_occupy']; + $color_vacant = $item['color_vacant']; + $is_flicker = $item['is_flicker']; + $service = CameraDeviceService::query($ip)->setColor( + $color_occupy, + $color_vacant, + $is_flicker + ); + // 返回告诉设备状态情况 + } } else { - echo $resData['message'] ?? ''; + echo $response['message'] ?? ''; exit; } - // 通过设备信息获取设备状态 - - $service->setColor($body); } - echo 'SUCCESS'; exit; - // 返回告诉设备状态情况 } } diff --git a/app/Services/Api/BaseApiService.php b/app/Services/Api/BaseApiService.php new file mode 100644 index 0000000..a26cdbb --- /dev/null +++ b/app/Services/Api/BaseApiService.php @@ -0,0 +1,38 @@ +hostUrl = env('SYS_URL', ''); + } + + /** + * @param string $url + * @param array $query + * @return Response + */ + public function getHttp(string $url, array $query = []): Response + { + $getUrl = $this->hostUrl . $url; + return Http::get($getUrl, $query); + } + + /** + * @param string $url + * @param array $data + * @return Response + */ + public function putHttp(string $url, array $data): Response + { + $getUrl = $this->hostUrl . $url; + return Http::put($getUrl, $data); + } +} diff --git a/app/Services/Api/CameraApiService.php b/app/Services/Api/CameraApiService.php new file mode 100644 index 0000000..71d3606 --- /dev/null +++ b/app/Services/Api/CameraApiService.php @@ -0,0 +1,48 @@ +getHttp($url); + if ($Response->successful()) { + return json_decode($Response->body(), true); + } + return []; + } + + // 获取车位相机设备信息 + public function getCameraStatus() + { + $url = '/api/device/camera/getStatus'; + $Response = $this->getHttp($url); + if ($Response->successful()) { + return json_decode($Response->body(), true); + } + return []; + } + + // 获取变更车位相机设备状态 + public function putCameraStatus($data) + { + $url = '/api/device/camera/getStatus'; + $Response = $this->putHttp($url, $data); + if ($Response->successful()) { + return json_decode($Response->body(), true); + } + return []; + } +} diff --git a/app/Services/BaseService.php b/app/Services/BaseService.php deleted file mode 100644 index 8916ce7..0000000 --- a/app/Services/BaseService.php +++ /dev/null @@ -1,48 +0,0 @@ -device = [ - 'protocol' => env('DEVICE_PROTOCOL', 'http'), - 'ip' => $ip, - 'port' => '', - 'username' => env('DEVICE_USERNAME', 'admin'), - 'password' => env('DEVICE_PASSWORD', 'Xingtong1124'), - ]; - } - - protected function getUri() - { - $protocol = $this->device['protocol']; - $ip = $this->device['ip']; - $port = $this->device['port'] ?? ''; - $uri = $protocol . '://' . $ip; - if ($port) { - $uri .= ':' . $port; - } - return $uri; - } - - - protected function getUrl($uri) - { - $url = $this->getUri() . $uri; - $query = [ - 'format' => $this->format, - 'security' => 1, - 'iv' => md5(rand('000000', '999999')) - ]; - return $url . '?' . http_build_query($query); - } -} diff --git a/app/Services/CameraDeviceService.php b/app/Services/CameraDeviceService.php deleted file mode 100644 index c128d12..0000000 --- a/app/Services/CameraDeviceService.php +++ /dev/null @@ -1,77 +0,0 @@ -getUrl($this->MULTI_PARKING_SPACES_LAMP); - $response = Http::timeout(3) - ->withoutVerifying() - ->withDigestAuth($this->device['username'], $this->device['password']) - ->put($url, $body);//$this->getBody($color_occupy, $color_vacant, $is_flicker) - } - - protected function getBody($color_occupy, $color_vacant, $is_flicker) - { - $is_flicker = (bool)$is_flicker; - $body = [ - "lampType" => "internal", - "VehicleNumber" => [ - "VehicleChannelNum" => 1, - "VehicleChannelList" => [ - [ - "LampSoure" => "internal", // unrelated - "VehicleNoExist" => [ - "enabled" => true, - "flashEnabled" => $is_flicker, - "lampColor" => $color_vacant - ], - "VehicleExist" => [ - "enabled" => true, - "flashEnabled" => $is_flicker, - "lampColor" => $color_occupy - ] - ] - ] - ], - "replaceLampCtrl" => [ - "enabled" => false, - "ipV4Address" => "0.0.0.0", - "portNo" => 80, - "username" => "admin", - "password" => "dddf4589f3430f7b7395bb9d35e05b27" - ], - "lampStatusArr" => [ - [ - "name" => "内置灯", - "value" => "相机自控", - "\$\$hashKey" => "062" - ], - [ - "name" => "外置灯1", - "value" => "相机自控", - "\$\$hashKey" => "063" - ], - [ - "name" => "外置灯2", - "value" => "相机自控", - "\$\$hashKey" => "064" - ], - [ - "name" => "外置灯3", - "value" => "相机自控", - "\$\$hashKey" => "065" - ] - ] - ]; - return $body; - } - -} diff --git a/app/Services/Device/BaseService.php b/app/Services/Device/BaseService.php new file mode 100644 index 0000000..0e008e8 --- /dev/null +++ b/app/Services/Device/BaseService.php @@ -0,0 +1,63 @@ +device = [ + 'protocol' => env('DEVICE_PROTOCOL', 'http'), + 'ip' => $ip, + 'port' => $port, + 'username' => env('DEVICE_USERNAME', 'admin'), + 'password' => env('DEVICE_PASSWORD', 'Xingtong1124'), + ]; + } + + protected function getUrl($uri): string + { + $url = $this->getUri() . $uri; + $query = [ + 'format' => $this->format, + 'security' => 1, + 'iv' => md5(rand('000000', '999999')) + ]; + return $url . '?' . http_build_query($query); + } + + protected function getUri(): string + { + $protocol = $this->device['protocol']; + $ip = $this->device['ip']; + $port = $this->device['port'] ?? ''; + $uri = $protocol . '://' . $ip; + if ($port) { + $uri .= ':' . $port; + } + return $uri; + } + + protected function putHttp($url, $body) + { + return Http::timeout(3)->withoutVerifying()->withDigestAuth( + $this->device['username'], + $this->device['password'] + )->put($url, $body); + } + + protected function getHttp($url, $query = null) + { + return Http::timeout(3)->withoutVerifying()->withDigestAuth( + $this->device['username'], + $this->device['password'] + )->get($url, $query); + } +} diff --git a/app/Services/Device/CameraDeviceService.php b/app/Services/Device/CameraDeviceService.php new file mode 100644 index 0000000..955bbc2 --- /dev/null +++ b/app/Services/Device/CameraDeviceService.php @@ -0,0 +1,144 @@ +getUrl($this->MULTI_PARKING_SPACES_LAMP); + $body = $this->getColorBody($color_occupy, $color_vacant, $is_flicker); + if (!$body) { + return []; + } + $response = $this->putHttp($url, $body); + return json_decode($response->body(), true); + } + + /** + * 获取车位相机信息 + * @param $color_occupy + * @param $color_vacant + * @param $is_flicker + * @return array + */ + public function getColorBody( + $color_occupy, + $color_vacant, + $is_flicker + ): array { + $is_flicker = (bool)$is_flicker; + $url = $this->getUrl($this->MULTI_PARKING_SPACES_LAMP); + $response = $this->getHttp($url); + $body = json_decode($response->body(), true); + if ($body) { + if (isset($body['VehicleNumber']['VehicleChannelList'][0])) { + $VehicleChannelList + = $body['VehicleNumber']['VehicleChannelList'][0]; + if (isset($VehicleChannelList['VehicleNoExist'])) { + // 是否闪烁 + $body['VehicleNumber']['VehicleChannelList'][0]['VehicleNoExist']['flashEnabled'] + = $is_flicker; + // 无车显示颜色 + $body['VehicleNumber']['VehicleChannelList'][0]['VehicleNoExist']['lampColor'] + = $color_vacant; + } + if (isset($VehicleChannelList['VehicleExist'])) { + // 是否闪烁 + $body['VehicleNumber']['VehicleChannelList'][0]['VehicleExist']['flashEnabled'] + = $is_flicker; + // 有车显示颜色 + $body['VehicleNumber']['VehicleChannelList'][0]['VehicleExist']['lampColor'] + = $color_occupy; + } + } + } else { + return []; + } + return $body; + } + + public function getStatus(): bool + { + $url = $this->getUrl($this->SYSTEM_STATUS); + $response = $this->getHttp($url); + return $response->successful(); + } + + /* + * body 信息 + [ + "lampType" => "internal", + "VehicleNumber" => [ + "VehicleChannelNum" => 1, + "VehicleChannelList" => [ + [ + "LampSoure" => "internal", // unrelated + "VehicleNoExist" => [ + "enabled" => true, + "flashEnabled" => $is_flicker, + "lampColor" => $color_vacant + ], + "VehicleExist" => [ + "enabled" => true, + "flashEnabled" => $is_flicker, + "lampColor" => $color_occupy + ] + ] + ] + ], + "replaceLampCtrl" => [ + "enabled" => false, + "ipV4Address" => "0.0.0.0", + "portNo" => 80, + "username" => "admin", + "password" => "dddf4589f3430f7b7395bb9d35e05b27" + ], + "lampStatusArr" => [ + [ + "name" => "内置灯", + "value" => "相机自控", + "\$\$hashKey" => "062" + ], + [ + "name" => "外置灯1", + "value" => "相机自控", + "\$\$hashKey" => "063" + ], + [ + "name" => "外置灯2", + "value" => "相机自控", + "\$\$hashKey" => "064" + ], + [ + "name" => "外置灯3", + "value" => "相机自控", + "\$\$hashKey" => "065" + ] + ] + ] + */ +}