123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Log;
- using MECF.Framework.Common.Communications;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.SUNWAY
- {
- public abstract class SPaceTRanRobotHandler : HandlerBase
- {
- public SPaceTRanRobot Device { get; }
- protected string _commandType;
- protected string _command;
- protected string _parameter;
- public SPaceTRanRobotHandler(SPaceTRanRobot device, string command, string parameter = null)
- : base(BuildMessage(command, parameter, device))
- {
- Device = device;
- _command = command;
- _parameter = parameter;
- Name = command;
- AckTimeout = TimeSpan.FromSeconds(30);
- CompleteTimeout = TimeSpan.FromSeconds(60);
- }
- protected static string BuildMessage(string command, string parameter, SPaceTRanRobot rbt)
- {
- return $"{command}{parameter ?? ""}" + "\r";
- }
- public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
- {
- SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
- ResponseMessage = msg;
- if (msg.IsError)
- {
- Device.NoteError(response.RawMessage);
- transactionComplete = true;
- return true;
- }
- if (msg.IsAck)
- {
- SetState(EnumHandlerState.Acked);
- }
- if (msg.IsComplete)
- {
- SetState(EnumHandlerState.Completed);
- Device.OnActionDone(null);
- transactionComplete = true;
- return true;
- }
- transactionComplete = false;
- return false;
- }
- }
- public class SPaceTRanRobotSetHandler : SPaceTRanRobotHandler
- {
- public SPaceTRanRobotSetHandler(SPaceTRanRobot robot, string command, string parameter = null)
- : base(robot, command, parameter)
- {
- string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
- LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
- }
- }
- public class SPaceTRanRobotReadHandler : SPaceTRanRobotHandler
- {
- public SPaceTRanRobotReadHandler(SPaceTRanRobot robot, string command, string parameter = null)
- : base(robot, command, parameter)
- {
- string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
- LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII.");
- }
- public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
- {
- SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
- ResponseMessage = msg;
- if (msg.IsError)
- {
- Device.NoteError(response.RawMessage);
- transactionComplete = true;
- return true;
- }
- if (msg.IsAck)
- {
- SetState(EnumHandlerState.Acked);
- }
- Device.ParseReadData(_command, response.RawMessage);
- if (msg.IsComplete)
- {
- SetState(EnumHandlerState.Completed);
- Device.OnActionDone(null);
- transactionComplete = true;
- return true;
- }
- transactionComplete = false;
- return false;
- }
- }
- public class SPaceTRanRobotMotionHandler : SPaceTRanRobotHandler
- {
- public SPaceTRanRobotMotionHandler(SPaceTRanRobot robot, string command, string parameter = null)
- : base(robot, command, parameter)
- {
- string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
- LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
- }
- public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
- {
- SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
- ResponseMessage = msg;
- LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
- if (msg.IsError)
- {
- Device.NoteError(response.RawMessage);
- transactionComplete = true;
- return true;
- }
- if (msg.IsAck)
- {
- SetState(EnumHandlerState.Acked);
- }
- if (msg.IsComplete)
- {
- SetState(EnumHandlerState.Completed);
- Device.OnActionDone(null);
- transactionComplete = true;
- return true;
- }
- transactionComplete = false;
- return false;
- }
- }
- }
|