using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using System;
|
|
namespace XImaging.Automation.Library.HxDriverLib.Messaging
|
{
|
public class BiosenDriverMessage : IMessage
|
{
|
#region 消息类型定义
|
/// <summary>
|
/// 消息类型0: 心跳包
|
/// </summary>
|
public static int MESSAGE_TYPE_HEARTBEAT { get; } = 0;
|
/// <summary>
|
/// 消息类型1:方法消息(延迟返回消息)
|
/// </summary>
|
public static int MESSAGE_TYPE_TASK { get; } = 1;
|
/// <summary>
|
/// 消息类型2:查询方法消息(即时返回消息)
|
/// </summary>
|
public static int MESSAGE_TYPE_QUERY { get; } = 2;
|
/// <summary>
|
/// 消息类型3:错误处理消息(无返回消息)
|
/// </summary>
|
public static int MESSAGE_TYPE_TROUBLESHOOT { get; } = 3;
|
/// <summary>
|
/// 消息类型4:确认消息
|
/// </summary>
|
public static int MESSAGE_TYPE_ACK { get; } = 4;
|
/// <summary>
|
/// 消息类型5:完成消息
|
/// </summary>
|
public static int MESSAGE_TYPE_FINISH { get; } = 5;
|
#endregion
|
|
#region 方法状态定义
|
public static int METHOD_STATUS_NULL { get; } = -1;
|
/// <summary>
|
/// 方法状态0:准备执行(指令队列中)
|
/// </summary>
|
public static int METHOD_STATUS_SCHEDULED { get; } = 0;
|
/// <summary>
|
/// 方法状态1:正在执行
|
/// </summary>
|
public static int METHOD_STATUS_INPROGRES { get; } = 1;
|
/// <summary>
|
/// 方法状态2:执行完成且成功
|
/// </summary>
|
public static int METHOD_STATUS_COMPLETED { get; } = 2;
|
/// <summary>
|
/// 方法状态3:执行完成且失败
|
/// </summary>
|
public static int METHOD_STATUS_FAILED { get; } = 3;
|
/// <summary>
|
/// 方法状态4:该方法在指令队列中被取消
|
/// </summary>
|
public static int METHOD_STATUS_CANCELED { get; } = 4;
|
#endregion
|
|
protected string m_strGUID;
|
protected int m_strEndpoint;
|
protected JObject m_jsonOriginMessage;
|
protected string m_strMessageID;
|
protected int m_iMessageType;
|
protected string m_strMethod;
|
protected string m_strEquipmentID;
|
protected string m_strWorkflowID;
|
protected string m_strExperimentID;
|
protected JObject m_jsonParameters;
|
protected int m_iTroubleshoot;
|
protected string m_strTimestamp;
|
|
public string GUID { get { return m_strGUID; } }
|
|
public int LocalEndpoint { get { return m_strEndpoint; } }
|
|
/// <summary>
|
/// 消息ID,唯一
|
/// </summary>
|
public string MessageID { get { return m_strMessageID; } }
|
/// <summary>
|
/// 消息类型,见定义
|
/// </summary>
|
public int MessageType { get { return m_iMessageType; } }
|
/// <summary>
|
/// 方法名称
|
/// </summary>
|
public string Method { get { return m_strMethod; } }
|
/// <summary>
|
/// 设备ID
|
/// </summary>
|
public string EquipmentID { get { return m_strEquipmentID; } }
|
/// <summary>
|
/// 工作流ID
|
/// </summary>
|
public string WorkflowID { get { return m_strWorkflowID; } }
|
/// <summary>
|
///
|
/// </summary>
|
public string ExperimentID { get { return m_strExperimentID; } }
|
/// <summary>
|
/// 消息生成时间
|
/// </summary>
|
public string Timestamp { get { return m_strTimestamp; } }
|
}
|
|
public class ReqMessage : BiosenDriverMessage
|
{
|
#region 私有成员
|
|
protected string m_strDescription;
|
protected int m_iEstimateTime;
|
protected int m_iTimeout;
|
protected long m_lExpiredTime;
|
|
#endregion
|
/// <summary>
|
/// 原始消息
|
/// </summary>
|
public JObject OriginJSON { get { return m_jsonOriginMessage; } }
|
|
/// <summary>
|
/// 方法需要的参数
|
/// </summary>
|
public JObject Parameters { get { return m_jsonParameters; } }
|
/// <summary>
|
///
|
/// </summary>
|
public int Troubleshoot { get { return m_iTroubleshoot; } }
|
/// <summary>
|
/// 方法描述文字
|
/// </summary>
|
public string Description { get { return m_strDescription; } }
|
|
public int EstimateTime { get { return m_iEstimateTime; } }
|
|
public int Timeout { get { return m_iTimeout; } }
|
/// <summary>
|
/// 消息逾期时间
|
/// </summary>
|
public long ExpiredTime { get { return m_lExpiredTime; } }
|
|
/// <summary>
|
/// 构造类1
|
/// </summary>
|
/// <param name="message"></param>
|
public ReqMessage(string message)
|
{
|
JObject obj = (JObject)JsonConvert.DeserializeObject(message);
|
LogConstant.logger.Print("Parse message " + message + " to HxMessage.");
|
|
this.m_jsonOriginMessage = obj;
|
this.m_strGUID = obj["guid"].ToString();
|
this.m_strEndpoint = obj.Value<int>("endpoint");
|
this.m_strMessageID = obj["message_id"].ToString();
|
this.m_iMessageType = obj.Value<int>("message_type");
|
this.m_strEquipmentID = obj["equipment_id"].ToString();
|
|
if (this.MessageType != MESSAGE_TYPE_HEARTBEAT)
|
{
|
this.m_strWorkflowID = obj["workflow_id"].ToString();
|
this.m_strExperimentID = obj["experiment_id"].ToString();
|
this.m_strMethod = obj["method"].ToString();
|
this.m_strDescription = obj["description"].ToString();
|
}
|
|
if (this.MessageType == MESSAGE_TYPE_TASK || this.MessageType == MESSAGE_TYPE_QUERY)
|
{
|
this.m_jsonParameters = (JObject)obj["parameters"];
|
this.m_iEstimateTime = obj.Value<int>("estimate_time");
|
this.m_iTimeout = obj.Value<int>("timeout");
|
}
|
else
|
{
|
this.m_jsonParameters = null;
|
this.m_iEstimateTime = 0;
|
this.m_iTimeout = 0;
|
}
|
|
if (this.MessageType == MESSAGE_TYPE_TROUBLESHOOT)
|
this.m_iTroubleshoot = obj.Value<int>("troubleshoot");
|
else
|
this.m_iTroubleshoot = 0;
|
|
this.m_lExpiredTime = obj.Value<long>("expired");
|
this.m_strTimestamp = obj["timestamp"].ToString();
|
}
|
}
|
|
public class ResMessage : BiosenDriverMessage
|
{
|
#region 私有成员
|
private JObject m_jsonData;
|
private int m_iMethodStatus;
|
private string m_strErrorCode;
|
private string m_strErrorText;
|
|
#endregion
|
/// <summary>
|
/// 方法需要的参数
|
/// </summary>
|
public JObject Data { get { return m_jsonData; } }
|
/// <summary>
|
///
|
/// </summary>
|
public int MethodStatus { get { return m_iMethodStatus; } }
|
/// <summary>
|
/// 错误码
|
/// </summary>
|
public string ErrorCode { get { return m_strErrorCode; } }
|
/// <summary>
|
/// 错误描述
|
/// </summary>
|
public string ErrorText { get { return m_strErrorText; } }
|
/// <summary>
|
/// 错误处理方案
|
/// </summary>
|
public int Troubleshoot { get { return m_iTroubleshoot; } }
|
|
public ResMessage(string message)
|
{
|
JObject obj = (JObject)JsonConvert.DeserializeObject(message);
|
LogConstant.logger.Print("Parse message " + message + " to HxMessage.");
|
|
this.m_strGUID = obj["guid"].ToString();
|
|
if (obj.ContainsKey("endpoint"))
|
this.m_strEndpoint = obj.Value<int>("endpoint");
|
else
|
this.m_strEndpoint = 0;
|
|
this.m_strMessageID = obj["message_id"].ToString();
|
this.m_iMessageType = obj.Value<int>("message_type");
|
this.m_strEquipmentID = obj["equipment_id"].ToString();
|
|
if (this.MessageType != MESSAGE_TYPE_HEARTBEAT)
|
{
|
this.m_strWorkflowID = obj["workflow_id"].ToString();
|
this.m_strExperimentID = obj["experiment_id"].ToString();
|
this.m_strMethod = obj["method"].ToString();
|
}
|
|
if (this.MessageType == MESSAGE_TYPE_FINISH)
|
{
|
this.m_jsonData = (JObject)obj["data"];
|
this.m_iMethodStatus = obj.Value<int>("method_status");
|
|
if (this.MethodStatus == METHOD_STATUS_COMPLETED)
|
{
|
this.m_strErrorCode = "";
|
this.m_strErrorText = "";
|
this.m_iTroubleshoot = 0;
|
}
|
else
|
{
|
this.m_strErrorCode = obj["error_code"].ToString();
|
this.m_strErrorText = obj["error_text"].ToString();
|
this.m_iTroubleshoot = obj.Value<int>("troubleshoot");
|
}
|
}
|
else
|
{
|
this.m_jsonData = null;
|
this.m_iMethodStatus = METHOD_STATUS_NULL;
|
this.m_strErrorCode = null;
|
this.m_strErrorText = null;
|
this.m_iTroubleshoot = 0;
|
}
|
|
this.m_strTimestamp = obj["timestamp"].ToString();
|
}
|
}
|
|
public class PostMessage
|
{
|
#region 私有成员
|
private string m_strMessageType;
|
private string m_strMessageID;
|
private string m_strEquipmentID;
|
private string m_strWorkflowID;
|
private string m_strExperimentID;
|
private string m_strMethodMessageID;
|
private string m_strMethod;
|
// private JObject m_jsonData;
|
private string m_strTimestamp;
|
#endregion
|
|
public string MessageType { get { return this.m_strMessageType; } }
|
public string MessageID { get { return this.m_strMessageID; } }
|
public string EquipmentID { get { return this.m_strEquipmentID; } }
|
public string WorkflowID { get { return this.m_strWorkflowID; } }
|
|
public string ExperimentID { get { return this.m_strExperimentID; } }
|
public string MethodMessageID { get { return this.m_strMethodMessageID; } }
|
public string Method { get { return this.m_strMethod; } }
|
// public JObject Data { get { return this.m_jsonData; } }
|
public string Timestamp { get { return this.m_strTimestamp; } }
|
|
public PostMessage(ReqMessage rMessage)
|
{
|
this.m_strMessageID = Guid.NewGuid().ToString();
|
this.m_strEquipmentID = rMessage.EquipmentID;
|
this.m_strWorkflowID = rMessage.WorkflowID;
|
this.m_strExperimentID = rMessage.ExperimentID;
|
this.m_strMethodMessageID = rMessage.MessageID;
|
this.m_strMethod = rMessage.Method;
|
this.m_strTimestamp = DateTime.Now.ToString();
|
}
|
|
public virtual JObject ToJObject()
|
{
|
return JObject.FromObject(new { });
|
}
|
}
|
|
public class DataObjectMessage : PostMessage
|
{
|
private string m_strMessageType;
|
private JObject m_jsonData;
|
|
public new string MessageType { get { return this.m_strMessageType; } }
|
|
public JObject Data { get { return this.m_jsonData; } }
|
|
public DataObjectMessage(ReqMessage rMessage, JObject obj) : base(rMessage)
|
{
|
this.m_strMessageType = "data";
|
this.m_jsonData = obj;
|
}
|
|
public override JObject ToJObject()
|
{
|
JObject obj = JObject.FromObject(
|
new
|
{
|
message_type = MessageType,
|
message_id = MessageID,
|
method = Method,
|
equipment_id = EquipmentID,
|
workflow_id = WorkflowID,
|
experiment_id = ExperimentID,
|
method_message_id = MethodMessageID,
|
data = Data,
|
timestamp = DateTime.Now
|
}
|
);
|
return obj;
|
}
|
|
public override string ToString()
|
{
|
return this.ToJObject().ToString();
|
}
|
}
|
|
|
public class FileObjectMessage : PostMessage
|
{
|
private string m_strMessageType;
|
private string m_strFilepath;
|
|
public new string MessageType { get { return this.m_strMessageType; } }
|
|
public string FilePath { get { return this.m_strFilepath; } }
|
|
public FileObjectMessage(ReqMessage rMessage, string fileFullPath) : base(rMessage)
|
{
|
this.m_strMessageType = "file";
|
this.m_strFilepath = fileFullPath;
|
}
|
|
public override JObject ToJObject()
|
{
|
JObject obj = JObject.FromObject(
|
new
|
{
|
message_type = MessageType,
|
message_id = MessageID,
|
method = Method,
|
equipment_id = EquipmentID,
|
workflow_id = WorkflowID,
|
experiment_id = ExperimentID,
|
method_message_id = MethodMessageID,
|
file = m_strFilepath,
|
timestamp = DateTime.Now
|
}
|
);
|
return obj;
|
}
|
|
public override string ToString()
|
{
|
return this.ToJObject().ToString();
|
}
|
}
|
|
public class ErrorObjectMessage : PostMessage
|
{
|
private string m_strMessageType;
|
private JObject m_jsonError;
|
public new string MessageType { get { return this.m_strMessageType; } }
|
public JObject Data { get { return this.m_jsonError; } }
|
|
public ErrorObjectMessage(ReqMessage rMessage, JObject obj) : base(rMessage)
|
{
|
this.m_strMessageType = "error";
|
this.m_jsonError = obj;
|
}
|
|
public override JObject ToJObject()
|
{
|
return this.m_jsonError;
|
}
|
|
public override string ToString()
|
{
|
return this.ToJObject().ToString();
|
}
|
}
|
|
}
|