EquipmentController.cs 3.63 KB
using HHECS.BllModel;
using HHECS.DAQServer.DataAccess;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.DAQServer.Models;
using HHECS.EquipmentModel;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using System.Text.Json;

namespace HHECS.DAQServer.Controllers
{
    /// <summary>
    /// 设备数据
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentController : ControllerBase
    {
        private readonly DataContext _context;

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

        /// <summary>
        /// 推送设备实时数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult SendEquipmentData(IEnumerable<EquipmentDataDto> data)
        {
            try
            {
                var d = _context.EquipmentDataRecord.Where(x => true).Count();
                if (!data.Any())
                {
                    return BllResultFactory.Error($"数据不能为空!");
                }

                var records = data.Select(x => new EquipmentDataRecord
                {
                    Plmeid = x.Plmeid,
                    EquipmentCode = x.EquipmentSN,
                    Tags = JsonSerializer.Serialize(x.Reported),
                    IsHandle = false,
                    Version = x.Version,
                    CreateTime = DateTime.Now,
                    Timestamp = x.Timestamp,
                }).ToList();
                _context.EquipmentDataRecord.AddRange(records);
                _context.SaveChanges();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }






        /// <summary>
        /// 获取设备实时属性
        /// </summary>
        /// <param name="equipmentInfoQueryModel"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult GetEquipmentProps(EquipmentInfoQueryModel equipmentInfoQueryModel)
        {

            try
            {
                Expression<Func<EquipmentProp, bool>> filter = x => true;
                if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.EquipmentCode))
                {
                    filter = x => x.Equipment.Code == equipmentInfoQueryModel.EquipmentCode;
                }
                else if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.Requestor))
                {
                    var area = equipmentInfoQueryModel.Requestor;
                    if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.InventoryCode))
                    {
                        area = equipmentInfoQueryModel.Requestor + "-" + equipmentInfoQueryModel.InventoryCode;
                    }
                    filter = x => x.Equipment.Code.StartsWith(equipmentInfoQueryModel.Requestor);

                }



                var res = _context.EquipmentProp.Where(filter).Include(x => x.Equipment).ToList();

                var group = res.GroupBy(x => x.Equipment.Code);


                var response = group.Select(x => new
                {
                    EquipmentCode = x.Key,
                    Props = x.Select(y => new
                    {
                        Prop = y.EquipmentTypePropTemplateCode,
                        y.Value
                    })
                });
                return BllResultFactory.Success(response);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"{ex}");
            }
        }
    }
}