2023-12-12 16:20:07 +08:00
|
|
|
|
<?php
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
2023-12-14 15:05:07 +08:00
|
|
|
|
use app\common\model\device\Device;
|
2023-12-14 11:11:26 +08:00
|
|
|
|
use app\common\model\device\DeviceOffline;
|
|
|
|
|
use app\common\model\device\MonitorAlarm;
|
2023-12-13 16:26:52 +08:00
|
|
|
|
use app\common\model\device\MonitorThreshold;
|
2023-12-14 11:11:26 +08:00
|
|
|
|
use app\common\model\land\LandProduct;
|
2023-12-12 16:20:07 +08:00
|
|
|
|
use app\common\model\LandCollection;
|
2023-12-14 11:11:26 +08:00
|
|
|
|
use app\common\model\product\ProductDevice;
|
2023-12-23 11:09:21 +08:00
|
|
|
|
use app\common\model\User;
|
2023-12-12 16:20:07 +08:00
|
|
|
|
use Exception;
|
2023-12-14 11:11:26 +08:00
|
|
|
|
use think\Db;
|
2023-12-13 10:31:57 +08:00
|
|
|
|
use think\facade\Log;
|
2023-12-12 16:20:07 +08:00
|
|
|
|
|
2023-12-12 16:20:38 +08:00
|
|
|
|
class DataCollectController extends BaseApiController
|
2023-12-12 16:20:07 +08:00
|
|
|
|
{
|
2023-12-13 16:26:52 +08:00
|
|
|
|
public array $notNeedLogin = ['collect', 'disabled'];
|
2023-12-12 16:20:07 +08:00
|
|
|
|
// 种植数据采集
|
|
|
|
|
public function collect()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2023-12-13 10:31:57 +08:00
|
|
|
|
|
|
|
|
|
$parmas = $this->request->post();
|
2023-12-13 17:52:51 +08:00
|
|
|
|
Log::info('采集消息发布'.json_encode($parmas));
|
2023-12-14 11:11:26 +08:00
|
|
|
|
if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){
|
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
|
}
|
2023-12-23 11:09:21 +08:00
|
|
|
|
$payload= json_decode($parmas['payload'], true);
|
|
|
|
|
|
|
|
|
|
$user = User::where('account', $parmas['username'])->find();
|
|
|
|
|
|
2023-12-23 10:21:59 +08:00
|
|
|
|
$device = explode('_', $parmas['topic']); // 命名规则:camera_deviceid deviceid为设备主键id
|
2023-12-23 11:09:21 +08:00
|
|
|
|
$deviceId = $device[1]; // 设备id
|
|
|
|
|
|
|
|
|
|
$productDevice = ProductDevice::where('device_id', $deviceId)->find();
|
2023-12-23 10:21:59 +08:00
|
|
|
|
$landProduct = LandProduct::where('product_id', $productDevice['product_id'])->find();
|
2023-12-23 11:09:21 +08:00
|
|
|
|
$landId = $landProduct['land_id']; // 土地id
|
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// mqtt服务端 消息发布事件
|
|
|
|
|
if ($parmas['event'] == 'message.publish') {
|
|
|
|
|
|
|
|
|
|
$data = [
|
2023-12-23 11:09:21 +08:00
|
|
|
|
'user_id' => $user['id'],
|
2023-12-23 10:21:59 +08:00
|
|
|
|
'land_id' => $landId,
|
|
|
|
|
'device_id' => $deviceId,
|
2023-12-14 11:11:26 +08:00
|
|
|
|
'qos'=>$parmas['qos'],
|
2023-12-12 16:20:07 +08:00
|
|
|
|
'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'],
|
2023-12-13 13:44:21 +08:00
|
|
|
|
'soil_conductivity' => $payload['soil_conductivity'],
|
2023-12-12 16:20:07 +08:00
|
|
|
|
'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'],
|
2023-12-13 13:42:40 +08:00
|
|
|
|
'client_id'=>$parmas['clientid'],
|
2023-12-14 11:11:26 +08:00
|
|
|
|
'create_time'=>date('Y-m-d H:i:s'),
|
|
|
|
|
'update_time'=>date('Y-m-d H:i:s'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
LandCollection::create($data);
|
|
|
|
|
|
|
|
|
|
// 设备告警
|
2023-12-14 14:52:43 +08:00
|
|
|
|
$monitorThreshold = (new MonitorThreshold())->select()->toArray()[0];
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// 风速告警
|
|
|
|
|
if ($payload['wind_speed'] > $monitorThreshold['wind_speed_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'wind_speed', '风速偏快' ,$payload['wind_speed']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambient_temperature 环境温度
|
|
|
|
|
if ($payload['ambient_temperature'] < $monitorThreshold['air_temp_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_temperature', '气温偏低' ,$payload['ambient_temperature']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambient_temperature 环境温度
|
|
|
|
|
if ($payload['ambient_temperature'] > $monitorThreshold['air_temp_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_temperature', '气温偏高' ,$payload['ambient_temperature']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambient_humidity 环境湿度
|
|
|
|
|
if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_humidity', '环境湿度偏低' ,$payload['ambient_humidity']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
// ambient_humidity 环境湿度
|
|
|
|
|
if ($payload['ambient_humidity'] < $monitorThreshold['air_mois_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_humidity', '环境湿度偏高' ,$payload['ambient_humidity']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// carbon_dioxide 二氧化碳含量
|
|
|
|
|
if ($payload['carbon_dioxide'] > $monitorThreshold['air_co2_content_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'carbon_dioxide', '空气二氧化碳含量偏高' ,$payload['carbon_dioxide']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
// carbon_dioxide 二氧化碳含量
|
|
|
|
|
if ($payload['carbon_dioxide'] < $monitorThreshold['air_co2_content_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'carbon_dioxide', '空气二氧化碳含量偏低' ,$payload['carbon_dioxide']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambient_air_pressure 大气压力
|
|
|
|
|
if ($payload['ambient_air_pressure'] >= 120)
|
|
|
|
|
{
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_air_pressure', '大气压力偏高' ,$payload['ambient_air_pressure']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
if ($payload['ambient_air_pressure'] < 10)
|
|
|
|
|
{
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'ambient_air_pressure', '大气压力偏低' ,$payload['ambient_air_pressure']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rainfall 雨量
|
|
|
|
|
// if ($payload['rainfall'] >= 100)
|
|
|
|
|
// ambient_lighting 光照
|
2023-12-13 11:24:38 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// soil_temperature 土壤温度
|
|
|
|
|
if ($payload['soil_temperature'] >= $monitorThreshold['soil_temp_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_temperature', '土壤温度偏高' ,$payload['soil_temperature']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
// soil_temperature 土壤温度
|
|
|
|
|
if ($payload['soil_temperature'] < $monitorThreshold['soil_temp_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_temperature', '土壤温度偏低' ,$payload['soil_temperature']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-13 16:26:52 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// soil_conductivity 土壤电导率
|
|
|
|
|
// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
// self::createAlarm($land[3], $deviceId, '土壤电导率', '土壤湿度偏高' ,$payload['soil_conductivity']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// }
|
|
|
|
|
// if ($payload['soil_conductivity'] < $monitorThreshold['soil_mois_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
// self::createAlarm($land[3], $deviceId, '土壤电导率', '土壤湿度偏低' ,$payload['soil_conductivity']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// }
|
2023-12-13 16:26:52 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// soil_conductivity 土壤湿度
|
|
|
|
|
if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_moisture', '土壤湿度偏高' ,$payload['soil_moisture']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
if ($payload['soil_moisture'] < $monitorThreshold['soil_mois_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_moisture', '土壤湿度偏低' ,$payload['soil_moisture']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-13 16:26:52 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// soil_PH 土壤ph值
|
|
|
|
|
if ($payload['soil_PH'] < $monitorThreshold['soil_ph_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_PH', '土壤PH值偏碱性' ,$payload['soil_moisture']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
if ($payload['soil_PH'] < $monitorThreshold['soil_ph_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_PH', '土壤PH值偏酸性' ,$payload['soil_moisture']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// soil_potassium_phosphate_nitrogen 土壤磷酸钾:氮
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏高' ,$payload['soil_potassium_phosphate_nitrogen']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_nitrogen'] < $monitorThreshold['soil_n_content_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_nitrogen', '土壤磷酸钾-含氮量偏低' ,$payload['soil_potassium_phosphate_nitrogen']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// soil_potassium_phosphate_phosphorus 土壤磷酸钾:磷
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏高' ,$payload['soil_potassium_phosphate_phosphorus']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_phosphorus'] < $monitorThreshold['soil_p_content_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_phosphorus', '土壤磷酸钾-含磷量偏低' ,$payload['soil_potassium_phosphate_phosphorus']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// soil_potassium_phosphate_potassium 土壤磷酸钾:钾
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_max']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏高' ,$payload['soil_potassium_phosphate_potassium']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-15 10:13:46 +08:00
|
|
|
|
if ($payload['soil_potassium_phosphate_potassium'] < $monitorThreshold['soil_k_content_min']) {
|
2023-12-23 11:09:21 +08:00
|
|
|
|
self::createAlarm($landId, $deviceId, 'soil_potassium_phosphate_potassium', '土壤磷酸钾-含钾量偏低' ,$payload['soil_potassium_phosphate_potassium']);
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mqtt服务端 连接断开事件 客户端断开表示该土块的所有设备都已断开 共用同一个mqtt客户端
|
|
|
|
|
if ($parmas['event'] == 'client.disconnected') {
|
2023-12-23 10:21:59 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
// 查询该土地关联的设备
|
2023-12-23 10:21:59 +08:00
|
|
|
|
$addData = [
|
|
|
|
|
'land_id' => $landId,
|
|
|
|
|
'device_id' => $deviceId,
|
|
|
|
|
'last_online_time' => $parmas['disconnected_at']/1000,
|
|
|
|
|
'disconnected_time' => $parmas['disconnected_at']/1000,
|
|
|
|
|
'create_time' => time(),
|
|
|
|
|
'update_time' => time()
|
|
|
|
|
];
|
|
|
|
|
DeviceOffline::create($addData);
|
|
|
|
|
Device::where('id', $deviceId)->update(['is_online' => 2]);
|
2023-12-14 11:41:04 +08:00
|
|
|
|
return $this->success('接收成功');
|
2023-12-14 11:11:26 +08:00
|
|
|
|
}
|
2023-12-14 16:32:25 +08:00
|
|
|
|
// 连接链接事件 设备已上线
|
|
|
|
|
if ($parmas['event'] == 'client.connected') {
|
2023-12-23 10:21:59 +08:00
|
|
|
|
Device::where('id', $deviceId)->update(['is_online' => 1]);
|
2023-12-14 16:32:25 +08:00
|
|
|
|
return $this->success('接收成功');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-12 16:20:07 +08:00
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return $this->fail($e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-12-13 16:26:52 +08:00
|
|
|
|
|
2023-12-14 11:11:26 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 16:26:52 +08:00
|
|
|
|
public function disabled()
|
|
|
|
|
{
|
|
|
|
|
$parmas = $this->request->post();
|
2023-12-13 17:52:51 +08:00
|
|
|
|
Log::info('连接断开事件'.json_encode($parmas));
|
2023-12-13 16:26:52 +08:00
|
|
|
|
}
|
2023-12-12 16:20:07 +08:00
|
|
|
|
}
|