修复土质监控数据和水质监控数据
parent
73681f2d52
commit
7ec61b5d2e
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\DeviceStatus;
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Services\LinkosDeviceLogService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Throwable;
|
||||
|
||||
class SoilMonitoringLogFixCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'soil-monitoring-log:fix {hour? : 时间格式 Y-m-d H:i:s, 示例: 2022-11-11 00:00:00}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '修复土壤监控数据';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(LinkosDeviceLogService $linkosDeviceLogService)
|
||||
{
|
||||
$time = now()->subHour()->startOfHour();
|
||||
|
||||
if ($hour = $this->argument('hour')) {
|
||||
$time = Carbon::createFromFormat('Y-m-d H:i:s', $hour)->startOfHour();
|
||||
}
|
||||
|
||||
$devices = Device::where('type', DeviceType::Soil)
|
||||
->where('status', DeviceStatus::Online)
|
||||
->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
if (is_null(SoilMonitoringLog::where('device_id', $device->id)->where('monitored_at', $time)->first())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$last = SoilMonitoringLog::where('device_id', $device->id)
|
||||
->where('monitored_at', $time->copy()->subHour())
|
||||
->first();
|
||||
|
||||
if ($last === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
SoilMonitoringLog::create([
|
||||
'device_id' => $device->id,
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
'conductivity' => $last->conductivity,
|
||||
'humidity' => $last->humidity,
|
||||
'temperature' => $last->temperature,
|
||||
'n' => $last->n,
|
||||
'p' => $last->p,
|
||||
'k' => $last->k,
|
||||
'monitored_at' => $time,
|
||||
]);
|
||||
|
||||
$linkosDeviceLogService->handleSoilMonitoringDailyLog($device, $time);
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\DeviceStatus;
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use App\Services\LinkosDeviceLogService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Throwable;
|
||||
|
||||
class WaterQualityMonitoringLogFixCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'water-quality-monitoring-log:fix {hour? : 时间格式 Y-m-d H:i:s, 示例: 2022-11-11 00:00:00}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '修复水质监控数据';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(LinkosDeviceLogService $linkosDeviceLogService)
|
||||
{
|
||||
$time = now()->subHour()->startOfHour();
|
||||
|
||||
if ($hour = $this->argument('hour')) {
|
||||
$time = Carbon::createFromFormat('Y-m-d H:i:s', $hour)->startOfHour();
|
||||
}
|
||||
|
||||
$devices = Device::where('type', DeviceType::WaterQuality)
|
||||
->where('status', DeviceStatus::Online)
|
||||
->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
if (is_null(WaterQualityMonitoringLog::where('device_id', $device->id)->where('monitored_at', $time)->first())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$last = WaterQualityMonitoringLog::where('device_id', $device->id)
|
||||
->where('monitored_at', $time->copy()->subHour())
|
||||
->first();
|
||||
|
||||
if ($last === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
WaterQualityMonitoringLog::create([
|
||||
'device_id' => $device->id,
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
'chlorine' => $last->chlorine,
|
||||
'conductivity' => $last->conductivity,
|
||||
'oxygen' => $last->oxygen,
|
||||
'ph' => $last->ph,
|
||||
'temperature' => $last->temperature,
|
||||
'turbidity' => $last->turbidity,
|
||||
'monitored_at' => $time,
|
||||
]);
|
||||
|
||||
$linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $time);
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,13 @@ class Kernel extends ConsoleKernel
|
|||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
// $schedule->command('inspire')->hourly();
|
||||
$schedule->command(Commands\SoilMonitoringLogFixCommand::class)
|
||||
->hourly()
|
||||
->runInBackground();
|
||||
|
||||
$schedule->command(Commands\WaterQualityMonitoringLogFixCommand::class)
|
||||
->hourly()
|
||||
->runInBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue