using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.Model;
using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.Icemoker
{
    public class IceMakerMachine : BasePeripheral
    {
        IceMakerHelper iceMakerHelper = new IceMakerHelper();
        public override void Init()
        {
            iceMakerHelper.Open(communicationPar.SerialPort, communicationPar.BaudRate);

            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                IsConnected = iceMakerHelper.IsOpen;
                if (!IsConnected) IsWork = false;
                while (IsConnected)
                {
                    IsWork = true;
                    if (status != null)
                    {
                        SetStatus("GetDeviceStatus", iceMakerHelper.GetDeviceStatus());
                    }
                    Thread.Sleep(500);
                }
                Thread.Sleep(1000);
            }), $"设备[{DeviceId}]制冰机读取线程", true);

            EventBus.EventBus.GetInstance().Subscribe<StartMakeIce>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                iceMakerHelper.StartCook();
            });
            EventBus.EventBus.GetInstance().Subscribe<StopMakeIce>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                iceMakerHelper.EndCook();
            });
            EventBus.EventBus.GetInstance().Subscribe<SetIceNumber>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                var parm = @event as SetIceNumber;
                iceMakerHelper.IceValue(Convert.ToByte(parm?.Value));
            });
        }

        public override void Start()
        {
        }

        public override void Stop()
        {
        }

        public override void WriteData(string address, object value)
        {
        }

        protected override void InitStatus()
        {
        }
    }
}