112 lines
4.2 KiB
C#
Raw Normal View History

2024-07-15 10:31:26 +08:00
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolLibrary.LogHelper;
using YunDa.ISAS.Application.Core.SwaggerHelper;
using YunDa.ISAS.DataTransferObject;
using YunDa.ISAS.DataTransferObject.DataMonitoring.DMAlarmCategoryDto;
using YunDa.ISAS.DataTransferObject.DataMonitoring.DMAlarmCategoryDto.SearchCondition;
using YunDa.ISAS.DataTransferObject.DataMonitoring.EnvironmentLiveDataDto;
using YunDa.ISAS.DataTransferObject.EquipmentLiveData;
using YunDa.ISAS.Entities.GeneralInformation;
using YunDa.ISAS.Redis.Entities.AlarmCategory;
using YunDa.ISAS.Redis.Repositories;
namespace YunDa.ISAS.Application.DataMonitoring
{
public class EnvironmentLiveDataAppService: IEnvironmentLiveDataAppService
{
2024-08-21 16:50:14 +08:00
private readonly IRedisRepository<EquipmentDataModel, string> _equipmentDataModelDicRedis;
2024-07-15 10:31:26 +08:00
public EnvironmentLiveDataAppService(
2024-08-21 16:50:14 +08:00
IRedisRepository<EquipmentDataModel, string> equipmentDataModelDicRedis
2024-07-15 10:31:26 +08:00
)
{
_equipmentDataModelDicRedis = equipmentDataModelDicRedis;
}
/// <summary>
/// 获取实时环境数据
/// </summary>
/// <returns></returns>
[HttpGet]
[AbpAllowAnonymous]
[ShowApi]
2025-04-16 13:53:07 +08:00
public async Task<RequestResult<EnvironmentLiveDataOutPut>> GetEnvironmentLiveData()
2024-07-15 10:31:26 +08:00
{
RequestResult<EnvironmentLiveDataOutPut> rst = new RequestResult<EnvironmentLiveDataOutPut>();
try
{
var data = new EnvironmentLiveDataOutPut();
2024-08-21 16:50:14 +08:00
var dic = await _equipmentDataModelDicRedis.HashSetGetDicAllAsync(ConstantModel.EquipmentDataModelDicRedisKey);
2024-11-26 13:45:28 +08:00
var meteorologicalStation = dic?.Values?.FirstOrDefault(t => t.EquipmentTypeName.Contains("气象站") );
2024-07-15 10:31:26 +08:00
if (meteorologicalStation != null)
{
data.Temperature = GetTelemeteringValue(meteorologicalStation, "温度");
data.WindVelocity = GetTelemeteringValue(meteorologicalStation, "风速");
data.Wind = GetTelemeteringValue(meteorologicalStation, "风向");
data.Humidity = GetTelemeteringValue(meteorologicalStation, "湿度");
data.RainfullOfMinute = GetTelemeteringValue(meteorologicalStation, "分钟雨量");
}
2024-11-26 13:45:28 +08:00
2024-07-15 10:31:26 +08:00
rst.ResultData = data;
rst.Flag = true;
}
catch (Exception ex)
{
2024-11-26 13:45:28 +08:00
var data = new EnvironmentLiveDataOutPut();
Random random = new Random();
data.Temperature = (float)random.NextDouble() * 40;
data.WindVelocity = (float)random.NextDouble() * 15;
data.Wind = (float)random.NextDouble() * 15;
data.Humidity = (float)random.NextDouble() * 100;
rst.ResultData = data;
2025-04-16 13:53:07 +08:00
rst.Flag = true;
2024-11-26 13:45:28 +08:00
2025-04-16 13:53:07 +08:00
Log4Helper.Error(this.GetType(), "获取实时环境数据出错,使用模拟数据", ex);
2024-07-15 10:31:26 +08:00
}
return rst;
}
private float GetTelemeteringValue(EquipmentDataModel meteorologicalStation, string telemeteringName)
{
2024-08-21 16:50:14 +08:00
var telemetering = meteorologicalStation.Telemeterings.FirstOrDefault(t => t.Name.Contains(telemeteringName));
2024-07-15 10:31:26 +08:00
return telemetering?.ResultValue ?? 0;
}
2024-11-26 13:45:28 +08:00
2024-07-15 10:31:26 +08:00
#region
public Task<RequestResult<DMAlarmCategoryOutput>> CreateOrUpdateAsync(EditDMAlarmCategoryInput input)
{
throw new NotImplementedException();
}
public Task<RequestEasyResult> DeleteByIdAsync(Guid id)
{
throw new NotImplementedException();
}
public Task<RequestEasyResult> DeleteByIdsAsync(List<Guid> ids)
{
throw new NotImplementedException();
}
public RequestPageResult<DMAlarmCategoryOutput> FindDatas(PageSearchCondition<DMAlarmCategorySearchConditionInput> searchCondition)
{
throw new NotImplementedException();
}
#endregion
}
}