lcly-data-admin/app/Console/Commands/Linkos/WaterQualityReportCommand.php

129 lines
4.8 KiB
PHP

<?php
namespace App\Console\Commands\Linkos;
use App\Models\Device;
use App\Models\WaterQualityMonitoringDailyLog;
use App\Models\WaterQualityMonitoringLog;
use App\Services\LinkosDeviceLogService;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class WaterQualityReportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'linkos:water-quality-report';
/**
* The console command description.
*
* @var string
*/
protected $description = 'linkos 水质设备数据构造';
/**
* @var \App\Services\LinkosDeviceLogService
*/
protected $linkosDeviceLogService;
/**
* Execute the console command.
*/
public function handle(LinkosDeviceLogService $linkosDeviceLogService)
{
$this->linkosDeviceLogService = $linkosDeviceLogService;
$this->fill(now()->subHour());
}
protected function fill(Carbon $monitoredAt)
{
$data = [
'conductivity' => rand(560, 565),
'oxygen' => rand(1000, 1100) / 100,
'chlorine' => 0.09,
'turbidity' => rand(100000, 103000) / 100,
'temp' => null,
'ph' => rand(750, 760) / 100,
'is_filled' => false,
];
$devices = Device::where('sn', '004A7701240041C9')->oldest('id')->get();
foreach ($devices as $device) {
if (! $data['is_filled']) {
$lastWaterQualityMonitoringLog = WaterQualityMonitoringLog::where('device_id', $device->id)
->whereBetween('monitored_at', [$monitoredAt->copy()->startOfDay(), $monitoredAt])
->latest('monitored_at')
->first();
if ($lastWaterQualityMonitoringLog) {
$temperature = $lastWaterQualityMonitoringLog->temperature;
if ($monitoredAt->hour <= 5) {
$temperature -= ($monitoredAt->hour - $lastWaterQualityMonitoringLog->monitored_at->hour) * 0.5;
} elseif ($monitoredAt->hour <= 11) {
if ($lastWaterQualityMonitoringLog->monitored_at->hour <= 5) {
$temperature -= (6 - ($lastWaterQualityMonitoringLog->monitored_at->hour + 1)) * 0.5;
$temperature += ($monitoredAt->hour + 1 - 6) * 0.5;
} else {
$temperature += ($monitoredAt->hour - $lastWaterQualityMonitoringLog->monitored_at->hour) * 0.5;
}
}
$data['temp'] = $temperature;
} else {
$temperature = null;
if ($hjzDevice = Device::where('sn', '041508ec545640000730171a')->first()) {
$temperature = WaterQualityMonitoringDailyLog::where('device_id', $hjzDevice->id)
->where('monitored_at', $monitoredAt->toDateString())
->value('temperature');
if (is_null($temperature)) {
$temperature = WaterQualityMonitoringDailyLog::where('device_id', $hjzDevice->id)
->latest('monitored_at')
->value('temperature');
if ($temperature) {
$temperature = mt_rand($temperature * 100 - 100, $temperature * 100 + 100) / 100;
}
}
}
if (is_null($temperature)) {
$temperature = mt_rand(1800, 2000) / 100;
}
if ($monitoredAt->hour <= 5) {
$temperature -= ($monitoredAt->hour + 1) * 0.5;
} elseif ($monitoredAt->hour <= 11) {
$temperature += ($monitoredAt->hour + 1 - 6) * 0.5;
}
$data['temp'] = $temperature;
}
if ($lastWaterQualityMonitoringLog) {
$data = array_merge($data, [
'conductivity' => $lastWaterQualityMonitoringLog->conductivity,
'oxygen' => $lastWaterQualityMonitoringLog->oxygen,
'chlorine' => $lastWaterQualityMonitoringLog->chlorine,
'turbidity' => $lastWaterQualityMonitoringLog->turbidity,
'ph' => $lastWaterQualityMonitoringLog->ph,
]);
}
$data['is_filled'] = true;
}
$this->linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $data, $monitoredAt);
$this->linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $monitoredAt);
}
}
}