Browse Source

自动更新车位相机灯光

master
wanghongjun 3 weeks ago
parent
commit
38d7013b9a
  1. 22
      app/Http/Controllers/Admin/ParkingAccessRecordController.php
  2. 21
      app/Http/Controllers/Device/CameraApiController.php
  3. 22
      app/Models/ParkingCameraSendLog.php
  4. 11
      app/Models/ParkingSpaceCamera.php
  5. 227
      app/Services/HikParkingCameraService.php
  6. 49
      app/Services/ParkingSpaceService.php
  7. 34
      database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php
  8. 1
      routes/device/api.php

22
app/Http/Controllers/Admin/ParkingAccessRecordController.php

@ -186,17 +186,17 @@ class ParkingAccessRecordController extends BaseController
'status' => 1 'status' => 1
]); ]);
// 车灯变化 // 车灯变化
$SpaceType = ParkingSpaceType::query()->find($space_type_id); // $SpaceType = ParkingSpaceType::query()->find($space_type_id);
$SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr); // $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr);
//
$color_occupy = $SpaceType['default_color_occupy']; // $color_occupy = $SpaceType['default_color_occupy'];
$color_vacant = $SpaceType['default_color_vacant']; // $color_vacant = $SpaceType['default_color_vacant'];
$is_flicker = $SpaceType['default_is_warning']; // $is_flicker = $SpaceType['default_is_warning'];
if ($SpaceTypeAttr) { // if ($SpaceTypeAttr) {
$color_occupy = $SpaceTypeAttr['color_occupy']; // $color_occupy = $SpaceTypeAttr['color_occupy'];
$color_vacant = $SpaceTypeAttr['color_vacant']; // $color_vacant = $SpaceTypeAttr['color_vacant'];
$is_flicker = $SpaceTypeAttr['is_warning']; // $is_flicker = $SpaceTypeAttr['is_warning'];
} // }
DB::commit(); DB::commit();
} catch (Exception $e) { } catch (Exception $e) {

21
app/Http/Controllers/Device/CameraApiController.php

@ -3,10 +3,10 @@
namespace App\Http\Controllers\Device; namespace App\Http\Controllers\Device;
use App\Models\ParkingCamera; use App\Models\ParkingCamera;
use App\Services\HikParkingCameraService;
use App\Services\ParkingCameraService; use App\Services\ParkingCameraService;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
class CameraApiController extends BaseController class CameraApiController extends BaseController
{ {
@ -46,13 +46,22 @@ class CameraApiController extends BaseController
public function getCameraBody(): JsonResponse public function getCameraBody(): JsonResponse
{ {
$body = Redis::get('camera_body'); $body = HikParkingCameraService::getBody();
if (!$body) { if (!$body) {
return $this->responseService->error('empty'); 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');
} }
} }

22
app/Models/ParkingCameraSendLog.php

@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ParkingCameraSendLog extends Model
{
use HasFactory;
protected $table = 'parking_camera_send_log';
protected $fillable = [
'camera_ip',
'color_occupy',
'color_vacant',
'is_flicker',
'status'
];
}

11
app/Models/ParkingSpaceCamera.php

@ -97,4 +97,15 @@ class ParkingSpaceCamera extends Model
{ {
return self::query()->where('camera_id', $camera_id)->count(); return self::query()->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 '';
}
} }

227
app/Services/HikParkingCameraService.php

@ -2,191 +2,72 @@
namespace App\Services; namespace App\Services;
use Illuminate\Support\Facades\Http; use App\Models\ParkingCameraSendLog;
use Illuminate\Support\Facades\Redis;
class HikParkingCameraService class HikParkingCameraService
{ {
public array $device; /**
* 创建变更设备信息
public string $format = 'json'; * @param $camera_ip
* @param $color_occupy
protected string $MULTI_PARKING_SPACES_LAMP = '/ISAPI/Parking/parkingSpace/multiParkingSpacesLamp'; * @param $color_vacant
* @param $is_flicker
public function __construct($device) * @return void
{ */
$this->device = [ public static function setBody($camera_ip, $color_occupy, $color_vacant, $is_flicker)
'protocol' => 'http',
'ip' => '192.168.66.64',
'port' => '',
'username' => 'admin',
'password' => 'Xingtong1124',
];
//$this->device = $device;
}
public function getLight()
{ {
$url = $this->getUri() . $this->MULTI_PARKING_SPACES_LAMP; $body = [
$query = [ 'camera_ip' => $camera_ip,
'format' => $this->format, 'color_occupy' => $color_occupy,
'security' => 1, 'color_vacant' => $color_vacant,
'iv' => md5(rand('000000', '999999')) 'is_flicker' => $is_flicker ? 1 : 0,
]; ];
$url .= '?' . http_build_query($query); $exists = ParkingCameraSendLog::query()->where($body)->where(
'status',
0
)->exists();
$response = Http::timeout(3) if (!$exists) {
->withoutVerifying() $body['created_at'] = get_datetime();
->withDigestAuth($this->device['username'], $this->device['password']) ParkingCameraSendLog::query()->create($body);
->get($url);
//dd($response->json(), $response->body(), $response->object(), $response);
try {
$response = $response->json();
if ($response->getStatusCode() == '200') {
}
} catch (\Exception $e) {
throw $e;
} }
} }
// 组合 设备ip地址 /**
protected function getUri() * 返回需要变更设备信息
* @return array
*/
public static function getBody(): array
{ {
$protocol = $this->device['protocol']; $columns = [
$ip = $this->device['ip']; 'id',
$port = $this->device['port'] ?? ''; 'camera_ip as ip',
$uri = $protocol . '://' . $ip; 'color_occupy',
if ($port) { 'color_vacant',
$uri .= ':' . $port; 'is_flicker',
}
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"
]
]
]; ];
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 = [ $model = ParkingCameraSendLog::query()->find($id);
"lampType" => "internal", $model->update([
"VehicleNumber" => [ 'status' => 1,
"VehicleChannelNum" => 1, 'updated_at' => get_datetime()
"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);
} }
} }

49
app/Services/ParkingSpaceService.php

@ -7,6 +7,7 @@ use App\Models\AdminFloor;
use App\Models\ParkingLicensePlate; use App\Models\ParkingLicensePlate;
use App\Models\ParkingSpace; use App\Models\ParkingSpace;
use App\Models\ParkingSpaceAttributes; use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceCamera;
use App\Models\ParkingSpaceType; use App\Models\ParkingSpaceType;
use App\Models\ParkingSpaceTypeAttr; use App\Models\ParkingSpaceTypeAttr;
use Exception; use Exception;
@ -440,34 +441,34 @@ class ParkingSpaceService extends BaseService
); );
// 切换车位类型,同步切换车位灯颜色 // 切换车位类型,同步切换车位灯颜色
if ($oldValues['status'] != 2) { 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);
$SpaceType = ParkingSpaceType::query()->find($space_type_id); $SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr);
$SpaceTypeAttr = ParkingSpaceTypeAttr::getSpaceTypeAttrInfo($space_type_id, $space_attr);
$color_occupy = $SpaceType['default_color_occupy'];
$color_occupy = $SpaceType['default_color_occupy']; $color_vacant = $SpaceType['default_color_vacant'];
$color_vacant = $SpaceType['default_color_vacant']; $is_flicker = $SpaceType['default_is_warning'];
$is_flicker = $SpaceType['default_is_warning']; if ($SpaceTypeAttr) {
if ($SpaceTypeAttr) { $color_occupy = $SpaceTypeAttr['color_occupy'];
$color_occupy = $SpaceTypeAttr['color_occupy']; $color_vacant = $SpaceTypeAttr['color_vacant'];
$color_vacant = $SpaceTypeAttr['color_vacant']; $is_flicker = $SpaceTypeAttr['is_warning'];
$is_flicker = $SpaceTypeAttr['is_warning']; }
} // 维修灯
// 维修灯 if ($is_repair) {
if ($is_repair) { $color_occupy = $SpaceType['repair_light'];
$color_occupy = $SpaceType['repair_light']; $color_vacant = $SpaceType['repair_light'];
$color_vacant = $SpaceType['repair_light']; $is_flicker = false;
$is_flicker = false; }
}
$HikParkingCameraService = new HikParkingCameraService($this->logService); $camera_ip = ParkingSpaceCamera::getCameraIp($space_id);
$body = $HikParkingCameraService->getBody($color_occupy, $color_vacant, $is_flicker); if ($camera_ip) {
$HikParkingCameraService->updatelight($body); 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']; $space_type_id = $oldValues['space_type_id'];
// 修改为类型的维修指定灯光 // 修改为类型的维修指定灯光
$this->changeSpaceCameraColor( $this->changeSpaceCameraColor(
$number, $id,
$space_type_id, $space_type_id,
$space_attr_id, $space_attr_id,
$status == 2 $status == 2

34
database/migrations/2026_06_09_143449_create_parking_camera_send_log_table.php

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('parking_camera_send_log', function (Blueprint $table) {
$table->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');
}
};

1
routes/device/api.php

@ -11,5 +11,6 @@ Route::group(['prefix' => 'device'], function () {
Route::put('/camera/getStatus', [CameraApiController::class, 'updateCameraStatus']); Route::put('/camera/getStatus', [CameraApiController::class, 'updateCameraStatus']);
// 获取车位相机设备更新信息 // 获取车位相机设备更新信息
Route::get('/camera/getBody', [CameraApiController::class, 'getCameraBody']); Route::get('/camera/getBody', [CameraApiController::class, 'getCameraBody']);
Route::put('/camera/getBody', [CameraApiController::class, 'setCameraBody']);
}); });

Loading…
Cancel
Save