浏览代码

MQTT重连

master
pry 2 年前
父节点
当前提交
8fc0329f7f
共有 16 个文件被更改,包括 135 次插入95 次删除
  1. +2
    -2
      HBLConsole.Business/HBLConsole.Business.csproj
  2. +6
    -1
      HBLConsole.Business/MessageServerBase.cs
  3. +48
    -27
      HBLConsole.Communication/MqttHelper.cs
  4. +1
    -1
      HBLConsole.Factory/HBLConsole.Factory.csproj
  5. +2
    -2
      HBLConsole.GVL/HBLConsole.GVL.csproj
  6. +15
    -7
      HBLConsole.GVL/InternetInfo.cs
  7. +1
    -1
      HBLConsole.Interface/HBLConsole.Interface.csproj
  8. +4
    -1
      HBLConsole.MainConsole/HBLConsole.MainConsole.csproj
  9. +1
    -0
      HBLConsole.MainConsole/HeartbeatReport.cs
  10. +10
    -0
      HBLConsole.MainConsole/Main.cs
  11. +5
    -1
      HBLConsole.Model/HBLConsole.Model.csproj
  12. +1
    -1
      HBLConsole.Model/MorkOrderPushPar.cs
  13. +12
    -1
      HBLConsole.Model/OrderData.cs
  14. +6
    -4
      HBLConsole/App.xaml.cs
  15. +21
    -0
      HBLConsole/DialogWindow/ViewModel/ListDialogViewModel.cs
  16. +0
    -46
      HBLConsole/Service/InitService.cs

+ 2
- 2
HBLConsole.Business/HBLConsole.Business.csproj 查看文件

@@ -16,10 +16,10 @@

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
<Reference Include="BPA.Utility">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Utility.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Utility.dll</HintPath>
</Reference>
</ItemGroup>



+ 6
- 1
HBLConsole.Business/MessageServerBase.cs 查看文件

@@ -18,7 +18,12 @@ namespace HBLConsole.Business
if (orderInfo == null) return;
if (orderInfo is MorkOrderPush morkOrderpush)
{
ActionManagerment.GetInstance.Send("orderSend", morkOrderpush);
Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Add(new OrderData()
{
OrderStatus = ORDER_STATUS.WAIT,
IsSelected = true,
OrderPush = morkOrderpush
});
}
}



+ 48
- 27
HBLConsole.Communication/MqttHelper.cs 查看文件

@@ -37,33 +37,19 @@ namespace HBLConsole.Communication
/// </summary>
public Action Reconnection { get; set; }

Action UseDisconnectedAction;


public async void MqttInitAsync(string UserName, string pass, string IP, int port, string clientID)
{
p1:
options = new MqttClientOptionsBuilder().WithTcpServer(IP, port).WithClientId(clientID).WithCredentials(UserName, pass).Build();
client = new MqttFactory().CreateMqttClient();
client.UseDisconnectedHandler(async c =>
client.UseDisconnectedHandler(c =>
{
Thread.Sleep(2000);
while (!InternetInfo.GetInstance.NetworkConnectState)
{
Thread.Sleep(2000);
}
MessageLog.GetInstance.Show($"断开连接");
try
{
MessageLog.GetInstance.Show($"重连中");
await client.ConnectAsync(options);
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.ToString());
}
if (client.IsConnected)
if (UseDisconnectedAction == null)
{
MessageLog.GetInstance.Show("MQTT重连成功!");
if (Reconnection != null) Reconnection();
Reconnect();
UseDisconnectedAction();
}

}).UseApplicationMessageReceivedHandler(c =>
@@ -74,20 +60,55 @@ namespace HBLConsole.Communication
MessageLog.GetInstance.Show($"连接成功");
});


