diff --git a/BeSafePlus/BeSafePlus.csproj b/BeSafePlus/BeSafePlus.csproj index 3153c9edf02424f18dda843c65654f2f4c724a02..7e1e74ecbd13360564c96189bf93a0eaea40f6d7 100644 --- a/BeSafePlus/BeSafePlus.csproj +++ b/BeSafePlus/BeSafePlus.csproj @@ -133,6 +133,7 @@ <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" /> + <PackageReference Include="MQTTnet" Version="5.0.1.1416" /> <PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="3.116.1" /> <PackageReference Include="SkiaSharp.Views.Maui.Controls.Compatibility" Version="2.88.9" /> <PackageReference Include="sqlite-net-pcl" Version="1.9.172" /> diff --git a/BeSafePlus/Clients/RedCapClient.cs b/BeSafePlus/Clients/RedCapClient.cs index 36cfd572cb60fda3c5aa477c7821b5e4f87a2a5c..97399eea6f48808166c0afdb02e23b7a685bf065 100644 --- a/BeSafePlus/Clients/RedCapClient.cs +++ b/BeSafePlus/Clients/RedCapClient.cs @@ -49,7 +49,7 @@ namespace BeSafePlus.Clients var report = JsonSerializer.Serialize<RedCapEntry>(redCapEntry, SerializerOptions); var content = new StringContent(report, Encoding.UTF8, "application/json"); - TestingTools.TestingTools.LogData(report, "beSafeActivityReport", "json", true); + TestingTools.LogData(report, "beSafeActivityReport", "json", true); var request = new HttpRequestMessage(HttpMethod.Post, endpoint) { diff --git a/BeSafePlus/Sensors/SensorEventHandler.cs b/BeSafePlus/Sensors/SensorEventHandler.cs index 9761678c555a6d1df83d6453469bf19c9e038a69..fa51ae8f579ea7fdcb39d9bc02b73aefd157062c 100644 --- a/BeSafePlus/Sensors/SensorEventHandler.cs +++ b/BeSafePlus/Sensors/SensorEventHandler.cs @@ -8,7 +8,6 @@ using BeSafePlus.Database; using System.Diagnostics; using BeSafePlus.Sensors; using SkiaSharp; -using BeSafePlus.TestingTools; namespace BeSafePlus.Sensors { @@ -201,7 +200,7 @@ namespace BeSafePlus.Sensors _changeInActivityLevel = false; } - TestingTools.TestingTools.LogData(acceleration, "acceleration_log", "csv"); + TestingTools.LogData(acceleration, "acceleration_log", "csv"); } private void SetThresholds() diff --git a/BeSafePlus/TestingTools/MQTTLog.cs b/BeSafePlus/TestingTools/MQTTLog.cs new file mode 100644 index 0000000000000000000000000000000000000000..6857543ed37496d93cb14776870a566ade317efa --- /dev/null +++ b/BeSafePlus/TestingTools/MQTTLog.cs @@ -0,0 +1,54 @@ +using MQTTnet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BeSafePlus +{ + public class MQTTLog + { + private const string DefaultTopic = "besafeau/flare/log"; + public IMqttClient? _mqttClient; + public async Task Setup() + { + var factory = new MqttClientFactory(); + _mqttClient = factory.CreateMqttClient(); + var options = new MqttClientOptionsBuilder() + .WithTcpServer("broker.hivemq.com") + .Build(); + try + { + await _mqttClient.ConnectAsync(options); + Console.WriteLine("Connected to MQTT broker in background."); + } + catch (Exception ex) + { + Console.WriteLine($"Error connecting to MQTT broker in background: {ex.Message}"); + } + } + public async Task DisconnectAsync() + { + if (_mqttClient != null && _mqttClient.IsConnected) + { + await _mqttClient.DisconnectAsync(); + Console.WriteLine("Disconnected from MQTT broker in background."); + } + } + public async Task PublishMessageAsync(string payload, string topic = DefaultTopic) + { + if (_mqttClient == null || !_mqttClient.IsConnected) + { + Console.WriteLine("MQTT client is not connected in background."); + return; + } + var message = new MqttApplicationMessageBuilder() + .WithTopic(topic) + .WithPayload(payload) + .Build(); + await _mqttClient.PublishAsync(message); + Console.WriteLine($"Published message in background to topic {topic}: {payload}"); + } + } +} diff --git a/BeSafePlus/TestingTools/TestingTools.cs b/BeSafePlus/TestingTools/TestingTools.cs index c78a7e4a8c8925f54a10ec9902377d8c5af3109b..a9a7641a68c6555ea679bc218f4ab1f5898248d2 100644 --- a/BeSafePlus/TestingTools/TestingTools.cs +++ b/BeSafePlus/TestingTools/TestingTools.cs @@ -5,7 +5,7 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; -namespace BeSafePlus.TestingTools +namespace BeSafePlus { public static class TestingTools {