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

156 lines
5.2 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 = [])
{
$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'];
}
}