using Apache.NMS;
|
using Apache.NMS.ActiveMQ;
|
using Rhea.Common;
|
using Tiger.Model;
|
using Tiger.Model.SeaStone.Shelf;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.IO;
|
using Newtonsoft.Json;
|
using Tiger.Business.WMS.Seastone;
|
using Tiger.IBusiness;
|
using Tiger.Model.Entitys.MES.Position;
|
|
namespace Tiger.Business.WMS.Transaction
|
{
|
/// <summary>
|
/// WMS事务基类
|
/// </summary>
|
public abstract class WMSTransactionBase : TransactionBase, IWMSTransaction
|
{
|
/// <summary>
|
/// WMS事务基类
|
/// </summary>
|
public WMSTransactionBase()
|
{
|
//MQTTThread = new Thread(new ThreadStart(MQTT));
|
//MQTTThread.Start();
|
//Logger.Console.Info($"Start Transaction MQTT Thread(ID: {TransID}]");
|
}
|
|
/// <summary>
|
/// 关闭事务
|
/// </summary>
|
/// <param name="needSaveHistoryLog"></param>
|
/// <returns></returns>
|
public override bool Close(bool needSaveHistoryLog = false)
|
{
|
return base.Close(needSaveHistoryLog);
|
}
|
|
public override void Dispose()
|
{
|
try
|
{
|
//MQTTThread?.Abort();
|
//删除使用的MQTT Topic
|
//MQTTHelper.DeleteTopic(TransID);
|
Logger.Console.Info($"Dispose Transaction(ID: {TransID}]");
|
}
|
catch (System.Exception ex)
|
{
|
Logger.Console.Fatal(ex, $"Dispose Transaction Exception(ID: {TransID}]");
|
}
|
base.Dispose();
|
}
|
|
#region WMS业务
|
/// <summary>
|
/// 执行中条码
|
/// </summary>
|
public string ProcessingSn { get; set; }
|
/// <summary>
|
/// 执行中单据明细
|
/// </summary>
|
public string ProcessingOrderDetail { get; set; }
|
/// <summary>
|
/// 重置事务操作
|
/// </summary>
|
/// <returns></returns>
|
public ApiAction Reset()
|
{
|
var action = new ApiAction();
|
|
ResetTrans();
|
action.IsSuccessed = true;
|
//action.LocaleMsg = new($"操作已重置,请重新扫描");
|
action.LocaleMsg = new("WMS.Transaction.Reset");
|
|
return action;
|
}
|
/// <summary>
|
/// 重置事务数据,有需要则重写此方法
|
/// </summary>
|
public virtual void ResetTrans()
|
{
|
ProcessingSn = null;
|
}
|
#endregion
|
|
#region MQTT
|
/// <summary>
|
/// 设置当前条码的MQTT信息
|
/// </summary>
|
public ApiAction<ScanOutput> SetOutPutMqttMsg(ApiAction<ScanOutput> action, string locale = null)
|
{
|
MQTT.Message msg = new()
|
{
|
IsSuccessed = action.IsSuccessed,
|
Content = Biz.T(action.LocaleMsg, locale),
|
};
|
switch (action.Status)
|
{
|
case ApiAction.StatusCodes.Success:
|
msg.Voice = MQTT.Voice.Success;
|
msg.Color = "#FF1E90FF";
|
break;
|
case ApiAction.StatusCodes.Warning:
|
msg.Voice = MQTT.Voice.Warning;
|
msg.Color = "#FFB8860B";
|
break;
|
case ApiAction.StatusCodes.Error:
|
case ApiAction.StatusCodes.Failed:
|
msg.Voice = MQTT.Voice.Fail;
|
msg.Color = "#FFFF0000";
|
break;
|
case ApiAction.StatusCodes.Exception:
|
msg.Voice = MQTT.Voice.Fail;
|
msg.Color = "#FF8B0000";
|
break;
|
case ApiAction.StatusCodes.Normal:
|
case ApiAction.StatusCodes.NeedConfrim:
|
case ApiAction.StatusCodes.Confrimed:
|
default:
|
msg.Voice = MQTT.Voice.Silent;
|
msg.Color = "#FF000000";
|
break;
|
}
|
if (action.IsSuccessed)
|
{
|
msg.Voice = MQTT.Voice.Success;
|
msg.Color = "#FF228B22";
|
}
|
else if (!action.IsSuccessed)
|
{
|
msg.Voice = MQTT.Voice.Fail;
|
msg.Color = "#FFFF0000";
|
}
|
else
|
{
|
msg.Voice = MQTT.Voice.Silent;
|
msg.Color = "#FF000000";
|
}
|
action.Data.MqttMsg = msg;
|
return action;
|
}
|
|
private Thread MQTTThread;
|
private void Mqtt()
|
{
|
try
|
{
|
//Create the Connection factory
|
IConnectionFactory factory = new ConnectionFactory(ApiConfig.Configuration["MQTTService:IPAddress"]);
|
|
//Create the connection
|
using (IConnection connection = factory.CreateConnection())
|
{
|
connection.ClientId = $"{Environment.MachineName}-Receive-{TransID}";
|
connection.Start();
|
|
//Create the Session
|
using (ISession session = connection.CreateSession())
|
{
|
//Create the Consumer
|
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(TransID));
|
consumer.Listener += new MessageListener(consumer_Listener);
|
while (!IsFinished)
|
{
|
|
}
|
}
|
connection.Stop();
|
connection.Close();
|
}
|
}
|
catch (System.Exception e)
|
{
|
Console.WriteLine(e.Message);
|
}
|
}
|
private string ReceiveMsg;
|
private void consumer_Listener(IMessage message)
|
{
|
try
|
{
|
ITextMessage msg = (ITextMessage)message;
|
Console.WriteLine("MQTT Receive: " + msg.Text);
|
}
|
catch (System.Exception e)
|
{
|
Console.WriteLine(e.Message);
|
}
|
}
|
|
#endregion
|
}
|
}
|