main
Jing Li 2024-06-03 18:47:19 +08:00
parent 470c9c25c9
commit 11da69f2b3
3 changed files with 162 additions and 21 deletions

View File

@ -0,0 +1,135 @@
<?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;
$startAt = Carbon::createFromFormat('Y-m-d H:00:00', '2024-04-01 00:00:00');
do {
$this->fill($startAt);
$startAt->addHour();
} while ($startAt->lt('2024-06-03 20:00:00'));
}
protected function fill(Carbon $monitoredAt)
{
$linkosDeviceLogService = new LinkosDeviceLogService();
$data = [
'conductivity' => rand(560, 565),
'oxygen' => rand(1000, 1100) / 100,
'chlorine' => rand(8, 10) / 100,
'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;
}
$linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $data, $monitoredAt);
$linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $monitoredAt);
}
}
}

View File

@ -23,6 +23,10 @@ class Kernel extends ConsoleKernel
->hourlyAt(15)
->runInBackground();
$schedule->command(Commands\Linkos\WaterQualityReportCommand::class)
->hourlyAt(50)
->runInBackground();
$schedule->command(Commands\MonitorDeviceHealthCommand::class, ['yidong'])
->everyTenMinutes()
->runInBackground();

View File

@ -251,22 +251,24 @@ class DeviceController extends Controller
if ($device->supplier_key === 'device-supplier-linkos') {
if (isset($_dataList[$_key])) {
if($deviceColumn == 'ph'){
$data[$device->monitoring_point][$_key] = 7.49;
}elseif($deviceColumn == 'temperature'){
$data[$device->monitoring_point][$_key] = 10.00;
}elseif($deviceColumn == 'turbidity'){
$data[$device->monitoring_point][$_key] = 1028.60;
}else{
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
}
// if($deviceColumn == 'ph'){
// $data[$device->monitoring_point][$_key] = 7.49;
// }elseif($deviceColumn == 'temperature'){
// $data[$device->monitoring_point][$_key] = 10.00;
// }elseif($deviceColumn == 'turbidity'){
// $data[$device->monitoring_point][$_key] = 1028.60;
// }else{
// $data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
// }
$data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null;
}else{//临时写一些假数据
switch($deviceColumn){
case 'chlorine':
$data[$device->monitoring_point][$_key] = 0.016;
$data[$device->monitoring_point][$_key] = 0.016;
break;
case 'conductivity':
$data[$device->monitoring_point][$_key] = 563 ;//电导率
$data[$device->monitoring_point][$_key] = 563;//电导率
break;
case 'oxygen':
$data[$device->monitoring_point][$_key] = 0.09;//含氧量
@ -278,7 +280,7 @@ class DeviceController extends Controller
$data[$device->monitoring_point][$_key] = rand(950, 1050) / 100;
break;
case 'turbidity':
$data[$device->monitoring_point][$_key] = 0.33;
$data[$device->monitoring_point][$_key] = 1028.60;
break;
}
}
@ -386,14 +388,14 @@ class DeviceController extends Controller
if ($monitoringLog) {
$value = $monitoringLog->{$deviceColumn};
if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') {
$value = match ($deviceColumn) {
'ph' => 7.49,
'temperature' => 10.00,
'turbidity' => 1028.60,
default => $value,
};
}
// if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') {
// $value = match ($deviceColumn) {
// 'ph' => 7.49,
// 'temperature' => 10.00,
// 'turbidity' => 1028.60,
// default => $value,
// };
// }
$y[] = $value;
} elseif ($device->supplier_key === 'device-supplier-linkos') {
@ -403,7 +405,7 @@ class DeviceController extends Controller
'oxygen' => 0.09,
'ph' => rand(750, 770) / 100,
'temperature' => rand(900, 1100) / 100,
'turbidity' => 0.33,
'turbidity' => 1028.60,
default => null,
};
} else {