fajiao
2 years ago
21 changed files with 233 additions and 39 deletions
@ -1,5 +0,0 @@ |
|||||
using JiLinApp.Biz.Transmit; |
|
||||
|
|
||||
public class AlarmMqttService : IAlarmService |
|
||||
{ |
|
||||
} |
|
@ -1,5 +0,0 @@ |
|||||
using JiLinApp.Biz.Transmit; |
|
||||
|
|
||||
public class AlarmZmqService : IAlarmService |
|
||||
{ |
|
||||
} |
|
@ -1,5 +0,0 @@ |
|||||
namespace JiLinApp.Biz.Transmit; |
|
||||
|
|
||||
public interface IAlarmService |
|
||||
{ |
|
||||
} |
|
@ -0,0 +1,14 @@ |
|||||
|
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
|
public class AlarmServiceFactory |
||||
|
{ |
||||
|
public static IAlarmService CreateService(TransmitAlarmConfig config) |
||||
|
{ |
||||
|
IAlarmService service = (config.Type) switch |
||||
|
{ |
||||
|
"mqtt" => new AlarmMqttService(config.Mqtt), |
||||
|
_ => throw new NotSupportedException(), |
||||
|
}; ; |
||||
|
return service; |
||||
|
} |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
|
public class TransmitAlarmConfig |
||||
|
{ |
||||
|
public string Type { get; set; } |
||||
|
|
||||
|
public MqttConfig Mqtt { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class MqttConfig |
||||
|
{ |
||||
|
public int Port { get; set; } |
||||
|
|
||||
|
public string UserName { get; set; } |
||||
|
|
||||
|
public string Password { get; set; } |
||||
|
|
||||
|
public string ClientId { get; set; } |
||||
|
|
||||
|
public string TransmitTopic { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class ZmqConfig |
||||
|
{ |
||||
|
public string AlarmPubAddr { get; set; } |
||||
|
|
||||
|
public string AlarmPubTopic { get; set; } |
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
namespace JiLinApp.Biz.Transmit; |
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
public class AlarmMessage |
public class AlarmMessage |
||||
{ |
{ |
@ -0,0 +1,111 @@ |
|||||
|
using EC.Util.Common; |
||||
|
using MQTTnet; |
||||
|
using MQTTnet.Client; |
||||
|
using MQTTnet.Protocol; |
||||
|
using MQTTnet.Server; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
|
public class AlarmMqttService : IAlarmService |
||||
|
{ |
||||
|
#region Fields
|
||||
|
|
||||
|
private MqttConfig Config { get; } |
||||
|
|
||||
|
private MqttServer Server { get; } |
||||
|
|
||||
|
private IMqttClient Client { get; } |
||||
|
|
||||
|
#endregion Fields
|
||||
|
|
||||
|
public AlarmMqttService(MqttConfig config) |
||||
|
{ |
||||
|
MqttFactory factory = new(); |
||||
|
MqttServerOptions options = new MqttServerOptionsBuilder() |
||||
|
.WithDefaultEndpoint() |
||||
|
.WithDefaultEndpointPort(config.Port) |
||||
|
.Build(); |
||||
|
MqttServer server = factory.CreateMqttServer(options); |
||||
|
server.ValidatingConnectionAsync += Server_ValidatingConnectionAsync; |
||||
|
server.ClientConnectedAsync += Server_ClientConnectedAsync; |
||||
|
server.ClientDisconnectedAsync += Server_ClientDisconnectedAsync; |
||||
|
server.ClientAcknowledgedPublishPacketAsync += Server_ClientAcknowledgedPublishPacketAsync; |
||||
|
IMqttClient client = factory.CreateMqttClient(); |
||||
|
|
||||
|
Config = config; |
||||
|
Server = server; |
||||
|
Client = client; |
||||
|
} |
||||
|
|
||||
|
~AlarmMqttService() |
||||
|
{ |
||||
|
Close(); |
||||
|
} |
||||
|
|
||||
|
#region Base
|
||||
|
|
||||
|
public void Start() |
||||
|
{ |
||||
|
Server.StartAsync().Wait(); |
||||
|
|
||||
|
MqttClientOptions mqttClientOptions = new MqttClientOptionsBuilder() |
||||
|
.WithTcpServer("127.0.0.1", Config.Port) |
||||
|
.WithClientId(Config.ClientId) |
||||
|
.WithCredentials(Config.UserName, Config.Password) |
||||
|
.Build(); |
||||
|
Client.ConnectAsync(mqttClientOptions, CancellationToken.None); |
||||
|
} |
||||
|
|
||||
|
public void Close() |
||||
|
{ |
||||
|
Client.DisconnectAsync().Wait(); |
||||
|
Server.StopAsync().Wait(); |
||||
|
} |
||||
|
|
||||
|
public void SendAlarmMessage(AlarmMessage msg) |
||||
|
{ |
||||
|
MqttApplicationMessage mqttMsg = new MqttApplicationMessageBuilder() |
||||
|
.WithTopic(Config.TransmitTopic) |
||||
|
.WithPayload(JsonUtil.ToJson(msg)) |
||||
|
.Build(); |
||||
|
Client.PublishAsync(mqttMsg, CancellationToken.None).Wait(); |
||||
|
} |
||||
|
|
||||
|
#endregion Base
|
||||
|
|
||||
|
#region Server Event
|
||||
|
|
||||
|
private Task Server_ValidatingConnectionAsync(ValidatingConnectionEventArgs arg) |
||||
|
{ |
||||
|
if (arg.ClientId.Length == 0) arg.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; |
||||
|
else if (arg.UserName != Config.UserName) arg.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; |
||||
|
else if (arg.Password != Config.Password) arg.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
private Task Server_ClientConnectedAsync(ClientConnectedEventArgs arg) |
||||
|
{ |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
private Task Server_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg) |
||||
|
{ |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
private Task Server_ClientAcknowledgedPublishPacketAsync(ClientAcknowledgedPublishPacketEventArgs arg) |
||||
|
{ |
||||
|
string clientId = arg.ClientId; |
||||
|
ushort packetIdentifier = arg.PublishPacket.PacketIdentifier; |
||||
|
string topic = arg.PublishPacket.Topic; |
||||
|
string msg = Encoding.UTF8.GetString(arg.PublishPacket.Payload); |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
#endregion Server Event
|
||||
|
|
||||
|
#region
|
||||
|
|
||||
|
#endregion
|
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
|
public class AlarmZmqService : IAlarmService |
||||
|
{ |
||||
|
public void Start() |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
} |
||||
|
|
||||
|
public void Close() |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
} |
||||
|
|
||||
|
public void SendAlarmMessage(AlarmMessage msg) |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
namespace JiLinApp.Biz.TransmitAlarm; |
||||
|
|
||||
|
public interface IAlarmService |
||||
|
{ |
||||
|
void Start(); |
||||
|
|
||||
|
void Close(); |
||||
|
|
||||
|
void SendAlarmMessage(AlarmMessage msg); |
||||
|
} |
Loading…
Reference in new issue