123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using Aitex.Core.RT.DataCenter;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MECF.Framework.Simulator.Core.Driver
- {
- public class DeviceSimulator
- {
- public event Action<string> MessageIn;
- public event Action<string> MessageOut;
- public event Action<string> ErrorOccur;
- public virtual bool IsEnabled
- {
- get { return false; }
- }
- public virtual bool IsConnected
- {
- get { return false; }
- }
- protected SortedList<string, Action<string>> commandList = new SortedList<string, Action<string>>();
- protected string _lineDelimiter;
- protected char _msgDelimiter;
- protected int _commandIndex;
- public DeviceSimulator(int commandIndex, string lindDelimiter, char msgDelimiter)
- {
- _lineDelimiter = lindDelimiter;
- _msgDelimiter = msgDelimiter;
- _commandIndex = commandIndex;
- }
- protected virtual void AddCommandHandler(string command, Action<string> handler)
- {
- commandList.Add(command, handler);
- }
-
- protected virtual void ProcessUnsplitMessage(string message)
- {
- }
- protected virtual void ProcessUnsplitMessage(byte[] data)
- {
- }
- protected virtual void OnErrorMessage(string message)
- {
- if (ErrorOccur != null)
- {
- ErrorOccur(message);
- }
- }
- protected void OnReadMessage(byte[] data)
- {
- if(MessageIn!=null)
- {
- MessageIn(string.Join(" ", Array.ConvertAll(data, x => x.ToString("X2"))));
- }
- if(_commandIndex<0)
- {
- ProcessUnsplitMessage(data);
- return;
- }
- }
- protected void OnReadMessage(string message)
- {
- //if (message.Contains("FETCH"))
- //{
-
- //}
- if (MessageIn != null)
- {
- MessageIn(message);
- }
- if (_commandIndex < 0)
- {
- ProcessUnsplitMessage(message);
- return;
- }
- if (_msgDelimiter.Equals(' '))
- {
- string cmd = message.Substring(_commandIndex, 4);
- if (cmd.Contains(_msgDelimiter))
- cmd = cmd.Substring(0, cmd.IndexOf(_msgDelimiter));
- if (!commandList.ContainsKey(cmd))
- {
- CommandNotRecognized(message);
- }
- else
- {
- //Log.WriteIfEnabled( LogCategory.Debug, source, DeviceId + ":ProcessMessages: '" + msg.Message );
- var handler = commandList[cmd];
- if (handler == null)
- {
- //Log.WriteIfEnabled( LogCategory.Error, source, DeviceId + ":ProcessMessages: CANNOT FIND MESSAGE HANDLER '" + msg.Message );
- }
- else
- {
- if (!message.Contains("stat_pdo") && !message.Contains("statfx"))
- {
- //Log.WriteIfEnabled(LogCategory.Debug, source, "Received command " + message);
- }
- handler(message);
- }
- }
- }
- else
- {
- string[] msgComponents = message.Split(_msgDelimiter);
- int index = msgComponents[0] == "$$$" ? 1 : _commandIndex;
- if (msgComponents.Length <= index)
- {
- CommandNotRecognized(message);
- return;
- }
- // find the message handler in the dictionary
- string cmd = msgComponents[index];
- if (!commandList.ContainsKey(cmd))
- {
- CommandNotRecognized(message);
- }
- else
- {
- //Log.WriteIfEnabled( LogCategory.Debug, source, DeviceId + ":ProcessMessages: '" + msg.Message );
- var handler = commandList[cmd];
- if (handler == null)
- {
- //Log.WriteIfEnabled( LogCategory.Error, source, DeviceId + ":ProcessMessages: CANNOT FIND MESSAGE HANDLER '" + msg.Message );
- }
- else
- {
- if (!message.Contains("stat_pdo") && !message.Contains("statfx"))
- {
- //Log.WriteIfEnabled(LogCategory.Debug, source, "Received command " + message);
- }
- handler(message);
- }
- }
- }
- }
- protected virtual void CommandNotRecognized(string msg)
- {
- if (commandList.ContainsKey("Unknown") && commandList["Unknown"] != null)
- {
- commandList["Unknown"](msg);
- }
- else
- {
- ProcessWriteMessage("_ERR Unrecognized command");
- }
- }
- protected void OnWriteMessage(string msg)
- {
- if (MessageOut != null)
- MessageOut(msg);
- ProcessWriteMessage(msg);
- }
- protected void OnWriteMessage(byte[] bytes)
- {
- if (MessageOut != null)
- {
- MessageOut(string.Join(" ", Array.ConvertAll(bytes, x => x.ToString("X2"))));
- }
- ProcessWriteMessage(bytes);
- }
- protected virtual void ProcessWriteMessage(string msg)
- {
- }
- protected virtual void ProcessWriteMessage(byte[] bytes)
- {
- }
- }
- }
|