1
0
Fork 0

设备监控报告

develop
李静 2023-05-06 18:02:02 +08:00
parent 3d07fde008
commit 015e10682f
7 changed files with 418 additions and 0 deletions

View File

@ -0,0 +1,197 @@
<?php
namespace App\Console\Commands;
use App\Models\Device;
use App\Models\MeteorologicalMonitoringReport;
use App\Models\SoilMonitoringReport;
use App\Models\WaterQualityMonitoringReport;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
class DeviceLogReportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'device-log:report
{factory}
{--sleep=300 : 监控报告生成后的休眠时间(秒)}';
/**
* The console command description.
*
* @var string
*/
protected $description = '按厂家生成设备监控报告';
/**
* Execute the console command.
*/
public function handle()
{
$factory = $this->argument('factory');
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
while (true) {
/** @var \Illuminate\Database\Eloquent\Collection */
$devices = Device::with(['factory'])->poweredBy($factory)->get();
foreach ($devices as $device) {
$this->createMonitoringReport($device);
}
sleep($sleep);
};
}
protected function createMonitoringReport(Device $device)
{
switch ($device->factory?->key) {
case 'link-os':
$this->createLinkosDeviceMonitoringReport($device);
break;
}
}
protected function createLinkosDeviceMonitoringReport(Device $device)
{
switch ($device->type) {
case Device::TYPE_SOIL:
$lastMonitoringReport = SoilMonitoringReport::where('device_id', $device->id)
->latest('reported_at')
->first();
$lastReportedAt = $lastMonitoringReport?->reported_at;
$device->logs()
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
->oldest('reported_at')
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
/** @var \App\Models\DeviceLog */
foreach ($deviceLogs as $deviceLog) {
$monitoringReport = SoilMonitoringReport::firstOrCreate(
[
'device_id' => $device->id,
'reported_at' => $deviceLog->reported_at->startOfHour(),
],
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
);
foreach ([
'conductivity' => 'conductivity',
'soil_humidity' => 'humidity',
'soil_temperature' => 'temperature',
'nitrogen_content' => 'n',
'potassium_content' => 'k',
'phosphorus_content' => 'p',
] as $key => $attribute) {
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
continue;
}
$monitoringReport->{$attribute} = $deviceLog->data[$key];
}
$lastMonitoringReport = tap($monitoringReport)->save();
}
});
break;
case Device::TYPE_WATER_QUALITY:
$lastMonitoringReport = WaterQualityMonitoringReport::where('device_id', $device->id)
->latest('reported_at')
->first();
$lastReportedAt = $lastMonitoringReport?->reported_at;
$device->logs()
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
->oldest('reported_at')
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
/** @var \App\Models\DeviceLog */
foreach ($deviceLogs as $deviceLog) {
$monitoringReport = WaterQualityMonitoringReport::firstOrCreate(
[
'device_id' => $device->id,
'reported_at' => $deviceLog->reported_at->startOfHour(),
],
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
);
foreach ([
'chlorine' => 'chlorine',
'conductivity' => 'conductivity',
'oxygen' => 'oxygen',
'ph' => 'ph',
'temp' => 'temperature',
'turbidity' => 'turbidity',
] as $key => $attribute) {
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
continue;
}
$monitoringReport->{$attribute} = $deviceLog->data[$key];
}
$lastMonitoringReport = tap($monitoringReport)->save();
}
});
break;
case Device::TYPE_METEOROLOGICAL:
$lastMonitoringReport = MeteorologicalMonitoringReport::where('device_id', $device->id)
->latest('reported_at')
->first();
$lastReportedAt = $lastMonitoringReport?->reported_at;
$device->logs()
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
->oldest('reported_at')
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
/** @var \App\Models\DeviceLog */
foreach ($deviceLogs as $deviceLog) {
$monitoringReport = MeteorologicalMonitoringReport::firstOrCreate(
[
'device_id' => $device->id,
'reported_at' => $deviceLog->reported_at->startOfHour(),
],
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
);
foreach ([
'current_rainfall' => 'today_rainfall',
'day_rainfall' => 'yesterday_rainfall',
'accumulate_rainfall' => 'accumulate_rainfall',
'moment_rainfall' => 'moment_rainfall',
'pm10_concentration' => 'pm10',
'pm25_concentration' => 'pm25',
'box_illumination' => 'box_illumination',
'box_pressure' => 'box_pressure',
'box_carbon' => 'box_co2',
'box_temperature' => 'box_temperature',
'box_humidity' => 'box_humidity',
'box_noise' => 'box_noise',
'wind_degree' => 'wind_degree',
'wind_direction' => 'wind_direction',
'wind_power' => 'wind_power',
'wind_speed' => 'wind_speed',
] as $key => $attribute) {
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
continue;
}
$monitoringReport->{$attribute} = $deviceLog->data[$key];
}
$lastMonitoringReport = tap($monitoringReport)->save();
}
});
break;
}
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MeteorologicalMonitoringReport extends Model
{
use HasFactory;
protected $casts = [
'reported_at' => 'datetime'
];
protected $fillable = [
'device_id',
'today_rainfall',
'yesterday_rainfall',
'accumulate_rainfall',
'moment_rainfall',
'pm10',
'pm25',
'box_illumination',
'box_pressure',
'box_co2',
'box_temperature',
'box_humidity',
'box_noise',
'wind_degree',
'wind_direction',
'wind_power',
'wind_speed',
'reported_at',
];
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SoilMonitoringReport extends Model
{
use HasFactory;
protected $casts = [
'reported_at' => 'datetime',
];
protected $fillable = [
'device_id',
'temperature',
'humidity',
'n',
'p',
'k',
'conductivity',
'reported_at',
];
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class WaterQualityMonitoringReport extends Model
{
use HasFactory;
protected $casts = [
'reported_at' => 'datetime'
];
protected $fillable = [
'device_id',
'chlorine',
'conductivity',
'oxygen',
'ph',
'temperature',
'turbidity',
'reported_at',
];
}

View File

@ -0,0 +1,41 @@
<?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('soil_monitoring_reports', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->comment('设备ID');
$table->decimal('temperature', 8, 2)->nullable()->comment('温度(单位: ℃)');
$table->decimal('humidity', 8, 2)->nullable()->comment('湿度(单位: %RH');
$table->integer('n')->nullable()->comment('氮 (单位: mg/kg)');
$table->integer('p')->nullable()->comment('磷 (单位: mg/kg)');
$table->integer('k')->nullable()->comment('钾 (单位: mg/kg)');
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率(单位: us/cm');
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
$table->timestamps();
$table->unique(['device_id', 'reported_at']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('soil_monitoring_reports');
}
};

View File

@ -0,0 +1,41 @@
<?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('water_quality_monitoring_reports', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->comment('设备ID');
$table->decimal('chlorine', 8, 2)->nullable()->comment('余氯(单位: mg/L');
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率(单位: us/cm');
$table->decimal('oxygen', 8, 2)->nullable()->comment('溶解氧(单位: mg/L');
$table->decimal('ph', 8, 2)->nullable()->comment('PH');
$table->decimal('temperature', 8, 2)->nullable()->comment('温度(单位: ℃)');
$table->decimal('turbidity', 8, 2)->nullable()->comment('浊度(单位: NTU');
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
$table->timestamps();
$table->unique(['device_id', 'reported_at']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('water_quality_monitoring_reports');
}
};

View File

@ -0,0 +1,51 @@
<?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('meteorological_monitoring_reports', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id');
$table->decimal('today_rainfall', 8, 2)->nullable()->comment('今日积雨量 (单位: mm)');
$table->decimal('yesterday_rainfall', 8, 2)->nullable()->comment('昨日积雨量 (单位: mm)');
$table->decimal('accumulate_rainfall', 8, 2)->nullable()->comment('累积雨量 (单位: mm)');
$table->decimal('moment_rainfall', 8, 2)->nullable()->comment('瞬时雨量 (单位: mm)');
$table->decimal('pm10', 8, 2)->nullable()->comment('PM10浓度 (单位: ug/m3)');
$table->decimal('pm25', 8, 2)->nullable()->comment('PM2.5浓度 (单位: ug/m3)');
$table->decimal('box_illumination', 10, 2)->nullable()->comment('百叶箱光照度 (单位: Lux)');
$table->decimal('box_pressure', 8, 2)->nullable()->comment('百叶箱大气压力 (单位: Kpa)');
$table->decimal('box_co2', 8, 2)->nullable()->comment('百叶箱CO2浓度 (单位: ppm)');
$table->decimal('box_temperature', 8, 2)->nullable()->comment('百叶箱温度 (单位: ℃)');
$table->decimal('box_humidity', 8, 2)->nullable()->comment('百叶箱湿度 (单位: %RH)');
$table->decimal('box_noise', 8, 2)->nullable()->comment('百叶箱噪声 (单位: db)');
$table->integer('wind_degree')->nullable()->comment('风向度数');
$table->tinyInteger('wind_direction')->nullable()->comment('风向: 0 北风, 1 东北风, 2 东风, 3 东南风, 4 南风, 5 西南风, 6 西风, 7 西北风');
$table->integer('wind_power')->nullable()->comment('风力 (单位: 级)');
$table->decimal('wind_speed', 8, 2)->nullable()->comment('风速 (单位: m/s)');
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
$table->timestamps();
$table->unique(['device_id', 'reported_at']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('meteorological_monitoring_reports');
}
};