refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
+451
@@ -0,0 +1,451 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.Log;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.TruPlasmaRF
|
||||
{
|
||||
public class TruPlasmaRF : SerialPortDevice, IConnection
|
||||
{
|
||||
public string Address { get; }
|
||||
public bool IsConnected { get; }
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public string PortStatus { get; set; } = "Closed";
|
||||
|
||||
private TruPlasmaRFConnection _connection;
|
||||
public TruPlasmaRFConnection Connection
|
||||
{
|
||||
get { return _connection; }
|
||||
}
|
||||
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigCommunicationError = new R_TRIG();
|
||||
private R_TRIG _trigRetryConnect = new R_TRIG();
|
||||
|
||||
private PeriodicJob _thread;
|
||||
|
||||
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
|
||||
private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
|
||||
|
||||
public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
|
||||
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
private bool _enableLog;
|
||||
private string _scRoot;
|
||||
public TruPlasmaRF(string module, string name, string scRoot, string portName) : base(module, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
PortName = portName;
|
||||
}
|
||||
private void ResetPropertiesAndResponses()
|
||||
{
|
||||
|
||||
foreach (var ioResponse in IOResponseList)
|
||||
{
|
||||
ioResponse.ResonseContent = null;
|
||||
ioResponse.ResonseRecievedTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Initialize(string portName)
|
||||
{
|
||||
base.Initialize(portName);
|
||||
|
||||
ResetPropertiesAndResponses();
|
||||
|
||||
if (_connection != null && _connection.IsConnected && PortName == portName)
|
||||
return true;
|
||||
|
||||
if (_connection != null && _connection.IsConnected)
|
||||
_connection.Disconnect();
|
||||
|
||||
PortName = portName;
|
||||
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
_connection = new TruPlasmaRFConnection(PortName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
PortStatus = "Open";
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool InitConnection(string portName, int bautRate, int dataBits, Parity parity, StopBits stopBits)
|
||||
{
|
||||
|
||||
_connection = new TruPlasmaRFConnection(portName, bautRate, dataBits, parity, stopBits);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool OnTimer()
|
||||
{
|
||||
try
|
||||
{
|
||||
//_connection.MonitorTimeout();
|
||||
|
||||
if (!_connection.IsConnected || _connection.IsCommunicationError)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
}
|
||||
|
||||
_trigRetryConnect.CLK = !_connection.IsConnected;
|
||||
if (_trigRetryConnect.Q)
|
||||
{
|
||||
_connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
|
||||
if (!_connection.Connect())
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//_lstHandler.AddLast(new TruPlasmaRFQueryPinHandler(this, _deviceAddress));
|
||||
//_lstHandler.AddLast(new TruPlasmaRFSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
HandlerBase handler = null;
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0)
|
||||
{
|
||||
foreach (var monitorHandler in _lstMonitorHandler)
|
||||
{
|
||||
_lstHandler.AddLast(monitorHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (_lstHandler.Count > 0)
|
||||
{
|
||||
handler = _lstHandler.First.Value;
|
||||
|
||||
_lstHandler.RemoveFirst();
|
||||
}
|
||||
}
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal void NoteInterfaceActived(bool actived)
|
||||
{
|
||||
InterfaceActived = actived;
|
||||
}
|
||||
|
||||
internal void NoteAnyCommandResult(string result)
|
||||
{
|
||||
AnyCommandResult = result;
|
||||
}
|
||||
|
||||
public override void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
//_connection.EnableLog(_enableLog);
|
||||
|
||||
|
||||
_trigCommunicationError.CLK = _connection.IsCommunicationError;
|
||||
if (_trigCommunicationError.Q)
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
|
||||
_connection.SetCommunicationError(false, "");
|
||||
_trigCommunicationError.RST = true;
|
||||
|
||||
//_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
|
||||
|
||||
_trigRetryConnect.RST = true;
|
||||
|
||||
base.Reset();
|
||||
}
|
||||
|
||||
public override bool Home(out string reason)
|
||||
{
|
||||
return base.Home(out reason);
|
||||
}
|
||||
|
||||
|
||||
#region Command Functions
|
||||
public void PerformRawCommand(string command, string comandArgument)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFRawCommandHandler(this, command, comandArgument));
|
||||
}
|
||||
}
|
||||
|
||||
internal void NotePiValue(int value)
|
||||
{
|
||||
PiValue = value;
|
||||
}
|
||||
internal void NoteSetPiValueCompleted(bool value)
|
||||
{
|
||||
SetPiValueCompleted = value;
|
||||
}
|
||||
|
||||
public void PerformRawCommand(string command)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFRawCommandHandler(this, command));
|
||||
}
|
||||
}
|
||||
|
||||
public void GetControl()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFGetControlHandler(this));
|
||||
}
|
||||
}
|
||||
public void ReleaseControl()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFReleaseControlHandler(this));
|
||||
}
|
||||
}
|
||||
|
||||
public void PreSetPiValue(string piValue)
|
||||
{
|
||||
PreSetPiValue(int.Parse(piValue));
|
||||
}
|
||||
public void PreSetPiValue(int piValue)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFPreSetPiValueHandler(this, piValue));
|
||||
}
|
||||
}
|
||||
public void ReadPiValue(bool isSelected)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(TruPlasmaRFReadPiValueHandler));
|
||||
if (isSelected)
|
||||
{
|
||||
if (!existHandlers.Any())
|
||||
_lstMonitorHandler.AddFirst(new TruPlasmaRFReadPiValueHandler(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (existHandlers.Any())
|
||||
{
|
||||
_lstMonitorHandler.Remove(existHandlers.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ExecuteAnyCommand(TruPlasmaRFArugment rfArg)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFExecuteAnyCommandHandler(this, JsonConvert.SerializeObject(rfArg)));
|
||||
}
|
||||
}
|
||||
public void ExecuteAnyCommand(object rfArg)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new TruPlasmaRFExecuteAnyCommandHandler(this, rfArg.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void MonitorRawCommand(bool isSelected, string command, string comandArgument)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(TruPlasmaRFRawCommandHandler) && ((TruPlasmaRFHandler)handler)._command == command);
|
||||
if (isSelected)
|
||||
{
|
||||
if (!existHandlers.Any())
|
||||
_lstMonitorHandler.AddFirst(new TruPlasmaRFRawCommandHandler(this, command, comandArgument));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (existHandlers.Any())
|
||||
{
|
||||
_lstMonitorHandler.Remove(existHandlers.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void MonitorRawCommand(bool isSelected, string command)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(TruPlasmaRFRawCommandHandler) && ((TruPlasmaRFHandler)handler)._command == command);
|
||||
if (isSelected)
|
||||
{
|
||||
if (!existHandlers.Any())
|
||||
_lstMonitorHandler.AddFirst(new TruPlasmaRFRawCommandHandler(this, command));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (existHandlers.Any())
|
||||
{
|
||||
_lstMonitorHandler.Remove(existHandlers.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public string Error { get; private set; }
|
||||
public bool InterfaceActived { get; private set; }
|
||||
public string AnyCommandResult { get; private set; }
|
||||
public int PiValue { get; private set; }
|
||||
public bool SetPiValueCompleted { get; private set; }
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Note Functions
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
|
||||
|
||||
|
||||
public void NoteError(string reason)
|
||||
{
|
||||
if (reason != null)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
}
|
||||
Error = reason;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void NoteRawCommandInfo(string command, string data)
|
||||
{
|
||||
var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
|
||||
if (curIOResponse != null)
|
||||
{
|
||||
IOResponseList.Remove(curIOResponse);
|
||||
}
|
||||
IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class DOB
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Index { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string SubIndex { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Data { get; set; }
|
||||
}
|
||||
|
||||
public class TruPlasmaRFArugment
|
||||
{
|
||||
public string Command { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public List<DOB> DOBList { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using MECF.Framework.Common.Utilities;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.TruPlasmaRF
|
||||
{
|
||||
|
||||
public class TruPlasmaRFMessage : BinaryMessage
|
||||
{
|
||||
public string Command { get; set; }
|
||||
public string ErrorCode { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
}
|
||||
|
||||
public class TruPlasmaRFConnection : SerialPortConnectionBase
|
||||
{
|
||||
|
||||
private static byte _start = 0xAA;
|
||||
//private static byte _address = 0x02;
|
||||
//private static byte _GS = 0x00;
|
||||
private static byte _stop = 0x55;
|
||||
private static byte _ack = 0x06;
|
||||
|
||||
|
||||
public TruPlasmaRFConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
|
||||
: base(portName, baudRate, dataBits, parity, stopBits, "", false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool SendMessage(byte[] message)
|
||||
{
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
protected override MessageBase ParseResponse(byte[] rawMessage)
|
||||
{
|
||||
|
||||
TruPlasmaRFMessage msg = new TruPlasmaRFMessage();
|
||||
msg.RawMessage = rawMessage;
|
||||
//msg.Data = rawMessage;
|
||||
|
||||
|
||||
if (rawMessage.Length == 1 && rawMessage[0] == _ack)
|
||||
{
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
return msg;
|
||||
}
|
||||
|
||||
if (rawMessage.Length < 11)
|
||||
{
|
||||
msg.IsFormatError = true;
|
||||
LOG.Error($"invalid response");
|
||||
return msg;
|
||||
}
|
||||
|
||||
if (rawMessage[0] != _start )
|
||||
{
|
||||
msg.IsFormatError = true;
|
||||
LOG.Error($"invalid start byte");
|
||||
return msg;
|
||||
}
|
||||
if (rawMessage.Last() != _stop)
|
||||
{
|
||||
msg.IsFormatError = true;
|
||||
LOG.Error($"invalid stop byte");
|
||||
return msg;
|
||||
}
|
||||
|
||||
var contentBuffer = rawMessage.Skip(3).Take(rawMessage.Length - 6).ToArray();
|
||||
|
||||
var checkSum = Crc16.Crc16Ccitt(contentBuffer);
|
||||
var recSum = rawMessage[rawMessage.Length - 3] * 256 + rawMessage[rawMessage.Length - 2];
|
||||
//if(recSum != checkSum)
|
||||
//{
|
||||
// LOG.Error($"check sum failed,");
|
||||
// msg.IsFormatError = true;
|
||||
// return msg;
|
||||
//}
|
||||
|
||||
if (rawMessage[8] != 0xFF)
|
||||
{
|
||||
msg.IsError = true;
|
||||
msg.ErrorCode = rawMessage[8].ToString("X2");
|
||||
}
|
||||
|
||||
//var msgArray = rawMessage.Select(bt => bt.ToString("X2")).ToArray();
|
||||
//string msgCommand = string.Join(",", msgArray, 4, 4);
|
||||
//msg.Command = msgCommand;
|
||||
|
||||
//if(rawMessage.Length > 11)
|
||||
// msg.Data = rawMessage.Skip(8).Take(rawMessage.Length - 11).ToArray();
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
msg.IsComplete = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
+268
@@ -0,0 +1,268 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
using MECF.Framework.Common.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.TruPlasmaRF
|
||||
{
|
||||
public abstract class TruPlasmaRFHandler : HandlerBase
|
||||
{
|
||||
public TruPlasmaRF Device { get; }
|
||||
|
||||
public string _command;
|
||||
protected string _parameter;
|
||||
|
||||
|
||||
protected TruPlasmaRFHandler(TruPlasmaRF device, string command, string parameter = null)
|
||||
: base(BuildMessage(ToByteArray(command), ToByteArray(parameter)))
|
||||
{
|
||||
Device = device;
|
||||
_command = command;
|
||||
_parameter = parameter;
|
||||
Name = command;
|
||||
}
|
||||
|
||||
protected TruPlasmaRFHandler(TruPlasmaRF device, string command, byte[] parameter)
|
||||
: base(BuildMessage(ToByteArray(command), parameter))
|
||||
{
|
||||
Device = device;
|
||||
_command = command;
|
||||
//_parameter = parameter;
|
||||
Name = command;
|
||||
}
|
||||
|
||||
private static byte _start = 0xAA;
|
||||
private static byte _address = 0x02;
|
||||
private static byte _GS = 0x00;
|
||||
private static byte _stop = 0x55;
|
||||
|
||||
private static byte[] BuildMessage(byte[] commandArray, byte[] argumentArray)
|
||||
{
|
||||
List<byte> buffer = new List<byte>();
|
||||
|
||||
buffer.Add(_start);
|
||||
buffer.Add(_address);
|
||||
int length = 1 + (commandArray != null ? commandArray.Length : 0) + (argumentArray != null ? argumentArray.Length : 0);
|
||||
buffer.Add((byte)length);
|
||||
buffer.Add(_GS);
|
||||
|
||||
if (commandArray != null && commandArray.Length > 0)
|
||||
{
|
||||
//skip ManualExecuteCommand
|
||||
if (!(commandArray.Length == 1 && commandArray[0] == 0))
|
||||
{
|
||||
buffer.AddRange(commandArray);
|
||||
}
|
||||
}
|
||||
if (argumentArray != null && argumentArray.Length > 0)
|
||||
{
|
||||
buffer.AddRange(argumentArray);
|
||||
}
|
||||
var contentBuffer = buffer.Skip(3).Take(buffer.Count - 3).ToArray();
|
||||
//var checkSum = Crc16.CRC16_CCITT(contentBuffer);
|
||||
var checkSum = Crc16.Crc16Ccitt(contentBuffer);
|
||||
|
||||
buffer.AddRange(BitConverter.GetBytes(checkSum));
|
||||
|
||||
buffer.Add(_stop);
|
||||
|
||||
return buffer.ToArray();
|
||||
}
|
||||
|
||||
protected static string BuildCommandParameter(string jsonParameter)
|
||||
{
|
||||
var truArgObject = JsonConvert.DeserializeObject<TruPlasmaRFArugment>(jsonParameter);
|
||||
|
||||
JObject jsonObject = JObject.Parse(jsonParameter);
|
||||
List<string> commandList = new List<string>();
|
||||
commandList.Add(truArgObject.Command);
|
||||
foreach(var dob in truArgObject.DOBList)
|
||||
{
|
||||
if(dob.Index != null)
|
||||
{
|
||||
commandList.Add(dob.Index.Substring(0,2));
|
||||
commandList.Add(dob.Index.Substring(2));
|
||||
}
|
||||
if (dob.SubIndex != null)
|
||||
{
|
||||
commandList.Add(dob.SubIndex);
|
||||
}
|
||||
if (dob.Status != null)
|
||||
{
|
||||
commandList.Add(dob.Status);
|
||||
}
|
||||
if (dob.Type != null)
|
||||
{
|
||||
commandList.Add(dob.Type);
|
||||
}
|
||||
if (dob.Data != null)
|
||||
{
|
||||
var charArray = dob.Data.ToArray();
|
||||
for (int i = 0; i < charArray.Length - 1; i += 2)
|
||||
{
|
||||
commandList.Add(charArray[i].ToString() + charArray[i + 1].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return string.Join(",", commandList.ToArray());
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
|
||||
{
|
||||
TruPlasmaRFMessage response = msg as TruPlasmaRFMessage;
|
||||
ResponseMessage = msg;
|
||||
|
||||
if (response.IsError)
|
||||
{
|
||||
Device.NoteError(response.ErrorCode);
|
||||
}
|
||||
else
|
||||
{
|
||||
Device.NoteError(null);
|
||||
}
|
||||
|
||||
if (response.IsAck)
|
||||
{
|
||||
SetState(EnumHandlerState.Acked);
|
||||
}
|
||||
|
||||
|
||||
if (this.IsAcked && response.IsComplete)
|
||||
{
|
||||
SetState(EnumHandlerState.Completed);
|
||||
transactionComplete = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
transactionComplete = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static byte[] ToByteArray(string parameter)
|
||||
{
|
||||
if (parameter == null)
|
||||
return new byte[] { };
|
||||
|
||||
return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
|
||||
}
|
||||
protected static byte[] IntToByteArray(int num)
|
||||
{
|
||||
byte[] bytes = BitConverter.GetBytes(num);
|
||||
return bytes;
|
||||
}
|
||||
protected int ByteArrayToInt(byte[] bytes)
|
||||
{
|
||||
int temp = BitConverter.ToInt32(bytes, 0);
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class TruPlasmaRFExecuteAnyCommandHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFExecuteAnyCommandHandler(TruPlasmaRF device, string jsonParameter)
|
||||
: base(device, BuildCommandParameter(jsonParameter))
|
||||
{
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NoteAnyCommandResult(string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class TruPlasmaRFRawCommandHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFRawCommandHandler(TruPlasmaRF device, string command, string parameter = null)
|
||||
: base(device, command, parameter)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if(base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class TruPlasmaRFGetControlHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFGetControlHandler(TruPlasmaRF device)
|
||||
: base(device, "05,01,00,00", "FF")
|
||||
{
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NoteInterfaceActived(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class TruPlasmaRFReleaseControlHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFReleaseControlHandler(TruPlasmaRF device)
|
||||
: base(device, "05,02,00,00", "FF")
|
||||
{
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NoteInterfaceActived(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class TruPlasmaRFPreSetPiValueHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFPreSetPiValueHandler(TruPlasmaRF device, int piValue)
|
||||
: base(device, "02,06,00,01", IntToByteArray(piValue))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NoteSetPiValueCompleted(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class TruPlasmaRFReadPiValueHandler : TruPlasmaRFHandler
|
||||
{
|
||||
public TruPlasmaRFReadPiValueHandler(TruPlasmaRF device)
|
||||
: base(device, "01,12,00,01", "FF")
|
||||
{
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as TruPlasmaRFMessage;
|
||||
Device.NotePiValue(ByteArrayToInt(result.RawMessage.Skip(10).Take(4).ToArray()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user