update 采集是判断设备重连以及监听设备断开链接的时间

This commit is contained in:
chenbo 2023-12-14 11:11:26 +08:00
parent 5bfd8a7f5d
commit 7498de2d0d
2 changed files with 172 additions and 14 deletions

View File

@ -1,9 +1,14 @@
<?php <?php
namespace app\api\controller; namespace app\api\controller;
use app\common\model\device\DeviceOffline;
use app\common\model\device\MonitorAlarm;
use app\common\model\device\MonitorThreshold; use app\common\model\device\MonitorThreshold;
use app\common\model\land\LandProduct;
use app\common\model\LandCollection; use app\common\model\LandCollection;
use app\common\model\product\ProductDevice;
use Exception; use Exception;
use think\Db;
use think\facade\Log; use think\facade\Log;
class DataCollectController extends BaseApiController class DataCollectController extends BaseApiController
@ -19,10 +24,14 @@ class DataCollectController extends BaseApiController
if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){
return $this->fail('参数错误'); return $this->fail('参数错误');
} }
$payload= json_decode($parmas['payload'], true);
$land = explode('_', $parmas['username']); // 命名规则land_id id土地表主键id $land = explode('_', $parmas['username']); // 命名规则land_id id土地表主键id
$device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id // mqtt服务端 消息发布事件
$data = [ if ($parmas['event'] == 'message.publish') {
$payload= json_decode($parmas['payload'], true);
// 如果该设备之前离线,现在重新上线则删除之前的设备离线记录
$device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id
$data = [
'land_id' => $land[3], 'land_id' => $land[3],
'device_id'=>$device[1], 'device_id'=>$device[1],
'qos'=>$parmas['qos'], 'qos'=>$parmas['qos'],
@ -44,21 +53,157 @@ class DataCollectController extends BaseApiController
'client_id'=>$parmas['clientid'], 'client_id'=>$parmas['clientid'],
'create_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'), '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) { } catch (Exception $e) {
return $this->fail($e->getMessage()); 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() public function disabled()
{ {
$parmas = $this->request->post(); $parmas = $this->request->post();

View File

@ -0,0 +1,13 @@
<?php
namespace app\common\model\device;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
class DeviceOffline extends BaseModel
{
use SoftDelete;
protected $name = 'device_offline';
}