From 38d7013b9ad9fa2dcdfa18411b018fbe8148adbe Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Tue, 7 Jul 2026 15:22:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E8=BD=A6?= =?UTF-8?q?=E4=BD=8D=E7=9B=B8=E6=9C=BA=E7=81=AF=E5=85=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/ParkingAccessRecordController.php | 22 +- .../Device/CameraApiController.php | 21 +- app/Models/ParkingCameraSendLog.php | 22 ++ app/Models/ParkingSpaceCamera.php | 11 + app/Services/HikParkingCameraService.php | 227 +++++------------- app/Services/ParkingSpaceService.php | 49 ++-- ...9_create_parking_camera_send_log_table.php | 34 +++ routes/device/api.php | 1 + 8 files changed, 173 insertions(+), 214 deletions(-) create mode 100644 app/Models/ParkingCameraSendLog.php create mode 100644 database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php diff --git a/app/Http/Controllers/Admin/ParkingAccessRecordController.php b/app/Http/Controllers/Admin/ParkingAccessRecordController.php index 0c927dc..fe42ee1 100644 --- a/app/Http/Controllers/Admin/ParkingAccessRecordController.php +++ b/app/Http/Controllers/Admin/ParkingAccessRecordController.php @@ -186,17 +186,17 @@ class ParkingAccessRecordController extends BaseController 'status' => 1 ]); // 车灯变化 - $SpaceType = ParkingSpaceType::query()->find($space_type_id); - $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); - - $color_occupy = $SpaceType['default_color_occupy']; - $color_vacant = $SpaceType['default_color_vacant']; - $is_flicker = $SpaceType['default_is_warning']; - if ($SpaceTypeAttr) { - $color_occupy = $SpaceTypeAttr['color_occupy']; - $color_vacant = $SpaceTypeAttr['color_vacant']; - $is_flicker = $SpaceTypeAttr['is_warning']; - } +// $SpaceType = ParkingSpaceType::query()->find($space_type_id); +// $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); +// +// $color_occupy = $SpaceType['default_color_occupy']; +// $color_vacant = $SpaceType['default_color_vacant']; +// $is_flicker = $SpaceType['default_is_warning']; +// if ($SpaceTypeAttr) { +// $color_occupy = $SpaceTypeAttr['color_occupy']; +// $color_vacant = $SpaceTypeAttr['color_vacant']; +// $is_flicker = $SpaceTypeAttr['is_warning']; +// } DB::commit(); } catch (Exception $e) { diff --git a/app/Http/Controllers/Device/CameraApiController.php b/app/Http/Controllers/Device/CameraApiController.php index 1b28be5..13e5e1c 100644 --- a/app/Http/Controllers/Device/CameraApiController.php +++ b/app/Http/Controllers/Device/CameraApiController.php @@ -3,10 +3,10 @@ namespace App\Http\Controllers\Device; use App\Models\ParkingCamera; +use App\Services\HikParkingCameraService; use App\Services\ParkingCameraService; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Redis; class CameraApiController extends BaseController { @@ -46,13 +46,22 @@ class CameraApiController extends BaseController public function getCameraBody(): JsonResponse { - $body = Redis::get('camera_body'); + $body = HikParkingCameraService::getBody(); if (!$body) { return $this->responseService->error('empty'); - } else { - $bodyArr = json_decode($body); - Redis::delete('camera_body'); } - return $this->responseService->success($bodyArr, 'SUCCESS'); + return $this->responseService->success($body, 'SUCCESS'); + } + + // 变等完成回调完成 + public function setCameraBody(Request $request): JsonResponse + { + $data = $request->all(); + $id = $data['id'] ?? 0; + if ($id && is_numeric($id)) { + HikParkingCameraService::sendSuccessful($id); + return $this->responseService->success([], 'SUCCESS'); + } + return $this->responseService->error('empty'); } } diff --git a/app/Models/ParkingCameraSendLog.php b/app/Models/ParkingCameraSendLog.php new file mode 100644 index 0000000..838e9d3 --- /dev/null +++ b/app/Models/ParkingCameraSendLog.php @@ -0,0 +1,22 @@ +where('camera_id', $camera_id)->count(); } + + // 获取在线相机IP地址 + public static function getCameraIp($space_id) + { + $res = self::query()->where('space_id', $space_id)->first(); + if ($res) { + return ParkingCamera::query()->where('id', $res['camera_id']) + ->where('status', 1)->value('camera_ip'); + } + return ''; + } } diff --git a/app/Services/HikParkingCameraService.php b/app/Services/HikParkingCameraService.php index bb478a0..260e5b6 100644 --- a/app/Services/HikParkingCameraService.php +++ b/app/Services/HikParkingCameraService.php @@ -2,191 +2,72 @@ namespace App\Services; -use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Redis; +use App\Models\ParkingCameraSendLog; class HikParkingCameraService { - public array $device; - - public string $format = 'json'; - - protected string $MULTI_PARKING_SPACES_LAMP = '/ISAPI/Parking/parkingSpace/multiParkingSpacesLamp'; - - public function __construct($device) - { - $this->device = [ - 'protocol' => 'http', - 'ip' => '192.168.66.64', - 'port' => '', - 'username' => 'admin', - 'password' => 'Xingtong1124', - ]; - //$this->device = $device; - } - - public function getLight() + /** + * 创建变更设备信息 + * @param $camera_ip + * @param $color_occupy + * @param $color_vacant + * @param $is_flicker + * @return void + */ + public static function setBody($camera_ip, $color_occupy, $color_vacant, $is_flicker) { - $url = $this->getUri() . $this->MULTI_PARKING_SPACES_LAMP; - $query = [ - 'format' => $this->format, - 'security' => 1, - 'iv' => md5(rand('000000', '999999')) + $body = [ + 'camera_ip' => $camera_ip, + 'color_occupy' => $color_occupy, + 'color_vacant' => $color_vacant, + 'is_flicker' => $is_flicker ? 1 : 0, ]; - $url .= '?' . http_build_query($query); - - - - $response = Http::timeout(3) - ->withoutVerifying() - ->withDigestAuth($this->device['username'], $this->device['password']) - ->get($url); - //dd($response->json(), $response->body(), $response->object(), $response); - try { - $response = $response->json(); - if ($response->getStatusCode() == '200') { - - } - } catch (\Exception $e) { - throw $e; + $exists = ParkingCameraSendLog::query()->where($body)->where( + 'status', + 0 + )->exists(); + if (!$exists) { + $body['created_at'] = get_datetime(); + ParkingCameraSendLog::query()->create($body); } } - // 组合 设备ip地址 - protected function getUri() + /** + * 返回需要变更设备信息 + * @return array + */ + public static function getBody(): array { - $protocol = $this->device['protocol']; - $ip = $this->device['ip']; - $port = $this->device['port'] ?? ''; - $uri = $protocol . '://' . $ip; - if ($port) { - $uri .= ':' . $port; - } - return $uri; - } - - public 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" - ] - ] + $columns = [ + 'id', + 'camera_ip as ip', + 'color_occupy', + 'color_vacant', + 'is_flicker', ]; - return $body; + $body = ParkingCameraSendLog::query()->where('status', 0)->select( + $columns + )->get()->each(function ($item) { + $item['is_flicker'] = (bool)$item['is_flicker'] == 1; + return $item; + })->toArray(); + if (!$body) { + return []; + } + return ['list' => $body]; } - public function updatelight($body) + /** + * 变更成功回调 + * @param $id + * @return void + */ + public static function sendSuccessful($id): void { - /*$body = [ - "lampType" => "internal", - "VehicleNumber" => [ - "VehicleChannelNum" => 1, - "VehicleChannelList" => [ - [ - "LampSoure" => "internal", // unrelated - "VehicleNoExist" => [ - "enabled" => true, - "flashEnabled" => false, - "lampColor" => "close" - ], - "VehicleExist" => [ - "enabled" => true, - "flashEnabled" => false, - "lampColor" => "close" - ] - ] - ] - ], - "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" - ] - ] - ];*/ -// $url = $this->getUri() . $this->MULTI_PARKING_SPACES_LAMP; -// $query = [ -// 'format' => $this->format, -// 'security' => 1, -// 'iv' => md5(rand('000000', '999999')) -// ]; -// $url .= '?' . http_build_query($query); -// -// $response = Http::timeout(3) -// ->withoutVerifying() -// ->withDigestAuth($this->device['username'], $this->device['password']) -// ->put($url, $body); - - Redis::set('camera_body', json_encode($body), 300); - //dd($response->json(), $response->body(), $response->object(), $response); + $model = ParkingCameraSendLog::query()->find($id); + $model->update([ + 'status' => 1, + 'updated_at' => get_datetime() + ]); } } diff --git a/app/Services/ParkingSpaceService.php b/app/Services/ParkingSpaceService.php index 87435b8..dc97d46 100644 --- a/app/Services/ParkingSpaceService.php +++ b/app/Services/ParkingSpaceService.php @@ -7,6 +7,7 @@ use App\Models\AdminFloor; use App\Models\ParkingLicensePlate; use App\Models\ParkingSpace; use App\Models\ParkingSpaceAttributes; +use App\Models\ParkingSpaceCamera; use App\Models\ParkingSpaceType; use App\Models\ParkingSpaceTypeAttr; use Exception; @@ -440,34 +441,34 @@ class ParkingSpaceService extends BaseService ); // 切换车位类型,同步切换车位灯颜色 if ($oldValues['status'] != 2) { - $this->changeSpaceCameraColor($oldValues['number'], $type_id, $oldValues['space_attr_id']); + $this->changeSpaceCameraColor($id, $type_id, $oldValues['space_attr_id']); } } // 切换模式更改车位类型 后 更改车位灯颜色 - public function changeSpaceCameraColor($number, $space_type_id, $space_attr, $is_repair = false) + public function changeSpaceCameraColor($space_id, $space_type_id, $space_attr, $is_repair = false) { - if ($number == '1-1') { - $SpaceType = ParkingSpaceType::query()->find($space_type_id); - $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); - - $color_occupy = $SpaceType['default_color_occupy']; - $color_vacant = $SpaceType['default_color_vacant']; - $is_flicker = $SpaceType['default_is_warning']; - if ($SpaceTypeAttr) { - $color_occupy = $SpaceTypeAttr['color_occupy']; - $color_vacant = $SpaceTypeAttr['color_vacant']; - $is_flicker = $SpaceTypeAttr['is_warning']; - } - // 维修灯 - if ($is_repair) { - $color_occupy = $SpaceType['repair_light']; - $color_vacant = $SpaceType['repair_light']; - $is_flicker = false; - } - $HikParkingCameraService = new HikParkingCameraService($this->logService); - $body = $HikParkingCameraService->getBody($color_occupy, $color_vacant, $is_flicker); - $HikParkingCameraService->updatelight($body); + $SpaceType = ParkingSpaceType::query()->find($space_type_id); + $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); + + $color_occupy = $SpaceType['default_color_occupy']; + $color_vacant = $SpaceType['default_color_vacant']; + $is_flicker = $SpaceType['default_is_warning']; + if ($SpaceTypeAttr) { + $color_occupy = $SpaceTypeAttr['color_occupy']; + $color_vacant = $SpaceTypeAttr['color_vacant']; + $is_flicker = $SpaceTypeAttr['is_warning']; + } + // 维修灯 + if ($is_repair) { + $color_occupy = $SpaceType['repair_light']; + $color_vacant = $SpaceType['repair_light']; + $is_flicker = false; + } + + $camera_ip = ParkingSpaceCamera::getCameraIp($space_id); + if ($camera_ip) { + HikParkingCameraService::setBody($camera_ip, $color_occupy, $color_vacant, $is_flicker); } } @@ -610,7 +611,7 @@ class ParkingSpaceService extends BaseService $space_type_id = $oldValues['space_type_id']; // 修改为类型的维修指定灯光 $this->changeSpaceCameraColor( - $number, + $id, $space_type_id, $space_attr_id, $status == 2 diff --git a/database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php b/database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php new file mode 100644 index 0000000..60a489b --- /dev/null +++ b/database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('camera_ip', 15)->default('')->comment('相机ip'); + $table->string('color_occupy', 20)->default('')->comment('有车颜色'); + $table->string('color_vacant', 20)->default('')->comment('无车颜色'); + $table->tinyInteger('is_flicker')->default(0)->comment('是否闪烁'); + $table->tinyInteger('status')->default(0)->comment('发送状态'); + $table->timestamps(); + $table->innoDb(); + $table->comment('车位相机推送记录'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('parking_camera_send_log'); + } +}; diff --git a/routes/device/api.php b/routes/device/api.php index d69d755..174fb1f 100644 --- a/routes/device/api.php +++ b/routes/device/api.php @@ -11,5 +11,6 @@ Route::group(['prefix' => 'device'], function () { Route::put('/camera/getStatus', [CameraApiController::class, 'updateCameraStatus']); // 获取车位相机设备更新信息 Route::get('/camera/getBody', [CameraApiController::class, 'getCameraBody']); + Route::put('/camera/getBody', [CameraApiController::class, 'setCameraBody']); });