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

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