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 IT6724CPollingRead : PollingRead
    {
        private IT6724C _IT6724C { get; set; }
        private IEventAggregator _eventAggregator { get; set; }
        private IBaseService<IT6724CData> _iT6724CService { get; set; }
        public IT6724CPollingRead(IContainerProvider containerProvider) : base(containerProvider)
        {
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();
            var _devices = containerProvider.Resolve<Devices>();
            _iT6724CService = containerProvider.Resolve<IT6724CService>();
            DeviceName = "Backfeed";
            _IT6724C = _devices.DeviceDic["IT6724C"] as IT6724C;
        }
        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 _IT6724C!.查询实时电流(ct);
            //实时电压 = await _IT6724C!.查询实时电压(ct);
            //实时功率 = await _IT6724C!.查询实时功率(ct);
            var reuslt = await _iT6724CService.InsertAsync(new IT6724CData
            {
                RealTimeCurrent = 实时电流,
                RealTimePower = 实时电压,
                RealTimeVoltage = 实时功率,
                CollectTime = DateTime.Now
            });
            if (reuslt.IsSuccess)
            {
                var dataDic = new Dictionary<string, double>
                {
                    { "实时电流", 实时电流 },
                    { "实时电压", 实时电压 },
                    { "实时功率", 实时功率 }
                };
                _eventAggregator.GetEvent<CurveDataEvent>().Publish(("IT6724C", dataDic));
            }
            else
            {
                LoggerHelper.ErrorWithNotify(reuslt.Msg);
            }
        }
    }
}
