Compare commits
5 Commits
40df91fcc6
...
b18925675c
| Author | SHA1 | Date |
|---|---|---|
|
|
b18925675c | |
|
|
9b6581cdce | |
|
|
3242a5e5d8 | |
|
|
c229cb736a | |
|
|
cf7c12e01b |
|
|
@ -10,6 +10,8 @@ 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 App\Services\BiAngDeviceService;
|
use App\Services\BiAngDeviceService;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
@ -99,6 +101,22 @@ class DeviceLogDailyReportCommand extends Command
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case DeviceType::WaterQuality:
|
||||||
|
$lastReportedAt = WaterQualityMonitoringDailyLog::where('device_id', $device->id)
|
||||||
|
->latest('monitored_at')
|
||||||
|
->value('monitored_at');
|
||||||
|
|
||||||
|
$lastReportedAt ??= WaterQualityMonitoringLog::where('device_id', $device->id)
|
||||||
|
->oldest('monitored_at')
|
||||||
|
->value('monitored_at');
|
||||||
|
|
||||||
|
if ($lastReportedAt) {
|
||||||
|
$latestReportedAt = WaterQualityMonitoringLog::where('device_id', $device->id)
|
||||||
|
->latest('monitored_at')
|
||||||
|
->value('monitored_at');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case DeviceType::InsecticidalLamp:
|
case DeviceType::InsecticidalLamp:
|
||||||
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
|
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
|
||||||
->latest('reported_at')
|
->latest('reported_at')
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use App\Models\DeviceLog;
|
||||||
use App\Models\InsecticidalLampReport;
|
use App\Models\InsecticidalLampReport;
|
||||||
use App\Models\MeteorologicalMonitoringLog;
|
use App\Models\MeteorologicalMonitoringLog;
|
||||||
use App\Models\SoilMonitoringLog;
|
use App\Models\SoilMonitoringLog;
|
||||||
|
use App\Models\WaterQualityMonitoringLog;
|
||||||
use App\Services\BiAngDeviceService;
|
use App\Services\BiAngDeviceService;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
@ -64,6 +65,7 @@ class DeviceLogReportCommand extends Command
|
||||||
{
|
{
|
||||||
$lastReportedAt = match ($device->type) {
|
$lastReportedAt = match ($device->type) {
|
||||||
DeviceType::Soil => SoilMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
DeviceType::Soil => SoilMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
||||||
|
DeviceType::WaterQuality => WaterQualityMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
||||||
DeviceType::Meteorological => MeteorologicalMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
DeviceType::Meteorological => MeteorologicalMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
||||||
DeviceType::InsecticidalLamp => InsecticidalLampReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
DeviceType::InsecticidalLamp => InsecticidalLampReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
||||||
default => null,
|
default => null,
|
||||||
|
|
|
||||||
|
|
@ -246,7 +246,10 @@ class DeviceController extends Controller
|
||||||
}
|
}
|
||||||
for ($i = 5; $i >= 0; $i--) {
|
for ($i = 5; $i >= 0; $i--) {
|
||||||
$_key = now()->subHours($i)->format('Y-m-d H').':00:00';
|
$_key = now()->subHours($i)->format('Y-m-d H').':00:00';
|
||||||
$data[$device->monitoring_point][$_key] = null;
|
|
||||||
|
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
|
||||||
|
|
||||||
|
if ($device->supplier_key === 'device-supplier-linkos') {
|
||||||
if (isset($_dataList[$_key])) {
|
if (isset($_dataList[$_key])) {
|
||||||
if($deviceColumn == 'ph'){
|
if($deviceColumn == 'ph'){
|
||||||
$data[$device->monitoring_point][$_key] = 7.49;
|
$data[$device->monitoring_point][$_key] = 7.49;
|
||||||
|
|
@ -281,6 +284,7 @@ class DeviceController extends Controller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -380,7 +384,9 @@ class DeviceController extends Controller
|
||||||
$x[] = $monitoredAt;
|
$x[] = $monitoredAt;
|
||||||
|
|
||||||
if ($monitoringLog) {
|
if ($monitoringLog) {
|
||||||
if (is_null($value = $device->{$deviceColumn})) {
|
$value = $monitoringLog->{$deviceColumn};
|
||||||
|
|
||||||
|
if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') {
|
||||||
$value = match ($deviceColumn) {
|
$value = match ($deviceColumn) {
|
||||||
'ph' => 7.49,
|
'ph' => 7.49,
|
||||||
'temperature' => 10.00,
|
'temperature' => 10.00,
|
||||||
|
|
@ -390,7 +396,7 @@ class DeviceController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$y[] = $value;
|
$y[] = $value;
|
||||||
} else {
|
} elseif ($device->supplier_key === 'device-supplier-linkos') {
|
||||||
$y[] = match ($deviceColumn) {
|
$y[] = match ($deviceColumn) {
|
||||||
'chlorine' => 0.016,
|
'chlorine' => 0.016,
|
||||||
'conductivity' => 563,
|
'conductivity' => 563,
|
||||||
|
|
@ -400,6 +406,8 @@ class DeviceController extends Controller
|
||||||
'turbidity' => 0.33,
|
'turbidity' => 0.33,
|
||||||
default => null,
|
default => null,
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
$y[] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$startAt->addHours(1);
|
$startAt->addHours(1);
|
||||||
|
|
@ -536,6 +544,16 @@ class DeviceController extends Controller
|
||||||
'temperature',
|
'temperature',
|
||||||
'turbidity',
|
'turbidity',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
switch ($device->supplier_key) {
|
||||||
|
case 'device-supplier-biang':
|
||||||
|
$fields = [
|
||||||
|
'oxygen',
|
||||||
|
'turbidity',
|
||||||
|
];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/** @var \Illuminate\Support\Collection */
|
/** @var \Illuminate\Support\Collection */
|
||||||
$monitoringLogs = (
|
$monitoringLogs = (
|
||||||
$isSameDay
|
$isSameDay
|
||||||
|
|
@ -592,7 +610,7 @@ class DeviceController extends Controller
|
||||||
|
|
||||||
if (is_null($monitoringLog)) {
|
if (is_null($monitoringLog)) {
|
||||||
// 如果是水质设备,则写死假数据
|
// 如果是水质设备,则写死假数据
|
||||||
if($device->type == DeviceType::WaterQuality){
|
if($device->supplier_key === 'device-supplier-linkos' && $device->type == DeviceType::WaterQuality){
|
||||||
switch($field){
|
switch($field){
|
||||||
case 'chlorine':
|
case 'chlorine':
|
||||||
$data[$key] = 0.016;
|
$data[$key] = 0.016;
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ 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\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
|
@ -56,6 +58,10 @@ class BiAngDeviceService
|
||||||
$this->createSoilReport($device, $time);
|
$this->createSoilReport($device, $time);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case DeviceType::WaterQuality:
|
||||||
|
$this->createWaterQualityReport($device, $time);
|
||||||
|
break;
|
||||||
|
|
||||||
case DeviceType::Meteorological:
|
case DeviceType::Meteorological:
|
||||||
$this->createMeteorologicalReport($device, $time);
|
$this->createMeteorologicalReport($device, $time);
|
||||||
break;
|
break;
|
||||||
|
|
@ -128,6 +134,62 @@ class BiAngDeviceService
|
||||||
$soilReport->fill($attributes)->save();
|
$soilReport->fill($attributes)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function createWaterQualityReport(Device $device, Carbon $time): void
|
||||||
|
{
|
||||||
|
$reportedAt = $time->copy()->startOfHour();
|
||||||
|
|
||||||
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||||
|
$logs = DeviceLog::where('device_id', $device->id)
|
||||||
|
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||||
|
->oldest('reported_at')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($logs->isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
|
||||||
|
if (is_array($data = $log->data)) {
|
||||||
|
foreach ($data as $k => $v) {
|
||||||
|
$attribute = match ($k) {
|
||||||
|
'ec1' => 'conductivity',
|
||||||
|
'waterdo' => 'oxygen',
|
||||||
|
'zd' => '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();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建气象设备报告
|
* 创建气象设备报告
|
||||||
*/
|
*/
|
||||||
|
|
@ -320,6 +382,10 @@ class BiAngDeviceService
|
||||||
$this->createSoilDailyReport($device, $time);
|
$this->createSoilDailyReport($device, $time);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case DeviceType::WaterQuality:
|
||||||
|
$this->createWaterQualityDailyReport($device, $time);
|
||||||
|
break;
|
||||||
|
|
||||||
case DeviceType::InsecticidalLamp:
|
case DeviceType::InsecticidalLamp:
|
||||||
$this->createInsecticidalLampDailyReport($device, $time);
|
$this->createInsecticidalLampDailyReport($device, $time);
|
||||||
break;
|
break;
|
||||||
|
|
@ -385,6 +451,64 @@ class BiAngDeviceService
|
||||||
$soilDailyReport->fill($attributes)->save();
|
$soilDailyReport->fill($attributes)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建水质设备每日报告
|
||||||
|
*/
|
||||||
|
protected function createWaterQualityDailyReport(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 */
|
||||||
|
$waterQualityDailyReport = WaterQualityMonitoringDailyLog::firstOrNew([
|
||||||
|
'device_id' => $device->id,
|
||||||
|
'monitored_at' => $date->format('Y-m-d'),
|
||||||
|
], [
|
||||||
|
'agricultural_base_id' => $device->agricultural_base_id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$waterQualityDailyReport->fill($attributes)->save();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建气象设备每日报告
|
* 创建气象设备每日报告
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue