using BPASmartClient.EventBus;
using BPASmartClient.Model.PLC;
using BPASmartClient.Model.单片机;
using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.MCU
{
    public class MCUMachine : BasePeripheral
    {
        MCUSerialHelper mCUSerialHelper = new MCUSerialHelper();

        public override void Init()
        {
            mCUSerialHelper.Open(communicationPar.SerialPort, communicationPar.BaudRate);

            EventBus.EventBus.GetInstance().Subscribe<ReadMcu>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                var par = @event as ReadMcu;
                if (status != null)
                {
                    SetStatus(par?.TagName, mCUSerialHelper.GetInputStatus(Convert.ToByte(par?.ReadPar)));
                }
            });

            EventBus.EventBus.GetInstance().Subscribe<WriteMcu>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                var par = @event as WriteMcu;
                switch (par?.TagName)
                {
                    case "OutputControl":
                        mCUSerialHelper.OutputControl(Convert.ToByte(par?.Address), Convert.ToBoolean(par?.Value));
                        break;
                    case "ServoControl":
                        mCUSerialHelper.ServoControl(Convert.ToByte(par?.Address), Convert.ToByte(par?.Value));
                        break;
                    default:
                        break;
                }
            });
        }

        //public override void ReadData(string address)
        //{

        //}

        public override void Start()
        {

        }

        public override void Stop()
        {

        }

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

        }

        protected override void InitStatus()
        {

        }
    }
}