Compare commits

..

3 Commits

1 changed files with 50 additions and 51 deletions

View File

@ -8,6 +8,7 @@ use app\common\model\device\MonitorThreshold;
use app\common\model\land\LandProduct; use app\common\model\land\LandProduct;
use app\common\model\LandCollection; use app\common\model\LandCollection;
use app\common\model\product\ProductDevice; use app\common\model\product\ProductDevice;
use app\common\model\User;
use Exception; use Exception;
use think\Db; use think\Db;
use think\facade\Log; use think\facade\Log;
@ -25,15 +26,24 @@ class DataCollectController extends BaseApiController
if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){ if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){
return $this->fail('参数错误'); return $this->fail('参数错误');
} }
$land = explode('_', $parmas['username']); // 命名规则land_id id土地表主键id $payload= json_decode($parmas['payload'], true);
$user = User::where('account', $parmas['username'])->find();
$device = explode('_', $parmas['topic']); // 命名规则camera_deviceid deviceid为设备主键id
$deviceId = $device[1]; // 设备id
$productDevice = ProductDevice::where('device_id', $deviceId)->find();
$landProduct = LandProduct::where('product_id', $productDevice['product_id'])->find();
$landId = $landProduct['land_id']; // 土地id
// mqtt服务端 消息发布事件 // mqtt服务端 消息发布事件
if ($parmas['event'] == 'message.publish') { if ($parmas['event'] == 'message.publish') {
$payload= json_decode($parmas['payload'], true);
$device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id
$data = [ $data = [
'land_id' => $land[3], 'user_id' => $user['id'],
'device_id'=>$device[1], 'land_id' => $landId,
'device_id' => $deviceId,
'qos'=>$parmas['qos'], 'qos'=>$parmas['qos'],
'wind_speed' => $payload['wind_speed'], 'wind_speed' => $payload['wind_speed'],
'wind_direction' => $payload['wind_direction'], 'wind_direction' => $payload['wind_direction'],
@ -61,45 +71,45 @@ class DataCollectController extends BaseApiController
$monitorThreshold = (new MonitorThreshold())->select()->toArray()[0]; $monitorThreshold = (new MonitorThreshold())->select()->toArray()[0];
// 风速告警 // 风速告警
if ($payload['wind_speed'] > $monitorThreshold['wind_speed_max']) { if ($payload['wind_speed'] > $monitorThreshold['wind_speed_max']) {
self::createAlarm($land[3], $device[1], 'wind_speed', '风速偏快' ,$payload['wind_speed']); self::createAlarm($landId, $deviceId, 'wind_speed', '风速偏快' ,$payload['wind_speed']);
} }
// ambient_temperature 环境温度 // ambient_temperature 环境温度
if ($payload['ambient_temperature'] < $monitorThreshold['air_temp_min']) { if ($payload['ambient_temperature'] < $monitorThreshold['air_temp_min']) {
self::createAlarm($land[3], $device[1], 'ambient_temperature', '气温偏低' ,$payload['ambient_temperature']); self::createAlarm($landId, $deviceId, 'ambient_temperature', '气温偏低' ,$payload['ambient_temperature']);
} }
// ambient_temperature 环境温度 // ambient_temperature 环境温度
if ($payload['ambient_temperature'] > $monitorThreshold['air_temp_max']) { if ($payload['ambient_temperature'] > $monitorThreshold['air_temp_max']) {
self::createAlarm($land[3], $device[1], 'ambient_temperature', '气温偏高' ,$payload['ambient_temperature']); self::createAlarm($landId, $deviceId, 'ambient_temperature', '气温偏高' ,$payload['ambient_temperature']);
} }
// ambient_humidity 环境湿度 // ambient_humidity 环境湿度
if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_min']) { if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_min']) {
self::createAlarm($land[3], $device[1], 'ambient_humidity', '环境湿度偏低' ,$payload['ambient_humidity']); self::createAlarm($landId, $deviceId, 'ambient_humidity', '环境湿度偏低' ,$payload['ambient_humidity']);
} }
// ambient_humidity 环境湿度 // ambient_humidity 环境湿度
if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_max']) { if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_max']) {
self::createAlarm($land[3], $device[1], 'ambient_humidity', '环境湿度偏高' ,$payload['ambient_humidity']); self::createAlarm($landId, $deviceId, 'ambient_humidity', '环境湿度偏高' ,$payload['ambient_humidity']);
} }
// carbon_dioxide 二氧化碳含量 // carbon_dioxide 二氧化碳含量
if ($payload['carbon_dioxide'] > $monitorThreshold['air_co2_content_max']) { if ($payload['carbon_dioxide'] > $monitorThreshold['air_co2_content_max']) {
self::createAlarm($land[3], $device[1], 'carbon_dioxide', '空气二氧化碳含量偏高' ,$payload['carbon_dioxide']); self::createAlarm($landId, $deviceId, 'carbon_dioxide', '空气二氧化碳含量偏高' ,$payload['carbon_dioxide']);
} }
// carbon_dioxide 二氧化碳含量 // carbon_dioxide 二氧化碳含量
if ($payload['carbon_dioxide'] < $monitorThreshold['air_co2_content_min']) { if ($payload['carbon_dioxide'] < $monitorThreshold['air_co2_content_min']) {
self::createAlarm($land[3], $device[1], 'carbon_dioxide', '空气二氧化碳含量偏低' ,$payload['carbon_dioxide']); self::createAlarm($landId, $deviceId, 'carbon_dioxide', '空气二氧化碳含量偏低' ,$payload['carbon_dioxide']);
} }
// ambient_air_pressure 大气压力 // ambient_air_pressure 大气压力
if ($payload['ambient_air_pressure'] >= 120) if ($payload['ambient_air_pressure'] >= 120)
{ {
self::createAlarm($land[3], $device[1], 'ambient_air_pressure', '大气压力偏高' ,$payload['ambient_air_pressure']); self::createAlarm($landId, $deviceId, 'ambient_air_pressure', '大气压力偏高' ,$payload['ambient_air_pressure']);
} }
if ($payload['ambient_air_pressure'] < 10) if ($payload['ambient_air_pressure'] < 10)
{ {
self::createAlarm($land[3], $device[1], 'ambient_air_pressure', '大气压力偏低' ,$payload['ambient_air_pressure']); self::createAlarm($landId, $deviceId, 'ambient_air_pressure', '大气压力偏低' ,$payload['ambient_air_pressure']);
} }
// rainfall 雨量 // rainfall 雨量
@ -108,59 +118,59 @@ class DataCollectController extends BaseApiController
// soil_temperature 土壤温度 // soil_temperature 土壤温度
if ($payload['soil_temperature'] >= $monitorThreshold['soil_temp_max']) { if ($payload['soil_temperature'] >= $monitorThreshold['soil_temp_max']) {
self::createAlarm($land[3], $device[1], 'soil_temperature', '土壤温度偏高' ,$payload['soil_temperature']); self::createAlarm($landId, $deviceId, 'soil_temperature', '土壤温度偏高' ,$payload['soil_temperature']);
} }
// soil_temperature 土壤温度 // soil_temperature 土壤温度
if ($payload['soil_temperature'] < $monitorThreshold['soil_temp_min']) { if ($payload['soil_temperature'] < $monitorThreshold['soil_temp_min']) {
self::createAlarm($land[3], $device[1], 'soil_temperature', '土壤温度偏低' ,$payload['soil_temperature']); self::createAlarm($landId, $deviceId, 'soil_temperature', '土壤温度偏低' ,$payload['soil_temperature']);
} }
// soil_conductivity 土壤电导率 // soil_conductivity 土壤电导率
// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_max']) { // if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_max']) {
// self::createAlarm($land[3], $device[1], '土壤电导率', '土壤湿度偏高' ,$payload['soil_conductivity']); // self::createAlarm($land[3], $deviceId, '土壤电导率', '土壤湿度偏高' ,$payload['soil_conductivity']);
// } // }
// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_min']) { // if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_min']) {
// self::createAlarm($land[3], $device[1], '土壤电导率', '土壤湿度偏低' ,$payload['soil_conductivity']); // self::createAlarm($land[3], $deviceId, '土壤电导率', '土壤湿度偏低' ,$payload['soil_conductivity']);
// } // }
// soil_conductivity 土壤湿度 // soil_conductivity 土壤湿度
if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_max']) { if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_max']) {
self::createAlarm($land[3], $device[1], 'soil_moisture', '土壤湿度偏高' ,$payload['soil_moisture']); self::createAlarm($landId, $deviceId, 'soil_moisture', '土壤湿度偏高' ,$payload['soil_moisture']);
} }
if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_min']) { if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_min']) {
self::createAlarm($land[3], $device[1], 'soil_moisture', '土壤湿度偏低' ,$payload['soil_moisture']); self::createAlarm($landId, $deviceId, 'soil_moisture', '土壤湿度偏低' ,$payload['soil_moisture']);
} }
// soil_PH 土壤ph值 // soil_PH 土壤ph值
if ($payload['soil_PH'] < $monitorThreshold['soil_ph_max']) { if ($payload['soil_PH'] < $monitorThreshold['soil_ph_max']) {
self::createAlarm($land[3], $device[1], 'soil_PH', '土壤PH值偏碱性' ,$payload['soil_moisture']); self::createAlarm($landId, $deviceId, 'soil_PH', '土壤PH值偏碱性' ,$payload['soil_moisture']);
} }
if ($payload['soil_PH'] < $monitorThreshold['soil_ph_min']) { if ($payload['soil_PH'] < $monitorThreshold['soil_ph_min']) {
self::createAlarm($land[3], $device[1], 'soil_PH', '土壤PH值偏酸性' ,$payload['soil_moisture']); self::createAlarm($landId, $deviceId, 'soil_PH', '土壤PH值偏酸性' ,$payload['soil_moisture']);
} }
// soil_potassium_phosphate_nitrogen 土壤磷酸钾:氮 // soil_potassium_phosphate_nitrogen 土壤磷酸钾:氮
if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_max']) { if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_max']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏高' ,$payload['soil_potassium_phosphate_nitrogen']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏高' ,$payload['soil_potassium_phosphate_nitrogen']);
} }
if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_min']) { if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_min']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏低' ,$payload['soil_potassium_phosphate_nitrogen']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏低' ,$payload['soil_potassium_phosphate_nitrogen']);
} }
// soil_potassium_phosphate_phosphorus 土壤磷酸钾:磷 // soil_potassium_phosphate_phosphorus 土壤磷酸钾:磷
if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_max']) { if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_max']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏高' ,$payload['soil_potassium_phosphate_phosphorus']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏高' ,$payload['soil_potassium_phosphate_phosphorus']);
} }
if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_min']) { if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_min']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏低' ,$payload['soil_potassium_phosphate_phosphorus']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏低' ,$payload['soil_potassium_phosphate_phosphorus']);
} }
// soil_potassium_phosphate_potassium 土壤磷酸钾:钾 // soil_potassium_phosphate_potassium 土壤磷酸钾:钾
if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_max']) { if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_max']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏高' ,$payload['soil_potassium_phosphate_potassium']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏高' ,$payload['soil_potassium_phosphate_potassium']);
} }
if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_min']) { if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_min']) {
self::createAlarm($land[3], $device[1], 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏低' ,$payload['soil_potassium_phosphate_potassium']); self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏低' ,$payload['soil_potassium_phosphate_potassium']);
} }
return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]); return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]);
@ -168,34 +178,23 @@ class DataCollectController extends BaseApiController
// mqtt服务端 连接断开事件 客户端断开表示该土块的所有设备都已断开 共用同一个mqtt客户端 // mqtt服务端 连接断开事件 客户端断开表示该土块的所有设备都已断开 共用同一个mqtt客户端
if ($parmas['event'] == 'client.disconnected') { 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 = [ $addData = [
'land_id' => $land[3], 'land_id' => $landId,
'device_id' => $item['device_id'], 'device_id' => $deviceId,
'last_online_time' => $parmas['disconnected_at']/1000, 'last_online_time' => $parmas['disconnected_at']/1000,
'disconnected_time' => $parmas['disconnected_at']/1000, 'disconnected_time' => $parmas['disconnected_at']/1000,
'create_time' => time(), 'create_time' => time(),
'update_time' => time() 'update_time' => time()
]; ];
DeviceOffline::create($addData); DeviceOffline::create($addData);
Device::where('id', $deviceId)->update(['is_online' => 2]);
Device::where('id', $item['device_id'])->update(['is_online' => 2]);
}
return $this->success('接收成功'); return $this->success('接收成功');
} }
// 连接链接事件 设备已上线 // 连接链接事件 设备已上线
if ($parmas['event'] == 'client.connected') { if ($parmas['event'] == 'client.connected') {
// 查询该土地关联的设备 Device::where('id', $deviceId)->update(['is_online' => 1]);
$landProduct = LandProduct::where('land_id', $land[3])->find();
$productDevice = ProductDevice::where('product_id', $landProduct['product_id'])->select();
// 如果该设备之前离线,现在重新上线则删除之前的设备离线记录
DeviceOffline::where('land_id', $land[3])->delete();
foreach ($productDevice as $item) {
Device::where('id', $item['device_id'])->update(['is_online' => 1]);
}
return $this->success('接收成功'); return $this->success('接收成功');
} }