mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
143 lines
4.8 KiB
C#
143 lines
4.8 KiB
C#
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 MqttClientService : IMqttClientService
|
|
{
|
|
private static IMqttClient mqttClient;
|
|
private IMqttClientOptions options;
|
|
public static List<Zigbee2MqttDevice> devices = new List<Zigbee2MqttDevice>();
|
|
|
|
|
|
public MqttClientService(IMqttClientOptions 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();
|
|
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(options);
|
|
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;
|
|
}
|
|
}
|
|
}
|