using DeviceProcessor; using System; using System.Collections.Generic; using WayneChina_IcCardReader_SinoChem.MessageEntity; using WayneChina_IcCardReader_SinoChem.MessageEntity.Incoming; namespace WayneChina_IcCardReader_SinoChem { public class Handler : IHandler, IFdcCommunicableController { #region Fields private int nozzleIdA; private int nozzleIdB; private object syncObj = new object(); private readonly int addressA; private readonly int addressB; #endregion #region Interface implementation, UNUSED for now public Guid Id => Guid.NewGuid(); public Func BroadcastMessageViaFdc { get; set; } public Func> OnMessageReceivedViaFdc { get ; set; } #endregion #region Event Handler public event EventHandler OnCardReaderMessageReceived; #endregion #region Constructor public Handler(int bindingNozzleIdA, int bindingNozzleIdB, int addressA, int addressB) { nozzleIdA = bindingNozzleIdA; nozzleIdB = bindingNozzleIdB; this.addressA = addressA; this.addressB = addressB; } #endregion #region Methods public void Init(IContext context) { CardReaderContext = context; } public void Write(IcCardReaderMessageBase readerMessage) { if (CardReaderContext != null) { lock (syncObj) { CardReaderContext.Outgoing.Write(readerMessage); } } } public void Process(IContext context) { int targetNozzleId = -1; if (context.Incoming.Message.SourceAddress == addressA) { targetNozzleId = nozzleIdA; } else if (context.Incoming.Message.SourceAddress == addressB) { targetNozzleId = nozzleIdB; } OnCardReaderMessageReceived?.Invoke(this, new CardReaderMessageEventArgs(targetNozzleId, context.Incoming.Message)); } public byte GetAddressForNozzleId(int id) { if (SupportedNozzles.Contains(id)) { if (id == nozzleIdA) return (byte)addressA; if (id == nozzleIdB) return (byte)addressB; } return 0xFF; } #endregion #region Properties public int HandlerId { get { return Math.Max(nozzleIdA, nozzleIdB) / 2; } } public int AddressA { get { return addressA; } } public int AddressB { get { return addressB; } } public List SupportedNozzles { get { return new List { nozzleIdA, nozzleIdB }; } } public IContext CardReaderContext { get; set; } #endregion } }