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

97 lines
3.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'];
}
}