suyuan-breed/app/common/logic/RemoteRequestLogic.php

278 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace app\common\logic;
use app\common\model\device\Device;
use Symfony\Component\HttpClient\HttpClient;
class RemoteRequestLogic extends BaseLogic
{
// 获取监控数据
public static function getMonitorData($deviceIds = [])
{
// 查询产品绑定的设备,根据对应的设备检测项查询监测数据
$datas = [];
// 空气湿度和温度
$airDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'ambient_temperature,ambient_humidity')->find();
if (!empty($airDevice)) {
$airMonitor = self::requestMqtt($airDevice);
if (!empty($airMonitor)) {
$datas['ambient_temperature'] = $airMonitor[1][2];
$datas['ambient_humidity'] = $airMonitor[1][3];
} else {
$datas['ambient_temperature'] = 0;
$datas['ambient_humidity'] = 0;
}
2024-01-29 10:06:15 +08:00
} else {
$datas['ambient_temperature'] = 0;
$datas['ambient_humidity'] = 0;
}
// 氮气 nitrogen
$NDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'nitrogen')->find();
if (!empty($NDevice)) {
$NMonitor = self::requestMqtt($NDevice);
if (!empty($NMonitor)) {
$datas['nitrogen'] = $NMonitor[1][2];
2024-01-29 10:06:15 +08:00
}else {
$datas['nitrogen'] = 0;
}
} else {
$datas['nitrogen'] = 0;
}
// 防火 fireproof
$fireproofDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'fireproof')->find();
if (!empty($fireproofDevice)) {
$fireproofMonitor = self::requestMqtt($fireproofDevice);
if (!empty($fireproofMonitor)) {
$datas['fireproof'] = $fireproofMonitor[1][2];
2024-01-29 10:06:15 +08:00
} else {
$datas['fireproof'] = 0;
}
} else {
$datas['fireproof'] = 0;
}
// 甲烷 methane
$methaneDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'methane')->find();
if (!empty($methaneDevice)) {
$methaneMonitor = self::requestMqtt($methaneDevice);
if (!empty($methaneMonitor)) {
$datas['methane'] = $methaneMonitor[1][2];
2024-01-29 10:06:15 +08:00
} else {
$datas['methane'] = 0;
}
2024-01-29 10:06:15 +08:00
} else {
$datas['methane'] = 0;
}
// 噪音 noise
$noiseDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'noise')->find();
if (!empty($noiseDevice)) {
$noiseMonitor = self::requestMqtt($noiseDevice);
if (!empty($noiseMonitor)) {
$datas['noise'] = $noiseMonitor[1][2];
2024-01-29 10:06:15 +08:00
} else {
$datas['noise'] = 0;
}
} else {
$datas['noise'] = 0;
}
return $datas;
}
public static function requestMqtt($device)
{
2024-01-30 14:13:53 +08:00
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/data/query', [
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
2024-02-19 13:45:28 +08:00
public static function getAlarmData($deviceList = [])
{
2024-02-20 15:22:16 +08:00
$list = [];
2024-02-19 13:45:28 +08:00
// 获取设备报警信息
2024-02-20 15:22:16 +08:00
foreach ($deviceList as $device) {
2024-02-19 13:45:28 +08:00
$device['alarm_value'] = ''; // 警告数据
$device['alarm_time'] = ''; // 警告时间
$device['alarm_resolution'] = ''; // 解决方案
$device['alarm_reason'] = ''; // 警告原因
// 视频设备没有编码
if (empty($device['code'])) {
continue;
}
$temp = self::requestAlarmData($device);
2024-02-20 15:22:16 +08:00
if (empty($temp['values'])) {
$list[] = $device;
2024-02-19 13:45:28 +08:00
continue;
}
2024-02-20 15:22:16 +08:00
foreach ($temp['values'][4] as $key => $value) {
$device['alarm_value'] = $value;
$device['alarm_time'] = date('Y-m-d H:i:s', $temp['timestamps'][$key]/1000);
2024-02-20 15:22:33 +08:00
$device['alarm_reason'] = $temp['values'][0][$key];
$device['alarm_resolution'] = $temp['values'][2][$key];
2024-02-20 15:22:16 +08:00
$list[] = $device;
}
2024-02-19 13:45:28 +08:00
}
2024-02-20 15:22:16 +08:00
return $list;
2024-02-19 13:45:28 +08:00
}
public static function requestAlarmData($device = [])
{
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning', [
'query' => [
2024-02-20 15:33:19 +08:00
'deviceId' => $device['code'],
'limit' => 5,
'offset' => 0
2024-02-19 13:45:28 +08:00
]
]);
$monitor = json_decode($response->getContent(), true);
2024-02-20 15:22:16 +08:00
return $monitor['data'];
2024-02-19 13:45:28 +08:00
}
public static function getAlarmCount($deviceList = [])
{
$totalAlarmCount = 0;
$todayAlarmCount = 0;
// 获取设备报警信息
foreach ($deviceList as $deivce) {
2024-02-20 13:54:08 +08:00
if (empty($deivce['code'])) {
continue;
}
$row = self::requestAlarmCount($deivce);
$totalAlarmCount += $row['totoal_warning_count'];
$todayAlarmCount += $row['today_warning_count'];
2024-02-19 13:45:28 +08:00
}
return [$totalAlarmCount, $todayAlarmCount];
}
2024-02-20 10:02:07 +08:00
// 设备总告警数
2024-02-19 13:45:28 +08:00
public static function requestAlarmCount($device = [])
{
2024-02-20 13:54:08 +08:00
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning/statistics', [
2024-02-20 10:02:07 +08:00
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
2024-02-20 13:54:08 +08:00
return $monitor['data'];
2024-02-20 10:02:07 +08:00
}
// 获取近一周的监控数据
public static function getRangeMonitorData($deviceIds = [])
{
// 查询产品绑定的设备,根据对应的设备检测项查询监测数据
$datas = [];
// 空气温度 ambient_temperature
2024-02-20 15:22:16 +08:00
$ambientTemperatureDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'ambient_temperature,ambient_humidity')->find();
2024-02-20 10:02:07 +08:00
if (!empty($ambientTemperatureDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($ambientTemperatureDevice);
if (!empty($rangeData)) {
$ambientTemperatureDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$ambientTemperatureDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$ambientTemperatureDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
// 空气湿度 ambient_humidity
2024-02-20 15:22:16 +08:00
$ambientHumidityDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'ambient_temperature,ambient_humidity')->find();
2024-02-20 10:02:07 +08:00
if (!empty($ambientHumidityDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($ambientHumidityDevice);
if (!empty($rangeData)) {
$ambientHumidityDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$ambientHumidityDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$ambientHumidityDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
// 氮气 nitrogen
$NDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'nitrogen')->find();
if (!empty($NDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($NDevice);
if (!empty($rangeData)) {
$NDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$NDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$NDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
// 防火 fireproof
$fireproofDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'fireproof')->find();
if (!empty($fireproofDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($fireproofDevice);
if (!empty($rangeData)) {
$fireproofDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$fireproofDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$fireproofDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
2024-02-20 15:22:16 +08:00
2024-02-20 10:02:07 +08:00
// 甲烷 methane
$methaneDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'methane')->find();
if (!empty($methaneDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($methaneDevice);
if (!empty($rangeData)) {
$methaneDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$methaneDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];;
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$methaneDevice['RangeMonitorData'] =[0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
2024-02-20 15:22:16 +08:00
2024-02-20 10:02:07 +08:00
// 噪音 noise
$noiseDevice = Device::whereIn('id', $deviceIds)->where('monitor_item', 'noise')->find();
if (!empty($noiseDevice)) {
2024-02-20 15:22:16 +08:00
$rangeData = self::requestRangeMonitorData($noiseDevice);
if (!empty($rangeData)) {
$noiseDevice['RangeMonitorData'] = $rangeData[4];
} else {
2024-02-20 15:28:52 +08:00
$noiseDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 15:22:16 +08:00
}
2024-02-20 10:02:07 +08:00
} else {
2024-02-20 15:28:52 +08:00
$noiseDevice['RangeMonitorData'] = [0,0,0,0,0,0,0];
2024-02-20 10:02:07 +08:00
}
array_push($datas, $ambientTemperatureDevice, $ambientHumidityDevice, $fireproofDevice, $methaneDevice, $noiseDevice, $NDevice);
return $datas;
}
public static function requestRangeMonitorData($device)
{
2024-02-20 13:54:08 +08:00
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning/past_seven_days', [
2024-02-19 13:45:28 +08:00
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
2024-02-21 14:25:33 +08:00
public static function requestAnimalTemperature($rfid='')
{
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning/past_seven_days', [
'query' => [
'rfid' => $rfid
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data'];
}
}