161 lines
4.1 KiB
PHP
161 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Iot\Linkos;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use RuntimeException;
|
|
|
|
class HttpClient
|
|
{
|
|
public const ENDPOINT_URL = 'http://service.easylinkin.com';
|
|
|
|
public function __construct(
|
|
protected readonly string $apiKey,
|
|
protected readonly string $apiSecret,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* 获取设备的历史数据
|
|
*
|
|
* @param string $deviceId
|
|
* @param \Illuminate\Support\Carbon $start
|
|
* @param \Illuminate\Support\Carbon $end
|
|
* @param int $page
|
|
* @param int $perPage
|
|
* @return array
|
|
*/
|
|
public function deviceFlowList(string $deviceId, Carbon $start, Carbon $end, int $page = 1, int $perPage = 50): array
|
|
{
|
|
$result = $this->post('/api/deviceFlow/v1/list', [
|
|
'device_id' => $deviceId,
|
|
'start_time' => $start->unix() * 1000,
|
|
'end_time' => $end->unix() * 1000,
|
|
'pageable' => [
|
|
'page' => $page - 1,
|
|
'size' => $perPage,
|
|
],
|
|
]);
|
|
|
|
if (data_get($result, 'success') !== true) {
|
|
throw new RuntimeException(data_get($result, 'msg', '出错啦!'));
|
|
}
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
/**
|
|
* 设备数据下行
|
|
*
|
|
* @param string $deviceId
|
|
* @param string $service
|
|
* @param array $data
|
|
* @param boolean $confirm
|
|
* @param boolean $clear
|
|
* @param boolean $schedule
|
|
* @return array
|
|
*/
|
|
public function deviceDataDownlink(string $deviceId, string $service, array $data = [], bool $confirm = true, bool $clear = true, bool $schedule = false): array
|
|
{
|
|
return $this->post('/api/down', [
|
|
'device_id' => $deviceId,
|
|
'service_id' => $service,
|
|
'parameter' => $data,
|
|
'clear' => (int) $clear,
|
|
'schedule' => (int) $schedule,
|
|
'confirm' => (int) $confirm,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取设备最新属性数据
|
|
*/
|
|
public function getDeviceStatus(string $deviceId, array $props): array
|
|
{
|
|
$result = $this->get('/api/deviceStatus/v1/getDeviceStatus', [
|
|
'deviceCode' => $deviceId,
|
|
'prop' => implode(",", $props),
|
|
]);
|
|
|
|
if (data_get($result, 'success') !== true) {
|
|
throw new RuntimeException(data_get($result, 'msg', '出错啦!'));
|
|
}
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
public function get(string $url, array $query = []): array
|
|
{
|
|
return $this->request('GET', $url, [
|
|
'query' => $query,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function post(string $url, array $data = []): array
|
|
{
|
|
return $this->request('POST', $url, [
|
|
'json' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param string $method
|
|
* @param string $url
|
|
* @param array $options
|
|
* @return array
|
|
*
|
|
* @throws \Illuminate\Http\Client\RequestException
|
|
* @throws \RuntimeException
|
|
*/
|
|
public function request(string $method, string $url, array $options = []): array
|
|
{
|
|
$nonce = $this->nonce();
|
|
|
|
$timestamp = now()->getTimestampMs();
|
|
|
|
/** @var \Illuminate\Http\Client\Response */
|
|
$response = Http::withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
'api-key' => $this->apiKey,
|
|
'Nonce' => $nonce,
|
|
'Timestamp' => $timestamp,
|
|
'Signature' => $this->sign(compact('nonce', 'timestamp')),
|
|
])->baseUrl(self::ENDPOINT_URL)->send($method, $url, $options);
|
|
|
|
return $response->throw()->json();
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return string
|
|
*/
|
|
protected function sign(array $data): string
|
|
{
|
|
return sha1(
|
|
sprintf(
|
|
'%s%s%s',
|
|
$data['nonce'] ?? '',
|
|
$data['timestamp'] ?? '',
|
|
$this->apiSecret
|
|
)
|
|
);
|
|
}
|
|
|
|
protected function nonce(): string
|
|
{
|
|
$nonce = '';
|
|
|
|
for ($i = 0; $i < 8; $i++) {
|
|
$nonce .= mt_rand(0, 9);
|
|
}
|
|
|
|
return $nonce;
|
|
}
|
|
}
|