添加警报触发服务
parent
ff39f77fce
commit
1f5d1c8659
|
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Peidikeji\Setting\Models\Setting;
|
||||
use App\Models\DeviceWarning;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Device;
|
||||
|
||||
class DeviceWarningService
|
||||
{
|
||||
|
||||
/**
|
||||
* 警报字段对应中文
|
||||
*
|
||||
* @var array<string,string>
|
||||
*/
|
||||
protected $columnTexts = [
|
||||
'temperature' => '温度',
|
||||
'conductivity' => '电导率',
|
||||
'humidity'=>'湿度',
|
||||
'n'=>'氮',
|
||||
'p'=>'磷',
|
||||
'k'=>'钾',
|
||||
'oxygen' => '溶解氧',
|
||||
'chlorine' => '余氯',
|
||||
'turbidity' => '浊度',
|
||||
'ph' => 'ph值',
|
||||
];
|
||||
|
||||
protected $columnUnit = [
|
||||
'temperature' => '℃',
|
||||
'conductivity' => 'us/cm',
|
||||
'humidity'=>'%RH',
|
||||
'n'=>'mg/kg',
|
||||
'p'=>'mg/kg',
|
||||
'k'=>'mg/kg',
|
||||
'oxygen' => 'mg/L',
|
||||
'chlorine' => 'mg/L',
|
||||
'turbidity' => 'NTU',
|
||||
'ph' => '',
|
||||
];
|
||||
|
||||
public function judgeLog(Device $device, Model $log, Carbon $reportedAt){
|
||||
$slug = '';
|
||||
switch ($log::class) {
|
||||
case SoilMonitoringLog::class://土壤监测
|
||||
$slug = 'device_warning_rule_soil';
|
||||
break;
|
||||
case WaterQualityMonitoringLog::class://水质监测
|
||||
$slug = 'device_warning_rule_waterquality';
|
||||
break;
|
||||
}
|
||||
|
||||
$rule = Setting::where('slug', $slug)->value('value');
|
||||
if($rule){
|
||||
$rule = json_decode($rule, true);
|
||||
}
|
||||
|
||||
foreach($rule as $key => $values){
|
||||
$_value = $log->$key ?? null;
|
||||
$_warning = false;
|
||||
$_lv = 0;
|
||||
if($_value){
|
||||
foreach($values as $lv => $value){//1-4级警报
|
||||
foreach ($value as $item){
|
||||
if($item['min'] && empty($item['max'])){//设置了最小值,未设置最大值
|
||||
if($_value >= $item['min']){
|
||||
$_warning = true;
|
||||
$_lv = $lv;
|
||||
break 2;//跳出2层循环
|
||||
}
|
||||
}elseif(empty($item['min']) && $item['max']){//设置最大值,未设置最小值
|
||||
if($_value < $item['max']){
|
||||
$_warning = true;
|
||||
$_lv = $lv;
|
||||
break 2;//跳出2层循环
|
||||
}
|
||||
}elseif($item['min'] && $item['max']){//最大值,最小值都有设置
|
||||
if($_value >= $item['min'] && $_value < $item['max']){
|
||||
$_warning = true;
|
||||
$_lv = $lv;
|
||||
break 2;//跳出2层循环
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($_warning){//插入警报内容
|
||||
$this->inLog($device->sn, $reportedAt, $log, $_lv, $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function inLog(string $deviceSn, Carbon $reportedAt, Model $log, $lv, $column){
|
||||
DeviceWarning::create([
|
||||
'device_id' => $log->device_id,
|
||||
'base_id' => $log->agricultural_base_id,
|
||||
'lv' => $lv,
|
||||
'content' => $this->columnTexts[$column] . '达到' .$log->$column. $this->columnUnit[$column],
|
||||
'linkos_device_id' => $deviceSn,
|
||||
'linkos_reported_at' => $reportedAt
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue