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

236 lines
8.5 KiB
PHP

<?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;
}
} 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];
}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];
} 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];
} else {
$datas['methane'] = 0;
}
} 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];
} else {
$datas['noise'] = 0;
}
} else {
$datas['noise'] = 0;
}
return $datas;
}
public static function requestMqtt($device)
{
$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'];
}
public static function getAlarmData($deviceList = [])
{
// 获取设备报警信息
foreach ($deviceList as &$device) {
$device['alarm_value'] = ''; // 警告数据
$device['alarm_time'] = ''; // 警告时间
$device['alarm_resolution'] = ''; // 解决方案
$device['alarm_reason'] = ''; // 警告原因
// 视频设备没有编码
if (empty($device['code'])) {
continue;
}
$temp = self::requestAlarmData($device);
if (empty($temp)) {
continue;
}
$device['alarm_value'] = $temp[2];
$device['alarm_time'] = date('Y-m-d H:i:s', time()); // todo 对接接口字段
$device['alarm_reason'] = ''; // todo 对接接口字段
$device['alarm_resolution'] = 'test'; // todo 对接接口字段
}
return $deviceList;
}
public static function requestAlarmData($device = [])
{
$response = HttpClient::create()->request('GET', env('PROJECT.MQTT_PROJECT_URL'). 'api/xumu/warning', [
'query' => [
'deviceId' => $device['code']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
public static function getAlarmCount($deviceList = [])
{
$totalAlarmCount = 0;
$todayAlarmCount = 0;
// 获取设备报警信息
foreach ($deviceList as $deivce) {
}
return [$totalAlarmCount, $todayAlarmCount];
}
// 设备总告警数
public static function requestAlarmCount($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 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']
]
]);
$monitor = json_decode($response->getContent(), true);
return $monitor['data']['values'];
}
}