Browse Source

[feat] transmit alarm by mqtt serivce

master
fajiao 2 years ago
parent
commit
06ab6a94b6
  1. 4
      JiLinApp.Biz/JiLinApp.Biz.csproj
  2. 5
      JiLinApp.Biz/Transmit/Service/AlarmMqttService.cs
  3. 5
      JiLinApp.Biz/Transmit/Service/AlarmZmqService.cs
  4. 5
      JiLinApp.Biz/Transmit/Service/Interfaces/IAlarmService.cs
  5. 14
      JiLinApp.Biz/TransmitAlarm/Common/AlarmServiceFactory.cs
  6. 28
      JiLinApp.Biz/TransmitAlarm/Common/Config.cs
  7. 2
      JiLinApp.Biz/TransmitAlarm/Entity/AlarmMessage.cs
  8. 2
      JiLinApp.Biz/TransmitAlarm/Entity/AlarmMessageHelper.cs
  9. 111
      JiLinApp.Biz/TransmitAlarm/Service/AlarmMqttService.cs
  10. 19
      JiLinApp.Biz/TransmitAlarm/Service/AlarmZmqService.cs
  11. 10
      JiLinApp.Biz/TransmitAlarm/Service/Interfaces/IAlarmService.cs
  12. 5
      JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs
  13. 2
      JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs
  14. 9
      JiLinApp.sln
  15. 13
      JiLinApp/Core/Config.cs
  16. 7
      JiLinApp/Core/Global.cs
  17. 6
      JiLinApp/JiLinApp.csproj
  18. 2
      JiLinApp/Pages/FenceServerManage/MainViewModel.cs
  19. 11
      JiLinApp/Pages/PtzManage/MainViewModel.cs
  20. 2
      JiLinApp/Pages/VibrateServerManage/MainViewModel.cs
  21. 10
      JiLinApp/config/config.json

4
JiLinApp.Biz/JiLinApp.Biz.csproj

@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="MQTTnet" Version="4.1.4.563" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JiLinApp.Docking\JiLinApp.Docking.csproj" /> <ProjectReference Include="..\JiLinApp.Docking\JiLinApp.Docking.csproj" />
</ItemGroup> </ItemGroup>

5
JiLinApp.Biz/Transmit/Service/AlarmMqttService.cs

@ -1,5 +0,0 @@
using JiLinApp.Biz.Transmit;
public class AlarmMqttService : IAlarmService
{
}

5
JiLinApp.Biz/Transmit/Service/AlarmZmqService.cs

@ -1,5 +0,0 @@
using JiLinApp.Biz.Transmit;
public class AlarmZmqService : IAlarmService
{
}

5
JiLinApp.Biz/Transmit/Service/Interfaces/IAlarmService.cs

@ -1,5 +0,0 @@
namespace JiLinApp.Biz.Transmit;
public interface IAlarmService
{
}

14
JiLinApp.Biz/TransmitAlarm/Common/AlarmServiceFactory.cs

@ -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;
}
}

28
JiLinApp.Biz/TransmitAlarm/Common/Config.cs

@ -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; }
}

2
JiLinApp.Biz/Transmit/Entity/AlarmMessage.cs → JiLinApp.Biz/TransmitAlarm/Entity/AlarmMessage.cs

