154 lines
3.8 KiB
PHP
154 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Iot\Linkos;
|
|
|
|
use App\Exceptions\LinkosFarmException;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class FarmClient
|
|
{
|
|
public const ENDPOINT_URL = 'http://api.farm.0531yun.cn';
|
|
|
|
/**
|
|
* 授权令牌
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $token;
|
|
|
|
/**
|
|
* 授权令牌有效期
|
|
*
|
|
* @var int
|
|
*/
|
|
protected $expires;
|
|
|
|
public function __construct(
|
|
protected readonly string $loginName,
|
|
protected readonly string $loginPwd,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* 获取设备列表
|
|
*/
|
|
public function devices(?string $groupId = null, ?string $deviceType = null): array
|
|
{
|
|
$result = $this->get('/api/v2.0/entrance/device/getsysUserDevice', [
|
|
'groupId' => $groupId,
|
|
'deviceType' => $deviceType,
|
|
]);
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
/**
|
|
* 获取用户区域
|
|
*/
|
|
public function groups(?string $groupName = null): array
|
|
{
|
|
$result = $this->get('/api/v2.0/entrance/group/getsysUserGroup', [
|
|
'groupName' => $groupName,
|
|
]);
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
/**
|
|
* 获取设备实时数据
|
|
*/
|
|
public function realTimeData(array $devices = []): array
|
|
{
|
|
$result = $this->get('/api/v2.0/entrance/device/getRealTimeData', [
|
|
'deviceAddrs' => implode(',', $devices),
|
|
]);
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
/**
|
|
* 虫情区域统计
|
|
*/
|
|
public function wormStatistics(string $groupId, Carbon $beginTime, Carbon $endTime): array
|
|
{
|
|
$result = $this->get('/api/v2.0/worm/deviceData/getWormStatisticsByGroup', [
|
|
'groupId' => $groupId,
|
|
'beginTime' => $beginTime->toDateTimeString(),
|
|
'endTime' => $endTime->toDateTimeString(),
|
|
]);
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
/**
|
|
* 虫情设备分析报表
|
|
*/
|
|
public function wormAnalyseData(string $device, Carbon $beginTime, Carbon $endTime, int $pages, int $limit = 100): array
|
|
{
|
|
$result = $this->get('/api/v2.0/worm/deviceData/getWormDataList', [
|
|
'deviceAddr' => $device,
|
|
'beginTime' => $beginTime->toDateTimeString(),
|
|
'endTime' => $endTime->toDateTimeString(),
|
|
'pages' => $pages,
|
|
'limit' => $limit,
|
|
]);
|
|
|
|
return $result['data'];
|
|
}
|
|
|
|
public function get(string $url, array $query = []): array
|
|
{
|
|
return $this->request('GET', $url, [
|
|
'query' => $query,
|
|
]);
|
|
}
|
|
|
|
public function post(string $url, array $data = []): array
|
|
{
|
|
return $this->request('POST', $url, [
|
|
'json' => $data,
|
|
]);
|
|
}
|
|
|
|
protected function request(string $method, string $url, array $options = []): array
|
|
{
|
|
$headers = [
|
|
'Content-Type' => 'application/json',
|
|
];
|
|
if ($url !== '/api/v2.0/entrance/user/userLogin') {
|
|
$headers['token'] = $this->token();
|
|
}
|
|
|
|
/** @var \Illuminate\Http\Client\Response */
|
|
$response = Http::withHeaders($headers)->baseUrl(self::ENDPOINT_URL)->send($method, $url, $options);
|
|
|
|
$json = $response->throw()->json();
|
|
|
|
if (data_get($json, 'code') === 1000) {
|
|
return $json;
|
|
}
|
|
|
|
throw new LinkosFarmException(
|
|
data_get($json, 'message', '出错啦'),
|
|
data_get($json, 'code', 0),
|
|
);
|
|
}
|
|
|
|
protected function token(): string
|
|
{
|
|
if ($this->token && $this->expires > now()->unix() + 30) {
|
|
return $this->token;
|
|
}
|
|
|
|
$result = $this->post('/api/v2.0/entrance/user/userLogin', [
|
|
'loginName' => $this->loginName,
|
|
'loginPwd' => $this->loginPwd,
|
|
]);
|
|
|
|
$this->expires = $result['data']['expDate'];
|
|
|
|
return $this->token = $result['data']['token'];
|
|
}
|
|
}
|