This commit is contained in:
chenbo 2024-02-20 10:02:07 +08:00
parent 3ade7df922
commit 520e1037d7
2 changed files with 93 additions and 2 deletions

View File

@ -17,7 +17,7 @@ use think\exception\ValidateException;
class FarmController extends BaseApiController
{
public array $notNeedLogin = [
'farmCount','breedTypeCount','farmInfo','deviceAlarmCount','deviceCount'
'farmCount','breedTypeCount','farmInfo','deviceAlarmCount','deviceCount','alarmRangeList'
];
public function initialize()
{
@ -181,4 +181,15 @@ class FarmController extends BaseApiController
return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount'));
}
public function alarmRangeList()
{
$params = $this->request->param();
$farmIds = [$params['id']];
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
$list = RemoteRequestLogic::getRangeMonitorData($deviceIds);
return $this->success('成功', $list);
}
}

View File

@ -143,9 +143,89 @@ class RemoteRequestLogic extends BaseLogic
return [$totalAlarmCount, $todayAlarmCount];
}
// 设备总告警数
public static function requestAlarmCount($device = [])
{
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning', [
// todo 接口地址
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). '', [
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
// 设备今日告警数
public static function requestTodayAlarmCount($device = [])
{
// todo 接口地址
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). '', [
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
// 获取近一周的监控数据
public static function getRangeMonitorData($deviceIds = [])
{
// 查询产品绑定的设备,根据对应的设备检测项查询监测数据
$datas = [];
// 空气温度 ambient_temperature
$ambientTemperatureDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'ambient_temperature')->find();
if (!empty($ambientTemperatureDevice)) {
$ambientTemperatureDevice['RangeMonitorData'] = self::requestRangeMonitorData($ambientTemperatureDevice);
} else {
$ambientTemperatureDevice['RangeMonitorData'] = [];
}
// 空气湿度 ambient_humidity
$ambientHumidityDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'ambient_humidity')->find();
if (!empty($ambientHumidityDevice)) {
$ambientHumidityDevice['RangeMonitorData'] = self::requestRangeMonitorData($ambientHumidityDevice);
} else {
$ambientHumidityDevice['RangeMonitorData'] = [];
}
// 氮气 nitrogen
$NDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'nitrogen')->find();
if (!empty($NDevice)) {
$NDevice['RangeMonitorData'] = self::requestRangeMonitorData($NDevice);
} else {
$NDevice['RangeMonitorData'] = [];
}
// 防火 fireproof
$fireproofDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'fireproof')->find();
if (!empty($fireproofDevice)) {
$fireproofDevice['RangeMonitorData'] = self::requestRangeMonitorData($fireproofDevice);
} else {
$fireproofDevice['RangeMonitorData'] = [];
}
// 甲烷 methane
$methaneDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'methane')->find();
if (!empty($methaneDevice)) {
$methaneDevice['RangeMonitorData'] = self::requestRangeMonitorData($methaneDevice);
} else {
$methaneDevice['RangeMonitorData'] = [];
}
// 噪音 noise
$noiseDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'noise')->find();
if (!empty($noiseDevice)) {
$noiseDevice['RangeMonitorData'] = self::requestRangeMonitorData($noiseDevice);
} else {
$noiseDevice['RangeMonitorData'] = [];
}
array_push($datas, $ambientTemperatureDevice, $ambientHumidityDevice, $fireproofDevice, $methaneDevice, $noiseDevice, $NDevice);
return $datas;
}
public static function requestRangeMonitorData($device)
{
// todo 接口地址
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). '', [
'query' => [
'deviceId' => $device['code']
]