try
{
await client.ConnectAsync(options);
}
catch (Exception ex) { MessageLog.GetInstance.Show(ex.Message); }
if (!client.IsConnected)
catch (Exception ex)
{
Thread.Sleep(2000);
MessageLog.GetInstance.Show(ex.Message);
MessageLog.GetInstance.Show("mqtt连接失败!重连执行中");
goto p1;
}
MessageLog.GetInstance.Show("MQTT连接成功!");
if (ConnectOk != null) ConnectOk();

if (client.IsConnected)
{
MessageLog.GetInstance.Show("MQTT连接成功!");
if (ConnectOk != null) ConnectOk();
}

}

private void Reconnect()
{
UseDisconnectedAction = new Action(() =>
{
Thread.Sleep(2000);
while (!InternetInfo.GetInstance.NetworkConnectState)
{
Thread.Sleep(2000);
}
MessageLog.GetInstance.Show($"断开连接");
while (!client.IsConnected)
{
try
{
MessageLog.GetInstance.Show($"重连中");
client.ConnectAsync(options).Wait();
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.ToString());
}
Thread.Sleep(30000);
}

if (client.IsConnected)
{
MessageLog.GetInstance.Show("MQTT重连成功!");
if (Reconnection != null) Reconnection();
}
UseDisconnectedAction = null;
});
}

/// <summary>


+ 1
- 1
HBLConsole.Factory/HBLConsole.Factory.csproj 查看文件

@@ -12,7 +12,7 @@

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
</ItemGroup>



+ 2
- 2
HBLConsole.GVL/HBLConsole.GVL.csproj 查看文件

@@ -18,10 +18,10 @@

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
<Reference Include="BPA.Utility">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Utility.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Utility.dll</HintPath>
</Reference>
</ItemGroup>



+ 15
- 7
HBLConsole.GVL/InternetInfo.cs 查看文件

@@ -33,14 +33,22 @@ namespace HBLConsole.GVL

ConsulAddress = System.Configuration.ConfigurationManager.AppSettings["ConsulAddress"];
ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
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:MqttAddress"];
MqttPort = int.Parse(config["MQTT:MqttPort"]);
OrderStatusChange = config["API:robotstatuschange"];
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.ToString());

}

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:MqttAddress"];
MqttPort = int.Parse(config["MQTT:MqttPort"]);
OrderStatusChange = config["API:robotstatuschange"];
}

/// <summary>


+ 1
- 1
HBLConsole.Interface/HBLConsole.Interface.csproj 查看文件

@@ -10,7 +10,7 @@

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
</ItemGroup>



+ 4
- 1
HBLConsole.MainConsole/HBLConsole.MainConsole.csproj 查看文件

@@ -14,7 +14,10 @@

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
<Reference Include="BPA.Models">
<HintPath>D:\BPACommon_output\net5.0\BPA.Models.dll</HintPath>
</Reference>
</ItemGroup>



+ 1
- 0
HBLConsole.MainConsole/HeartbeatReport.cs 查看文件

@@ -14,6 +14,7 @@ using HBLConsole.GVL;
using BPA.Message;
using HBLConsole.Communication;
using BPA.Message.Enum;
using BPA.Models.Robot;
//using Communication.MQTT;
//using BPA.Message.Kafka;
//using Model.Enums;


+ 10
- 0
HBLConsole.MainConsole/Main.cs 查看文件

@@ -21,6 +21,16 @@ namespace HBLConsole.MainConsole

List<string> Topics = new List<string>();

public void DataInit()
{
Json<MorkOrderPushPar>.GetInstance.Read();
}

public void DataSave()
{
Json<MorkOrderPushPar>.GetInstance.Save();
}

