Update
parent
7820f01b32
commit
d7459ec032
|
|
@ -3,7 +3,9 @@
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Enums\DeviceType;
|
use App\Enums\DeviceType;
|
||||||
|
use App\Iot\BiAng\HttpClient;
|
||||||
use App\Models\Device;
|
use App\Models\Device;
|
||||||
|
use App\Services\BiAngDeviceLogService;
|
||||||
use App\Services\DeviceLogService;
|
use App\Services\DeviceLogService;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
@ -26,18 +28,11 @@ class DeviceLogSyncCommand extends Command
|
||||||
*/
|
*/
|
||||||
protected $description = '按设备厂商同步数据';
|
protected $description = '按设备厂商同步数据';
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \App\Services\DeviceLogService
|
|
||||||
*/
|
|
||||||
protected $deviceLogService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
*/
|
*/
|
||||||
public function handle(DeviceLogService $deviceLogService)
|
public function handle()
|
||||||
{
|
{
|
||||||
$this->deviceLogService = $deviceLogService;
|
|
||||||
|
|
||||||
$factory = $this->argument('factory');
|
$factory = $this->argument('factory');
|
||||||
|
|
||||||
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 60, $this->option('sleep'));
|
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 60, $this->option('sleep'));
|
||||||
|
|
@ -77,7 +72,11 @@ class DeviceLogSyncCommand extends Command
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->deviceLogService->sync($device, $start, $end);
|
switch ($factory) {
|
||||||
|
case 'biang':
|
||||||
|
(new BiAngDeviceLogService())->sync($device);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->info('同步成功!');
|
$this->info('同步成功!');
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,123 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Iot\BiAng;
|
||||||
|
|
||||||
|
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,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (data_get($result, 'code') === 200) {
|
||||||
|
return $result['data'];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException(
|
||||||
|
sprintf("%s:%s", data_get($result, 'code', 500), data_get($result, 'msg', '出错啦!'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最新的气象数据
|
||||||
|
*/
|
||||||
|
public function getLatestMeteorologicalReport(string $deviceId)
|
||||||
|
{
|
||||||
|
$result = $this->get(
|
||||||
|
$this->apiUrl('/api/open-api/open/weather/getCurrentDeviceData'),
|
||||||
|
[
|
||||||
|
'deviceId' => $deviceId,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (data_get($result, 'code') === 200) {
|
||||||
|
return $result['data'];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException(
|
||||||
|
sprintf("%s:%s", data_get($result, 'code', 500), data_get($result, 'msg', '出错啦!'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return $response->throw()->json();
|
||||||
|
}
|
||||||
|
|
||||||
|
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, '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,129 +3,72 @@
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Enums\DeviceType;
|
use App\Enums\DeviceType;
|
||||||
use App\Iot\Linkos\HttpClient as LinkosHttpClient;
|
use App\Iot\BiAng\HttpClient as BiAngHttpClient;
|
||||||
use App\Models\Device;
|
use App\Models\Device;
|
||||||
use App\Models\LinkosDeviceLog;
|
use App\Models\DeviceLog;
|
||||||
use App\Models\MeteorologicalMonitoringDailyLog;
|
use App\Models\MeteorologicalMonitoringDailyLog;
|
||||||
use App\Models\MeteorologicalMonitoringLog;
|
use App\Models\MeteorologicalMonitoringLog;
|
||||||
use App\Models\SoilMonitoringDailyLog;
|
use App\Models\SoilMonitoringDailyLog;
|
||||||
use App\Models\SoilMonitoringLog;
|
use App\Models\SoilMonitoringLog;
|
||||||
use App\Models\WaterQualityMonitoringDailyLog;
|
|
||||||
use App\Models\WaterQualityMonitoringLog;
|
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
class DeviceLogService
|
class BiAngDeviceLogService
|
||||||
{
|
{
|
||||||
/**
|
public function sync(Device $device): void
|
||||||
* 同步设备历史流水
|
|
||||||
*/
|
|
||||||
public function sync(Device $device, Carbon $start, Carbon $end): void
|
|
||||||
{
|
{
|
||||||
switch ($device->supplier?->key) {
|
$httpClient = new BiAngHttpClient("", "");
|
||||||
case 'linkos':
|
|
||||||
$this->syncLinkosDeviceLogs($device, $start, $end);
|
|
||||||
break;
|
|
||||||
case 'biang':
|
|
||||||
// $this->syncLinkosDeviceLogs($device, $start, $end);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 同步 Linkos 设备历史流水
|
|
||||||
*/
|
|
||||||
protected function syncLinkosDeviceLogs(Device $device, Carbon $start, Carbon $end): void
|
|
||||||
{
|
|
||||||
/** @var \App\Iot\Linkos\HttpClient */
|
|
||||||
$httpClient = app(LinkosHttpClient::class);
|
|
||||||
|
|
||||||
$page = 1;
|
|
||||||
|
|
||||||
$perPage = 50;
|
|
||||||
|
|
||||||
do {
|
|
||||||
$data = $httpClient->deviceFlowList(
|
|
||||||
$device->sn, $start, $end, $page, $perPage
|
|
||||||
);
|
|
||||||
|
|
||||||
$countResults = count($data['content']);
|
|
||||||
|
|
||||||
if ($countResults === 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($data['content'] as $item) {
|
|
||||||
if (! isset($item['data'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果多合一气象监测器包含土壤监控时,需过滤掉气象监控的数据
|
|
||||||
if ($device->isTypeSoil() && Arr::hasAny($item['data'], [
|
|
||||||
'current_rainfall',
|
|
||||||
'day_rainfall',
|
|
||||||
'accumulate_rainfall',
|
|
||||||
'moment_rainfall',
|
|
||||||
'pm10_concentration',
|
|
||||||
'pm25_concentration',
|
|
||||||
'box_illumination',
|
|
||||||
'box_pressure',
|
|
||||||
'box_carbon',
|
|
||||||
'box_temperature',
|
|
||||||
'box_humidity',
|
|
||||||
'box_noise',
|
|
||||||
'wind_degree',
|
|
||||||
'wind_direction',
|
|
||||||
'wind_power',
|
|
||||||
'wind_speed',
|
|
||||||
])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkosDeviceLog::firstOrCreate([
|
|
||||||
'device_id' => $device->sn,
|
|
||||||
'reported_at' => $item['createTime'],
|
|
||||||
], [
|
|
||||||
'device_unit' => $device->model,
|
|
||||||
'data' => empty($item['data']) ? (new \stdClass) : $item['data'],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($data);
|
|
||||||
|
|
||||||
$page++;
|
|
||||||
} while ($countResults === $perPage);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 linkos 设备报告
|
|
||||||
*/
|
|
||||||
public function createReportToLinkosDevice(Device $device, Carbon $time): void
|
|
||||||
{
|
|
||||||
switch ($device->type) {
|
switch ($device->type) {
|
||||||
case DeviceType::Soil:
|
case DeviceType::Soil:
|
||||||
$this->createReportToLinkosSoilDevice($device, $time);
|
$data = $httpClient->getLatestSoilReport($device->sn);
|
||||||
|
|
||||||
|
DeviceLog::firstOrCreate([
|
||||||
|
'device_id' => $device->sn,
|
||||||
|
'reported_at' => $data['time'],
|
||||||
|
], [
|
||||||
|
'data' => Arr::except($data, ['deviceId', 'time']),
|
||||||
|
]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DeviceType::Meteorological:
|
case DeviceType::Meteorological:
|
||||||
$this->createReportToLinkosMeteorologicalDevice($device, $time);
|
$data = $httpClient->getLatestMeteorologicalReport($device->sn);
|
||||||
break;
|
|
||||||
|
|
||||||
case DeviceType::WaterQuality:
|
DeviceLog::firstOrCreate([
|
||||||
$this->createReportToLinkosWaterQualityDevice($device, $time);
|
'device_id' => $device->sn,
|
||||||
|
'reported_at' => $data['time'],
|
||||||
|
], [
|
||||||
|
'data' => Arr::except($data, ['deviceId', 'time']),
|
||||||
|
]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 linkos 土壤设备报告
|
* 创建设备报告
|
||||||
*/
|
*/
|
||||||
protected function createReportToLinkosSoilDevice(Device $device, Carbon $time): void
|
public function createReportToDevice(Device $device, Carbon $time): void
|
||||||
|
{
|
||||||
|
switch ($device->type) {
|
||||||
|
case DeviceType::Soil:
|
||||||
|
$this->createSoilReport($device, $time);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DeviceType::Meteorological:
|
||||||
|
$this->createMeteorologicalReport($device, $time);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建土壤设备报告
|
||||||
|
*/
|
||||||
|
protected function createSoilReport(Device $device, Carbon $time): void
|
||||||
{
|
{
|
||||||
$reportedAt = $time->copy()->startOfHour();
|
$reportedAt = $time->copy()->startOfHour();
|
||||||
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||||
$logs = LinkosDeviceLog::where('device_id', $device->sn)
|
$logs = DeviceLog::where('device_id', $device->sn)
|
||||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||||
->oldest('reported_at')
|
->oldest('reported_at')
|
||||||
->get();
|
->get();
|
||||||
|
|
@ -134,19 +77,16 @@ class DeviceLogService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = $logs->reduce(function (array $attributes, LinkosDeviceLog $log) {
|
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
|
||||||
if (is_array($data = $log->data)) {
|
if (is_array($data = $log->data)) {
|
||||||
foreach ($data as $k => $v) {
|
foreach ($data as $k => $v) {
|
||||||
$attribute = match ($k) {
|
$attribute = match ($k) {
|
||||||
'nitrogen_content' => 'n',
|
'soilAlkalineHydrolyzedNitrogen' => 'n',
|
||||||
'potassium_content' => 'k',
|
'soilAvailablePotassium' => 'k',
|
||||||
'phosphorus_content' => 'p',
|
'soilAvailablePhosphorus' => 'p',
|
||||||
'electroconductibility' => 'conductivity',
|
'soilConductivity' => 'conductivity',
|
||||||
'temperature' => 'temperature',
|
'soilTemperature' => 'temperature',
|
||||||
'moisture_content' => 'moisture',
|
'soilMoisture' => 'humidity',
|
||||||
'conductivity' => 'conductivity',
|
|
||||||
'soil_humidity' => 'humidity',
|
|
||||||
'soil_temperature' => 'temperature',
|
|
||||||
default => null,
|
default => null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -183,14 +123,14 @@ class DeviceLogService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 linkos 气象设备报告
|
* 创建气象设备报告
|
||||||
*/
|
*/
|
||||||
protected function createReportToLinkosMeteorologicalDevice(Device $device, Carbon $time): void
|
protected function createMeteorologicalReport(Device $device, Carbon $time): void
|
||||||
{
|
{
|
||||||
$reportedAt = $time->copy()->startOfHour();
|
$reportedAt = $time->copy()->startOfHour();
|
||||||
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||||
$logs = LinkosDeviceLog::where('device_id', $device->sn)
|
$logs = DeviceLog::where('device_id', $device->sn)
|
||||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||||
->oldest('reported_at')
|
->oldest('reported_at')
|
||||||
->get();
|
->get();
|
||||||
|
|
@ -199,26 +139,16 @@ class DeviceLogService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = $logs->reduce(function (array $attributes, LinkosDeviceLog $log) {
|
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
|
||||||
if (is_array($data = $log->data)) {
|
if (is_array($data = $log->data)) {
|
||||||
foreach ($data as $k => $v) {
|
foreach ($data as $k => $v) {
|
||||||
$attribute = match ($k) {
|
$attribute = match ($k) {
|
||||||
// 'day_rainfall' => 'yesterday_rainfall',
|
'rainfall' => 'moment_rainfall',
|
||||||
'current_rainfall' => 'current_rainfall',
|
'lightIntensity' => 'illumination',
|
||||||
'accumulate_rainfall' => 'accumulated_rainfall',
|
'airTemperature' => 'air_temperature',
|
||||||
'moment_rainfall' => 'moment_rainfall',
|
'airHumidity' => 'air_humidity',
|
||||||
'pm10_concentration' => 'pm10',
|
'windDirection' => 'wind_direction',
|
||||||
'pm25_concentration' => 'pm25',
|
'windSpeed' => 'wind_speed',
|
||||||
'box_illumination' => 'illumination',
|
|
||||||
'box_pressure' => 'air_pressure',
|
|
||||||
'box_carbon' => 'co2',
|
|
||||||
'box_temperature' => 'air_temperature',
|
|
||||||
'box_humidity' => 'air_humidity',
|
|
||||||
'box_noise' => 'noise',
|
|
||||||
'wind_degree' => 'wind_degree',
|
|
||||||
'wind_direction' => 'wind_direction',
|
|
||||||
'wind_power' => 'wind_power',
|
|
||||||
'wind_speed' => 'wind_speed',
|
|
||||||
default => null,
|
default => null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -255,91 +185,25 @@ class DeviceLogService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 linkos 水质设备报告
|
* 创建设备每日报告
|
||||||
*/
|
*/
|
||||||
protected function createReportToLinkosWaterQualityDevice(Device $device, Carbon $time): void
|
public function createDailyReport(Device $device, Carbon $time): void
|
||||||
{
|
|
||||||
$reportedAt = $time->copy()->startOfHour();
|
|
||||||
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
||||||
$logs = LinkosDeviceLog::where('device_id', $device->sn)
|
|
||||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
|
||||||
->oldest('reported_at')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
if ($logs->isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$attributes = $logs->reduce(function (array $attributes, LinkosDeviceLog $log) {
|
|
||||||
if (is_array($data = $log->data)) {
|
|
||||||
foreach ($data as $k => $v) {
|
|
||||||
$attribute = match ($k) {
|
|
||||||
'chlorine' => 'chlorine',
|
|
||||||
'conductivity' => 'conductivity',
|
|
||||||
'oxygen' => 'oxygen',
|
|
||||||
'ph' => 'ph',
|
|
||||||
'temp' => 'temperature',
|
|
||||||
'turbidity' => 'turbidity',
|
|
||||||
default => null,
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($attribute) {
|
|
||||||
$attributes[$attribute] = $v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $attributes;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
$waterQualityReport = WaterQualityMonitoringLog::where([
|
|
||||||
'device_id' => $device->id,
|
|
||||||
'monitored_at' => $reportedAt,
|
|
||||||
])->first();
|
|
||||||
|
|
||||||
if ($waterQualityReport === null) {
|
|
||||||
$lastWaterQualityReport = WaterQualityMonitoringLog::where([
|
|
||||||
'device_id' => $device->id,
|
|
||||||
'monitored_at' => $reportedAt->copy()->subHour(),
|
|
||||||
])->first();
|
|
||||||
|
|
||||||
$waterQualityReport = $lastWaterQualityReport?->replicate() ?: new WaterQualityMonitoringLog();
|
|
||||||
|
|
||||||
$waterQualityReport->fill([
|
|
||||||
'device_id' => $device->id,
|
|
||||||
'monitored_at' => $reportedAt,
|
|
||||||
'agricultural_base_id' => $device->agricultural_base_id,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$waterQualityReport->fill($attributes)->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 linkos 设备每日报告
|
|
||||||
*/
|
|
||||||
public function createDailyReportToLinkosDevice(Device $device, Carbon $time): void
|
|
||||||
{
|
{
|
||||||
switch ($device->type) {
|
switch ($device->type) {
|
||||||
case DeviceType::Meteorological:
|
case DeviceType::Meteorological:
|
||||||
$this->createDailyReportToLinkosMeteorologicalDevice($device, $time);
|
$this->createMeteorologicalDailyReport($device, $time);
|
||||||
break;
|
|
||||||
|
|
||||||
case DeviceType::WaterQuality:
|
|
||||||
$this->createDailyReportToLinkosWaterQualityDevice($device, $time);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DeviceType::Soil:
|
case DeviceType::Soil:
|
||||||
$this->createDailyReportToLinkosSoilDevice($device, $time);
|
$this->createSoilDailyReport($device, $time);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 linkos 土壤设备每日报告
|
* 创建土壤设备每日报告
|
||||||
*/
|
*/
|
||||||
protected function createDailyReportToLinkosSoilDevice(Device $device, Carbon $date): void
|
protected function createSoilDailyReport(Device $device, Carbon $date): void
|
||||||
{
|
{
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||||
$soilReports = SoilMonitoringLog::where('device_id', $device->id)
|
$soilReports = SoilMonitoringLog::where('device_id', $device->id)
|
||||||
|
|
@ -396,9 +260,9 @@ class DeviceLogService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 linkos 气象设备每日报告
|
* 创建气象设备每日报告
|
||||||
*/
|
*/
|
||||||
protected function createDailyReportToLinkosMeteorologicalDevice(Device $device, Carbon $date): void
|
protected function createMeteorologicalDailyReport(Device $device, Carbon $date): void
|
||||||
{
|
{
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||||
$meteorologicalReports = MeteorologicalMonitoringLog::where('device_id', $device->id)
|
$meteorologicalReports = MeteorologicalMonitoringLog::where('device_id', $device->id)
|
||||||
|
|
@ -538,62 +402,4 @@ class DeviceLogService
|
||||||
|
|
||||||
$meteorologicalDailyReport->fill($attributes)->save();
|
$meteorologicalDailyReport->fill($attributes)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 linkos 水质设备每日报告
|
|
||||||
*/
|
|
||||||
protected function createDailyReportToLinkosWaterQualityDevice(Device $device, Carbon $date): void
|
|
||||||
{
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
||||||
$waterQualityReports = WaterQualityMonitoringLog::where('device_id', $device->id)
|
|
||||||
->whereDate('monitored_at', $date)
|
|
||||||
->oldest('monitored_at')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
if ($waterQualityReports->isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$attributes = value(function ($waterQualityReports) {
|
|
||||||
$data = [
|
|
||||||
'chlorine' => ['sum' => 0, 'count' => 0],
|
|
||||||
'conductivity' => ['sum' => 0, 'count' => 0],
|
|
||||||
'oxygen' => ['sum' => 0, 'count' => 0],
|
|
||||||
'ph' => ['sum' => 0, 'count' => 0],
|
|
||||||
'temperature' => ['sum' => 0, 'count' => 0],
|
|
||||||
'turbidity' => ['sum' => 0, 'count' => 0],
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($waterQualityReports as $waterQualityReport) {
|
|
||||||
foreach ($data as $k => $item) {
|
|
||||||
if (is_null($v = $waterQualityReport->{$k})) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$item['sum'] = bcadd($item['sum'], $v, 2);
|
|
||||||
$item['count']++;
|
|
||||||
|
|
||||||
$data[$k] = $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$attributes = [];
|
|
||||||
|
|
||||||
foreach ($data as $key => $item) {
|
|
||||||
$attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $attributes;
|
|
||||||
}, $waterQualityReports);
|
|
||||||
|
|
||||||
/** @var \App\Models\WaterQualityMonitoringDailyLog */
|
|
||||||
$WaterQualityMonitoringDailyLog = WaterQualityMonitoringDailyLog::firstOrNew([
|
|
||||||
'device_id' => $device->id,
|
|
||||||
'monitored_at' => $date->format('Y-m-d'),
|
|
||||||
], [
|
|
||||||
'agricultural_base_id' => $device->agricultural_base_id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$WaterQualityMonitoringDailyLog->fill($attributes)->save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue