From e72d5ccd5777121e2a8f7f9f67222f09eaf07300 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Tue, 12 Dec 2023 17:55:43 +0000 Subject: [PATCH 01/32] replacing homeqassistant with MQTT --- API/MqttClientWrapper.cs | 74 ++++++++++++++ API/Teams.cs | 8 +- API/TokenStorage.cs | 57 ----------- MainWindow.xaml | 23 +++-- MainWindow.xaml.cs | 211 +++++++++++++++++++++++++++++++-------- TEAMS2HA.csproj | 6 +- 6 files changed, 269 insertions(+), 110 deletions(-) create mode 100644 API/MqttClientWrapper.cs delete mode 100644 API/TokenStorage.cs diff --git a/API/MqttClientWrapper.cs b/API/MqttClientWrapper.cs new file mode 100644 index 0000000..39be9aa --- /dev/null +++ b/API/MqttClientWrapper.cs @@ -0,0 +1,74 @@ +using MQTTnet; +using MQTTnet.Client; +using System; +using System.Threading.Tasks; + +namespace TEAMS2HA.API +{ + public class MqttClientWrapper + { + private IMqttClient _mqttClient; + private MqttClientOptions _mqttOptions; + public bool IsConnected => _mqttClient.IsConnected; + // Constructor for MqttClientWrapper class + // Takes in clientId, mqttBroker, username, and password as parameters + public MqttClientWrapper(string clientId, string mqttBroker, string username, string password) + { + // Create a new instance of MqttFactory + var factory = new MqttFactory(); + + // Create a new instance of MqttClient using the factory + _mqttClient = factory.CreateMqttClient(); + + // Build the MqttClientOptions using the MqttClientOptionsBuilder + _mqttOptions = new MqttClientOptionsBuilder() + .WithClientId(clientId) // Set the client ID + .WithTcpServer(mqttBroker) // Set the MQTT broker + .WithCredentials(username, password) // Set the username and password + .WithCleanSession() // Enable clean session + .Build(); // Build the options + } + + // This method is used to establish a connection to the MQTT broker asynchronously + public async Task ConnectAsync() + { + try + { + // Attempt to connect to the MQTT broker using the provided options + await _mqttClient.ConnectAsync(_mqttOptions); + } + catch (Exception ex) + { + // Handle any exceptions that occur during the connection process + // (e.g., connection failure) + } + } + + // Method to publish a message to a specified topic + public async Task PublishAsync(string topic, string payload) + { + // Create a new MQTT application message builder + var message = new MqttApplicationMessageBuilder() + .WithTopic(topic) // Set the topic of the message + .WithPayload(payload) // Set the payload of the message + .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce) // Set the quality of service level to at least once + .Build(); // Build the message + + // Publish the message using the MQTT client + await _mqttClient.PublishAsync(message); + } + + // Method to disconnect from the MQTT broker + public async Task DisconnectAsync() + { + // Check if the MQTT client is connected + if (_mqttClient.IsConnected) + { + // Disconnect from the MQTT broker + await _mqttClient.DisconnectAsync(); + } + } + + // Additional methods as needed + } +} \ No newline at end of file diff --git a/API/Teams.cs b/API/Teams.cs index 2d9dfbf..a8f5238 100644 --- a/API/Teams.cs +++ b/API/Teams.cs @@ -10,8 +10,14 @@ namespace TEAMS2HA.API { + public class WebSocketClient { + public event EventHandler TeamsUpdateReceived; + public class TeamsUpdateEventArgs : EventArgs + { + public MeetingUpdate MeetingUpdate { get; set; } + } #region Private Fields private AppSettings _appSettings; private readonly ClientWebSocket _clientWebSocket; @@ -228,7 +234,7 @@ private void OnMessageReceived(object sender, string message) { State.Instance.issharing = "Not Sharing"; } - + TeamsUpdateReceived?.Invoke(this, new TeamsUpdateEventArgs { MeetingUpdate = meetingUpdate }); // need to edit state class to add handraised, recording, and backgroundblur } } diff --git a/API/TokenStorage.cs b/API/TokenStorage.cs deleted file mode 100644 index 5658bea..0000000 --- a/API/TokenStorage.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Security.Cryptography; -using System.Text; - -namespace TEAMS2HA.API -{ - public static class TokenStorage - { - public static void SaveHomeassistantToken(string token) - { - SaveToken(token, "HomeassistantToken"); - } - - public static string GetHomeassistantToken() - { - return GetToken("HomeassistantToken"); - } - - public static void SaveTeamsToken(string token) - { - SaveToken(token, "TeamsToken"); - } - - public static string GetTeamsToken() - { - return GetToken("TeamsToken"); - } - - private static void SaveToken(string token, string settingKey) - { - if (!string.IsNullOrEmpty(token)) - { - byte[] encryptedToken = ProtectedData.Protect( - Encoding.UTF8.GetBytes(token), - null, - DataProtectionScope.CurrentUser); - Properties.Settings.Default[settingKey] = Convert.ToBase64String(encryptedToken); - Properties.Settings.Default.Save(); - } - } - - private static string GetToken(string settingKey) - { - string encryptedTokenBase64 = Properties.Settings.Default[settingKey] as string; - if (!string.IsNullOrEmpty(encryptedTokenBase64)) - { - byte[] encryptedToken = Convert.FromBase64String(encryptedTokenBase64); - byte[] decryptedToken = ProtectedData.Unprotect( - encryptedToken, - null, - DataProtectionScope.CurrentUser); - return Encoding.UTF8.GetString(decryptedToken); - } - return null; - } - } -} diff --git a/MainWindow.xaml b/MainWindow.xaml index 54efb2d..19187c9 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -34,6 +34,7 @@ + @@ -45,28 +46,32 @@ - - -