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