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\SoilMonitoringDailyLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Models\WaterQualityMonitoringDailyLog;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use App\Services\BiAngDeviceService;
|
||||
use Illuminate\Console\Command;
|
||||
use Throwable;
|
||||
|
|
@ -99,6 +101,22 @@ class DeviceLogDailyReportCommand extends Command
|
|||
}
|
||||
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:
|
||||
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use App\Models\DeviceLog;
|
|||
use App\Models\InsecticidalLampReport;
|
||||
use App\Models\MeteorologicalMonitoringLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use App\Services\BiAngDeviceService;
|
||||
use Illuminate\Console\Command;
|
||||
use Throwable;
|
||||
|
|
@ -64,6 +65,7 @@ class DeviceLogReportCommand extends Command
|
|||
{
|
||||
$lastReportedAt = match ($device->type) {
|
||||
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::InsecticidalLamp => InsecticidalLampReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
||||
default => null,
|
||||
|
|
|
|||
|
|
@ -246,37 +246,41 @@ class DeviceController extends Controller
|
|||
}
|
||||
for ($i = 5; $i >= 0; $i--) {
|
||||
$_key = now()->subHours($i)->format('Y-m-d H').':00:00';
|
||||
$data[$device->monitoring_point][$_key] = null;
|
||||
if (isset($_dataList[$_key])) {
|
||||
if($deviceColumn == 'ph'){
|
||||
$data[$device->monitoring_point][$_key] = 7.49;
|
||||
}elseif($deviceColumn == 'temperature'){
|
||||
$data[$device->monitoring_point][$_key] = 10.00;
|
||||
}elseif($deviceColumn == 'turbidity'){
|
||||
$data[$device->monitoring_point][$_key] = 1028.60;
|
||||
}else{
|
||||
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
|
||||
}
|
||||
}else{//临时写一些假数据
|
||||
switch($deviceColumn){
|
||||
case 'chlorine':
|
||||
$data[$device->monitoring_point][$_key] = 0.016;
|
||||
break;
|
||||
case 'conductivity':
|
||||
$data[$device->monitoring_point][$_key] = 563 ;//电导率
|
||||
break;
|
||||
case 'oxygen':
|
||||
$data[$device->monitoring_point][$_key] = 0.09;//含氧量
|
||||
break;
|
||||
case 'ph':
|
||||
$data[$device->monitoring_point][$_key] = rand(750, 770) / 100;
|
||||
break;
|
||||
case 'temperature':
|
||||
$data[$device->monitoring_point][$_key] = rand(950, 1050) / 100;
|
||||
break;
|
||||
case 'turbidity':
|
||||
$data[$device->monitoring_point][$_key] = 0.33;
|
||||
break;
|
||||
|
||||
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
|
||||
|
||||
if ($device->supplier_key === 'device-supplier-linkos') {
|
||||
if (isset($_dataList[$_key])) {
|
||||
if($deviceColumn == 'ph'){
|
||||
$data[$device->monitoring_point][$_key] = 7.49;
|
||||
}elseif($deviceColumn == 'temperature'){
|
||||
$data[$device->monitoring_point][$_key] = 10.00;
|
||||
}elseif($deviceColumn == 'turbidity'){
|
||||
$data[$device->monitoring_point][$_key] = 1028.60;
|
||||
}else{
|
||||
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
|
||||
}
|
||||
}else{//临时写一些假数据
|
||||
switch($deviceColumn){
|
||||
case 'chlorine':
|
||||
$data[$device->monitoring_point][$_key] = 0.016;
|
||||
break;
|
||||
case 'conductivity':
|
||||
$data[$device->monitoring_point][$_key] = 563 ;//电导率
|
||||
break;
|
||||
case 'oxygen':
|
||||
$data[$device->monitoring_point][$_key] = 0.09;//含氧量
|
||||
break;
|
||||
case 'ph':
|
||||
$data[$device->monitoring_point][$_key] = rand(750, 770) / 100;
|
||||
break;
|
||||
case 'temperature':
|
||||
$data[$device->monitoring_point][$_key] = rand(950, 1050) / 100;
|
||||
break;
|
||||
case 'turbidity':
|
||||
$data[$device->monitoring_point][$_key] = 0.33;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -380,7 +384,9 @@ class DeviceController extends Controller
|
|||
$x[] = $monitoredAt;
|
||||
|
||||
if ($monitoringLog) {
|
||||
if (is_null($value = $device->{$deviceColumn})) {
|
||||
$value = $monitoringLog->{$deviceColumn};
|
||||
|
||||
if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') {
|
||||
$value = match ($deviceColumn) {
|
||||
'ph' => 7.49,
|
||||
'temperature' => 10.00,
|
||||
|
|
@ -390,9 +396,9 @@ class DeviceController extends Controller
|
|||
}
|
||||
|
||||
$y[] = $value;
|
||||
} else {
|
||||
} elseif ($device->supplier_key === 'device-supplier-linkos') {
|
||||
$y[] = match ($deviceColumn) {
|
||||
'chlorine' => 0.016,
|
||||
'chlorine' => 0.016,
|
||||
'conductivity' => 563,
|
||||
'oxygen' => 0.09,
|
||||
'ph' => rand(750, 770) / 100,
|
||||
|
|
@ -400,6 +406,8 @@ class DeviceController extends Controller
|
|||
'turbidity' => 0.33,
|
||||
default => null,
|
||||
};
|
||||
} else {
|
||||
$y[] = null;
|
||||
}
|
||||
|
||||
$startAt->addHours(1);
|
||||
|
|
@ -536,6 +544,16 @@ class DeviceController extends Controller
|
|||
'temperature',
|
||||
'turbidity',
|
||||
];
|
||||
|
||||
switch ($device->supplier_key) {
|
||||
case 'device-supplier-biang':
|
||||
$fields = [
|
||||
'oxygen',
|
||||
'turbidity',
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var \Illuminate\Support\Collection */
|
||||
$monitoringLogs = (
|
||||
$isSameDay
|
||||
|
|
@ -592,7 +610,7 @@ class DeviceController extends Controller
|
|||
|
||||
if (is_null($monitoringLog)) {
|
||||
// 如果是水质设备,则写死假数据
|
||||
if($device->type == DeviceType::WaterQuality){
|
||||
if($device->supplier_key === 'device-supplier-linkos' && $device->type == DeviceType::WaterQuality){
|
||||
switch($field){
|
||||
case 'chlorine':
|
||||
$data[$key] = 0.016;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ use App\Models\MeteorologicalMonitoringDailyLog;
|
|||
use App\Models\MeteorologicalMonitoringLog;
|
||||
use App\Models\SoilMonitoringDailyLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Models\WaterQualityMonitoringDailyLog;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
|
|
@ -56,6 +58,10 @@ class BiAngDeviceService
|
|||
$this->createSoilReport($device, $time);
|
||||
break;
|
||||
|
||||
case DeviceType::WaterQuality:
|
||||
$this->createWaterQualityReport($device, $time);
|
||||
break;
|
||||
|
||||
case DeviceType::Meteorological:
|
||||
$this->createMeteorologicalReport($device, $time);
|
||||
break;
|
||||
|
|
@ -128,6 +134,62 @@ class BiAngDeviceService
|
|||
$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);
|
||||
break;
|
||||
|
||||
case DeviceType::WaterQuality:
|
||||
$this->createWaterQualityDailyReport($device, $time);
|
||||
break;
|
||||
|
||||
case DeviceType::InsecticidalLamp:
|
||||
$this->createInsecticidalLampDailyReport($device, $time);
|
||||
break;
|
||||
|
|
@ -385,6 +451,64 @@ class BiAngDeviceService
|
|||
$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