EquipmentController.cs 1.69 KB
using HHECS.BllModel;
using HHECS.DAQServer.DataAccess;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.DAQServer.Models;
using Microsoft.AspNetCore.Mvc;
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
            {
                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);
            }
        }
    }
}