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
{
///
/// WMS事务基类
///
public abstract class WMSTransactionBase : TransactionBase, IWMSTransaction
{
///
/// WMS事务基类
///
public WMSTransactionBase()
{
//MQTTThread = new Thread(new ThreadStart(MQTT));
//MQTTThread.Start();
//Logger.Console.Info($"Start Transaction MQTT Thread(ID: {TransID}]");
}
///
/// 关闭事务
///
///
///
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业务
///
/// 执行中条码
///
public string ProcessingSn { get; set; }
///
/// 执行中单据明细
///
public string ProcessingOrderDetail { get; set; }
///
/// 重置事务操作
///
///
public ApiAction Reset()
{
var action = new ApiAction();
ResetTrans();
action.IsSuccessed = true;
//action.LocaleMsg = new($"操作已重置,请重新扫描");
action.LocaleMsg = new("WMS.Transaction.Reset");
return action;
}
///
/// 重置事务数据,有需要则重写此方法
///
public virtual void ResetTrans()
{
ProcessingSn = null;
}
#endregion
#region MQTT
///
/// 设置当前条码的MQTT信息
///
public ApiAction SetOutPutMqttMsg(ApiAction 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
}
}