IotService.cs 3.75 KB
using DataAcquisition.Models;
using DataAcquisition.ViewModels.IOT;
using HHECS.BllModel;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DataAcquisition.Services
{
    public class IotService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        private readonly string identity = "iot";
        private readonly Guid _clientId;

        public IotService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
        {
            _httpClientFactory = httpClientFactory;
            _clientId = configuration.GetValue<Guid>("ClientId");
        }

        /// <summary>
        /// 设备数据
        /// </summary>
        /// <param name="queues"></param>
        /// <returns></returns>
        public BllResult SendEquipmentData(IEnumerable<EquipmentDataQueue> queues)
        {
            var data = queues.Select(x => new IotEquipmentDataDto
            {
                Plmeid = x.Id == Guid.Empty ? Guid.NewGuid() : x.Id,
                EquipmentSN = x.EquipmentCode,
                Reported = JsonSerializer.Deserialize<List<TagItem>>(x.Reported) ?? new List<TagItem>(),
                Timestamp = x.SourceTimestamp,
                Version = x.Version,
            }).ToList();
            return Post("Equipment/SendEquipmentData", data);
        }

        public BllResult UpdateClientStatus()
        {
            var data = new
            {
                ClientId = _clientId
            };
            return Post("Equipment/UpdateClientStatus", data);
        }

        /// <summary>
        /// 设备报警
        /// </summary>
        /// <param name="equipmentAlarm"></param>
        /// <returns></returns>
        public BllResult SendEquipmentAlarm(IotEquipmentAlarm equipmentAlarm)
        {
            return Post("", equipmentAlarm);
        }

        /// <summary>
        /// 设备运行参数
        /// </summary>
        /// <param name="equipmmentRunParameter"></param>
        /// <returns></returns>
        public BllResult SendEquipmentRunParameter(IotEquipmmentRunParameter equipmmentRunParameter)
        {
            return Post("", equipmmentRunParameter);
        }

        /// <summary>
        /// 设备状态
        /// </summary>
        /// <param name="equipmentStatus"></param>
        /// <returns></returns>
        public BllResult SendEquipmentStatus(IotEquipmentStatus equipmentStatus)
        {
            return Post("", equipmentStatus);
        }

        private BllResult Post<T>(string requestUri, T data)
        {
            try
            {
                var client = _httpClientFactory.CreateClient(identity);
                var json = JsonSerializer.Serialize(data, JsonSerializeOptions);
                var responseMessage = client.PostAsync(requestUri, new StringContent(json, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();
                if (!responseMessage.IsSuccessStatusCode)
                {
                    return BllResultFactory.Error(responseMessage.ReasonPhrase ?? string.Empty);
                }
                var responseContent = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                return JsonSerializer.Deserialize<BllResult>(responseContent, JsonDeserializeOptions)!;
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        private static readonly JsonSerializerOptions JsonSerializeOptions = new JsonSerializerOptions
        {
            ReferenceHandler = ReferenceHandler.IgnoreCycles,
        };

        private static readonly JsonSerializerOptions JsonDeserializeOptions = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true,
        };
    }
}