public void BusinessInit()
{
Topics.Clear();


+ 5
- 1
HBLConsole.Model/HBLConsole.Model.csproj 查看文件

@@ -4,9 +4,13 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
</ItemGroup>

<ItemGroup>
<Reference Include="BPA.Message">
<HintPath>..\..\..\..\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
<HintPath>D:\BPACommon_output\net5.0\BPA.Message.dll</HintPath>
</Reference>
</ItemGroup>



HBLConsole/Model/MorkOrderPushPar.cs → HBLConsole.Model/MorkOrderPushPar.cs 查看文件

@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using BPA.Message;

namespace HBLConsole
namespace HBLConsole.Model
{
public class MorkOrderPushPar
{

HBLConsole/Model/OrderData.cs → HBLConsole.Model/OrderData.cs 查看文件

@@ -8,7 +8,7 @@ using BPA.Message.Enum;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;

namespace HBLConsole
namespace HBLConsole.Model
{
public class OrderData : ObservableObject
{
@@ -21,5 +21,16 @@ namespace HBLConsole
public ORDER_STATUS OrderStatus { get { return _mOrderStatus; } set { _mOrderStatus = value; OnPropertyChanged(); } }
private ORDER_STATUS _mOrderStatus;

public string StartDate { get { return _mStartDate; } set { _mStartDate = value; OnPropertyChanged(); } }
private string _mStartDate;

public string EndDate { get { return _mEndDate; } set { _mEndDate = value; OnPropertyChanged(); } }
private string _mEndDate;

public string CompleteDate { get { return _mCompleteDate; } set { _mCompleteDate = value; OnPropertyChanged(); } }
private string _mCompleteDate;



}
}

+ 6
- 4
HBLConsole/App.xaml.cs 查看文件

@@ -25,10 +25,12 @@ namespace HBLConsole
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//listDialogView = new ListDialogView();
//listDialogView.ShowDialog();
SystemHelper.GetInstance.AutoStart(false);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
InitService.GetInstance.Register();
InitService.GetInstance.DataInit();
//InitService.GetInstance.Register();
MainConsole.Main.GetInstance.DataInit();
mainView.Show();
if (Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Count > 0)
{
@@ -40,14 +42,14 @@ namespace HBLConsole

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
InitService.GetInstance.DataSave();
MainConsole.Main.GetInstance.DataSave();
Process.Start($"{AppDomain.CurrentDomain.BaseDirectory}{AppDomain.CurrentDomain.FriendlyName}.exe");
}

protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
InitService.GetInstance.DataSave();
MainConsole.Main.GetInstance.DataSave();
}
}
}

+ 21
- 0
HBLConsole/DialogWindow/ViewModel/ListDialogViewModel.cs 查看文件

@@ -23,6 +23,27 @@ namespace HBLConsole.DialogWindow.ViewModel
public ListDialogViewModel()
{

//for (int i = 0; i < 10; i++)
//{
// Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Add(new OrderData()
// {
// IsSelected = true,
// OrderStatus = BPA.Message.Enum.ORDER_STATUS.WAIT,
// OrderPush = new BPA.Message.MorkOrderPush()
// {
// GoodsName = i.ToString(),
// RecipeId = i,
// SortNum = i,
// SuborderId = i.ToString(),
// GoodBatchings = new List<BPA.Message.GoodBatchings>()
// }
// });
//}
//Json<MorkOrderPushPar>.GetInstance.Save();




CloseCommand = new RelayCommand(() =>
{
Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Clear();


+ 0
- 46
HBLConsole/Service/InitService.cs 查看文件

@@ -1,46 +0,0 @@
using BPA.Message;
using BPA.Message.Enum;
using HBLConsole.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HBLConsole
{
public class InitService
{

private volatile static InitService _Instance;
public static InitService GetInstance => _Instance ?? (_Instance = new InitService());
private InitService() { }

public void DataInit()
{
Json<MorkOrderPushPar>.GetInstance.Read();
}

public void DataSave()
{
Json<MorkOrderPushPar>.GetInstance.Save();
}

public void Register()
{
ActionManagerment.GetInstance.Register(new Action<object>((o) =>
{
if (o is MorkOrderPush morkOrderPush)
{
Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Add(new OrderData()
{
OrderStatus = ORDER_STATUS.WAIT,
IsSelected = true,
OrderPush = morkOrderPush
});
}
}), "orderSend");
}

}
}

正在加载...
取消
保存