This commit is contained in:
chenbo 2024-02-19 13:45:28 +08:00
parent 15977d3d89
commit 31d7dbe201
3 changed files with 106 additions and 57 deletions

View File

@ -3,6 +3,7 @@
namespace app\api\controller\dataview;
use app\api\controller\BaseApiController;
use app\common\logic\RemoteRequestLogic;
use app\common\model\device\Device;
use app\common\model\device\MonitorAlarm;
use app\common\model\farm\Farm;
@ -36,34 +37,32 @@ class DeviceController extends BaseApiController
// 设备告警统计
public function deviceAlarmCount()
{
$landId = $this->request->get('land_id', 0);
$list = MonitorAlarm::alias('ma')->field('ma.*,d.code AS device_code,d.name AS device_name,d.image')
->join('device d', 'ma.device_id=d.id')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) use ($landId) {
if ($landId != 0) {
$query->where('ma.land_id', $landId);
} else {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
}
})
->whereWeek('ma.create_time')
->order('ma.id', 'desc')
->limit(100)
->select();
$deviceList = Device::alias('d')->where(function ($query) {
if ($this->streetCode != '') {
$farmIds = Farm::where('street', $this->streetCode)->column('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');
$query->whereIn('d.id', $deviceIds);
} else {
$farmIds = Farm::where('area', $this->areaCode)->column('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');
$query->whereIn('d.id', $deviceIds);
}
})->select()->toArray();
// 请求mqtt接口
$list = RemoteRequestLogic::getAlarmData($deviceList);
return $this->success('成功', compact('list'));
}
// 监测设备数量统计
public function deviceCount()
{
$total = Device::alias('d')->where(function ($query) {
$query = Device::alias('d')->where(function ($query) {
if ($this->streetCode != '') {
$farmIds = Farm::where('street', $this->streetCode)->column('id');
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
@ -77,43 +76,17 @@ class DeviceController extends BaseApiController
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
$query->whereIn('d.id', $deviceIds);
}
})
->count();
});
$online = Device::alias('d')->where(function ($query) {
if ($this->streetCode != '') {
$farmIds = Farm::where('street', $this->streetCode)->column('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');
$query->whereIn('d.id', $deviceIds);
} else {
$farmIds = Farm::where('area', $this->areaCode)->column('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');
$query->whereIn('d.id', $deviceIds);
}
})->where('d.is_online', 1)->count();
$total = $query->count();
$offline = Device::alias('d')->where(function ($query) {
if ($this->streetCode != '') {
$farmIds = Farm::where('street', $this->streetCode)->column('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');
$query->whereIn('d.id', $deviceIds);
} else {
$farmIds = Farm::where('area', $this->areaCode)->column('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');
$query->whereIn('d.id', $deviceIds);
}
})->where('d.is_online', 2)->count();
$online = $query->where('d.is_online', 1)->count();
$alarmCount = 0;
$todayAlarmCount = 0;
$offline = $query->where('d.is_online', 2)->count();
$deviceList = $query->select()->toArray();
// 请求mqtt
[$alarmCount,$todayAlarmCount] = RemoteRequestLogic::getAlarmCount($deviceList);
return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount'));

View File

@ -16,7 +16,7 @@ use think\exception\ValidateException;
class FarmController extends BaseApiController
{
public array $notNeedLogin = [
'farmCount','breedTypeCount'
'farmCount','breedTypeCount','farmInfo'
];
public function initialize()
{
@ -115,4 +115,21 @@ class FarmController extends BaseApiController
return $this->success('成功', compact('farmCount', 'totalScale', 'breedCount'));
}
public function farmInfo()
{
// 获取栏舍信息
$params = $this->request->param();
$farmInfo = Farm::where('id', $params['id'])->findOrEmpty()->toArray();
$data = FenceHouse::where('farm_id',$params['id'])->findOrEmpty()->toArray();
$product = Product::where('fence_house_id', $data['id'])->findOrEmpty()->toArray();
if(empty($product)){
return $this->fail('栏舍暂未绑定设备',[]);
}
$deviceIds = ProductDevice::where('product_id', $product['id'])->column('device_id');
$device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty();
$farmInfo['video_url'] = $device['video_url'];
$farmInfo['device_id'] = $device['id'];
return $this->success('成功', compact('farmInfo'));
}
}

View File

@ -94,4 +94,63 @@ class RemoteRequestLogic extends BaseLogic
$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'];
}
}