-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
public interface IApplicationService | ||
{ | ||
public void Restart(); | ||
public void Stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
using System.Text; | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Threading; | ||
using AvaloniaEdit.Utils; | ||
using log4net; | ||
using MQTTnet; | ||
using MQTTnet.Client; | ||
using MQTTnet.Protocol; | ||
using MQTTnet.Server; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Core.SDKs.Services.MQTT; | ||
|
||
public enum MqttMsgType | ||
{ | ||
重复启动, | ||
下载指定插件 | ||
} | ||
public class MqttManager | ||
{ | ||
|
||
private static readonly ILog Log = LogManager.GetLogger(nameof(MqttManager)); | ||
public static MqttServer Server; | ||
private static FileStream fileStream; | ||
public static async Task Init() | ||
{ | ||
|
||
var mqttFactory = new MqttFactory(); | ||
if (File.Exists($"{AppDomain.CurrentDomain.BaseDirectory}.port")) | ||
{ | ||
try | ||
{ | ||
File.Delete($"{AppDomain.CurrentDomain.BaseDirectory}.port"); | ||
} | ||
catch (Exception e) | ||
{ | ||
using (FileStream fs = new FileStream($"{AppDomain.CurrentDomain.BaseDirectory}.port", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
byte[] bt = new byte[fs.Length]; | ||
fs.Read(bt, 0, bt.Length); | ||
fs.Close(); | ||
var i = int.Parse(Encoding.UTF8.GetString(bt)); | ||
var options = new MqttClientOptionsBuilder() | ||
.WithTcpServer("localhost", i) // 指定MQTT代理服务器的地址和端口 | ||
.Build(); | ||
var mqttClient = mqttFactory.CreateMqttClient(); | ||
var mqttClientConnectResult = mqttClient.ConnectAsync(options).Result; | ||
if (mqttClientConnectResult.ResultCode == MqttClientConnectResultCode.Success) | ||
{ | ||
Log.Debug("MQTT连接成功"); | ||
var jObject = new JObject(); | ||
jObject.Add("type",(int)MqttMsgType.重复启动); | ||
mqttClient.PublishAsync( new MqttApplicationMessage { Topic = "test", Payload = Encoding.UTF8.GetBytes(jObject.ToString()),QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce }); | ||
} | ||
|
||
ServiceManager.Services.GetService<IApplicationService>().Stop(); | ||
return; | ||
} | ||
|
||
} | ||
|
||
} | ||
int nowPort = 6600; | ||
restart: | ||
var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder() | ||
.WithDefaultEndpoint().WithDefaultEndpointPort(nowPort).Build(); | ||
Server = mqttFactory.CreateMqttServer(mqttServerOptions); | ||
Server.ClientConnectedAsync+= Server_ClientConnectedAsync; | ||
Server.ClientDisconnectedAsync+= Server_ClientDisconnectedAsync; | ||
Server.InterceptingPublishAsync += Server_InterceptingPublishAsync; | ||
|
||
|
||
try | ||
{ | ||
await Server.StartAsync(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Server.ClientConnectedAsync -= Server_ClientConnectedAsync; | ||
Server.ClientDisconnectedAsync -= Server_ClientDisconnectedAsync; | ||
Server.InterceptingPublishAsync -= Server_InterceptingPublishAsync; | ||
nowPort++; | ||
Log.Debug($"MQTT启动失败,尝试启动端口{nowPort}"); | ||
goto restart; | ||
} | ||
|
||
|
||
fileStream = new FileStream($"{AppDomain.CurrentDomain.BaseDirectory}.port",FileMode.CreateNew); | ||
fileStream.Write(Encoding.UTF8.GetBytes(nowPort.ToString())); | ||
fileStream.Flush(); | ||
} | ||
|
||
private static async Task Server_InterceptingPublishAsync(InterceptingPublishEventArgs arg) | ||
{ | ||
var s = Encoding.UTF8.GetString(arg.ApplicationMessage.Payload); | ||
Log.Debug( $"Publish {arg.ApplicationMessage.Topic} {s}"); | ||
try | ||
{ | ||
var jObject = JObject.Parse(s); | ||
var jToken = jObject["type"]; | ||
var o = jToken.ToObject<int>(); | ||
switch ((MqttMsgType)o) | ||
{ | ||
case MqttMsgType.重复启动: | ||
{ | ||
Dispatcher.UIThread.InvokeAsync(() => | ||
{ | ||
if (Application.Current!.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.MainWindow!.Show(); | ||
desktop.MainWindow.WindowState = WindowState.Normal; | ||
ServiceManager.Services.GetService<IWindowTool>() | ||
.SetForegroundWindow(desktop.MainWindow.TryGetPlatformHandle().Handle); | ||
} | ||
}); | ||
|
||
break; | ||
} | ||
case MqttMsgType.下载指定插件: | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error("错误",e); | ||
} | ||
|
||
} | ||
|
||
private static async Task Server_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg) | ||
{ | ||
Log.Debug( $"Client {arg.ClientId} disconnected."); | ||
} | ||
|
||
private static async Task Server_ClientConnectedAsync(ClientConnectedEventArgs arg) | ||
{ | ||
Log.Debug( $"Client {arg.ClientId} connected."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters