8 changed files with 334 additions and 196 deletions
@ -0,0 +1,38 @@ |
|||
<?php |
|||
|
|||
namespace App\Services\Api; |
|||
|
|||
use Illuminate\Http\Client\Response; |
|||
use Illuminate\Support\Facades\Http; |
|||
|
|||
class BaseApiService |
|||
{ |
|||
protected string $hostUrl; |
|||
|
|||
public function __construct() |
|||
{ |
|||
$this->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); |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
<?php |
|||
|
|||
namespace App\Services\Api; |
|||
|
|||
class CameraApiService extends BaseApiService |
|||
{ |
|||
|
|||
/** |
|||
* @return CameraApiService |
|||
*/ |
|||
public static function query(): CameraApiService |
|||
{ |
|||
return new self(); |
|||
} |
|||
|
|||
// 获取车位相机设备变更颜色信息 |
|||
public function getCameraBody() |
|||
{ |
|||
$url = '/api/device/camera/getBody'; |
|||
$Response = $this->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 []; |
|||
} |
|||
} |
|||
@ -1,48 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
use GuzzleHttp\Client; |
|||
|
|||
class BaseService |
|||
{ |
|||
protected array $device; |
|||
protected array $username; |
|||
protected array $password; |
|||
public string $format = 'json'; |
|||
|
|||
public function __construct($ip) |
|||
{ |
|||
$this->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); |
|||
} |
|||
} |
|||
@ -1,77 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
use Illuminate\Support\Facades\Http; |
|||
|
|||
class CameraDeviceService extends BaseService |
|||
{ |
|||
|
|||
protected string $MULTI_PARKING_SPACES_LAMP = '/ISAPI/Parking/parkingSpace/multiParkingSpacesLamp'; |
|||
|
|||
public function setColor($body) |
|||
{ |
|||
$url = $this->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; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
<?php |
|||
|
|||
namespace App\Services\Device; |
|||
|
|||
use Illuminate\Support\Facades\Http; |
|||
|
|||
class BaseService |
|||
{ |
|||
public string $format = 'json'; |
|||
protected array $device; |
|||
protected array $username; |
|||
protected array $password; |
|||
|
|||
public function __construct($ip, $port = '') |
|||
{ |
|||
$this->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); |
|||
} |
|||
} |
|||
@ -0,0 +1,144 @@ |
|||
<?php |
|||
|
|||
namespace App\Services\Device; |
|||
|
|||
class CameraDeviceService extends BaseService |
|||
{ |
|||
|
|||
protected string $MULTI_PARKING_SPACES_LAMP = '/ISAPI/Parking/parkingSpace/multiParkingSpacesLamp'; |
|||
protected string $SYSTEM_STATUS = '/ISAPI/System/status'; |
|||
|
|||
/** |
|||
* @param $ip |
|||
* @return CameraDeviceService |
|||
*/ |
|||
public static function query($ip): CameraDeviceService |
|||
{ |
|||
return new self($ip); |
|||
} |
|||
|
|||
/** |
|||
* 更新车位相机灯光颜色 |
|||
* @param string $color_occupy |
|||
* @param string $color_vacant |
|||
* @param bool $is_flicker |
|||
* @return array |
|||
*/ |
|||
public function setColor( |
|||
string $color_occupy, |
|||
string $color_vacant, |
|||
bool $is_flicker |
|||
): array { |
|||
$url = $this->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" |
|||
] |
|||
] |
|||
] |
|||
*/ |
|||
} |
|||
Loading…
Reference in new issue