杀虫灯每日报告

dev
Jing Li 2023-08-12 13:22:50 +08:00
parent 31f0c0a88b
commit 317622f0fc
6 changed files with 157 additions and 3 deletions

View File

@ -4,6 +4,8 @@ namespace App\Console\Commands\BiAng;
use App\Enums\DeviceType;
use App\Models\Device;
use App\Models\InsecticidalLampDailyReport;
use App\Models\InsecticidalLampReport;
use App\Models\MeteorologicalMonitoringDailyLog;
use App\Models\MeteorologicalMonitoringLog;
use App\Models\SoilMonitoringDailyLog;
@ -91,6 +93,22 @@ class DeviceLogDailyReportCommand extends Command
->value('monitored_at');
}
break;
case DeviceType::InsecticidalLamp:
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
->latest('reported_at')
->value('reported_at');
$lastReportedAt ??= InsecticidalLampReport::where('device_id', $device->id)
->oldest('reported_at')
->value('reported_at');
if ($lastReportedAt) {
$latestReportedAt = InsecticidalLampReport::where('device_id', $device->id)
->latest('reported_at')
->value('reported_at');
}
break;
}
return [$lastReportedAt, $latestReportedAt];

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class InsecticidalLampDailyReport extends Model
{
use HasFactory;
protected $casts = [
'reported_at' => 'date',
];
protected $fillable = [
'device_id',
'agricultural_base_id',
'battery_vol',
'killed_num',
'air_temperature',
'air_humidity',
'solar_panel_vol',
'high_vol',
'reported_at',
];
}

View File

@ -20,8 +20,6 @@ class InsecticidalLampReport extends Model
'killed_num',
'air_temperature',
'air_humidity',
'lng',
'lat',
'solar_panel_vol',
'high_vol',
'reported_at',

View File

@ -7,6 +7,7 @@ use App\Enums\DeviceType;
use App\Iot\BiAng\HttpClient as BiAngHttpClient;
use App\Models\Device;
use App\Models\DeviceLog;
use App\Models\InsecticidalLampDailyReport;
use App\Models\InsecticidalLampReport;
use App\Models\MeteorologicalMonitoringDailyLog;
use App\Models\MeteorologicalMonitoringLog;
@ -266,6 +267,10 @@ class BiAngDeviceLogService
case DeviceType::Soil:
$this->createSoilDailyReport($device, $time);
break;
case DeviceType::InsecticidalLamp:
$this->createInsecticidalLampDailyReport($device, $time);
break;
}
}
@ -464,4 +469,70 @@ class BiAngDeviceLogService
$meteorologicalDailyReport->fill($attributes)->save();
}
/**
* 杀虫灯每日报告
*/
protected function createInsecticidalLampDailyReport(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$insecticidalLampReports = InsecticidalLampReport::where('device_id', $device->id)
->whereDate('reported_at', $date)
->oldest('reported_at')
->get();
if ($insecticidalLampReports->isEmpty()) {
return;
}
$attributes = value(function ($insecticidalLampReports) {
$data = [
'killed_num' => ['sum' => 0, 'count' => 0],
'battery_vol' => ['sum' => 0, 'count' => 0],
'air_temperature' => ['sum' => 0, 'count' => 0],
'air_humidity' => ['sum' => 0, 'count' => 0],
'solar_panel_vol' => ['sum' => 0, 'count' => 0],
'high_vol' => ['sum' => 0, 'count' => 0],
];
foreach ($insecticidalLampReports as $insecticidalLampReport) {
foreach ($data as $k => $item) {
if (is_null($v = $insecticidalLampReport->{$k})) {
continue;
}
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
$data[$k] = $item;
}
}
$attributes = [];
foreach ($data as $key => $item) {
if ($item['count'] > 0) {
if ($key === 'killed_num') {
$attributes[$key] = $item['sum'];
} else {
$attributes[$key] = round(bcdiv($item['sum'], $item['count'], 2), 2);
}
} else {
$attributes[$key] = null;
}
}
return $attributes;
}, $insecticidalLampReports);
/** @var \App\Models\InsecticidalLampDailyReport */
$insecticidalLampDailyReport = InsecticidalLampDailyReport::firstOrNew([
'device_id' => $device->id,
'reported_at' => $date->format('Y-m-d'),
], [
'agricultural_base_id' => $device->agricultural_base_id,
]);
$insecticidalLampDailyReport->fill($attributes)->save();
}
}

View File

@ -18,7 +18,7 @@ return new class extends Migration
$table->unsignedBigInteger('device_id');
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
$table->double('battery_vol')->nullable()->comment('蓄电池电压');
$table->unsignedInteger('killed_num')->default(0)->comment('杀虫');
$table->unsignedInteger('killed_num')->default(0)->comment('杀虫');
$table->double('air_temperature')->nullable()->comment('大气气温');
$table->double('air_humidity')->nullable()->comment('大气湿度');
$table->double('solar_panel_vol')->nullable()->comment('太阳板电压');

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('insecticidal_lamp_daily_reports', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id');
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
$table->double('battery_vol')->nullable()->comment('蓄电池电压');
$table->unsignedInteger('killed_num')->default(0)->comment('杀虫数');
$table->double('air_temperature')->nullable()->comment('大气气温');
$table->double('air_humidity')->nullable()->comment('大气湿度');
$table->double('solar_panel_vol')->nullable()->comment('太阳板电压');
$table->double('high_vol')->nullable()->comment('高压值');
$table->date('reported_at')->comment('监控日期');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('insecticidal_lamp_reports');
}
};