168 lines
4.2 KiB
PHP
168 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Iot\BiAng;
|
|
|
|
use App\Exceptions\BiAngException;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Str;
|
|
use RuntimeException;
|
|
|
|
class HttpClient
|
|
{
|
|
public function __construct(
|
|
protected readonly string $username,
|
|
protected readonly string $password,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* 获取最新的土壤数据
|
|
*/
|
|
public function getLatestSoilReport(string $deviceId)
|
|
{
|
|
$result = $this->get(
|
|
$this->apiUrl('/api/open-api/open/soilMoisture/getCurrentDeviceData'),
|
|
[
|
|
'deviceId' => $deviceId,
|
|
]
|
|
);
|
|
|
|
return $result['data'] ?? [];
|
|
}
|
|
|
|
/**
|
|
* 获取最新的气象数据
|
|
*/
|
|
public function getLatestMeteorologicalReport(string $deviceId)
|
|
{
|
|
$result = $this->get(
|
|
$this->apiUrl('/api/open-api/open/weather/getCurrentDeviceData'),
|
|
[
|
|
'deviceId' => $deviceId,
|
|
]
|
|
);
|
|
|
|
return $result['data'] ?? [];
|
|
}
|
|
|
|
/**
|
|
* 获取最新的杀虫灯数据
|
|
*/
|
|
public function getLatestLampReport(string $deviceId)
|
|
{
|
|
$result = $this->get(
|
|
$this->apiUrl2('/open-api/open/getCurrentLampData'),
|
|
[
|
|
'deviceId' => $deviceId,
|
|
]
|
|
);
|
|
|
|
return $result['data'] ?? [];
|
|
}
|
|
|
|
/**
|
|
* 虫情设备/昆虫性诱设备 - 查询某个时间段内的图片
|
|
*/
|
|
public function getWormPhotos(string $deviceId, Carbon $start, Carbon $end)
|
|
{
|
|
$result = $this->get(
|
|
$this->apiUrl('/api/open-api/open/getDevicePhotos'),
|
|
[
|
|
'deviceId' => $deviceId,
|
|
'startTime' => $start->toDateString(),
|
|
'endTime' => $end->toDateString(),
|
|
]
|
|
);
|
|
|
|
return $result['data'] ?? [];
|
|
}
|
|
|
|
/**
|
|
* 虫情设备 - (识别款)图片虫数识别统计
|
|
*/
|
|
public function getWormStatistics(string $deviceId, Carbon $start, Carbon $end)
|
|
{
|
|
$result = $this->get(
|
|
$this->apiUrl('/api/open-api/open/getAllStatistics'),
|
|
[
|
|
'imei' => $deviceId,
|
|
'startAt' => $start->toDateString(),
|
|
'endAt' => $end->toDateString(),
|
|
]
|
|
);
|
|
|
|
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
|
|
{
|
|
switch (strtoupper($method)) {
|
|
case 'GET':
|
|
$options['query'] = array_merge($options['query'], [
|
|
'username' => $this->username,
|
|
'password' => $this->password,
|
|
]);
|
|
break;
|
|
|
|
case 'POST':
|
|
$options['json'] = array_merge($options['json'], [
|
|
'username' => $this->username,
|
|
'password' => $this->password,
|
|
]);
|
|
break;
|
|
}
|
|
|
|
/** @var \Illuminate\Http\Client\Response */
|
|
$response = Http::withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
])->send($method, $url, $options);
|
|
|
|
$result = $response->throw()->json();
|
|
|
|
if (data_get($result, 'code') === 200) {
|
|
return $result;
|
|
}
|
|
|
|
throw new BiAngException($result['code'].':'.($result['msg']??'出错啦!'), 500);
|
|
}
|
|
|
|
protected function apiUrl(string $path): string
|
|
{
|
|
return 'http://yun.bigdata5s.com'.Str::start($path, '/');
|
|
}
|
|
|
|
protected function apiUrl2(string $path): string
|
|
{
|
|
return 'http://yun-api.bigdata5s.com'.Str::start($path, '/');
|
|
}
|
|
}
|