@ -1,4 +1,4 @@
namespace JiLinApp.Biz.Transmit; namespace JiLinApp.Biz.TransmitAlarm;
public class AlarmMessage public class AlarmMessage
{ {

2
JiLinApp.Biz/Transmit/Entity/AlarmMessageHelper.cs → JiLinApp.Biz/TransmitAlarm/Entity/AlarmMessageHelper.cs

@ -2,7 +2,7 @@
using JiLinApp.Docking.FenceAlarm; using JiLinApp.Docking.FenceAlarm;
using JiLinApp.Docking.VibrateAlarm; using JiLinApp.Docking.VibrateAlarm;
namespace JiLinApp.Biz.Transmit; namespace JiLinApp.Biz.TransmitAlarm;
public static class AlarmMessageHelper public static class AlarmMessageHelper
{ {

111
JiLinApp.Biz/TransmitAlarm/Service/AlarmMqttService.cs

@ -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
}

19
JiLinApp.Biz/TransmitAlarm/Service/AlarmZmqService.cs

@ -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();
}
}

10
JiLinApp.Biz/TransmitAlarm/Service/Interfaces/IAlarmService.cs

@ -0,0 +1,10 @@
namespace JiLinApp.Biz.TransmitAlarm;
public interface IAlarmService
{
void Start();
void Close();
void SendAlarmMessage(AlarmMessage msg);
}

5
JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs

@ -1,5 +1,4 @@
using System.Data; using System.Timers;
using System.Timers;
using Timer = System.Timers.Timer; using Timer = System.Timers.Timer;
namespace JiLinApp.Docking.FenceAlarm; namespace JiLinApp.Docking.FenceAlarm;
@ -16,7 +15,7 @@ public class UdpManager
private List<UdpAlarmHostDevice> deviceList = new(); private List<UdpAlarmHostDevice> deviceList = new();
#endregion fields #endregion Fields
public UdpManager() public UdpManager()
{ {

2
JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs

@ -21,7 +21,7 @@ public class TcpManager
private List<ClientMessage> list { get; } = new(); private List<ClientMessage> list { get; } = new();
#endregion fields #endregion Fields
public TcpManager() public TcpManager()
{ {

9
JiLinApp.sln

@ -7,9 +7,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiLinApp", "JiLinApp\JiLinA
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EC.Util", "EC.Util\EC.Util.csproj", "{A41E47C9-1930-4FF0-955E-B91EC859D262}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EC.Util", "EC.Util\EC.Util.csproj", "{A41E47C9-1930-4FF0-955E-B91EC859D262}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiLinApp.Docking", "JiLinApp.Docking\JiLinApp.Docking.csproj", "{B664DFEE-E137-44AC-A766-95D384E3F70B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiLinApp.Docking", "JiLinApp.Docking\JiLinApp.Docking.csproj", "{B664DFEE-E137-44AC-A766-95D384E3F70B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiLinApp.Biz", "JiLinApp.Biz\JiLinApp.Biz.csproj", "{7CE63A50-C92C-4554-8E0C-F7BED355C1FD}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiLinApp.Biz", "JiLinApp.Biz\JiLinApp.Biz.csproj", "{7CE63A50-C92C-4554-8E0C-F7BED355C1FD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{97F952A2-D735-42D6-A21B-DCEA87C2D6C9}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

13
JiLinApp/Core/Config.cs

@ -1,4 +1,5 @@
using EC.Util.CameraSDK; using EC.Util.CameraSDK;
using JiLinApp.Biz.TransmitAlarm;
using System.Collections.Generic; using System.Collections.Generic;
namespace JiLinApp.Core; namespace JiLinApp.Core;
@ -7,6 +8,8 @@ public class AppConfig
{ {
public BaseConfig Base { get; set; } public BaseConfig Base { get; set; }
public TransmitAlarmConfig TransmitAlarm { get; set; }
public List<CameraInfo> CameraList { get; set; } public List<CameraInfo> CameraList { get; set; }
} }
@ -14,13 +17,3 @@ public class BaseConfig
{ {
public bool Console { get; set; } public bool Console { get; set; }
} }
public class MqttConfig
{
}
public class ZmqConfig
{
public string AlarmPubAddr { get; set; }
public string AlarmPubTopic { get; set; }
}

7
JiLinApp/Core/Global.cs

@ -1,4 +1,5 @@
using EC.Util.Common; using EC.Util.Common;
using JiLinApp.Biz.TransmitAlarm;
using JiLinApp.Docking.Alarm; using JiLinApp.Docking.Alarm;
using NewLife.Configuration; using NewLife.Configuration;
using System; using System;
@ -11,8 +12,11 @@ public static class Global
#region Fields #region Fields
private static JsonConfigProvider ConfigProvider { get; } private static JsonConfigProvider ConfigProvider { get; }
public static AppConfig AppConfig { get; } public static AppConfig AppConfig { get; }
public static IAlarmService AlarmService { get; }
#endregion Fields #endregion Fields
static Global() static Global()
@ -26,6 +30,9 @@ public static class Global
// 控制台 // 控制台
if (AppConfig.Base.Console) SystemUtil.AllocConsole(); if (AppConfig.Base.Console) SystemUtil.AllocConsole();
AlarmService = AlarmServiceFactory.CreateService(AppConfig.TransmitAlarm);
AlarmService?.Start();
} }
catch (Exception) catch (Exception)
{ {

6
JiLinApp/JiLinApp.csproj

@ -6,6 +6,12 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<AssemblyVersion></AssemblyVersion> <AssemblyVersion></AssemblyVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningLevel>6</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<WarningLevel>6</WarningLevel>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" /> <PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="MaterialDesignColors" Version="2.1.2"> <PackageReference Include="MaterialDesignColors" Version="2.1.2">

2
JiLinApp/Pages/FenceServerManage/MainViewModel.cs

@ -12,7 +12,7 @@ public class MainViewModel : ViewModelBase
private Main View { get; set; } private Main View { get; set; }
#endregion fields #endregion Fields
public MainViewModel(IRegionManager region, IEventAggregator ea) : base(region, ea) public MainViewModel(IRegionManager region, IEventAggregator ea) : base(region, ea)
{ {

11
JiLinApp/Pages/PtzManage/MainViewModel.cs

@ -1,4 +1,4 @@
using JiLinApp.Biz.Transmit; using JiLinApp.Biz.TransmitAlarm;
using JiLinApp.Core; using JiLinApp.Core;
using JiLinApp.Docking.FenceAlarm; using JiLinApp.Docking.FenceAlarm;
using JiLinApp.Docking.VibrateAlarm; using JiLinApp.Docking.VibrateAlarm;
@ -15,7 +15,9 @@ public class MainViewModel : ViewModelBase
private Main View { get; set; } private Main View { get; set; }
private AppConfig AppConfig { get; set; } private AppConfig AppConfig { get; }
private IAlarmService AlarmService { get; }
#endregion Fields #endregion Fields
@ -23,6 +25,7 @@ public class MainViewModel : ViewModelBase
{ {
// Bind global fields // Bind global fields
AppConfig = Global.AppConfig; AppConfig = Global.AppConfig;
AlarmService = Global.AlarmService;
// Bind invoke event // Bind invoke event
ea.GetEvent<ShakeTcpAlarmSendEvent>().Subscribe(ShakeTcpAlarmReact); ea.GetEvent<ShakeTcpAlarmSendEvent>().Subscribe(ShakeTcpAlarmReact);
@ -38,13 +41,13 @@ public class MainViewModel : ViewModelBase
internal void ShakeTcpAlarmReact(TcpAlarmHostMessage msg) internal void ShakeTcpAlarmReact(TcpAlarmHostMessage msg)
{ {
SendAlarm(msg.ToAlarmMessage()); AlarmService.SendAlarmMessage(msg.ToAlarmMessage());
View.GotoPreset(); View.GotoPreset();
} }
internal void FenceUdpAlarmReact(UdpAlarmHostMessage msg) internal void FenceUdpAlarmReact(UdpAlarmHostMessage msg)
{ {
SendAlarm(msg.ToAlarmMessage()); AlarmService.SendAlarmMessage(msg.ToAlarmMessage());
View.GotoPreset(); View.GotoPreset();
} }

2
JiLinApp/Pages/VibrateServerManage/MainViewModel.cs

@ -12,7 +12,7 @@ public class MainViewModel : ViewModelBase
private Main View { get; set; } private Main View { get; set; }
#endregion fields #endregion Fields
public MainViewModel(IRegionManager region, IEventAggregator ea) : base(region, ea) public MainViewModel(IRegionManager region, IEventAggregator ea) : base(region, ea)
{ {

10
JiLinApp/config/config.json

@ -2,6 +2,16 @@
"base": { "base": {
"console": false "console": false
}, },
"transmitAlarm": {
"type": "mqtt",
"mqtt": {
"port": "1883",
"username": "admin",
"password": "public",
"clientId": "alarmClient",
"transmitTopic": "alarm/transmit"
}
},
"cameraList": [ "cameraList": [
{ {
"ip": "192.168.1.65", "ip": "192.168.1.65",

Loading…
Cancel
Save