12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO.Ports;
- using System.Threading;
- using Edge.Core.Parser.BinaryParser.MessageEntity;
- using Edge.Core.Processor;
- using Edge.Core.IndustryStandardInterface.Pump;
- using Edge.Core.Parser;
- using System.Threading.Tasks;
- using Edge.Core.Processor.Communicator;
- namespace Mocks
- {
- public class ComPortCommunicatorMock<T> : ICommunicator<byte[], T> where T : MessageBase
- {
- private object syncObject = new object();
- private int isStarted = 0;
- public string Identity { get; set; }
- public event EventHandler OnConnected;
- public event EventHandler OnDisconnected;
- public event EventHandler<CommunicatorEventArg<byte[], T>> OnRawDataWriting;
- public event EventHandler<CommunicatorEventArg<byte[], T>> OnDataReceived;
- public event EventHandler<CommunicatorErrorMessageReadEventArg> OnErrorMessageRead;
- public bool Write(T message)
- {
- var safe = this.OnRawDataWriting;
- var arg = new CommunicatorEventArg<byte[], T>() { Message = message, Continue = true };
- safe?.Invoke(this, arg);
- return true;
- }
- public void Dispose()
- {
- this.isStarted = 0;
- }
- public async Task<bool> Start()
- {
- if (0 == Interlocked.CompareExchange(ref this.isStarted, 1, 0))
- {
- return true;
- }
- return false;
- }
- #region mock functions
- /// <summary>
- /// simulate a message was read and parsed from remote device, then push this message finally to Hanlder.
- /// </summary>
- /// <param name="message"></param>
- public void FireOnDataReceived(T message)
- {
- var safe = this.OnDataReceived;
- safe?.Invoke(this, new CommunicatorEventArg<byte[], T>() { Message = message });
- }
- public bool Write(T message, object extraControlParameter)
- {
- throw new NotImplementedException();
- }
- #endregion
- }
- }
|