From 1f5d1c86598fe36e65392517b5df60cd16e2febc Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 16 Nov 2022 16:52:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AD=A6=E6=8A=A5=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/DeviceWarningService.php | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 app/Services/DeviceWarningService.php diff --git a/app/Services/DeviceWarningService.php b/app/Services/DeviceWarningService.php new file mode 100644 index 0000000..74163f5 --- /dev/null +++ b/app/Services/DeviceWarningService.php @@ -0,0 +1,110 @@ + + */ + 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 + ]); + } +}