Yazar | SHA1 | Mesaj | Tarih |
---|---|---|---|
applelon | 1ea1ccefa7 | Merge branch 'master' of http://10.2.1.24:10244/applelon/BPASmartClient | 2 yıl önce |
applelon | ab347df256 | 增加订单状态改变上报 | 2 yıl önce |
@@ -17,6 +17,7 @@ | |||
<ItemGroup> | |||
<ProjectReference Include="..\BPASmartClient.Device\BPASmartClient.Device.csproj" /> | |||
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" /> | |||
<ProjectReference Include="..\BPASmartClient.Http\BPASmartClient.Http.csproj" /> | |||
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" /> | |||
<ProjectReference Include="..\BPASmartClient.MQTT\BPASmartClient.MQTT.csproj" /> | |||
</ItemGroup> | |||
@@ -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 | |||
{ | |||
} | |||
} |
@@ -31,61 +31,13 @@ namespace BPASmartClient.Business | |||
NetworkConnectState = UniversalHelper.GetInstance().GetNetworkState(); | |||
Thread.Sleep(2000); | |||
} | |||
ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]); | |||
} | |||
/// <summary> | |||
/// Consul 地址 | |||
/// </summary> | |||
public static string ConsulAddress { get; set; } | |||
/// <summary> | |||
/// 网络连接状态 | |||
/// </summary> | |||
public static bool NetworkConnectState { get; set; } | |||
/// <summary> | |||
/// 广告地址 | |||
/// </summary> | |||
public static Uri SorbetesAddress { get; set; } | |||
/// <summary> | |||
/// 客户端ID | |||
/// </summary> | |||
public static int ClientId { get; set; } | |||
/// <summary> | |||
/// IOTAPI 地址 | |||
/// </summary> | |||
public static string IotApiAddress { get; set; } | |||
#region MQTT连接信息 | |||
/// <summary> | |||
/// mqtt 服务地址 | |||
/// </summary> | |||
public static string MqttAddress { get; set; } | |||
/// <summary> | |||
/// mqtt 用户名 | |||
/// </summary> | |||
public static string MqttUserName { get; set; } | |||
/// <summary> | |||
/// mqtt 密码 | |||
/// </summary> | |||
public static string MqttPassword { get; set; } | |||
/// <summary> | |||
/// mqtt 端口号 | |||
/// </summary> | |||
public static int MqttPort { get; set; } | |||
#endregion | |||
#region API地址 | |||
/// <summary> | |||
@@ -23,14 +23,6 @@ namespace BPASmartClient.Business | |||
Plugin.GetInstance().Init(); | |||
//主题初始化 | |||
TopicDefine.GetInstance().Initialize(Plugin.GetInstance().GetPlugin<DeviceMgr>().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")); | |||
} | |||
/// <summary> | |||
@@ -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(); | |||
@@ -33,12 +33,11 @@ namespace BPASmartClient.Business | |||
private List<RecivedHandle> messageRecives = new List<RecivedHandle>(); | |||
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); | |||
} | |||
/// <summary> | |||
@@ -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 | |||
/// </summary> | |||
public void Initialize() | |||
{ | |||
running = true; | |||
deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>(); | |||
Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo) | |||
@@ -38,6 +43,8 @@ namespace BPASmartClient.Business | |||
} | |||
}); | |||
EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(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<OrderStatusRsp>(result); | |||
MessageLog.GetInstance.ShowEx(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess)); | |||
} | |||
/// <summary> | |||
/// 资源释放 | |||
/// </summary> | |||
@@ -0,0 +1,28 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BPASmartClient.Business | |||
{ | |||
/// <summary> | |||
/// 状态管理器 | |||
/// </summary> | |||
public class StatusMgr : IPlugin | |||
{ | |||
//运行标识 | |||
private bool running = false; | |||
//设备管理 | |||
private DeviceMgr deviceMgr; | |||
public void Initialize() | |||
{ | |||
deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>(); | |||
} | |||
public void Dispose() | |||
{ | |||
} | |||
} | |||
} |
@@ -29,8 +29,8 @@ namespace BPASmartClient.Business | |||
PushHeartbeatTopics = new Dictionary<DeviceClientType, string>(); | |||
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<ConfigMgr>().ClientId)); | |||
SubscribTopics.Add(TOPIC.GetInstance.GetBusinessTopic(device.DeviceType, Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId)); | |||
PushHeartbeatTopics.Add(device.DeviceType, TOPIC.GetInstance.GetHeatbeatTopic(device.DeviceType)); | |||
} | |||
@@ -5,7 +5,7 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="BPA.MQTTClient" Version="1.0.9" /> | |||
<PackageReference Include="BPA.MQTTClient" Version="1.0.10" /> | |||
<PackageReference Include="BPA.MQTTnet" Version="1.0.3" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | |||
</ItemGroup> | |||
@@ -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<MQTT_Entity>(); | |||
} | |||
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}); | |||
} | |||
} | |||
} | |||
} |
@@ -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 | |||
{ | |||
/// <summary> | |||
/// 订单状态改变事件 | |||
/// </summary> | |||
public class OrderStatusChangedEvent:BaseEvent | |||
{ | |||
/// <summary> | |||
/// 子订单ID | |||
/// </summary> | |||
public string SubOrderId { get; set; } | |||
/// <summary> | |||
/// 状态 | |||
/// </summary> | |||
public ORDER_STATUS Status { get; set; } | |||
} | |||
} |
@@ -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 | |||
{ | |||
/// <summary> | |||
/// 子订单ID | |||
/// </summary> | |||
public string SuborderId { get; set; } | |||
/// <summary> | |||
/// 订单状态 | |||
/// </summary> | |||
public int Result { get; set; } = 0; | |||
public bool isSuccess { get; set; } | |||
public bool data { get; set; } | |||
public string msg { get; set; } | |||
} | |||
} |
@@ -1,26 +1,29 @@ | |||
<?xml version="1.0" encoding="utf-8" ?> | |||
<configuration> | |||
<appSettings> | |||
<!--通用配置--> | |||
<appSettings> | |||
<!--通用配置--> | |||
<!--测试服务 Consul 地址--> | |||
<add key="ConsulAddress" value="http://111.9.47.105:9011/" /> | |||
<!--测试服务 Consul 地址--> | |||
<add key="ConsulAddress" value="http://111.9.47.105:9011/" /> | |||
<!--正式服务 Consul 地址--> | |||
<!--<add key="ConsulAddress" value="http://162.14.105.138:9005" />--> | |||
<!--正式服务 Consul 地址--> | |||
<!--<add key="ConsulAddress" value="http://162.14.105.138:9005" />--> | |||
<!--客户端ID--> | |||
<!--1:且时且多冰淇淋咖啡机,2:且时且多煮面机,3:海科煮面机测试店铺--> | |||
<add key="ClientId" value="12"/> | |||
<add key="ApolloUri" value="http://10.2.1.21:28080"/> | |||
<!--客户端ID--> | |||
<!--1:且时且多冰淇淋咖啡机,2:且时且多煮面机,3:海科煮面机测试店铺--> | |||
<add key="ClientId" value="12"/> | |||
<add key="ApolloUri" value="http://10.2.1.21:28080"/> | |||
<add key="OrderServiceUri" value="http://10.2.1.26:21527/order/"/> | |||
<add key="StockServiceUri" value="http://10.2.1.26:21527/stock/"/> | |||
<add key="COM_Coffee" value="COM3"/> | |||
<add key="BAUD_Coffee" value="115200"/> | |||
<add key="COM_IceCream" value="COM12"/> | |||
<add key="BAUD_IceCream" value="9600"/> | |||
<add key="IceCream_CXB_Threshold" value="90"/> | |||
<add key="COM_ICChip" value="COM6"/> | |||
<add key="BAUD_IChip" value="9600"/> | |||
</appSettings> | |||
<add key="COM_Coffee" value="COM3"/> | |||
<add key="BAUD_Coffee" value="115200"/> | |||
<add key="COM_IceCream" value="COM12"/> | |||
<add key="BAUD_IceCream" value="9600"/> | |||
<add key="IceCream_CXB_Threshold" value="90"/> | |||
<add key="COM_ICChip" value="COM6"/> | |||
<add key="BAUD_IChip" value="9600"/> | |||
</appSettings> | |||
</configuration> |