diff --git a/BPASmartClient.Business/BPASmartClient.Business.csproj b/BPASmartClient.Business/BPASmartClient.Business.csproj index 9e70abc5..d1f2ff3b 100644 --- a/BPASmartClient.Business/BPASmartClient.Business.csproj +++ b/BPASmartClient.Business/BPASmartClient.Business.csproj @@ -17,6 +17,7 @@ + diff --git a/BPASmartClient.Business/DataStorage.cs b/BPASmartClient.Business/DataStorage.cs deleted file mode 100644 index 558eedfd..00000000 --- a/BPASmartClient.Business/DataStorage.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BPASmartClient.Business -{ - public class DataStorage - { - } -} diff --git a/BPASmartClient.Business/InternetInfo.cs b/BPASmartClient.Business/InternetInfo.cs index e1196ba9..a4340802 100644 --- a/BPASmartClient.Business/InternetInfo.cs +++ b/BPASmartClient.Business/InternetInfo.cs @@ -31,61 +31,13 @@ namespace BPASmartClient.Business NetworkConnectState = UniversalHelper.GetInstance().GetNetworkState(); Thread.Sleep(2000); } - - ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]); - - } - - - /// - /// Consul 地址 - /// - public static string ConsulAddress { get; set; } - /// /// 网络连接状态 /// public static bool NetworkConnectState { get; set; } - /// - /// 广告地址 - /// - public static Uri SorbetesAddress { get; set; } - - /// - /// 客户端ID - /// - public static int ClientId { get; set; } - - /// - /// IOTAPI 地址 - /// - public static string IotApiAddress { get; set; } - - #region MQTT连接信息 - /// - /// mqtt 服务地址 - /// - public static string MqttAddress { get; set; } - - /// - /// mqtt 用户名 - /// - public static string MqttUserName { get; set; } - - /// - /// mqtt 密码 - /// - public static string MqttPassword { get; set; } - - /// - /// mqtt 端口号 - /// - public static int MqttPort { get; set; } - #endregion - #region API地址 /// diff --git a/BPASmartClient.Business/MainConsole.cs b/BPASmartClient.Business/MainConsole.cs index 1c9b0fd7..944d44f4 100644 --- a/BPASmartClient.Business/MainConsole.cs +++ b/BPASmartClient.Business/MainConsole.cs @@ -23,14 +23,6 @@ namespace BPASmartClient.Business Plugin.GetInstance().Init(); //主题初始化 TopicDefine.GetInstance().Initialize(Plugin.GetInstance().GetPlugin().GetDevices()); - //MQTT 重连成功 - MqttHelper.GetInstance().Reconnection = new Action(() => - { - MqttHelper.GetInstance().MqttSubscriptionAsync(TopicDefine.GetInstance().SubscribTopics.ToArray()); - }); - //MQTT 连接 - MqttHelper.GetInstance().MqttInitAsync(InternetInfo.MqttUserName, InternetInfo.MqttPassword, - InternetInfo.MqttAddress, InternetInfo.MqttPort, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); } /// diff --git a/BPASmartClient.Business/Plugin/ConfigMgr.cs b/BPASmartClient.Business/Plugin/ConfigMgr.cs index f2576e05..95f693a8 100644 --- a/BPASmartClient.Business/Plugin/ConfigMgr.cs +++ b/BPASmartClient.Business/Plugin/ConfigMgr.cs @@ -33,6 +33,8 @@ namespace BPASmartClient.Business { ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]); apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString(); + InternetInfo.OrderServer= System.Configuration.ConfigurationManager.AppSettings["OrderServiceUri"].ToString(); + InternetInfo.StockServer= System.Configuration.ConfigurationManager.AppSettings["StockServiceUri"].ToString(); InitDeviceModel(); InitMQTT(); diff --git a/BPASmartClient.Business/Plugin/MQTTMgr.cs b/BPASmartClient.Business/Plugin/MQTTMgr.cs index 27d837c0..fd61a0bb 100644 --- a/BPASmartClient.Business/Plugin/MQTTMgr.cs +++ b/BPASmartClient.Business/Plugin/MQTTMgr.cs @@ -33,12 +33,11 @@ namespace BPASmartClient.Business private List messageRecives = new List(); public void Initialize() { - - + running = true; //MQTT 连接成功 mqttProxy.Connected = new Action(() => { - MqttHelper.GetInstance().MqttSubscriptionAsync(TopicDefine.GetInstance().SubscribTopics.ToArray()); + mqttProxy.Subscrib(TopicDefine.GetInstance().SubscribTopics.ToArray()); }); //MQTT 连接成功 mqttProxy.LostConnect = new Action(() => @@ -75,7 +74,7 @@ namespace BPASmartClient.Business } Thread.Sleep(50); } - }, "MQTT 消息处理"); + }, "MQTT 消息处理",true); } /// diff --git a/BPASmartClient.Business/Plugin/OrderProxy.cs b/BPASmartClient.Business/Plugin/OrderProxy.cs index 3f8d3b15..05d9287e 100644 --- a/BPASmartClient.Business/Plugin/OrderProxy.cs +++ b/BPASmartClient.Business/Plugin/OrderProxy.cs @@ -1,13 +1,17 @@ using BPA.Message; using BPASmartClient.EventBus; using BPASmartClient.Helper; +using BPASmartClient.Http; +using BPASmartClient.Message; using BPASmartClient.Model; +using Newtonsoft.Json; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using static BPASmartClient.EventBus.EventBus; namespace BPASmartClient.Business { @@ -27,6 +31,7 @@ namespace BPASmartClient.Business /// public void Initialize() { + running = true; deviceMgr = Plugin.GetInstance().GetPlugin(); Plugin.GetInstance().GetPlugin().SetMessageReciveHandler(delegate (IMessage orderInfo) @@ -38,6 +43,8 @@ namespace BPASmartClient.Business } }); + EventBus.EventBus.GetInstance().Subscribe(0, OrderStatusChangedHandle); + ThreadManage.GetInstance().StartLong(() => { while (running) @@ -64,6 +71,25 @@ namespace BPASmartClient.Business }, "MQTT 消息处理"); } + public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack) + { + OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent; + string result = string.Empty; + OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId }; + try + { + string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt(); + string url = $"{InternetInfo.OrderServer}/order/robotstatuschange"; + result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST); + } + catch (Exception ex) + { + MessageLog.GetInstance.ShowEx(ex.ToString()); + } + var res = JsonConvert.DeserializeObject(result); + MessageLog.GetInstance.ShowEx(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess)); + } + /// /// 资源释放 /// diff --git a/BPASmartClient.Business/Plugin/StatusMgr.cs b/BPASmartClient.Business/Plugin/StatusMgr.cs new file mode 100644 index 00000000..55ecd024 --- /dev/null +++ b/BPASmartClient.Business/Plugin/StatusMgr.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Business +{ + /// + /// 状态管理器 + /// + public class StatusMgr : IPlugin + { + //运行标识 + private bool running = false; + //设备管理 + private DeviceMgr deviceMgr; + + public void Initialize() + { + deviceMgr = Plugin.GetInstance().GetPlugin(); + } + + public void Dispose() + { + } + } +} diff --git a/BPASmartClient.Business/TopicDefine.cs b/BPASmartClient.Business/TopicDefine.cs index f74ab8bb..fe3db646 100644 --- a/BPASmartClient.Business/TopicDefine.cs +++ b/BPASmartClient.Business/TopicDefine.cs @@ -29,8 +29,8 @@ namespace BPASmartClient.Business PushHeartbeatTopics = new Dictionary(); foreach (var device in devices) { - SubscribTopics.Add(TOPIC.GetInstance.GetOrderPushTopic(device.DeviceType, InternetInfo.ClientId)); - SubscribTopics.Add(TOPIC.GetInstance.GetBusinessTopic(device.DeviceType, InternetInfo.ClientId)); + SubscribTopics.Add(TOPIC.GetInstance.GetOrderPushTopic(device.DeviceType, Plugin.GetInstance().GetPlugin().ClientId)); + SubscribTopics.Add(TOPIC.GetInstance.GetBusinessTopic(device.DeviceType, Plugin.GetInstance().GetPlugin().ClientId)); PushHeartbeatTopics.Add(device.DeviceType, TOPIC.GetInstance.GetHeatbeatTopic(device.DeviceType)); } diff --git a/BPASmartClient.MQTT/BPASmartClient.MQTT.csproj b/BPASmartClient.MQTT/BPASmartClient.MQTT.csproj index 259d792a..69f50a3d 100644 --- a/BPASmartClient.MQTT/BPASmartClient.MQTT.csproj +++ b/BPASmartClient.MQTT/BPASmartClient.MQTT.csproj @@ -5,7 +5,7 @@ - + diff --git a/BPASmartClient.MQTT/MQTTProxy.cs b/BPASmartClient.MQTT/MQTTProxy.cs index 79b60e26..83491728 100644 --- a/BPASmartClient.MQTT/MQTTProxy.cs +++ b/BPASmartClient.MQTT/MQTTProxy.cs @@ -1,4 +1,8 @@ -using Microsoft.Extensions.Configuration; +using BPA.MQTTClient; +using Microsoft.Extensions.Configuration; +using MQTTnet; +using MQTTnet.Client; +using MQTTnet.Client.Receiving; using System; using System.Collections.Generic; using System.IO; @@ -16,34 +20,54 @@ namespace BPASmartClient.MQTT public bool IsConnected { get; set; } - public void Init() - { - - //IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); - //IConfiguration config = configurationBuilder.Build(); - //configurationBuilder. - //var mqttBroker = config.GetSection("BrokerHostSettings"); - //MQTT_Config = mqttBroker.Value.FromJSON(); - } + private IMqttClient client; public void Connect(string ip, int port, string clientId) { - + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddMqttClientHostedService(p => + { + p.Server = ip; + p.Port = port; + p.UserName = "rafiul"; + p.Password = "12345678"; + p.mqttClientConnectedHandlerDelegate = new MQTTnet.Client.Connecting.MqttClientConnectedHandlerDelegate(e => + { + IsConnected = true; + Connected?.Invoke(); + }); + //p.mqttClientDisconnectedHandlerDelegate = new MQTTnet.Client.Disconnecting.MqttClientDisconnectedHandlerDelegate(e => + //{ + // IsConnected = false; + // LostConnect?.Invoke(); + //}); + p.ConnectedResult += (s, e) => + { + client = e; + }; + p.MqttApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e => + { + MessageRecive?.Invoke(Encoding.Default.GetString(e.ApplicationMessage.Payload)); + }); + }); } public void CloseConnect() { - + client.Dispose(); } - public void Publish(string topic, string content) + public async void Publish(string topic, string content) { - + await client.PublishAsync(topic, content); } - public void Subscrib(params string[] topic) + public async void Subscrib(params string[] topics) { - + foreach (var topic in topics) + { + await client.SubscribeAsync(new MqttTopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce}); + } } } } diff --git a/BPASmartClient.Model/DoOrderEvent.cs b/BPASmartClient.Model/订单/DoOrderEvent.cs similarity index 100% rename from BPASmartClient.Model/DoOrderEvent.cs rename to BPASmartClient.Model/订单/DoOrderEvent.cs diff --git a/BPASmartClient.Model/订单/OrderStatusChangedEvent.cs b/BPASmartClient.Model/订单/OrderStatusChangedEvent.cs new file mode 100644 index 00000000..f5c25182 --- /dev/null +++ b/BPASmartClient.Model/订单/OrderStatusChangedEvent.cs @@ -0,0 +1,24 @@ +using BPA.Message.Enum; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + /// + /// 订单状态改变事件 + /// + public class OrderStatusChangedEvent:BaseEvent + { + /// + /// 子订单ID + /// + public string SubOrderId { get; set; } + /// + /// 状态 + /// + public ORDER_STATUS Status { get; set; } + } +} diff --git a/BPASmartClient.Model/订单/OrderStatusRsp.cs b/BPASmartClient.Model/订单/OrderStatusRsp.cs new file mode 100644 index 00000000..5d4bd022 --- /dev/null +++ b/BPASmartClient.Model/订单/OrderStatusRsp.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + public class OrderStatusRsp + { + /// + /// 子订单ID + /// + public string SuborderId { get; set; } + /// + /// 订单状态 + /// + public int Result { get; set; } = 0; + public bool isSuccess { get; set; } + public bool data { get; set; } + public string msg { get; set; } + + } +} diff --git a/BPASmartClient/App.config b/BPASmartClient/App.config index f874338a..c2bd3f09 100644 --- a/BPASmartClient/App.config +++ b/BPASmartClient/App.config @@ -1,26 +1,29 @@  - - + + - - + + - - + + - - - - + + + + + + - - - - - - - - + + + + + + + + + \ No newline at end of file