Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions PowerControlHub.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PowerControlHubApp", "PowerControlHubApp\PowerControlHubApp.csproj", "{9A5A950B-B42B-40DE-BDA7-299184BB23BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -85,6 +87,18 @@ Global
{31F2CD6A-B9E7-9FD4-3808-399C52A59619}.Release|ARM.Build.0 = Release|Any CPU
{31F2CD6A-B9E7-9FD4-3808-399C52A59619}.Release|ARM64.ActiveCfg = Release|Any CPU
{31F2CD6A-B9E7-9FD4-3808-399C52A59619}.Release|ARM64.Build.0 = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|ARM.ActiveCfg = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|ARM.Build.0 = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Debug|ARM64.Build.0 = Debug|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|Any CPU.Build.0 = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|ARM.ActiveCfg = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|ARM.Build.0 = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|ARM64.ActiveCfg = Release|Any CPU
{9A5A950B-B42B-40DE-BDA7-299184BB23BA}.Release|ARM64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 11 additions & 11 deletions PowerControlHub/Dht11SensorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,24 @@ class Dht11SensorHandler : public BaseSensor, public BroadcastLoggerSupport

void formatStatusJson(char* buffer, size_t size) override
{
// Validate output buffer
// Validate output buffer
if (!buffer || size == 0)
{
return;
}

char celsius[8];
char humidity[8];
char celsiusOffset[8];
char humidityOffset[8];
dtostrf(_celsius, 1, 1, celsius);
dtostrf(_humidity, 1, 1, humidity);
dtostrf(_temperatureOffset, 1, 1, celsiusOffset);
dtostrf(_humidityOffset, 1, 1, humidityOffset);
char celsius[16];
char humidity[16];
char celsiusOffset[16];
char humidityOffset[16];
SystemFunctions::safeJsonFloat(_celsius, celsius, sizeof(celsius), 1);
SystemFunctions::safeJsonFloat(_humidity, humidity, sizeof(humidity), 1);
SystemFunctions::safeJsonFloat(_temperatureOffset, celsiusOffset, sizeof(celsiusOffset), 1);
SystemFunctions::safeJsonFloat(_humidityOffset, humidityOffset,sizeof(humidityOffset), 1);

double dewPt = Environment::dewPoint(_celsius, _humidity);
char dewPointStr[8];
dtostrf(dewPt, 1, 1, dewPointStr);
char dewPointStr[16];
SystemFunctions::safeJsonFloat(static_cast<float>(dewPt), dewPointStr, sizeof(dewPointStr), 1);

char comfortBuf[24];
strncpy_P(comfortBuf, Environment::getComfortDescription(_celsius, _humidity, dewPt), sizeof(comfortBuf));
Expand Down
23 changes: 12 additions & 11 deletions PowerControlHub/GpsSensorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "BaseSensor.h"
#include "DateTimeManager.h"
#include "SensorCommandHandler.h"
#include "SystemFunctions.h"

constexpr unsigned long GpsCheckMs = 10;
constexpr unsigned long StatusUpdateMs = 1000UL;
Expand Down Expand Up @@ -422,17 +423,17 @@ class GpsSensorHandler : public BaseSensor, public BroadcastLoggerSupport
{
char lat[16];
char lon[16];
char alt[12];
char speed[12];
char course[12];
dtostrf(_latitude, 1, 6, lat);
dtostrf(_longitude, 1, 6, lon);
dtostrf(_altitude, 1, 2, alt);
dtostrf(_speedKmh, 1, 2, speed);
dtostrf(_courseDeg, 1, 2, course);

snprintf_P(buffer, size,
char alt[16];
char speed[16];
char course[16];

SystemFunctions::safeJsonFloat(static_cast<float>(_latitude), lat, sizeof(lat), 6);
SystemFunctions::safeJsonFloat(static_cast<float>(_longitude), lon, sizeof(lon), 6);
SystemFunctions::safeJsonFloat(static_cast<float>(_altitude), alt, sizeof(alt), 2);
SystemFunctions::safeJsonFloat(static_cast<float>(_speedKmh), speed, sizeof(speed), 2);
SystemFunctions::safeJsonFloat(static_cast<float>(_courseDeg), course, sizeof(course), 2);

snprintf_P(buffer, size,
PSTR("\"gps\":{\"lat\":%s,\"lon\":%s,\"alt\":%s,\"speed\":%s,\"course\":%s,\"sats\":%lu,\"valid\":%s}"),
lat, lon, alt, speed, course, _satellites, _hasValidFix ? "true" : "false");
}
Expand Down
15 changes: 15 additions & 0 deletions PowerControlHub/SystemFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,19 @@ void SystemFunctions::formatTimeParts(char* buffer, size_t bufferSize, const Tim
(unsigned)timeparts.hours,
(unsigned)timeparts.minutes,
(unsigned)timeparts.seconds);
}

void SystemFunctions::safeJsonFloat(float value, char* output, size_t outputSize, uint8_t decimals)
{
if (!output || outputSize == 0)
return;

if (isnan(value) || isinf(value))
{
strncpy(output, "null", outputSize);
output[outputSize - 1] = '\0';
return;
}

dtostrf(value, 1, decimals, output);
Comment thread
k3ldar marked this conversation as resolved.
}
14 changes: 14 additions & 0 deletions PowerControlHub/SystemFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ class SystemFunctions
*/
static void sanitizeJsonString(const char* input, char* output, size_t outputSize);

/**
* @brief Format a float as a JSON number, emitting null for non-finite values.
*
* IEEE 754 special values (NaN, +Inf, -Inf) are not valid JSON numbers.
* This helper emits the literal `null` for any non-finite value so that
* the output is always valid JSON.
*
* @param value The float value to format.
* @param output Destination buffer.
* @param outputSize Size of destination buffer including null terminator.
* @param decimals Number of decimal places (passed to dtostrf).
*/
static void safeJsonFloat(float value, char* output, size_t outputSize, uint8_t decimals = 2);

/**
* @brief Escape a string for safe embedding in HTML content.
*
Expand Down
15 changes: 15 additions & 0 deletions PowerControlHubApp/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PowerControlHubApp"
x:Class="PowerControlHubApp.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/AppColorsLight.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
66 changes: 66 additions & 0 deletions PowerControlHubApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Microsoft.Extensions.DependencyInjection;
using PowerControlHubApp.Services;

namespace PowerControlHubApp
{
public partial class App : Application
{
public App(ThemeService themeService)
{
InitializeComponent();
// Apply after InitializeComponent so Application.Resources is populated.
themeService.ApplySaved();
}

protected override Window CreateWindow(IActivationState? activationState)
{
var window = new Window(new AppShell());

#if WINDOWS
window.HandlerChanged += OnWindowHandlerChanged;
#endif

return window;
}

#if WINDOWS
private static void OnWindowHandlerChanged(object? sender, EventArgs e)
{
if (sender is not Window mauiWindow)
return;

// Detach — only needs to run once
mauiWindow.HandlerChanged -= OnWindowHandlerChanged;

if (mauiWindow.Handler?.PlatformView is not Microsoft.UI.Xaml.Window nativeWindow)
return;

var appWindow = nativeWindow.AppWindow;

// Restore saved position and size (stored in physical pixels)
int savedW = Preferences.Get("win_w", 0);
int savedH = Preferences.Get("win_h", 0);
int savedX = Preferences.Get("win_x", int.MinValue);
int savedY = Preferences.Get("win_y", int.MinValue);

if (savedW > 0 && savedH > 0)
appWindow.Resize(new Windows.Graphics.SizeInt32(savedW, savedH));

if (savedX != int.MinValue && savedY != int.MinValue)
appWindow.Move(new Windows.Graphics.PointInt32(savedX, savedY));

// Persist position/size whenever the window moves or is resized
appWindow.Changed += (aw, args) =>
{
if (!args.DidPositionChange && !args.DidSizeChange)
return;

Preferences.Set("win_x", aw.Position.X);
Preferences.Set("win_y", aw.Position.Y);
Preferences.Set("win_w", aw.Size.Width);
Preferences.Set("win_h", aw.Size.Height);
};
}
#endif
}
}
22 changes: 22 additions & 0 deletions PowerControlHubApp/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="PowerControlHubApp.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:PowerControlHubApp.Views"
Title="PowerControlHub"
Shell.NavBarIsVisible="False">

<ShellContent
x:Name="DashboardPage"
Title="Dashboard"
Route="DashboardPage"
ContentTemplate="{DataTemplate views:DashboardPage}" />

<ShellContent
x:Name="SettingsPage"
Title="Settings"
Route="SettingsPage"
ContentTemplate="{DataTemplate views:SettingsPage}" />

</Shell>
10 changes: 10 additions & 0 deletions PowerControlHubApp/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace PowerControlHubApp
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
43 changes: 43 additions & 0 deletions PowerControlHubApp/Converters/ValueConverters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Globalization;

namespace PowerControlHubApp.Converters;

/// <summary>Returns true when the integer value is greater than zero.</summary>
public class IntToBoolConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is int i && i > 0;

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
}

/// <summary>Returns true when the integer value equals zero (inverse of IntToBoolConverter).</summary>
public class IntToInverseBoolConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is not int i || i == 0;

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
}

/// <summary>Returns true when the string is non-empty.</summary>
public class StringToBoolConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is string s && !string.IsNullOrEmpty(s);

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
}

/// <summary>Returns a green colour for true (connected) and a red colour for false (disconnected).</summary>
public class BoolToStatusColorConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is true ? Color.FromArgb("#44cc44") : Color.FromArgb("#cc4444");

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
36 changes: 36 additions & 0 deletions PowerControlHubApp/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PowerControlHubApp.MainPage">

<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a submarine number ten" />

<Label
Text="Hello, World!"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />

<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>
24 changes: 24 additions & 0 deletions PowerControlHubApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace PowerControlHubApp
{
public partial class MainPage : ContentPage
{
int count = 0;

public MainPage()
{
InitializeComponent();
}

private void OnCounterClicked(object? sender, EventArgs e)
{
count++;

if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";

SemanticScreenReader.Announce(CounterBtn.Text);
}
}
}
Loading
Loading