From 64d3a7b8573a44f1954aaefb16c162d97fe6b9c6 Mon Sep 17 00:00:00 2001 From: weiz Date: Sat, 9 Dec 2023 14:45:33 +0800 Subject: [PATCH 01/19] update --- app/api/controller/PlantController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/api/controller/PlantController.php b/app/api/controller/PlantController.php index 08deb26f..a5444a60 100644 --- a/app/api/controller/PlantController.php +++ b/app/api/controller/PlantController.php @@ -35,6 +35,7 @@ return $this->fail('种植信息错误'); } $data['pic'] = json_decode($data['pic'],true); + $data['plant_date'] = date('Y-m-d H:i:s',$data['plant_date']); return $this->success('请求成功',$data->toArray()); } From 5f09f7a92af71c44b8a6b594eb8e3f5980ef9369 Mon Sep 17 00:00:00 2001 From: chan <709206448@qq.com> Date: Tue, 12 Dec 2023 16:20:07 +0800 Subject: [PATCH 02/19] =?UTF-8?q?=E5=9C=9F=E5=A3=A4=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=94=B6=E9=9B=86=E8=AF=A6=E6=83=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 54 ++++++++++++++++++++ app/common/model/LandCollection.php | 8 +++ 2 files changed, 62 insertions(+) create mode 100644 app/api/controller/DataCollectController.php create mode 100644 app/common/model/LandCollection.php diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php new file mode 100644 index 00000000..fc1e111f --- /dev/null +++ b/app/api/controller/DataCollectController.php @@ -0,0 +1,54 @@ +request->post(); + if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ + return $this->fail('参数错误'); + } + $payload= json_decode($parmas['payload'], true); + + $data = [ + 'land_name'=>$parmas['username'], + 'topic'=>$parmas['topic'], + 'qos'=>$parmas['qos'], + 'wind_speed' => $payload['wind_speed'], + 'wind_direction' => $payload['wind_direction'], + 'ambient_temperature' => $payload['ambient_temperature'], + 'ambient_humidity' => $payload['ambient_humidity'], + 'carbon_dioxide' => $payload['carbon_dioxide'], + 'ambient_air_pressure' => $payload['ambient_air_pressure'], + 'rainfall' => $payload['rainfall'], + 'ambient_lighting' => $payload['ambient_lighting'], + 'soil_temperature' => $payload['soil_temperature'], + 'soil_moisture' => $payload['soil_moisture'], + 'soil_PH' => $payload['soil_PH'], + 'soil_potassium_phosphate_nitrogen' => $payload['soil_potassium_phosphate_nitrogen'], + 'soil_potassium_phosphate_phosphorus' => $payload['soil_potassium_phosphate_phosphorus'], + 'soil_potassium_phosphate_potassium' => $payload['soil_potassium_phosphate_potassium'], + 'clientid'=>$parmas['clientid'], + 'create_time'=>date('Y-m-d H:i:s'), + 'update_time'=>date('Y-m-d H:i:s'), + ]; + + LandCollection::create($data); + return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + + } +} \ No newline at end of file diff --git a/app/common/model/LandCollection.php b/app/common/model/LandCollection.php new file mode 100644 index 00000000..374f47ec --- /dev/null +++ b/app/common/model/LandCollection.php @@ -0,0 +1,8 @@ + Date: Tue, 12 Dec 2023 16:20:38 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E6=94=B6=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index fc1e111f..9ed3ca7a 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -8,7 +8,7 @@ use Exception; use Monolog\Handler\WhatFailureGroupHandler; use think\response\Json; -class IndexController extends BaseApiController +class DataCollectController extends BaseApiController { public $notNeedLogin = ['collect']; // 种植数据采集 From 889c06ca96f2edc4672da8ca08063ab8421802ae Mon Sep 17 00:00:00 2001 From: chan <709206448@qq.com> Date: Tue, 12 Dec 2023 16:23:28 +0800 Subject: [PATCH 04/19] add --- app/api/controller/DataCollectController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 9ed3ca7a..848688ea 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -1,16 +1,12 @@ Date: Wed, 13 Dec 2023 10:31:57 +0800 Subject: [PATCH 05/19] debug --- app/api/controller/DataCollectController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 848688ea..24a9813c 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -3,6 +3,7 @@ namespace app\api\controller; use app\common\model\LandCollection; use Exception; +use think\facade\Log; class DataCollectController extends BaseApiController { @@ -11,7 +12,9 @@ class DataCollectController extends BaseApiController public function collect() { try { - $parmas = $this->request->post(); + + $parmas = $this->request->post(); + Log::info($parmas); if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ return $this->fail('参数错误'); } From 5a54f0b692bddd1ed573684ed15bebdf1f5b4b0c Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 11:24:38 +0800 Subject: [PATCH 06/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 24a9813c..a98a227d 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -19,10 +19,11 @@ class DataCollectController extends BaseApiController return $this->fail('参数错误'); } $payload= json_decode($parmas['payload'], true); - + $land = explode('_', $parmas['username']); // 命名规则:land_id id土地表主键id + $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id $data = [ - 'land_name'=>$parmas['username'], - 'topic'=>$parmas['topic'], + 'land_id' => $land[1], + 'topic'=>$device[1], 'qos'=>$parmas['qos'], 'wind_speed' => $payload['wind_speed'], 'wind_direction' => $payload['wind_direction'], @@ -41,7 +42,9 @@ class DataCollectController extends BaseApiController 'clientid'=>$parmas['clientid'], 'create_time'=>date('Y-m-d H:i:s'), 'update_time'=>date('Y-m-d H:i:s'), - ]; + ]; + + LandCollection::create($data); return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); From 03bde0d4848ed69d5a9609763c827e3726315e50 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 11:35:37 +0800 Subject: [PATCH 07/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index a98a227d..af7316f5 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -44,8 +44,6 @@ class DataCollectController extends BaseApiController 'update_time'=>date('Y-m-d H:i:s'), ]; - - LandCollection::create($data); return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); } catch (Exception $e) { From f36404ae718821da21713d2f0606fe84c7a7c5d0 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 11:54:28 +0800 Subject: [PATCH 08/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index af7316f5..45095d79 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -14,7 +14,7 @@ class DataCollectController extends BaseApiController try { $parmas = $this->request->post(); - Log::info($parmas); + Log::info(json_encode($parmas)); if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ return $this->fail('参数错误'); } From 1155dac72a72a86e22742e571c25225953927adc Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 13:40:25 +0800 Subject: [PATCH 09/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 45095d79..f82f829f 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -22,7 +22,7 @@ class DataCollectController extends BaseApiController $land = explode('_', $parmas['username']); // 命名规则:land_id id土地表主键id $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id $data = [ - 'land_id' => $land[1], + 'land_id' => $land[3], 'topic'=>$device[1], 'qos'=>$parmas['qos'], 'wind_speed' => $payload['wind_speed'], From 273f5d5197a604f8d3a4a7f85e1aa920ba77ff28 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 13:41:49 +0800 Subject: [PATCH 10/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index f82f829f..e0643c18 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -23,7 +23,7 @@ class DataCollectController extends BaseApiController $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id $data = [ 'land_id' => $land[3], - 'topic'=>$device[1], + 'device_id'=>$device[1], 'qos'=>$parmas['qos'], 'wind_speed' => $payload['wind_speed'], 'wind_direction' => $payload['wind_direction'], From 9dfd62ac731466d3a3dfd4466d37d7469384bb62 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 13:42:40 +0800 Subject: [PATCH 11/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index e0643c18..6bc12033 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -39,7 +39,7 @@ class DataCollectController extends BaseApiController 'soil_potassium_phosphate_nitrogen' => $payload['soil_potassium_phosphate_nitrogen'], 'soil_potassium_phosphate_phosphorus' => $payload['soil_potassium_phosphate_phosphorus'], 'soil_potassium_phosphate_potassium' => $payload['soil_potassium_phosphate_potassium'], - 'clientid'=>$parmas['clientid'], + 'client_id'=>$parmas['clientid'], 'create_time'=>date('Y-m-d H:i:s'), 'update_time'=>date('Y-m-d H:i:s'), ]; From 7659cea7670b8da7ceb58aaad85a62979b9fae05 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 13:44:21 +0800 Subject: [PATCH 12/19] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 6bc12033..208da856 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -34,6 +34,7 @@ class DataCollectController extends BaseApiController 'rainfall' => $payload['rainfall'], 'ambient_lighting' => $payload['ambient_lighting'], 'soil_temperature' => $payload['soil_temperature'], + 'soil_conductivity' => $payload['soil_conductivity'], 'soil_moisture' => $payload['soil_moisture'], 'soil_PH' => $payload['soil_PH'], 'soil_potassium_phosphate_nitrogen' => $payload['soil_potassium_phosphate_nitrogen'], From 20e2e3c1592df27eec4a0f44df2aadeee8b2a37c Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 16:26:52 +0800 Subject: [PATCH 13/19] =?UTF-8?q?add=20=E6=96=AD=E5=BC=80=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 208da856..8f0e6f34 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -1,13 +1,14 @@ toArray(); + + return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); } catch (Exception $e) { return $this->fail($e->getMessage()); } } + + public function disabled() + { + $parmas = $this->request->post(); + Log::info(json_encode($parmas)); + } } \ No newline at end of file From 5bfd8a7f5d52016d8f861816bcb3f9eceb751d71 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 13 Dec 2023 17:52:51 +0800 Subject: [PATCH 14/19] =?UTF-8?q?add=20=E6=96=AD=E5=BC=80=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 8f0e6f34..29d21bb7 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -15,7 +15,7 @@ class DataCollectController extends BaseApiController try { $parmas = $this->request->post(); - Log::info(json_encode($parmas)); + Log::info('采集消息发布'.json_encode($parmas)); if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ return $this->fail('参数错误'); } @@ -49,7 +49,7 @@ class DataCollectController extends BaseApiController LandCollection::create($data); // 报警监测 - $monitorThreshold = MonitorThreshold::find()->toArray(); + $monitorThreshold = MonitorThreshold::find(); return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); @@ -62,6 +62,6 @@ class DataCollectController extends BaseApiController public function disabled() { $parmas = $this->request->post(); - Log::info(json_encode($parmas)); + Log::info('连接断开事件'.json_encode($parmas)); } } \ No newline at end of file From 7498de2d0dfb845c9267b74267fc2e27be3d85c0 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Thu, 14 Dec 2023 11:11:26 +0800 Subject: [PATCH 15/19] =?UTF-8?q?update=20=E9=87=87=E9=9B=86=E6=98=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E9=87=8D=E8=BF=9E=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 173 +++++++++++++++++-- app/common/model/device/DeviceOffline.php | 13 ++ 2 files changed, 172 insertions(+), 14 deletions(-) create mode 100644 app/common/model/device/DeviceOffline.php diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 29d21bb7..7b9e2dff 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -1,9 +1,14 @@ request->post(); Log::info('采集消息发布'.json_encode($parmas)); - if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ - return $this->fail('参数错误'); - } - $payload= json_decode($parmas['payload'], true); + if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ + return $this->fail('参数错误'); + } $land = explode('_', $parmas['username']); // 命名规则:land_id id土地表主键id - $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id - $data = [ + // mqtt服务端 消息发布事件 + if ($parmas['event'] == 'message.publish') { + + $payload= json_decode($parmas['payload'], true); + // 如果该设备之前离线,现在重新上线则删除之前的设备离线记录 + $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id + $data = [ 'land_id' => $land[3], 'device_id'=>$device[1], - 'qos'=>$parmas['qos'], + 'qos'=>$parmas['qos'], 'wind_speed' => $payload['wind_speed'], 'wind_direction' => $payload['wind_direction'], 'ambient_temperature' => $payload['ambient_temperature'], @@ -42,23 +51,159 @@ class DataCollectController extends BaseApiController 'soil_potassium_phosphate_phosphorus' => $payload['soil_potassium_phosphate_phosphorus'], 'soil_potassium_phosphate_potassium' => $payload['soil_potassium_phosphate_potassium'], 'client_id'=>$parmas['clientid'], - 'create_time'=>date('Y-m-d H:i:s'), - 'update_time'=>date('Y-m-d H:i:s'), - ]; + 'create_time'=>date('Y-m-d H:i:s'), + 'update_time'=>date('Y-m-d H:i:s'), + ]; - LandCollection::create($data); + LandCollection::create($data); - // 报警监测 - $monitorThreshold = MonitorThreshold::find(); + // 设备告警 + $monitorThreshold = MonitorThreshold::find(); + // 风速告警 + if ($payload['wind_speed'] > $monitorThreshold['wind_speed_max']) { + self::createAlarm($land[3], $device[1], 'wind_speed', '风速偏快' ,$payload['wind_speed']); + } + // ambient_temperature 环境温度 + if ($payload['ambient_temperature'] < $monitorThreshold['air_temp_min']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '气温偏低' ,$payload['ambient_temperature']); + } - return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); + // ambient_temperature 环境温度 + if ($payload['ambient_temperature'] > $monitorThreshold['air_temp_max']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '气温偏高' ,$payload['ambient_temperature']); + } + + // ambient_humidity 环境湿度 + if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_min']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '空气湿度偏低' ,$payload['ambient_humidity']); + } + // ambient_humidity 环境湿度 + if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_max']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '空气湿度偏高' ,$payload['ambient_humidity']); + } + + // carbon_dioxide 二氧化碳含量 + if ($payload['carbon_dioxide'] > $monitorThreshold['air_co2_content_max']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '空气二氧化碳含量偏高' ,$payload['carbon_dioxide']); + } + // carbon_dioxide 二氧化碳含量 + if ($payload['carbon_dioxide'] < $monitorThreshold['air_co2_content_min']) { + self::createAlarm($land[3], $device[1], 'ambient_temperature', '空气二氧化碳含量偏低' ,$payload['carbon_dioxide']); + } + + // ambient_air_pressure 大气压力 + if ($payload['ambient_air_pressure'] >= 120) + { + self::createAlarm($land[3], $device[1], '大气压力', '大气压力偏高' ,$payload['ambient_air_pressure']); + } + if ($payload['ambient_air_pressure'] < 10) + { + self::createAlarm($land[3], $device[1], '大气压力', '大气压力偏低' ,$payload['ambient_air_pressure']); + } + + // rainfall 雨量 +// if ($payload['rainfall'] >= 100) + // ambient_lighting 光照 + + // soil_temperature 土壤温度 + if ($payload['soil_temperature'] >= $monitorThreshold['soil_temp_max']) { + self::createAlarm($land[3], $device[1], '土壤温度', '土壤温度偏高' ,$payload['soil_temperature']); + } + // soil_temperature 土壤温度 + if ($payload['soil_temperature'] < $monitorThreshold['soil_temp_min']) { + self::createAlarm($land[3], $device[1], '土壤温度', '土壤温度偏低' ,$payload['soil_temperature']); + } + + // soil_conductivity 土壤电导率 +// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_max']) { +// self::createAlarm($land[3], $device[1], '土壤电导率', '土壤湿度偏高' ,$payload['soil_conductivity']); +// } +// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_min']) { +// self::createAlarm($land[3], $device[1], '土壤电导率', '土壤湿度偏低' ,$payload['soil_conductivity']); +// } + + // soil_conductivity 土壤湿度 + if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_max']) { + self::createAlarm($land[3], $device[1], '土壤湿度', '土壤湿度偏高' ,$payload['soil_moisture']); + } + if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_min']) { + self::createAlarm($land[3], $device[1], '土壤湿度', '土壤湿度偏低' ,$payload['soil_moisture']); + } + + // soil_PH 土壤ph值 + if ($payload['soil_PH'] < $monitorThreshold['soil_ph_max']) { + self::createAlarm($land[3], $device[1], '土壤PH值', '土壤PH值偏碱性' ,$payload['soil_moisture']); + } + if ($payload['soil_PH'] < $monitorThreshold['soil_ph_min']) { + self::createAlarm($land[3], $device[1], '土壤PH值', '土壤PH值偏酸性' ,$payload['soil_moisture']); + } + + // soil_potassium_phosphate_nitrogen 土壤磷酸钾:氮 + if ($payload['soil_PH'] < $monitorThreshold['soil_n_content_max']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-氮', '土壤磷酸钾-含氮量偏高' ,$payload['soil_moisture']); + } + if ($payload['soil_PH'] < $monitorThreshold['soil_n_content_min']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-氮', '土壤磷酸钾-含氮量偏低' ,$payload['soil_moisture']); + } + + // soil_potassium_phosphate_phosphorus 土壤磷酸钾:磷 + if ($payload['soil_PH'] < $monitorThreshold['soil_p_content_max']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-磷', '土壤磷酸钾-含磷量偏高' ,$payload['soil_moisture']); + } + if ($payload['soil_PH'] < $monitorThreshold['soil_p_content_min']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-磷', '土壤磷酸钾-含磷量偏低' ,$payload['soil_moisture']); + } + + // soil_potassium_phosphate_potassium 土壤磷酸钾:钾 + if ($payload['soil_PH'] < $monitorThreshold['soil_k_content_max']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-钾', '土壤磷酸钾-含钾量偏高' ,$payload['soil_moisture']); + } + if ($payload['soil_PH'] < $monitorThreshold['soil_k_content_min']) { + self::createAlarm($land[3], $device[1], '土壤磷酸钾-钾', '土壤磷酸钾-含钾量偏低' ,$payload['soil_moisture']); + } + + return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); + } + + // mqtt服务端 连接断开事件 客户端断开表示该土块的所有设备都已断开 共用同一个mqtt客户端 + if ($parmas['event'] == 'client.disconnected') { + // 查询该土地关联的设备 + $landProduct = LandProduct::where('land_id', $land[3])->find(); + $productDevice = ProductDevice::where('product_id', $landProduct['product_id'])->select(); + foreach ($productDevice as $item) { + $addData = [ + 'land_id' => $land[3], + 'device_id' => $item['device_id'], + 'last_online_time' => $parmas['disconnected_at'], + 'disconnected_time' => $parmas['disconnected_time'], + 'create_time' => time(), + 'update_time' => time() + ]; + DeviceOffline::create($addData); + } + } } catch (Exception $e) { return $this->fail($e->getMessage()); } } + public static function createAlarm($user_id, $device_id, $type, $content, $value) + { + $data = [ + 'user_id' => $user_id, + 'device_id' => $device_id, + 'type' => $type, + 'content' => $content, + 'value' => $value, + 'create_time' => time(), + 'update_time' => time(), + ]; + + MonitorAlarm::create($data); + } + public function disabled() { $parmas = $this->request->post(); diff --git a/app/common/model/device/DeviceOffline.php b/app/common/model/device/DeviceOffline.php new file mode 100644 index 00000000..c4a01b77 --- /dev/null +++ b/app/common/model/device/DeviceOffline.php @@ -0,0 +1,13 @@ + Date: Thu, 14 Dec 2023 11:37:25 +0800 Subject: [PATCH 16/19] =?UTF-8?q?update=20=E9=87=87=E9=9B=86=E6=98=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E9=87=8D=E8=BF=9E=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 7b9e2dff..fca8db06 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -176,7 +176,7 @@ class DataCollectController extends BaseApiController 'land_id' => $land[3], 'device_id' => $item['device_id'], 'last_online_time' => $parmas['disconnected_at'], - 'disconnected_time' => $parmas['disconnected_time'], + 'disconnected_time' => $parmas['disconnected_at'], 'create_time' => time(), 'update_time' => time() ]; From 131db42a6ba53029d0d51d94e04e32cb4c31ddf0 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Thu, 14 Dec 2023 11:40:11 +0800 Subject: [PATCH 17/19] =?UTF-8?q?update=20=E9=87=87=E9=9B=86=E6=98=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E9=87=8D=E8=BF=9E=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index fca8db06..3784c5c6 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -175,8 +175,8 @@ class DataCollectController extends BaseApiController $addData = [ 'land_id' => $land[3], 'device_id' => $item['device_id'], - 'last_online_time' => $parmas['disconnected_at'], - 'disconnected_time' => $parmas['disconnected_at'], + 'last_online_time' => $parmas['disconnected_at']/1000, + 'disconnected_time' => $parmas['disconnected_at']/1000, 'create_time' => time(), 'update_time' => time() ]; From 35068581a16ec2b871005232058401786dd58fc1 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Thu, 14 Dec 2023 11:41:04 +0800 Subject: [PATCH 18/19] =?UTF-8?q?update=20=E9=87=87=E9=9B=86=E6=98=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E9=87=8D=E8=BF=9E=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 3784c5c6..5fcc3d27 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -182,6 +182,7 @@ class DataCollectController extends BaseApiController ]; DeviceOffline::create($addData); } + return $this->success('接收成功'); } } catch (Exception $e) { return $this->fail($e->getMessage()); From d28f12e0cfeb859be28c5397b052af0f97bccb8a Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Thu, 14 Dec 2023 11:50:43 +0800 Subject: [PATCH 19/19] =?UTF-8?q?update=20=E9=87=87=E9=9B=86=E6=98=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E9=87=8D=E8=BF=9E=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DataCollectController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index 5fcc3d27..41262335 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -30,6 +30,7 @@ class DataCollectController extends BaseApiController $payload= json_decode($parmas['payload'], true); // 如果该设备之前离线,现在重新上线则删除之前的设备离线记录 + DeviceOffline::where('land_id', $land[3])->delete(); $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id $data = [ 'land_id' => $land[3],