Commit 95285e79 by zonevg

mqtt 货架展示屏

parent c1711ef1
......@@ -63,6 +63,7 @@ namespace JmpCommon
MQTTPassword = "junmp123";
IsPushShelfPrint = false;
PushShelfPrintType = 0; //默认com
PushShelfPrintCom = "";
}
......@@ -102,6 +103,7 @@ namespace JmpCommon
public string MQTTPassword { get; set; }
public bool IsPushShelfPrint { get; set; }
public int PushShelfPrintType { get; set; }
public string PushShelfPrintCom { get; set; }
}
}
......@@ -175,29 +175,40 @@ namespace JmpServiceMgr
tb_mqttPort.Text = MyCache.SysConfig.MQTTPort.ToString();
chk_pushShelfPrint.Checked = MyCache.SysConfig.IsPushShelfPrint;
cbb_shelfPrintType.SelectedIndex = MyCache.SysConfig.PushShelfPrintType;
if (chk_pushShelfPrint.Checked)
{
cbb_shelfPrintCom.Visible = true;
cbb_shelfPrintCom.Items.Clear();
cbb_shelfPrintCom.Items.AddRange(SerialPort.GetPortNames());
if (cbb_shelfPrintCom.Items.Count > 0)
if (cbb_shelfPrintType.SelectedIndex == 0)
{
if (cbb_shelfPrintCom.Items.Contains(MyCache.SysConfig.PushShelfPrintCom))
{
cbb_shelfPrintCom.SelectedIndex =
cbb_shelfPrintCom.Items.IndexOf(MyCache.SysConfig.PushShelfPrintCom);
}
else
cbb_shelfPrintCom.Items.Clear();
cbb_shelfPrintCom.Items.AddRange(SerialPort.GetPortNames());
if (cbb_shelfPrintCom.Items.Count > 0)
{
cbb_shelfPrintCom.SelectedIndex = cbb_shelfPrintCom.Items.Count - 1;
if (cbb_shelfPrintCom.Items.Contains(MyCache.SysConfig.PushShelfPrintCom))
{
cbb_shelfPrintCom.SelectedIndex =
cbb_shelfPrintCom.Items.IndexOf(MyCache.SysConfig.PushShelfPrintCom);
}
else
{
cbb_shelfPrintCom.SelectedIndex = cbb_shelfPrintCom.Items.Count - 1;
}
}
}
else
{
lb_shelfPrintCom.Enabled = false;
cbb_shelfPrintCom.Enabled = false;
}
}
else
{
cbb_shelfPrintCom.Visible = false;
lb_shelfPrintType.Enabled = false;
cbb_shelfPrintType.Enabled = false;
lb_shelfPrintCom.Enabled = false;
cbb_shelfPrintCom.Enabled = false;
}
}
private void btnColse_Click(object sender, EventArgs e)
{
......@@ -318,8 +329,9 @@ namespace JmpServiceMgr
MyCache.SysConfig.MQTTPort = Convert.ToInt32(tb_mqttPort.Text);
MyCache.SysConfig.IsPushShelfPrint = chk_pushShelfPrint.Checked;
MyCache.SysConfig.PushShelfPrintCom = cbb_shelfPrintCom.Text;
if (chk_pushShelfPrint.Checked)
MyCache.SysConfig.PushShelfPrintType = cbb_shelfPrintType.SelectedIndex;
if (chk_pushShelfPrint.Checked && cbb_shelfPrintType.SelectedIndex == 0)
{
MyCache.SysConfig.PushShelfPrintCom = cbb_shelfPrintCom.Text;
}
......@@ -365,28 +377,49 @@ namespace JmpServiceMgr
{
if (box.Checked)
{
cbb_shelfPrintCom.Visible = true;
cbb_shelfPrintCom.Items.Clear();
cbb_shelfPrintCom.Items.AddRange(SerialPort.GetPortNames());
if (cbb_shelfPrintCom.Items.Count > 0)
{
if (cbb_shelfPrintCom.Items.Contains(MyCache.SysConfig.PushShelfPrintCom))
{
cbb_shelfPrintCom.SelectedIndex =
cbb_shelfPrintCom.Items.IndexOf(MyCache.SysConfig.PushShelfPrintCom);
}
else
{
cbb_shelfPrintCom.SelectedIndex = cbb_shelfPrintCom.Items.Count - 1;
}
}
lb_shelfPrintType.Enabled = true;
cbb_shelfPrintType.Enabled = true;
cbb_shelfPrintType_SelectedIndexChanged(null, null);
}
else
{
cbb_shelfPrintCom.Visible = false;
lb_shelfPrintType.Enabled = false;
cbb_shelfPrintType.Enabled = false;
lb_shelfPrintCom.Enabled = false;
cbb_shelfPrintCom.Enabled = false;
}
}
}
private void cbb_shelfPrintType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbb_shelfPrintType.SelectedIndex == 0)
{
lb_shelfPrintCom.Enabled = true;
cbb_shelfPrintCom.Enabled = true;
cbb_shelfPrintCom.Items.Clear();
cbb_shelfPrintCom.Items.AddRange(SerialPort.GetPortNames());
if (cbb_shelfPrintCom.Items.Count > 0)
{
if (cbb_shelfPrintCom.Items.Contains(MyCache.SysConfig.PushShelfPrintCom))
{
cbb_shelfPrintCom.SelectedIndex =
cbb_shelfPrintCom.Items.IndexOf(MyCache.SysConfig.PushShelfPrintCom);
}
else
{
cbb_shelfPrintCom.SelectedIndex = cbb_shelfPrintCom.Items.Count - 1;
}
}
}
else
{
lb_shelfPrintCom.Enabled = false;
cbb_shelfPrintCom.Enabled = false;
}
}
}
}
......@@ -56,6 +56,7 @@ namespace JmpServiceMgr
public List<RFIDReaderHelper> ReaderHelperList;
public List<HikFaceHelper> HikFaceHelperList;
public MQTTHelper MqttHelper;
public MQTTServer MqttServer;
public SerialPortHelper SerialPortHelper;
private CancellationTokenSource CTol;
......@@ -293,9 +294,17 @@ namespace JmpServiceMgr
MqttHelper.InitialMQTT();
if (MyCache.SysConfig.IsPushShelfPrint)
{
//启动
SerialPortHelper = new SerialPortHelper(this, CTol);
SerialPortHelper.InitialSerialPort();
if (MyCache.SysConfig.PushShelfPrintType == 0)
{
//com
SerialPortHelper = new SerialPortHelper(this, CTol);
SerialPortHelper.InitialSerialPort();
}
else
{
//启动mqtt服务端推送出入库信息
MqttServer = new MQTTServer();
}
}
HikFaceHelperList = new List<HikFaceHelper>();
......
using JmpCommon;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using MQTTnet.Client.Receiving;
using MQTTnet.Protocol;
using System;
using System.Text;
using System.Threading;
using Newtonsoft.Json.Linq;
using MQTTnet.Server;
using System.Configuration;
namespace JmpServiceMgr.Helper
{
public class MQTTServer : IDisposable
{
public MqttServer mqttServer { get; set; }
public MQTTServer()
{
StartMqttServer();
}
public async void StartMqttServer()
{
try
{
if (mqttServer == null)
{
var optionsBuilder = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.WithDefaultEndpointPort(int.Parse(ConfigurationManager.AppSettings["MQTTPort"]))
.WithConnectionValidator(
c =>
{
var currentUser = ConfigurationManager.AppSettings["UserName"];
var currentPWD = ConfigurationManager.AppSettings["Password"];
if (string.IsNullOrEmpty(currentUser) || string.IsNullOrEmpty(currentPWD))
{
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}
if (c.Username != currentUser)
{
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}
if (c.Password != currentPWD)
{
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}
c.ReasonCode = MqttConnectReasonCode.Success;
})
.WithSubscriptionInterceptor(
c =>
{
c.AcceptSubscription = true;
})
.WithApplicationMessageInterceptor(
c =>
{
c.AcceptPublish = true;
});
mqttServer = new MqttFactory().CreateMqttServer() as MqttServer;
mqttServer.StartedHandler = new MqttServerStartedHandlerDelegate(OnMqttServerStarted);
mqttServer.StoppedHandler = new MqttServerStoppedHandlerDelegate(OnMqttServerStopped);
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(OnMqttServerClientConnected);
mqttServer.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(OnMqttServerClientDisconnected);
mqttServer.ClientSubscribedTopicHandler = new MqttServerClientSubscribedTopicHandlerDelegate(OnMqttServerClientSubscribedTopic);
mqttServer.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(OnMqttServerClientUnsubscribedTopic);
mqttServer.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnMqttServer_ApplicationMessageReceived);
await mqttServer.StartAsync(optionsBuilder.Build());
//lbxMonitor.BeginInvoke(_updateMonitorAction,
// Logger.TraceLog(Logger.Level.Info, "MQTT Server is started."));
}
}
catch (Exception ex)
{
//lbxMonitor.BeginInvoke(_updateMonitorAction,
// Logger.TraceLog(Logger.Level.Fatal, $"MQTT Server start fail.>{ex.Message}"));
}
}
public async void StopMqttServer()
{
if (mqttServer == null) return;
try
{
await mqttServer?.StopAsync();
mqttServer = null;
//lbxMonitor.BeginInvoke(_updateMonitorAction,
// Logger.TraceLog(Logger.Level.Info, "MQTT Server is stopped."));
}
catch (Exception ex)
{
//lbxMonitor.BeginInvoke(_updateMonitorAction,
// Logger.TraceLog(Logger.Level.Fatal, $"MQTT Server stop fail.>{ex.Message}"));
}
}
public async void ServerPublishMqttTopic(string topic, string payload)
{
var message = new MqttApplicationMessage()
{
Topic = topic,
Payload = Encoding.UTF8.GetBytes(payload)
};
await mqttServer.PublishAsync(message);
//lbxMonitor.BeginInvoke(_updateMonitorAction,
// Logger.TraceLog(Logger.Level.Info, string.Format("MQTT Broker发布主题[{0}]成功!", topic)));
}
public void OnMqttServerStarted(EventArgs e)
{
//Console.WriteLine("MQTT服务启动完成!");
}
public void OnMqttServerStopped(EventArgs e)
{
//Console.WriteLine("MQTT服务停止完成!");
}
public void OnMqttServerClientConnected(MqttServerClientConnectedEventArgs e)
{
//Console.WriteLine($"客户端[{e.ClientId}]已连接");
}
public void OnMqttServerClientDisconnected(MqttServerClientDisconnectedEventArgs e)
{
//Console.WriteLine($"客户端[{e.ClientId}]已断开连接!");
}
public void OnMqttServerClientSubscribedTopic(MqttServerClientSubscribedTopicEventArgs e)
{
//Console.WriteLine($"客户端[{e.ClientId}]已成功订阅主题[{e.TopicFilter}]!");
}
public void OnMqttServerClientUnsubscribedTopic(MqttServerClientUnsubscribedTopicEventArgs e)
{
//Console.WriteLine($"客户端[{e.ClientId}]已成功取消订阅主题[{e.TopicFilter}]!");
}
public void OnMqttServer_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e)
{
try
{
if (string.IsNullOrEmpty(e.ClientId))
{
return;
}
var DeviceNo = e.ClientId;
var Topic = e.ApplicationMessage.Topic;
var Payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
if (Payload.Length > 2 && Payload.Substring(0, 2) == "/C") //控制消息
{
var data = Payload.Split('|');
if (data.Length == 3)
{
Topic = data[1];
var jObj = JObject.Parse(data[2]);
if (Topic == "ShelfGetWarehouse")
{
ServerPublishMqttTopic(DeviceNo, $"/C|ResponseShelfGetWarehouse|{{\"warehouseId\":\"{MyCache.ChannelCfg.warehouseId}\"}}");
}
}
}
//Console.WriteLine($"客户端[{e.ClientId}]>> 主题:{e.ApplicationMessage.Topic} 负荷:{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)} Qos:{e.ApplicationMessage.QualityOfServiceLevel} 保留:{e.ApplicationMessage.Retain}");
}
catch (Exception exception)
{
//catch
}
}
public void Dispose()
{
StopMqttServer();
}
}
}
......@@ -454,7 +454,7 @@ namespace JmpServiceMgr.Helper
}
//移除装备包
request.RFIDList.RemoveAll(x => bagEpcList.Contains(x.RFID));
if (bagEpcList.Count > 0)
{
//装备包获取件标信息
......@@ -566,7 +566,28 @@ namespace JmpServiceMgr.Helper
{
Task.Run(() =>
{
_form.SerialPortHelper.CreateOrderPushData(pushResultList);
if (MyCache.SysConfig.PushShelfPrintType == 0)
{
//com
_form.SerialPortHelper.CreateOrderPushData(pushResultList);
}
else
{
//mqtt
foreach (var client in _form.MqttServer.mqttServer.GetClientStatusAsync().Result)
{
_form.MqttServer.ServerPublishMqttTopic(client.ClientId, $"/C|ChannelIn|" +
JsonConvert.SerializeObject(pushResultList
.Where(x => x.currentState == 0)
.GroupBy(x => new { x.name, x.Size })
.Select(x => new
{
name = x.Key.name,
shelfAddress = x.FirstOrDefault()?.shelfAddress,
Size = x.Key.Size
})));
}
}
});
}
}
......
......@@ -101,6 +101,7 @@
<HintPath>..\Lib\SReaderAPI.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
......@@ -138,6 +139,7 @@
<Compile Include="Helper\CameraHelper.cs" />
<Compile Include="Helper\Model\ShelfMessageStatus.cs" />
<Compile Include="Helper\Model\TagReport.cs" />
<Compile Include="Helper\MQTTServer.cs" />
<Compile Include="Helper\SerialPortHelper.cs" />
<Compile Include="Helper\Reader\Solid401RFIDReader.cs" />
<Compile Include="Helper\MQTTHelper.cs" />
......@@ -174,7 +176,7 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="app.config" />
<None Include="app.config" />
<EmbeddedResource Include="RfidConfig.resx">
<DependentUpon>RfidConfig.cs</DependentUpon>
</EmbeddedResource>
......
......@@ -3,4 +3,9 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<appSettings>
<add key="MQTTPort" value="17777"/>
<add key="UserName" value="test"/>
<add key="Password" value="junmp123"/>
</appSettings>
</configuration>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论