using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace XImaging.Automation.Library.HxDriverLib.Messaging
{
public class BiosenDriverMessage : IMessage
{
#region 消息类型定义
///
/// 消息类型0: 心跳包
///
public static int MESSAGE_TYPE_HEARTBEAT { get; } = 0;
///
/// 消息类型1:方法消息(延迟返回消息)
///
public static int MESSAGE_TYPE_TASK { get; } = 1;
///
/// 消息类型2:查询方法消息(即时返回消息)
///
public static int MESSAGE_TYPE_QUERY { get; } = 2;
///
/// 消息类型3:错误处理消息(无返回消息)
///
public static int MESSAGE_TYPE_TROUBLESHOOT { get; } = 3;
///
/// 消息类型4:确认消息
///
public static int MESSAGE_TYPE_ACK { get; } = 4;
///
/// 消息类型5:完成消息
///
public static int MESSAGE_TYPE_FINISH { get; } = 5;
#endregion
#region 方法状态定义
public static int METHOD_STATUS_NULL { get; } = -1;
///
/// 方法状态0:准备执行(指令队列中)
///
public static int METHOD_STATUS_SCHEDULED { get; } = 0;
///
/// 方法状态1:正在执行
///
public static int METHOD_STATUS_INPROGRES { get; } = 1;
///
/// 方法状态2:执行完成且成功
///
public static int METHOD_STATUS_COMPLETED { get; } = 2;
///
/// 方法状态3:执行完成且失败
///
public static int METHOD_STATUS_FAILED { get; } = 3;
///
/// 方法状态4:该方法在指令队列中被取消
///
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; } }
///
/// 消息ID,唯一
///
public string MessageID { get { return m_strMessageID; } }
///
/// 消息类型,见定义
///
public int MessageType { get { return m_iMessageType; } }
///
/// 方法名称
///
public string Method { get { return m_strMethod; } }
///
/// 设备ID
///
public string EquipmentID { get { return m_strEquipmentID; } }
///
/// 工作流ID
///
public string WorkflowID { get { return m_strWorkflowID; } }
///
///
///
public string ExperimentID { get { return m_strExperimentID; } }
///
/// 消息生成时间
///
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
///
/// 原始消息
///
public JObject OriginJSON { get { return m_jsonOriginMessage; } }
///
/// 方法需要的参数
///
public JObject Parameters { get { return m_jsonParameters; } }
///
///
///
public int Troubleshoot { get { return m_iTroubleshoot; } }
///
/// 方法描述文字
///
public string Description { get { return m_strDescription; } }
public int EstimateTime { get { return m_iEstimateTime; } }
public int Timeout { get { return m_iTimeout; } }
///
/// 消息逾期时间
///
public long ExpiredTime { get { return m_lExpiredTime; } }
///
/// 构造类1
///
///
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("endpoint");
this.m_strMessageID = obj["message_id"].ToString();
this.m_iMessageType = obj.Value("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("estimate_time");
this.m_iTimeout = obj.Value("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("troubleshoot");
else
this.m_iTroubleshoot = 0;
this.m_lExpiredTime = obj.Value("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
///
/// 方法需要的参数
///
public JObject Data { get { return m_jsonData; } }
///
///
///
public int MethodStatus { get { return m_iMethodStatus; } }
///
/// 错误码
///
public string ErrorCode { get { return m_strErrorCode; } }
///
/// 错误描述
///
public string ErrorText { get { return m_strErrorText; } }
///
/// 错误处理方案
///
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("endpoint");
else
this.m_strEndpoint = 0;
this.m_strMessageID = obj["message_id"].ToString();
this.m_iMessageType = obj.Value("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("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("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();
}
}
}