1
0
Fork 0
internet-everythings-agricu.../app/Console/Commands/MqttPenwuPlan.php

86 lines
3.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Device;
use App\Models\AtomizingLog;
use App\Services\MqttService;
use Illuminate\Console\Command;
class MqttPenwuPlan extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mqtt:penwu-plan';
/**
* The console command description.
*
* @var string
*/
protected $description = 'MQTT 喷雾控制';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
//首先获取当前状态
$service = new MqttService();
$status = $service->getStatus();
//判断是否离线,或者状态异常;
if($status && $status['error'] == 0 && $status['status'] == 1){
//获取所有喷雾监控点,对应的自动喷雾配置
$deviceList = Device::where('type', Device::TYPE_ATOMIZING)->get();
$time = now()->format('H:i');//获取当前时间(时,分)
$time = '01:00';
foreach($deviceList as $device){
$_config = $device->extends ?? [];
if($_config && $_config['is_enable']){//判断该配置是否开启
foreach($_config['config'] as $item){
list($start, $end) = explode(',', $item['time_zone']);
//决定开启,关闭,
if($time == $start){//相等
// 如果当前对应位置已开启,则不作为
if(
($item['value'] == 'a' && $status['yv1'] == 0)
||($item['value'] == 'b' && $status['yv2'] == 0)
){
$service->open($item['value'], $item['input']);
//记录触发日志
AtomizingLog::create([
'device_id' => $device->id,
'type' => 1,
'content' => '自动开启【区域'.strtoupper($item['value']).'】,喷雾量'.$item['input'].'%',
]);
}
}elseif($time == $end){
if($status['is_running']){
if(
($item['value'] == 'a' && $status['yv1'] == 1)
||($item['value'] == 'b' && $status['yv2'] == 1)
){
$service->close();
//记录触发日志
AtomizingLog::create([
'device_id' => $device->id,
'type' => 2,
'content' => '自动关闭【区域'.strtoupper($item['value']).'】',
]);
}
}
}
}
}
}
}
return Command::SUCCESS;
}
}