From 8c04096511aa0e27628b72773895678f8199513e Mon Sep 17 00:00:00 2001
From: morte <202109169@post.au.dk>
Date: Tue, 1 Apr 2025 15:54:22 +0200
Subject: [PATCH] a start

---
 BeSafePlus/BeSafePlus.csproj             |  1 +
 BeSafePlus/Clients/RedCapClient.cs       |  2 +-
 BeSafePlus/Sensors/SensorEventHandler.cs |  3 +-
 BeSafePlus/TestingTools/MQTTLog.cs       | 54 ++++++++++++++++++++++++
 BeSafePlus/TestingTools/TestingTools.cs  |  2 +-
 5 files changed, 58 insertions(+), 4 deletions(-)
 create mode 100644 BeSafePlus/TestingTools/MQTTLog.cs

diff --git a/BeSafePlus/BeSafePlus.csproj b/BeSafePlus/BeSafePlus.csproj
index 3153c9e..7e1e74e 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 36cfd57..97399ee 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 9761678..fa51ae8 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 0000000..6857543
--- /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 c78a7e4..a9a7641 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
     {
-- 
GitLab