diff --git a/BPASmartClient.Business/InternetInfo.cs b/BPASmartClient.Business/InternetInfo.cs index 1a0e4106..e1196ba9 100644 --- a/BPASmartClient.Business/InternetInfo.cs +++ b/BPASmartClient.Business/InternetInfo.cs @@ -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() { "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); - // } - //} + + } diff --git a/BPASmartClient.Business/Plugin/ConfigMgr.cs b/BPASmartClient.Business/Plugin/ConfigMgr.cs index 9030a538..6b4b4e7e 100644 --- a/BPASmartClient.Business/Plugin/ConfigMgr.cs +++ b/BPASmartClient.Business/Plugin/ConfigMgr.cs @@ -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 { + /// + /// 配置管理器 + /// public class ConfigMgr : IPlugin { + /// + /// 门店ID + /// + public int ClientId { get; set; } + /// + /// MQTT Broker + /// + public MQTT_Entity MQTT_Config { get; set; } + + //Apollo地址 + private string apolloUri; + //设备集合 private List 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 GetDeviceConfigs() @@ -23,6 +45,9 @@ namespace BPASmartClient.Business return deviceConfigs; } + /// + /// 加载设备模型 + /// private void InitDeviceModel() { deviceConfigs = new List(); @@ -50,5 +75,28 @@ namespace BPASmartClient.Business } } + /// + /// 加载MQTT配置 + /// + private void InitMQTT() + { + + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); + configurationBuilder.AddApolloConfiguration(p => + { + p.AppId = "dev1_order"; + p.MetaServer = apolloUri; + p.Namespaces = new List() { "DEV.Config" }; + }); + IConfiguration config = configurationBuilder.Build(); + var mqttBroker = config.GetSection("BrokerHostSettings"); + MQTT_Config = mqttBroker.Value.FromJSON(); + } + + public class MQTT_Entity + { + public string Host { get; set; } + public int Port { get; set; } + } } } \ No newline at end of file diff --git a/BPASmartClient.Business/Plugin/MQTTMgr.cs b/BPASmartClient.Business/Plugin/MQTTMgr.cs index 3bdda5d7..e88e1f0b 100644 --- a/BPASmartClient.Business/Plugin/MQTTMgr.cs +++ b/BPASmartClient.Business/Plugin/MQTTMgr.cs @@ -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 { + /// + /// 消息处理接口 + /// + public delegate void RecivedHandle(IMessage message); + /// + /// MQTT 管理类 + /// public class MQTTMgr : IPlugin { + //客户端ID + private int clientId; + //运行标识 + private bool running = false; //消息缓存 private ConcurrentQueue msg = new ConcurrentQueue(); - + //MQTT 代理 + private MQTTProxy mqttProxy = new MQTTProxy(); + //消息处理者 + private List messageRecives = new List(); 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((receivce) => + mqttProxy.MessageRecive = new Action((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().MQTT_Config.Host, Plugin.GetInstance().GetPlugin().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().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 消息处理"); + } + + /// + /// 设置消息处理者 + /// + /// 消息处理者 + 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(); } + + } } diff --git a/BPASmartClient.Business/Plugin/OrderProxy.cs b/BPASmartClient.Business/Plugin/OrderProxy.cs index fa516224..9b8413ca 100644 --- a/BPASmartClient.Business/Plugin/OrderProxy.cs +++ b/BPASmartClient.Business/Plugin/OrderProxy.cs @@ -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 { + /// + /// 订单代理 + /// public class OrderProxy : IPlugin { + /// + /// 初始化 + /// public void Initialize() { + Plugin.GetInstance().GetPlugin().SetMessageReciveHandler(delegate (IMessage orderInfo) + { + //if(orderInfo == null) return; + //if (orderInfo is MorkOrderPush morkOrderpush) + //{ + // OrderData order = Json.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); + // } + //} + }); } + /// + /// 资源释放 + /// public void Dispose() { } diff --git a/BPASmartClient.DRCoffee/CoffeeMachine.cs b/BPASmartClient.DRCoffee/CoffeeMachine.cs index db8e06cc..b52b1877 100644 --- a/BPASmartClient.DRCoffee/CoffeeMachine.cs +++ b/BPASmartClient.DRCoffee/CoffeeMachine.cs @@ -85,7 +85,7 @@ namespace BPASmartClient.DRCoffee /// 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 temp = new List(); //一系列解包 diff --git a/BPASmartClient.GSIceCream/IceCreamMachine.cs b/BPASmartClient.GSIceCream/IceCreamMachine.cs index 79a66028..395d9c52 100644 --- a/BPASmartClient.GSIceCream/IceCreamMachine.cs +++ b/BPASmartClient.GSIceCream/IceCreamMachine.cs @@ -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(); }), "冰淇淋解析线程"); diff --git a/BPASmartClient.Helper/ThreadManage.cs b/BPASmartClient.Helper/ThreadManage.cs index fabded4f..285a0db3 100644 --- a/BPASmartClient.Helper/ThreadManage.cs +++ b/BPASmartClient.Helper/ThreadManage.cs @@ -13,11 +13,8 @@ namespace BPASmartClient.Helper /// /// 线程管理 /// - public class ThreadManage + public class ThreadManage:Singleton { - 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(); diff --git a/BPASmartClient.MQTT/MQTTProxy.cs b/BPASmartClient.MQTT/MQTTProxy.cs new file mode 100644 index 00000000..3ac66923 --- /dev/null +++ b/BPASmartClient.MQTT/MQTTProxy.cs @@ -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 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) + { + + } + } +} diff --git a/BPASmartClient.SCChip/ICChipMachine.cs b/BPASmartClient.SCChip/ICChipMachine.cs index 4ef0958e..c7504b93 100644 --- a/BPASmartClient.SCChip/ICChipMachine.cs +++ b/BPASmartClient.SCChip/ICChipMachine.cs @@ -77,7 +77,7 @@ namespace BPASmartClient.SCChip /// private void MainLoop() { - ThreadManage.GetInstance.StartLong(new Action(() => + ThreadManage.GetInstance().StartLong(new Action(() => { ResolveMsg(); }),"单片机解析线程"); diff --git a/BPASmartClient/App.config b/BPASmartClient/App.config index 47c7cf8c..f874338a 100644 --- a/BPASmartClient/App.config +++ b/BPASmartClient/App.config @@ -11,7 +11,8 @@ - + + diff --git a/BPASmartClient/BPASmartClient.csproj b/BPASmartClient/BPASmartClient.csproj index c0235485..778cdffc 100644 --- a/BPASmartClient/BPASmartClient.csproj +++ b/BPASmartClient/BPASmartClient.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net6.0-windows enable true diff --git a/SmartClient.sln b/SmartClient.sln index 6b2d78b8..b418b7a6 100644 --- a/SmartClient.sln +++ b/SmartClient.sln @@ -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