Browse Source

自动更新状态,优化数据

master
wanghongjun 4 weeks ago
parent
commit
bece21c9c1
  1. 30
      app/Console/Commands/CheckEquipmentStatus.php
  2. 82
      app/Console/Commands/SetCameraData.php
  3. 38
      app/Services/Api/BaseApiService.php
  4. 48
      app/Services/Api/CameraApiService.php
  5. 48
      app/Services/BaseService.php
  6. 77
      app/Services/CameraDeviceService.php
  7. 63
      app/Services/Device/BaseService.php
  8. 144
      app/Services/Device/CameraDeviceService.php

30
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();
}
}

82
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;
// 返回告诉设备状态情况
}
}

38
app/Services/Api/BaseApiService.php

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

48
app/Services/Api/CameraApiService.php

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

48
app/Services/BaseService.php

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

77
app/Services/CameraDeviceService.php

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

63
app/Services/Device/BaseService.php

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

144
app/Services/Device/CameraDeviceService.php

@ -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…
Cancel
Save