Author | SHA1 | Message | Date |
---|---|---|---|
applelon | 6791426032 | Merge branch 'master' of http://10.2.1.24:10244/applelon/BPASmartClient | 2 years ago |
applelon | d2712a16db | MQTT 加载 | 2 years ago |
@@ -10,6 +10,8 @@ using System.Threading; | |||
using BPASmartClient.Helper; | |||
using BPASmartClient.Message; | |||
using BPA.ApolloClient; | |||
using BPA.ApolloClient.Options; | |||
using Com.Ctrip.Framework.Apollo; | |||
namespace BPASmartClient.Business | |||
{ | |||
@@ -31,36 +33,8 @@ namespace BPASmartClient.Business | |||
} | |||
ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]); | |||
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); | |||
configurationBuilder.AddApolloConfiguration(p => | |||
{ | |||
p.AppId = "dev1_order"; | |||
p.MetaServer = "http://10.2.1.21:28070/"; | |||
p.Namespaces = new List<string>() { "DEV.Config"}; | |||
}); | |||
IConfiguration config = configurationBuilder.Build(); | |||
var mqttBroker = config.GetSection("BrokerHostSettings"); | |||
//while (StockServer == null) | |||
//{ | |||
// try | |||
// { | |||
// IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddConsul(ConsulAddress, "root/basic.json", false, 5_000); | |||
// IConfiguration config = configurationBuilder.Build(); | |||
// MqttUserName = config["MQTT:TcpAccount"]; | |||
// MqttPassword = config["MQTT:TcpPwd"]; | |||
// MqttAddress = config["MQTT:IP"]; | |||
// MqttPort = int.Parse(config["MQTT:Client"]); | |||
// ApiAddress = config["GateWay:BaseURL"]; | |||
// OrderServer = config["GateWay:Order"]; | |||
// StockServer = config["GateWay:Stock"]; | |||
// IotApiAddress = config["IOT:Address"]; | |||
// } | |||
// catch (Exception ex) | |||
// { | |||
// MessageLog.GetInstance.Show(ex.ToString()); | |||
// Thread.Sleep(30000); | |||
// } | |||
//} | |||
} | |||
@@ -1,11 +1,29 @@ | |||
using BPASmartClient.Model; | |||
using BPA.ApolloClient; | |||
using BPA.Message; | |||
using BPASmartClient.Model; | |||
using Microsoft.Extensions.Configuration; | |||
using System.Xml.Linq; | |||
using System.Xml.XPath; | |||
namespace BPASmartClient.Business | |||
{ | |||
/// <summary> | |||
/// 配置管理器 | |||
/// </summary> | |||
public class ConfigMgr : IPlugin | |||
{ | |||
/// <summary> | |||
/// 门店ID | |||
/// </summary> | |||
public int ClientId { get; set; } | |||
/// <summary> | |||
/// MQTT Broker | |||
/// </summary> | |||
public MQTT_Entity MQTT_Config { get; set; } | |||
//Apollo地址 | |||
private string apolloUri; | |||
//设备集合 | |||
private List<DeviceConfig> deviceConfigs; | |||
public void Dispose() | |||
{ | |||
@@ -13,7 +31,11 @@ namespace BPASmartClient.Business | |||
public void Initialize() | |||
{ | |||
ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]); | |||
apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString(); | |||
InitDeviceModel(); | |||
InitMQTT(); | |||
} | |||
public List<DeviceConfig> GetDeviceConfigs() | |||
@@ -23,6 +45,9 @@ namespace BPASmartClient.Business | |||
return deviceConfigs; | |||
} | |||
/// <summary> | |||
/// 加载设备模型 | |||
/// </summary> | |||
private void InitDeviceModel() | |||
{ | |||
deviceConfigs = new List<DeviceConfig>(); | |||
@@ -50,5 +75,28 @@ namespace BPASmartClient.Business | |||
} | |||
} | |||
/// <summary> | |||
/// 加载MQTT配置 | |||
/// </summary> | |||
private void InitMQTT() | |||
{ | |||
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); | |||
configurationBuilder.AddApolloConfiguration(p => | |||
{ | |||
p.AppId = "dev1_order"; | |||
p.MetaServer = apolloUri; | |||
p.Namespaces = new List<string>() { "DEV.Config" }; | |||
}); | |||
IConfiguration config = configurationBuilder.Build(); | |||
var mqttBroker = config.GetSection("BrokerHostSettings"); | |||
MQTT_Config = mqttBroker.Value.FromJSON<MQTT_Entity>(); | |||
} | |||
public class MQTT_Entity | |||
{ | |||
public string Host { get; set; } | |||
public int Port { get; set; } | |||
} | |||
} | |||
} |
@@ -1,4 +1,7 @@ | |||
using HBLConsole.Communication; | |||
using BPA.Message; | |||
using BPASmartClient.Helper; | |||
using BPASmartClient.MQTT; | |||
using HBLConsole.Communication; | |||
using System; | |||
using System.Collections.Concurrent; | |||
using System.Collections.Generic; | |||
@@ -8,37 +11,89 @@ using System.Threading.Tasks; | |||
namespace BPASmartClient.Business | |||
{ | |||
/// <summary> | |||
/// 消息处理接口 | |||
/// </summary> | |||
public delegate void RecivedHandle(IMessage message); | |||
/// <summary> | |||
/// MQTT 管理类 | |||
/// </summary> | |||
public class MQTTMgr : IPlugin | |||
{ | |||
//客户端ID | |||
private int clientId; | |||
//运行标识 | |||
private bool running = false; | |||
//消息缓存 | |||
private ConcurrentQueue<string> msg = new ConcurrentQueue<string>(); | |||
//MQTT 代理 | |||
private MQTTProxy mqttProxy = new MQTTProxy(); | |||
//消息处理者 | |||
private List<RecivedHandle> messageRecives = new List<RecivedHandle>(); | |||
public void Initialize() | |||
{ | |||
//MQTT 重连成功 | |||
MqttHelper.GetInstance().Reconnection = new Action(() => | |||
//MQTT 连接成功 | |||
mqttProxy.Connected = new Action(() => | |||
{ | |||
MqttHelper.GetInstance().MqttSubscriptionAsync(TopicDefine.GetInstance().SubscribTopics.ToArray()); | |||
}); | |||
//MQTT 连接成功 | |||
MqttHelper.GetInstance().ConnectOk = new Action(() => | |||
mqttProxy.LostConnect = new Action(() => | |||
{ | |||
MqttHelper.GetInstance().MqttSubscriptionAsync(TopicDefine.GetInstance().SubscribTopics.ToArray()); | |||
}); | |||
//MQTT 数据接收 | |||
MqttHelper.GetInstance().MqttReceive = new Action<MQTTnet.MqttApplicationMessageReceivedEventArgs>((receivce) => | |||
mqttProxy.MessageRecive = new Action<string>((message) => | |||
{ | |||
msg.Enqueue(Encoding.UTF8.GetString(receivce.ApplicationMessage.Payload)); | |||
msg.Enqueue(message); | |||
}); | |||
//MQTT 初始化 | |||
MqttHelper.GetInstance().MqttInitAsync(InternetInfo.MqttUserName, InternetInfo.MqttPassword, | |||
InternetInfo.MqttAddress, InternetInfo.MqttPort, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); | |||
mqttProxy.Connect(Plugin.GetInstance().GetPlugin<ConfigMgr>().MQTT_Config.Host, Plugin.GetInstance().GetPlugin<ConfigMgr>().MQTT_Config.Port, | |||
Guid.NewGuid().ToString()); | |||
ThreadManage.GetInstance().StartLong(() => | |||
{ | |||
while (running) | |||
{ | |||
while (msg.Count > 0 && msg.TryDequeue(out string temp)) | |||
{ | |||
if (0 == clientId) | |||
clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId; | |||
var package = BPAPackage.Deserialize(temp); | |||
if (package?.ClientId == clientId) | |||
{ | |||
if (package.Message != null) | |||
{ | |||
for (int i = messageRecives.Count - 1; i >= 0; i--) { | |||
messageRecives[i]?.Invoke(package.Message); | |||
} | |||
} | |||
} | |||
} | |||
Thread.Sleep(50); | |||
} | |||
}, "MQTT 消息处理"); | |||
} | |||
/// <summary> | |||
/// 设置消息处理者 | |||
/// </summary> | |||
/// <param name="messageRecive">消息处理者</param> | |||
public void SetMessageReciveHandler(RecivedHandle messageRecive) | |||
{ | |||
if (messageRecives.Contains(messageRecive)) | |||
return; | |||
messageRecives.Add(messageRecive); | |||
} | |||
public void Dispose() | |||
{ | |||
MqttHelper.GetInstance().Close(); | |||
running = false; | |||
mqttProxy.CloseConnect(); | |||
messageRecives.Clear(); | |||
} | |||
} | |||
} |
@@ -1,4 +1,6 @@ | |||
using HBLConsole.Communication; | |||
using BPA.Message; | |||
using BPASmartClient.Helper; | |||
using HBLConsole.Communication; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
@@ -7,12 +9,35 @@ using System.Threading.Tasks; | |||
namespace BPASmartClient.Business | |||
{ | |||
/// <summary> | |||
/// 订单代理 | |||
/// </summary> | |||
public class OrderProxy : IPlugin | |||
{ | |||
/// <summary> | |||
/// 初始化 | |||
/// </summary> | |||
public void Initialize() | |||
{ | |||
Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo) | |||
{ | |||
//if(orderInfo == null) return; | |||
//if (orderInfo is MorkOrderPush morkOrderpush) | |||
//{ | |||
// OrderData order = Json<KeepDataBase>.Data.orderLists.FirstOrDefault(par => par.OrderPush?.SuborderId == morkOrderpush.SuborderId); | |||
// if (order == null)//防止重复订单 | |||
// { | |||
// ActionManage.GetInstance.Send("AddOrder", morkOrderpush); | |||
// GeneralConfig.morkOrderPushes.Enqueue(morkOrderpush); | |||
// //ActionManage.GetInstance.Send("DataParse", morkOrderpush); | |||
// } | |||
//} | |||
}); | |||
} | |||
/// <summary> | |||
/// 资源释放 | |||
/// </summary> | |||
public void Dispose() | |||
{ | |||
} | |||
@@ -85,7 +85,7 @@ namespace BPASmartClient.DRCoffee | |||
/// </summary> | |||
private void MainLoop() | |||
{ | |||
ThreadManage.GetInstance.StartLong(new Action(() => | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
if (!free) | |||
{ | |||
@@ -94,7 +94,7 @@ namespace BPASmartClient.DRCoffee | |||
Thread.Sleep(200); | |||
}),"咖啡机询问线程"); | |||
ThreadManage.GetInstance.StartLong(new Action(() => | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
List<byte> temp = new List<byte>(); | |||
//一系列解包 | |||
@@ -77,7 +77,7 @@ namespace BPASmartClient.GSIceCream | |||
private MSG_RESOLVE_STEP currentStep; | |||
private void MainLoop() | |||
{ | |||
ThreadManage.GetInstance.StartLong(new Action(() => | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
if (!free) | |||
{ | |||
@@ -86,7 +86,7 @@ namespace BPASmartClient.GSIceCream | |||
Thread.Sleep(500); | |||
}), "冰淇淋询问线程"); | |||
ThreadManage.GetInstance.StartLong(new Action(() => | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
ResolveMsg(); | |||
}), "冰淇淋解析线程"); | |||
@@ -13,11 +13,8 @@ namespace BPASmartClient.Helper | |||
/// <summary> | |||
/// 线程管理 | |||
/// </summary> | |||
public class ThreadManage | |||
public class ThreadManage:Singleton<ThreadManage> | |||
{ | |||
private volatile static ThreadManage _Instance; | |||
public static ThreadManage GetInstance => _Instance ?? (_Instance = new ThreadManage()); | |||
private ThreadManage() { } | |||
string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-"; | |||
private static readonly object _lock = new object(); | |||
StringBuilder callbackKey = new StringBuilder(); | |||
@@ -0,0 +1,37 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BPASmartClient.MQTT | |||
{ | |||
public class MQTTProxy | |||
{ | |||
public Action<string> MessageRecive { get; set; } | |||
public Action Connected { get; set; } | |||
public Action LostConnect { get; set; } | |||
public bool IsConnected { get; set; } | |||
public void Connect(string ip, int port, string clientId) | |||
{ | |||
} | |||
public void CloseConnect() | |||
{ | |||
} | |||
public void Publish(string topic, string content) | |||
{ | |||
} | |||
public void Subscrib(params string[] topic) | |||
{ | |||
} | |||
} | |||
} |
@@ -77,7 +77,7 @@ namespace BPASmartClient.SCChip | |||
/// </summary> | |||
private void MainLoop() | |||
{ | |||
ThreadManage.GetInstance.StartLong(new Action(() => | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
ResolveMsg(); | |||
}),"单片机解析线程"); | |||
@@ -11,7 +11,8 @@ | |||
<!--客户端ID--> | |||
<!--1:且时且多冰淇淋咖啡机,2:且时且多煮面机,3:海科煮面机测试店铺--> | |||
<add key="ClientId" value="12"/> | |||
<add key="ClientId" value="12"/> | |||
<add key="ApolloUri" value="http://10.2.1.21:28080"/> | |||
<add key="COM_Coffee" value="COM3"/> | |||
<add key="BAUD_Coffee" value="115200"/> | |||
@@ -1,7 +1,7 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<OutputType>WinExe</OutputType> | |||
<OutputType>Exe</OutputType> | |||
<TargetFramework>net6.0-windows</TargetFramework> | |||
<Nullable>enable</Nullable> | |||
<UseWPF>true</UseWPF> | |||
@@ -76,6 +76,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.CustomResour | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lebai.SDK", "Lebai.SDK\Lebai.SDK.csproj", "{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}" | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPA.ApolloClient", "..\..\Users\Documents\WeChat Files\applelon\FileStorage\File\2022-04\BPA.ApolloClient\BPA.ApolloClient\BPA.ApolloClient.csproj", "{33E1A4FB-534D-442E-AA7D-8767EAC7DCFA}" | |||
EndProject | |||
Global | |||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
Debug|Any CPU = Debug|Any CPU | |||
@@ -194,6 +196,10 @@ Global | |||
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{33E1A4FB-534D-442E-AA7D-8767EAC7DCFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
{33E1A4FB-534D-442E-AA7D-8767EAC7DCFA}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{33E1A4FB-534D-442E-AA7D-8767EAC7DCFA}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{33E1A4FB-534D-442E-AA7D-8767EAC7DCFA}.Release|Any CPU.Build.0 = Release|Any CPU | |||
EndGlobalSection | |||
GlobalSection(SolutionProperties) = preSolution | |||
HideSolutionNode = FALSE | |||