停车场系统数据中间件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

63 lines
1.6 KiB

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