using BOB.Singleton;
using Common.PubEvent;
using DeviceCommand.Device;
using Logger;
using Model.SQLModel;
using Service.Implement;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BOB.Services
{
    public class EAEL9080PollingRead:PollingRead
    {
        private EAEL9080 _EAEL9080 { get; set; }
        private IEventAggregator _eventAggregator { get; set; }
        private IBaseService<EAELD9080Data> _eAEL9080Service { get; set; }
        public EAEL9080PollingRead(IContainerProvider containerProvider) : base(containerProvider)
        {
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();
            var _devices = containerProvider.Resolve<Devices>();
            _eAEL9080Service = containerProvider.Resolve<EAEL9080Service>();
            DeviceName = "EAEL9080";
            _EAEL9080 = _devices.DeviceDic["EAEL9080"] as EAEL9080;
        }
        public double 实时电流 { get; set; } = double.NaN;
        public double 实时电压 { get; set; } = double.NaN;
        public double 实时功率 { get; set; } = double.NaN;
        public override async Task ReadDeviceDataAsync(CancellationToken ct = default)
        {
            实时电流 = Random.Shared.NextDouble() * 10;
            实时电压 = Random.Shared.NextDouble() * 10;
            实时功率 = Random.Shared.NextDouble() * 10;
            //实时电流 = await _EAEL9080!.查询电流(ct);
            //实时电压 = await _EAEL9080!.查询电压(ct);
            //实时功率 = await _EAEL9080!.查询功率(ct);
            var reuslt = await _eAEL9080Service.InsertAsync(new EAELD9080Data
            {
                RealTimeCurrent = 实时电流,
                RealTimePower = 实时电压,
                RealTimeVoltage = 实时功率,
                CollectTime = DateTime.Now
            });
            if (reuslt.IsSuccess)
            {
                var dataDic = new Dictionary<string, double>
                {
                    { "实时电流", 实时电流 },
                    { "实时电压", 实时电压 },
                    { "实时功率", 实时功率 }
                };
                _eventAggregator.GetEvent<CurveDataEvent>().Publish(("EAEL9080", dataDic));
            }
            else
            {
                LoggerHelper.ErrorWithNotify(reuslt.Msg);
            }
           
        }
    }
}
