mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
Two mqtt client - Local and online
This commit is contained in:
parent
1cc12876f7
commit
5b4355d43c
@ -27,14 +27,16 @@ namespace MyCore.Controllers.Devices
|
|||||||
private LocationDatabaseService _LocationDatabaseService;
|
private LocationDatabaseService _LocationDatabaseService;
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private UserDatabaseService _UserDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
|
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider)
|
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._DeviceDatabaseService = DeviceDatabaseService;
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
||||||
this._ProviderDatabaseService = ProviderDatabaseService;
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
||||||
this._LocationDatabaseService = LocationDatabaseService;
|
this._LocationDatabaseService = LocationDatabaseService;
|
||||||
this._UserDatabaseService = UserDatabaseService;
|
this._UserDatabaseService = UserDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
|
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: Devices
|
// GET: Devices
|
||||||
@ -171,7 +173,7 @@ namespace MyCore.Controllers.Devices
|
|||||||
/// <param name="userId">User Id</param>
|
/// <param name="userId">User Id</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[HttpGet("zigbee2Mqtt/{userId}")]
|
[HttpGet("zigbee2Mqtt/{userId}")]
|
||||||
public ObjectResult GetDevicesFromZigbee2Mqtt(string userId)
|
public async Task<ObjectResult> GetDevicesFromZigbee2Mqtt(string userId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -184,6 +186,32 @@ namespace MyCore.Controllers.Devices
|
|||||||
// GET ALL LOCAL DEVICES
|
// GET ALL LOCAL DEVICES
|
||||||
var devices = MqttClientService.devices;
|
var devices = MqttClientService.devices;
|
||||||
|
|
||||||
|
// Test mqtt
|
||||||
|
await MqttClientService.PublishMessage("test", "zdz").ContinueWith(res => {
|
||||||
|
|
||||||
|
if (res.Status == TaskStatus.RanToCompletion)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Publish error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test online mqtt
|
||||||
|
await MqttClientOnlineService.PublishMessage("test", "zdz").ContinueWith(res => {
|
||||||
|
|
||||||
|
if (res.Status == TaskStatus.RanToCompletion)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Publish error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return new OkObjectResult(devices);
|
return new OkObjectResult(devices);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex)
|
catch (InvalidOperationException ex)
|
||||||
|
|||||||
@ -5,4 +5,10 @@
|
|||||||
public static BrokerHostSettings BrokerHostSettings;
|
public static BrokerHostSettings BrokerHostSettings;
|
||||||
public static ClientSettings ClientSettings;
|
public static ClientSettings ClientSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class AppSettingsOnlineProvider
|
||||||
|
{
|
||||||
|
public static BrokerOnlineHostSettings BrokerHostOnlineSettings;
|
||||||
|
public static ClientOnlineSettings ClientOnlineSettings;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,4 +12,14 @@ namespace Mqtt.Client.AspNetCore.Options
|
|||||||
ServiceProvider = serviceProvider;
|
ServiceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class AspCoreMqttOnlineClientOptionBuilder : MqttClientOptionsBuilder
|
||||||
|
{
|
||||||
|
public IServiceProvider ServiceOnlineProvider { get; }
|
||||||
|
|
||||||
|
public AspCoreMqttOnlineClientOptionBuilder(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
ServiceOnlineProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,4 +5,10 @@
|
|||||||
public string Host { set; get; }
|
public string Host { set; get; }
|
||||||
public int Port { set; get; }
|
public int Port { set; get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class BrokerOnlineHostSettings
|
||||||
|
{
|
||||||
|
public string Host { set; get; }
|
||||||
|
public int Port { set; get; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,4 +6,11 @@
|
|||||||
public string UserName { set; get; }
|
public string UserName { set; get; }
|
||||||
public string Password { set; get; }
|
public string Password { set; get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ClientOnlineSettings
|
||||||
|
{
|
||||||
|
public string Id { set; get; }
|
||||||
|
public string UserName { set; get; }
|
||||||
|
public string Password { set; get; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,4 +11,11 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
IMqttApplicationMessageReceivedHandler
|
IMqttApplicationMessageReceivedHandler
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IMqttOnlineClientService : IHostedService,
|
||||||
|
IMqttClientConnectedHandler,
|
||||||
|
IMqttClientDisconnectedHandler,
|
||||||
|
IMqttApplicationMessageReceivedHandler
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
135
MyCore/Extensions/MqttClientOnlineService.cs
Normal file
135
MyCore/Extensions/MqttClientOnlineService.cs
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
using MQTTnet;
|
||||||
|
using MQTTnet.Client;
|
||||||
|
using MQTTnet.Client.Connecting;
|
||||||
|
using MQTTnet.Client.Disconnecting;
|
||||||
|
using MQTTnet.Client.Options;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mqtt.Client.AspNetCore.Services
|
||||||
|
{
|
||||||
|
public class MqttClientOnlineService : IMqttOnlineClientService
|
||||||
|
{
|
||||||
|
private static IMqttClient mqttClient;
|
||||||
|
private IMqttClientOptions onlineOptions;
|
||||||
|
public static List<Zigbee2MqttDevice> devices = new List<Zigbee2MqttDevice>();
|
||||||
|
|
||||||
|
public MqttClientOnlineService(IMqttClientOptions options)
|
||||||
|
{
|
||||||
|
this.onlineOptions = options;
|
||||||
|
mqttClient = new MqttFactory().CreateMqttClient();
|
||||||
|
ConfigureMqttClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ConfigureMqttClient()
|
||||||
|
{
|
||||||
|
mqttClient.ConnectedHandler = this;
|
||||||
|
mqttClient.DisconnectedHandler = this;
|
||||||
|
mqttClient.ApplicationMessageReceivedHandler = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||||
|
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||||
|
var payload = "";
|
||||||
|
if (e.ApplicationMessage.Payload != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
|
||||||
|
payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||||
|
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
var topic = e.ApplicationMessage.Topic;
|
||||||
|
|
||||||
|
switch (topic)
|
||||||
|
{
|
||||||
|
case "zigbee2mqtt/bridge/config/devices":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var test = JsonConvert.DeserializeObject<List<Zigbee2MqttDevice>>(payload);
|
||||||
|
devices = test;
|
||||||
|
|
||||||
|
// TODO Update in DB, current devices state
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error during retrieving devices ! Exception: {ex}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//return new Task(null);
|
||||||
|
// TODO check what to do
|
||||||
|
//await PublishMessage("test", "teeest");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine("connected");
|
||||||
|
//await mqttClient.SubscribeAsync("hello/world");
|
||||||
|
await mqttClient.SubscribeAsync("#");
|
||||||
|
await PublishMessage("zigbee2mqtt/bridge/config/devices/get", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs eventArgs)
|
||||||
|
{
|
||||||
|
if (!mqttClient.IsConnected)
|
||||||
|
{
|
||||||
|
await mqttClient.ReconnectAsync();
|
||||||
|
}
|
||||||
|
//throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
await mqttClient.ConnectAsync(onlineOptions);
|
||||||
|
if (!mqttClient.IsConnected)
|
||||||
|
{
|
||||||
|
await mqttClient.ReconnectAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if(cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var disconnectOption = new MqttClientDisconnectOptions
|
||||||
|
{
|
||||||
|
ReasonCode = MqttClientDisconnectReason.NormalDisconnection,
|
||||||
|
ReasonString = "NormalDiconnection"
|
||||||
|
};
|
||||||
|
await mqttClient.DisconnectAsync(disconnectOption, cancellationToken);
|
||||||
|
}
|
||||||
|
await mqttClient.DisconnectAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task PublishMessage(string topic, string message)
|
||||||
|
{
|
||||||
|
var mqttMessage = new MqttApplicationMessageBuilder()
|
||||||
|
.WithTopic(topic)
|
||||||
|
.WithPayload(message)
|
||||||
|
.WithExactlyOnceQoS()
|
||||||
|
.WithRetainFlag()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
if (mqttClient.IsConnected)
|
||||||
|
await mqttClient.PublishAsync(mqttMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Zigbee2MqttDevice> GetDevices()
|
||||||
|
{
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
MyCore/Extensions/MqttClientOnlineServiceProvider.cs
Normal file
12
MyCore/Extensions/MqttClientOnlineServiceProvider.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace Mqtt.Client.AspNetCore.Services
|
||||||
|
{
|
||||||
|
public class MqttClientOnlineServiceProvider
|
||||||
|
{
|
||||||
|
public readonly IMqttOnlineClientService MqttOnlineClientService;
|
||||||
|
|
||||||
|
public MqttClientOnlineServiceProvider(IMqttOnlineClientService mqttOnlineClientService)
|
||||||
|
{
|
||||||
|
MqttOnlineClientService = mqttOnlineClientService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -23,6 +23,12 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
public MqttClientService(IMqttClientOptions options)
|
public MqttClientService(IMqttClientOptions options)
|
||||||
{
|
{
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this.options = new MqttClientOptionsBuilder()
|
||||||
|
.WithClientId("ApiService")
|
||||||
|
.WithTcpServer("192.168.31.140") // TODO replace by localhost
|
||||||
|
.WithCredentials("mqtt", "mqtt")
|
||||||
|
.WithCleanSession()
|
||||||
|
.Build();
|
||||||
mqttClient = new MqttFactory().CreateMqttClient();
|
mqttClient = new MqttFactory().CreateMqttClient();
|
||||||
ConfigureMqttClient();
|
ConfigureMqttClient();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,14 +26,32 @@ namespace MyCore.Service.Extensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddMqttClientOnlineHostedService(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddMqttClientOnlineServiceWithConfig(aspOnlineOptionBuilder =>
|
||||||
|
{
|
||||||
|
var clientSettings = AppSettingsOnlineProvider.ClientOnlineSettings;
|
||||||
|
var brokerHostSettings = AppSettingsOnlineProvider.BrokerHostOnlineSettings;
|
||||||
|
|
||||||
|
aspOnlineOptionBuilder
|
||||||
|
.WithCredentials(clientSettings.UserName, clientSettings.Password)
|
||||||
|
.WithClientId(clientSettings.Id)
|
||||||
|
.WithTcpServer(brokerHostSettings.Host, brokerHostSettings.Port);
|
||||||
|
});
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
|
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
|
||||||
{
|
{
|
||||||
services.AddSingleton<IMqttClientOptions>(serviceProvider =>
|
// No need as we implement options in service (localhost)
|
||||||
|
/*services.AddSingleton<IMqttClientOptions>(serviceProvider =>
|
||||||
{
|
{
|
||||||
var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
|
var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
|
||||||
configure(optionBuilder);
|
configure(optionBuilder);
|
||||||
return optionBuilder.Build();
|
return optionBuilder.Build();
|
||||||
});
|
});*/
|
||||||
services.AddSingleton<MqttClientService>();
|
services.AddSingleton<MqttClientService>();
|
||||||
services.AddSingleton<IHostedService>(serviceProvider =>
|
services.AddSingleton<IHostedService>(serviceProvider =>
|
||||||
{
|
{
|
||||||
@ -48,5 +66,27 @@ namespace MyCore.Service.Extensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IServiceCollection AddMqttClientOnlineServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttOnlineClientOptionBuilder> configure)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IMqttClientOptions>(serviceOnlineProvider =>
|
||||||
|
{
|
||||||
|
var optionBuilder = new AspCoreMqttOnlineClientOptionBuilder(serviceOnlineProvider);
|
||||||
|
configure(optionBuilder);
|
||||||
|
return optionBuilder.Build();
|
||||||
|
});
|
||||||
|
services.AddSingleton<MqttClientOnlineService>();
|
||||||
|
services.AddSingleton<IHostedService>(serviceProvider =>
|
||||||
|
{
|
||||||
|
return serviceProvider.GetService<MqttClientOnlineService>();
|
||||||
|
});
|
||||||
|
services.AddSingleton<MqttClientOnlineServiceProvider>(serviceProvider =>
|
||||||
|
{
|
||||||
|
var mqttOnlineClientService = serviceProvider.GetService<MqttClientOnlineService>();
|
||||||
|
var mqttOnlineClientServiceProvider = new MqttClientOnlineServiceProvider(mqttOnlineClientService);
|
||||||
|
return mqttOnlineClientServiceProvider;
|
||||||
|
});
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,6 +70,10 @@ namespace MyCore
|
|||||||
BrokerHostSettings brokerHostSettings = new BrokerHostSettings();
|
BrokerHostSettings brokerHostSettings = new BrokerHostSettings();
|
||||||
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
||||||
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
||||||
|
|
||||||
|
BrokerOnlineHostSettings brokerOnlineHostSettings = new BrokerOnlineHostSettings();
|
||||||
|
Configuration.GetSection(nameof(BrokerOnlineHostSettings)).Bind(brokerOnlineHostSettings);
|
||||||
|
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MapClientSettings()
|
private void MapClientSettings()
|
||||||
@ -77,6 +81,10 @@ namespace MyCore
|
|||||||
ClientSettings clientSettings = new ClientSettings();
|
ClientSettings clientSettings = new ClientSettings();
|
||||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
||||||
AppSettingsProvider.ClientSettings = clientSettings;
|
AppSettingsProvider.ClientSettings = clientSettings;
|
||||||
|
|
||||||
|
ClientOnlineSettings clientOnlineSettings = new ClientOnlineSettings();
|
||||||
|
Configuration.GetSection(nameof(ClientOnlineSettings)).Bind(clientOnlineSettings);
|
||||||
|
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
@ -196,6 +204,7 @@ namespace MyCore
|
|||||||
services.AddScoped<LocationDatabaseService>();
|
services.AddScoped<LocationDatabaseService>();
|
||||||
|
|
||||||
services.AddMqttClientHostedService();
|
services.AddMqttClientHostedService();
|
||||||
|
services.AddMqttClientOnlineHostedService();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
"AccessTokenExpiration": 86400,
|
"AccessTokenExpiration": 86400,
|
||||||
"RefreshTokenExpiration": 518400
|
"RefreshTokenExpiration": 518400
|
||||||
},
|
},
|
||||||
|
|
||||||
"SecuritySettings": {
|
"SecuritySettings": {
|
||||||
"Secret": "azertyuiopqsdfgh",
|
"Secret": "azertyuiopqsdfgh",
|
||||||
"Issuer": "MyCore",
|
"Issuer": "MyCore",
|
||||||
@ -23,6 +24,7 @@
|
|||||||
"IdType": "Name",
|
"IdType": "Name",
|
||||||
"TokenExpiryInHours": 2
|
"TokenExpiryInHours": 2
|
||||||
},
|
},
|
||||||
|
|
||||||
"BrokerHostSettings": {
|
"BrokerHostSettings": {
|
||||||
"Host": "192.168.31.140",
|
"Host": "192.168.31.140",
|
||||||
"Port": 1883
|
"Port": 1883
|
||||||
@ -32,5 +34,16 @@
|
|||||||
"Id": "5eb020f043ba8930506acbdd",
|
"Id": "5eb020f043ba8930506acbdd",
|
||||||
"UserName": "mqtt",
|
"UserName": "mqtt",
|
||||||
"Password": "mqtt"
|
"Password": "mqtt"
|
||||||
|
},
|
||||||
|
|
||||||
|
"BrokerOnlineHostSettings": {
|
||||||
|
"Host": "myhomie.be",
|
||||||
|
"Port": 1883
|
||||||
|
},
|
||||||
|
|
||||||
|
"ClientOnlineSettings": {
|
||||||
|
"Id": "5eb020f043ba8930506acbaa",
|
||||||
|
"UserName": "thomas",
|
||||||
|
"Password": "MyCore,1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user