Compare commits

..

No commits in common. "b18925675cce43ea86eb0d2a6bea927960e363c9" and "40df91fcc68d8a2f00d5d4cea982333a3fd9d42d" have entirely different histories.

4 changed files with 35 additions and 197 deletions

View File

@ -10,8 +10,6 @@ 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;
@ -101,22 +99,6 @@ 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')

View File

@ -8,7 +8,6 @@ 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;
@ -65,7 +64,6 @@ 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,

View File

@ -246,41 +246,37 @@ 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 (isset($_dataList[$_key])) {
if($deviceColumn == 'ph'){
if ($device->supplier_key === 'device-supplier-linkos') { $data[$device->monitoring_point][$_key] = 7.49;
if (isset($_dataList[$_key])) { }elseif($deviceColumn == 'temperature'){
if($deviceColumn == 'ph'){ $data[$device->monitoring_point][$_key] = 10.00;
$data[$device->monitoring_point][$_key] = 7.49; }elseif($deviceColumn == 'turbidity'){
}elseif($deviceColumn == 'temperature'){ $data[$device->monitoring_point][$_key] = 1028.60;
$data[$device->monitoring_point][$_key] = 10.00; }else{
}elseif($deviceColumn == 'turbidity'){ $data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
$data[$device->monitoring_point][$_key] = 1028.60; }
}else{ }else{//临时写一些假数据
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null; switch($deviceColumn){
} case 'chlorine':
}else{//临时写一些假数据 $data[$device->monitoring_point][$_key] = 0.016;
switch($deviceColumn){ break;
case 'chlorine': case 'conductivity':
$data[$device->monitoring_point][$_key] = 0.016; $data[$device->monitoring_point][$_key] = 563 ;//电导率
break; break;
case 'conductivity': case 'oxygen':
$data[$device->monitoring_point][$_key] = 563 ;//电导率 $data[$device->monitoring_point][$_key] = 0.09;//含氧量
break; break;
case 'oxygen': case 'ph':
$data[$device->monitoring_point][$_key] = 0.09;//含氧量 $data[$device->monitoring_point][$_key] = rand(750, 770) / 100;
break; break;
case 'ph': case 'temperature':
$data[$device->monitoring_point][$_key] = rand(750, 770) / 100; $data[$device->monitoring_point][$_key] = rand(950, 1050) / 100;
break; break;
case 'temperature': case 'turbidity':
$data[$device->monitoring_point][$_key] = rand(950, 1050) / 100; $data[$device->monitoring_point][$_key] = 0.33;
break; break;
case 'turbidity':
$data[$device->monitoring_point][$_key] = 0.33;
break;
}
} }
} }
} }
@ -384,9 +380,7 @@ class DeviceController extends Controller
$x[] = $monitoredAt; $x[] = $monitoredAt;
if ($monitoringLog) { if ($monitoringLog) {
$value = $monitoringLog->{$deviceColumn}; if (is_null($value = $device->{$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,
@ -396,9 +390,9 @@ class DeviceController extends Controller
} }
$y[] = $value; $y[] = $value;
} elseif ($device->supplier_key === 'device-supplier-linkos') { } else {
$y[] = match ($deviceColumn) { $y[] = match ($deviceColumn) {
'chlorine' => 0.016, 'chlorine' => 0.016,
'conductivity' => 563, 'conductivity' => 563,
'oxygen' => 0.09, 'oxygen' => 0.09,
'ph' => rand(750, 770) / 100, 'ph' => rand(750, 770) / 100,
@ -406,8 +400,6 @@ class DeviceController extends Controller
'turbidity' => 0.33, 'turbidity' => 0.33,
default => null, default => null,
}; };
} else {
$y[] = null;
} }
$startAt->addHours(1); $startAt->addHours(1);
@ -544,16 +536,6 @@ 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
@ -610,7 +592,7 @@ class DeviceController extends Controller
if (is_null($monitoringLog)) { if (is_null($monitoringLog)) {
// 如果是水质设备,则写死假数据 // 如果是水质设备,则写死假数据
if($device->supplier_key === 'device-supplier-linkos' && $device->type == DeviceType::WaterQuality){ if($device->type == DeviceType::WaterQuality){
switch($field){ switch($field){
case 'chlorine': case 'chlorine':
$data[$key] = 0.016; $data[$key] = 0.016;

View File

@ -13,8 +13,6 @@ 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;
@ -58,10 +56,6 @@ 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;
@ -134,62 +128,6 @@ 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();
}
/** /**
* 创建气象设备报告 * 创建气象设备报告
*/ */
@ -382,10 +320,6 @@ 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;
@ -451,64 +385,6 @@ 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();
}
/** /**
* 创建气象设备每日报告 * 创建气象设备每日报告
*/ */