DigitalTwinController.cs 2.3 KB
using FreeSql;
using HHECS.BllModel;
using HHECS.DAQServer.DataAccess;
using HHECS.DAQServer.Dto.DigitalTwin;
using HHECS.EquipmentModel;
using LinqKit;
using Microsoft.AspNetCore.Mvc;

namespace HHECS.DAQServer.Controllers
{
    /// <summary>
    /// 数字孪生接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DigitalTwinController : ControllerBase
    {
        private readonly DataContext _context;

        public DigitalTwinController(DataContext dataContext)
        {
            _context = dataContext;
        }

        /// <summary>
        /// 获取设备实时数据
        /// </summary>
        /// <param name="equipmentInfoRequestDto"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        public BllResult<List<EquipmentInfoResponseDto>> GetEquipmentInfo(EquipmentInfoRequestDto equipmentInfoRequestDto)
        {
            try
            {
                var filter = PredicateBuilder.New<Equipment>(true);
                if (!string.IsNullOrWhiteSpace(equipmentInfoRequestDto.EquipmentCode))
                {
                    filter = filter.And(x => x.Code == equipmentInfoRequestDto.EquipmentCode);
                }
                else if (!string.IsNullOrWhiteSpace(equipmentInfoRequestDto.Requestor))
                {
                    var area = equipmentInfoRequestDto.Requestor;
                    filter = filter.And(x => x.Code.StartsWith(equipmentInfoRequestDto.Requestor));
                }

                var equipmentData = _context.Equipment.Where(filter).IncludeMany(x => x.EquipmentProps).ToList(x => new EquipmentInfoResponseDto
                {
                    EquipmentCode = x.Code,
                    EquipmentName = x.Name,
                    EquipmentProps = x.EquipmentProps.Select(p => new EquipmentPropDto
                    {
                        EquipmentPropCode = p.EquipmentTypePropTemplateCode,
                        EquipmentPropName = p.Remark,
                        Value = p.Value,
                    }),
                });
                return BllResultFactory.Success(equipmentData);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<EquipmentInfoResponseDto>>(ex.Message);
            }
        }
    }
}