@@ -1,4 +1,5 @@ | |||||
using HBLConsole.Communication; | |||||
using BPASmartClient.Message; | |||||
using HBLConsole.Communication; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Linq; | using System.Linq; | ||||
@@ -25,6 +26,11 @@ namespace BPASmartClient.Business | |||||
TopicDefine.GetInstance().Initialize(Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()); | TopicDefine.GetInstance().Initialize(Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()); | ||||
//设备开启 | //设备开启 | ||||
Plugin.GetInstance().GetPlugin<DeviceMgr>().StartService(); | Plugin.GetInstance().GetPlugin<DeviceMgr>().StartService(); | ||||
MessageLog.GetInstance.InfoNotify = new Action<string>((msg => | |||||
{ | |||||
Console.WriteLine(msg); | |||||
})); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -28,7 +28,7 @@ namespace BPASmartClient.Business | |||||
{ | { | ||||
deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>(); | deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>(); | ||||
mqttMgr = Plugin.GetInstance().GetPlugin<MQTTMgr>(); | mqttMgr = Plugin.GetInstance().GetPlugin<MQTTMgr>(); | ||||
running = true; | |||||
ThreadManage.GetInstance().Start(() => | ThreadManage.GetInstance().Start(() => | ||||
{ | { | ||||
while (running) | while (running) | ||||
@@ -54,7 +54,7 @@ namespace BPASmartClient.Business | |||||
wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus(); | wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus(); | ||||
deviceStatus.Healthy = device.IsHealth ? BPA.Message.Enum.DeviceHealthy.Health : BPA.Message.Enum.DeviceHealthy.UnHealth; | deviceStatus.Healthy = device.IsHealth ? BPA.Message.Enum.DeviceHealthy.Health : BPA.Message.Enum.DeviceHealthy.UnHealth; | ||||
var msg=BPAPackage.Make(deviceStatus, device.DeviceId, device.DeviceType); | var msg=BPAPackage.Make(deviceStatus, device.DeviceId, device.DeviceType); | ||||
mqttMgr.Publish(TOPIC.GetInstance.GetHeatbeatTopic(device.DeviceType), msg.Serialize()); | |||||
mqttMgr.Publish(TopicDefine.GetInstance().PushHeartbeatTopics[device.DeviceType], msg.Serialize()); | |||||
} | } | ||||
Thread.Sleep(1000); | Thread.Sleep(1000); | ||||
} | } | ||||
@@ -63,6 +63,7 @@ namespace BPASmartClient.Business | |||||
public void Dispose() | public void Dispose() | ||||
{ | { | ||||
running = false; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -20,7 +20,7 @@ namespace BPASmartClient.Device | |||||
/// <summary> | /// <summary> | ||||
/// 设备所有状态 | /// 设备所有状态 | ||||
/// </summary> | /// </summary> | ||||
public DeviceStatus Status { get; set; } | |||||
public DeviceStatus Status { get; set; } = new DeviceStatus(); | |||||
/// <summary> | /// <summary> | ||||
/// 设备名称 | /// 设备名称 | ||||
/// </summary> | /// </summary> | ||||
@@ -13,7 +13,7 @@ namespace BPASmartClient.Helper | |||||
/// <summary> | /// <summary> | ||||
/// 线程管理 | /// 线程管理 | ||||
/// </summary> | /// </summary> | ||||
public class ThreadManage:Singleton<ThreadManage> | |||||
public class ThreadManage : Singleton<ThreadManage> | |||||
{ | { | ||||
string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-"; | string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-"; | ||||
private static readonly object _lock = new object(); | private static readonly object _lock = new object(); | ||||
@@ -84,12 +84,27 @@ namespace BPASmartClient.Helper | |||||
/// </summary> | /// </summary> | ||||
/// <param name="action"></param> | /// <param name="action"></param> | ||||
/// <param name="key"></param> | /// <param name="key"></param> | ||||
public void Start(Action action, string key) | |||||
public void Start(Action action, string key, bool isRestart = true) | |||||
{ | { | ||||
MessageLog.GetInstance.Show($"线程 【{key}】启动"); | |||||
CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource()); | CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource()); | ||||
bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() => | bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() => | ||||
{ | { | ||||
if (action != null) action(); | |||||
try | |||||
{ | |||||
if (action != null) action(); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
MessageLog.GetInstance.Show($"线程 【{key}】运行发生异常"); | |||||
if (isRestart) | |||||
{ | |||||
MessageLog.GetInstance.Show($"线程 【{key}】正在重启"); | |||||
CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1); | |||||
Threads.TryRemove(guid + key, out Task item2); | |||||
Start(action, key, isRestart); | |||||
} | |||||
} | |||||
}), CancellationTokenSources[guid + key].Token).ContinueWith(new Action<Task, object>((t, o) => | }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action<Task, object>((t, o) => | ||||
{ | { | ||||
ThreadStatus(t, o.ToString()); | ThreadStatus(t, o.ToString()); | ||||
@@ -59,14 +59,15 @@ namespace BPASmartClient.MQTT | |||||
public async void Publish(string topic, string content) | public async void Publish(string topic, string content) | ||||
{ | { | ||||
await client.PublishAsync(topic, content); | |||||
if (client.IsConnected) | |||||
await client.PublishAsync(topic, content); | |||||
} | } | ||||
public async void Subscrib(params string[] topics) | public async void Subscrib(params string[] topics) | ||||
{ | { | ||||
foreach (var topic in topics) | foreach (var topic in topics) | ||||
{ | { | ||||
await client.SubscribeAsync(new MqttTopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce}); | |||||
await client.SubscribeAsync(new MqttTopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -1,7 +1,7 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||
<PropertyGroup> | <PropertyGroup> | ||||
<OutputType>Exe</OutputType> | |||||
<OutputType>WinExe</OutputType> | |||||
<TargetFramework>net6.0-windows</TargetFramework> | <TargetFramework>net6.0-windows</TargetFramework> | ||||
<Nullable>enable</Nullable> | <Nullable>enable</Nullable> | ||||
<UseWPF>true</UseWPF> | <UseWPF>true</UseWPF> | ||||
@@ -1,4 +1,5 @@ | |||||
using BPASmartClient.Business; | |||||
using BPA.Message; | |||||
using BPASmartClient.Business; | |||||
using BPASmartClient.EventBus; | using BPASmartClient.EventBus; | ||||
using BPASmartClient.Model; | using BPASmartClient.Model; | ||||
using BPASmartClient.Model.冰淇淋.Enum; | using BPASmartClient.Model.冰淇淋.Enum; | ||||
@@ -45,10 +46,13 @@ namespace BPASmartClient | |||||
private void Button_Click(object sender, RoutedEventArgs e) | private void Button_Click(object sender, RoutedEventArgs e) | ||||
{ | { | ||||
new Demo_MakeCoffeeEvent() { Id = 1 }.Publish(); | |||||
new DRCoffee_MakeCoffeeEvent() { Id = 1, DrinkCode= DrCoffeeDrinksCode.两杯意式浓缩 }.Publish(); | |||||
new DRCoffee_CoffeeCommCmdEvent() { Id = 1,CommCmd = DrCoffeeCommCmd.冲煮系统快速冲洗指令}.Publish(); | |||||
//new Demo_MakeCoffeeEvent() { Id = 1 }.Publish(); | |||||
//new DRCoffee_MakeCoffeeEvent() { Id = 1, DrinkCode= DrCoffeeDrinksCode.两杯意式浓缩 }.Publish(); | |||||
//new DRCoffee_CoffeeCommCmdEvent() { Id = 1,CommCmd = DrCoffeeCommCmd.冲煮系统快速冲洗指令}.Publish(); | |||||
MorkOrderPush morkOrderPush = new MorkOrderPush() { DeviceId=2}; | |||||
var temp=BPAPackage.Make(morkOrderPush, 12, BPA.Message.Enum.DeviceClientType.MORKS); | |||||
Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish("DOWN/MORKS/ORDER/12", temp.Serialize()); | |||||
} | } | ||||
} | } | ||||
} | } |