物联平台数据归档
parent
e9d8b6c341
commit
fd0c7ab1ac
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace App\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
|
||||
class AsJsonObject implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! array_key_exists($key, $attributes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! is_null($data = $attributes[$key])) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($model, string $key, $value, array $attributes)
|
||||
{
|
||||
$value = is_array($value) ? $value : [];
|
||||
|
||||
return json_encode($value, JSON_FORCE_OBJECT);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\LinkosDeviceLog;
|
||||
use App\Models\MeteorologicalMonitoringLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class LinkosDeviceLogArchiveCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'linkos:device-log-archive';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'LinkOS 设备流水归档';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$devices = Device::whereIn('type', [DeviceType::Meteorological, DeviceType::Soil, DeviceType::WaterQuality])->get();
|
||||
|
||||
// 物联平台目前只有水质监测设备和气象监测设备
|
||||
LinkosDeviceLog::orderBy('reported_at', 'asc')->lazy()->each(function ($log) use ($devices) {
|
||||
if (empty($log->data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($devices as $device) {
|
||||
if ($device->sn !== $log->device_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ($device->type) {
|
||||
DeviceType::Soil => $this->handleSoildDeviceLog($device, $log),
|
||||
DeviceType::WaterQuality => $this->handleWaterQualityDeviceLog($device, $log),
|
||||
DeviceType::Meteorological => $this->handleMeteorologicalDeviceLog($device, $log),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
protected function handleSoildDeviceLog(Device $device, LinkosDeviceLog $log)
|
||||
{
|
||||
$attributes = [
|
||||
'conductivity' => 'conductivity',
|
||||
'soil_humidity' => 'humidity',
|
||||
'soil_temperature' => 'temperature',
|
||||
'nitrogen_content' => 'n',
|
||||
'phosphorus_content' => 'p',
|
||||
'potassium_content' => 'k',
|
||||
];
|
||||
|
||||
if (! Arr::hasAny($log->data, array_keys($attributes))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$monitoringLog = SoilMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $log->reported_at->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $attribute) {
|
||||
if (! array_key_exists($key, $log->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringLog->{$attribute} = $log->data[$key];
|
||||
}
|
||||
|
||||
$monitoringLog->save();
|
||||
}
|
||||
|
||||
protected function handleWaterQualityDeviceLog(Device $device, LinkosDeviceLog $log)
|
||||
{
|
||||
$attributes = [
|
||||
'conductivity' => 'conductivity',
|
||||
'oxygen' => 'oxygen',
|
||||
'chlorine' => 'chlorine',
|
||||
'turbidity' => 'turbidity',
|
||||
'temp' => 'temperature',
|
||||
'ph' => 'ph',
|
||||
];
|
||||
|
||||
if (! Arr::hasAny($log->data, array_keys($attributes))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$monitoringLog = WaterQualityMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $log->reported_at->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $attribute) {
|
||||
if (! array_key_exists($key, $log->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringLog->{$attribute} = $log->data[$key];
|
||||
}
|
||||
|
||||
$monitoringLog->save();
|
||||
}
|
||||
|
||||
protected function handleMeteorologicalDeviceLog(Device $device, LinkosDeviceLog $log)
|
||||
{
|
||||
$attributes = [
|
||||
'wind_speed' => 'wind_speed',
|
||||
'wind_power' => 'wind_power',
|
||||
'wind_direction' => 'wind_direction',
|
||||
'wind_degree' => 'wind_degree',
|
||||
'box_humidity' => 'air_humidity',
|
||||
'box_temperature' => 'air_temperature',
|
||||
'box_pressure' => 'air_pressure',
|
||||
'box_carbon' => 'co2',
|
||||
'box_noise' => 'noise',
|
||||
'box_illumination' => 'illumination',
|
||||
'accumulate_rainfall' => 'accumulated_rainfall',
|
||||
'current_rainfall' => 'current_rainfall',
|
||||
'moment_rainfall' => 'moment_rainfall',
|
||||
'day_rainfall' => 'day_rainfall',
|
||||
'pm25_concentration' => 'pm25',
|
||||
'pm10_concentration' => 'pm10',
|
||||
];
|
||||
|
||||
if (! Arr::hasAny($log->data, array_keys($attributes))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$monitoringLog = MeteorologicalMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $log->reported_at->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $attribute) {
|
||||
if (! array_key_exists($key, $log->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringLog->{$attribute} = $log->data[$key];
|
||||
}
|
||||
|
||||
$monitoringLog->save();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Casts\AsJsonObject;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
|
|
@ -10,6 +11,7 @@ class LinkosDeviceLog extends Model
|
|||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'data' => AsJsonObject::class,
|
||||
'reported_at' => 'datetime',
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -27,9 +27,8 @@ return new class extends Migration
|
|||
$table->timestamps();
|
||||
|
||||
// 索引
|
||||
$table->index('device_id');
|
||||
$table->index('agricultural_base_id');
|
||||
$table->unique('monitored_at');
|
||||
$table->unique(['device_id', 'monitored_at']);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,9 +37,8 @@ return new class extends Migration
|
|||
$table->timestamps();
|
||||
|
||||
// 索引
|
||||
$table->index('device_id');
|
||||
$table->index('agricultural_base_id');
|
||||
$table->unique('monitored_at');
|
||||
$table->unique(['device_id', 'monitored_at']);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,9 +26,8 @@ return new class extends Migration
|
|||
$table->timestamp('monitored_at')->comment('监控时间(小时)');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('device_id');
|
||||
$table->index('agricultural_base_id');
|
||||
$table->unique('monitored_at');
|
||||
$table->unique(['device_id', 'monitored_at']);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue