From 7622c3972a09e2b4379dd57b07ed62df5d919355 Mon Sep 17 00:00:00 2001
From: NinjaPug <36635276+programmingPug@users.noreply.github.com>
Date: Sat, 29 Mar 2025 12:41:39 -0400
Subject: [PATCH] updates for starting service and publish package
---
ManageService.ps1 | 8 +-
PCPalConfigurator/ConfiguratorForm.cs | 2 +-
PCPalService/PCPalService.csproj | 1 +
PCPalService/PCPalService.csproj.user | 6 +
PCPalService/Program.cs | 14 +-
.../PublishProfiles/FolderProfile.pubxml | 17 ++
.../PublishProfiles/FolderProfile.pubxml.user | 8 +
PCPalService/Worker.cs | 165 ++++++++----------
8 files changed, 119 insertions(+), 102 deletions(-)
create mode 100644 PCPalService/PCPalService.csproj.user
create mode 100644 PCPalService/Properties/PublishProfiles/FolderProfile.pubxml
create mode 100644 PCPalService/Properties/PublishProfiles/FolderProfile.pubxml.user
diff --git a/ManageService.ps1 b/ManageService.ps1
index 317ccdd..18269f3 100644
--- a/ManageService.ps1
+++ b/ManageService.ps1
@@ -1,21 +1,21 @@
# Define the service name and path to the published service
-$ServiceName = "ESP32BackgroundService"
-$ServiceExePath = "$PSScriptRoot\ESP32BackgroundService.exe"
+$ServiceName = "PCPalService"
+$ServiceExePath = "$PSScriptRoot\PCPalService\bin\Release\net8.0\publish\win-x64\PCPalService.exe"
# Function to check if the service exists
function ServiceExists {
return Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
}
-# Install the service
function Install-Service {
if (ServiceExists) {
Write-Host "Service '$ServiceName' already exists."
return
}
Write-Host "Installing service '$ServiceName'..."
- sc.exe create $ServiceName binPath= "`"$ServiceExePath`"" start= auto
+ sc.exe create $ServiceName binPath= "`"$ServiceExePath`"" start= auto obj= "LocalSystem"
sc.exe failure $ServiceName reset= 0 actions= restart/5000
+ sc.exe description $ServiceName "PCPal Background Monitoring Service"
Start-Service -Name $ServiceName
Write-Host "Service installed and started successfully with auto-restart enabled."
}
diff --git a/PCPalConfigurator/ConfiguratorForm.cs b/PCPalConfigurator/ConfiguratorForm.cs
index 5b46d0f..c5f0e1b 100644
--- a/PCPalConfigurator/ConfiguratorForm.cs
+++ b/PCPalConfigurator/ConfiguratorForm.cs
@@ -37,7 +37,7 @@ namespace PCPalConfigurator
private static string GetConfigPath()
{
- string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PCPal");
+ string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "PCPal");
Directory.CreateDirectory(folder);
return Path.Combine(folder, "config.json");
}
diff --git a/PCPalService/PCPalService.csproj b/PCPalService/PCPalService.csproj
index 3365386..1fa2427 100644
--- a/PCPalService/PCPalService.csproj
+++ b/PCPalService/PCPalService.csproj
@@ -10,6 +10,7 @@
+
diff --git a/PCPalService/PCPalService.csproj.user b/PCPalService/PCPalService.csproj.user
new file mode 100644
index 0000000..b7f1fc5
--- /dev/null
+++ b/PCPalService/PCPalService.csproj.user
@@ -0,0 +1,6 @@
+
+
+
+ <_LastSelectedProfileId>C:\Users\ckoch\OneDrive\Documents\GitHub\PCPal\PCPalService\Properties\PublishProfiles\FolderProfile.pubxml
+
+
\ No newline at end of file
diff --git a/PCPalService/Program.cs b/PCPalService/Program.cs
index 4147b4e..613ddec 100644
--- a/PCPalService/Program.cs
+++ b/PCPalService/Program.cs
@@ -1,7 +1,10 @@
using PCPalService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
using System;
+using Microsoft.Extensions.Hosting.WindowsServices;
+
namespace PCPalService
{
@@ -13,10 +16,11 @@ namespace PCPalService
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
- Host.CreateDefaultBuilder(args)
- .ConfigureServices((hostContext, services) =>
- {
- services.AddHostedService();
- });
+ Host.CreateDefaultBuilder(args)
+ .UseWindowsService()
+ .ConfigureServices((hostContext, services) =>
+ {
+ services.AddHostedService();
+ });
}
}
diff --git a/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml b/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml
new file mode 100644
index 0000000..c6a1201
--- /dev/null
+++ b/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml
@@ -0,0 +1,17 @@
+
+
+
+
+ Release
+ Any CPU
+ bin\Release\net8.0\publish\win-x64\
+ FileSystem
+ <_TargetId>Folder
+ net8.0
+ win-x64
+ true
+ false
+ false
+ false
+
+
\ No newline at end of file
diff --git a/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml.user b/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml.user
new file mode 100644
index 0000000..296c820
--- /dev/null
+++ b/PCPalService/Properties/PublishProfiles/FolderProfile.pubxml.user
@@ -0,0 +1,8 @@
+
+
+
+
+ True|2025-03-29T16:38:44.4981820Z||;False|2025-03-29T12:38:09.2222007-04:00||;True|2025-03-29T12:24:00.0314544-04:00||;True|2025-03-29T12:02:53.1292190-04:00||;False|2025-03-29T11:58:39.8048030-04:00||;False|2025-03-29T11:58:17.4946697-04:00||;False|2025-03-29T11:57:29.9733195-04:00||;False|2025-03-29T11:56:43.0182350-04:00||;False|2025-03-29T11:56:07.6627870-04:00||;True|2025-03-28T17:00:47.0111533-04:00||;True|2025-03-28T16:59:59.8928039-04:00||;True|2025-03-28T16:58:17.3481844-04:00||;True|2025-03-28T16:57:41.5786839-04:00||;True|2025-03-28T16:55:58.0823642-04:00||;
+
+
+
\ No newline at end of file
diff --git a/PCPalService/Worker.cs b/PCPalService/Worker.cs
index 50b39d4..100c20e 100644
--- a/PCPalService/Worker.cs
+++ b/PCPalService/Worker.cs
@@ -1,14 +1,13 @@
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.IO.Ports;
-using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
-using LibreHardwareMonitor.Hardware;
using Newtonsoft.Json;
-using Microsoft.Extensions.Hosting;
-using Microsoft.Extensions.Logging;
+using LibreHardwareMonitor.Hardware;
namespace PCPalService
{
@@ -17,124 +16,129 @@ namespace PCPalService
private readonly ILogger _logger;
private SerialPort serialPort;
private Computer computer;
- private readonly string ConfigFile = GetConfigPath();
- private const string LogFile = "service_log.txt";
+
+ private const string AppDataFolder = "PCPal";
+ private const string ConfigFileName = "config.json";
public Worker(ILogger logger)
{
_logger = logger;
- computer = new Computer { IsCpuEnabled = true, IsMemoryEnabled = true };
- computer.Open();
- }
-
- private static string GetConfigPath()
- {
- string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PCPal");
- Directory.CreateDirectory(folder); // Ensure folder exists
- return Path.Combine(folder, "config.json");
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
- Log("ESP32 Background Service Starting...");
+ _logger.LogInformation("Service starting...");
+ computer = new Computer
+ {
+ IsCpuEnabled = true,
+ IsMemoryEnabled = true,
+ IsGpuEnabled = true,
+ IsMotherboardEnabled = true
+ };
+ computer.Open();
while (!stoppingToken.IsCancellationRequested)
{
try
{
- if (!File.Exists(ConfigFile))
+ string configPath = GetConfigPath();
+ if (!File.Exists(configPath))
{
- Log("Config file not found. Retrying in 5 seconds...");
+ _logger.LogWarning("Config file not found. Retrying in 5 seconds...");
await Task.Delay(5000, stoppingToken);
continue;
}
- var config = LoadConfig();
+ var config = LoadConfig(configPath);
+
if (serialPort == null || !serialPort.IsOpen)
{
- string portName = AutoDetectESP32();
- if (portName != null)
+ string port = AutoDetectESP32();
+ if (port != null)
{
- serialPort = new SerialPort(portName, 115200);
+ serialPort = new SerialPort(port, 115200);
serialPort.Open();
- Log($"Connected to ESP32-C3 on {portName}");
+ _logger.LogInformation($"Connected to ESP32 on {port}");
}
else
{
- Log("ESP32-C3 not found. Retrying in 5 seconds...");
+ _logger.LogWarning("ESP32 not found. Retrying in 5 seconds...");
await Task.Delay(5000, stoppingToken);
continue;
}
}
- string line1 = GetLCDContent(config.Line1Selection, config.Line1CustomText, config.Line1PostText);
- string line2 = GetLCDContent(config.Line2Selection, config.Line2CustomText, config.Line2PostText);
+ string line1 = GetSensorLine(config.Line1Selection, config.Line1CustomText, config.Line1PostText);
+ string line2 = GetSensorLine(config.Line2Selection, config.Line2CustomText, config.Line2PostText);
- SendCommand($"CMD:LCD,0,{line1}");
- SendCommand($"CMD:LCD,1,{line2}");
+ serialPort.WriteLine($"CMD:LCD,0,{line1}");
+ serialPort.WriteLine($"CMD:LCD,1,{line2}");
+ _logger.LogInformation("Data sent to LCD.");
+
+ await Task.Delay(5000, stoppingToken);
}
catch (Exception ex)
{
- Log($"Error in service loop: {ex.Message}");
+ _logger.LogError(ex, "Error in main loop");
+ await Task.Delay(5000, stoppingToken);
}
-
- await Task.Delay(5000, stoppingToken);
}
serialPort?.Close();
}
+ private string GetConfigPath()
+ {
+ string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), AppDataFolder);
+ Directory.CreateDirectory(folder);
+ return Path.Combine(folder, ConfigFileName);
+ }
+
+ private ConfigData LoadConfig(string path)
+ {
+ string json = File.ReadAllText(path);
+ return JsonConvert.DeserializeObject(json) ?? new ConfigData();
+ }
private string AutoDetectESP32()
{
foreach (string port in SerialPort.GetPortNames())
{
- if (IsESP32Device(port)) return port;
+ try
+ {
+ using (SerialPort test = new SerialPort(port, 115200))
+ {
+ test.Open();
+ test.WriteLine("CMD:GET_LCD_TYPE");
+ Thread.Sleep(500);
+ string response = test.ReadExisting();
+ if (response.Contains("LCD_TYPE:1602A"))
+ return port;
+ }
+ }
+ catch { }
}
return null;
}
- private bool IsESP32Device(string portName)
+ private string GetSensorLine(string selection, string prefix, string suffix)
{
- try
- {
- using (SerialPort testPort = new SerialPort(portName, 115200))
- {
- testPort.Open();
- testPort.WriteLine("CMD:GET_LCD_TYPE");
- Thread.Sleep(500);
- string response = testPort.ReadExisting();
- testPort.Close();
- return response.Contains("LCD_TYPE:1602A");
- }
- }
- catch { return false; }
- }
+ if (selection == "Custom Text")
+ return prefix;
- private void UpdateLCD()
- {
- ConfigData config = LoadConfig();
- string line1 = GetLCDContent(config.Line1Selection, config.Line1CustomText, config.Line1PostText);
- string line2 = GetLCDContent(config.Line2Selection, config.Line2CustomText, config.Line2PostText);
-
-
- SendCommand($"CMD:LCD,0,{line1}");
- SendCommand($"CMD:LCD,1,{line2}");
- }
-
- private string GetLCDContent(string selection, string prefix, string postfix)
- {
var parsed = ParseSensorSelection(selection);
if (parsed == null)
return "N/A";
string value = GetSensorValue(parsed.Value.hardwareType, parsed.Value.sensorName, parsed.Value.sensorType);
- return $"{prefix}{value}{postfix}";
+ return $"{prefix}{value}{suffix}";
}
-
private (HardwareType hardwareType, string sensorName, SensorType sensorType)? ParseSensorSelection(string input)
{
+ if (string.IsNullOrWhiteSpace(input))
+ return null;
+
try
{
var parts = input.Split(':', 2);
@@ -142,7 +146,6 @@ namespace PCPalService
var hardwareType = Enum.Parse(parts[0].Trim());
var sensorInfo = parts[1].Trim();
-
int idx = sensorInfo.LastIndexOf('(');
if (idx == -1) return null;
@@ -157,14 +160,15 @@ namespace PCPalService
return null;
}
}
+
private string GetSensorValue(HardwareType type, string name, SensorType sensorType)
{
- foreach (IHardware hardware in computer.Hardware)
+ foreach (var hardware in computer.Hardware)
{
if (hardware.HardwareType == type)
{
hardware.Update();
- foreach (ISensor sensor in hardware.Sensors)
+ foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType == sensorType && sensor.Name == name)
{
@@ -175,39 +179,16 @@ namespace PCPalService
}
return "N/A";
}
-
- private ConfigData LoadConfig()
- {
- if (File.Exists(ConfigFile))
- {
- string json = File.ReadAllText(ConfigFile);
- return JsonConvert.DeserializeObject(json) ?? new ConfigData();
- }
- return new ConfigData();
- }
-
- private void SendCommand(string command)
- {
- serialPort.WriteLine(command);
- Log($"Sent: {command}");
- }
-
- private void Log(string message)
- {
- string logEntry = $"{DateTime.Now}: {message}";
- File.AppendAllText(LogFile, logEntry + Environment.NewLine);
- _logger.LogInformation(message);
- }
}
- class ConfigData
+ public class ConfigData
{
public string Line1Selection { get; set; }
public string Line1CustomText { get; set; }
+ public string Line1PostText { get; set; }
public string Line2Selection { get; set; }
public string Line2CustomText { get; set; }
- public string Line1PostText { get; set; }
public string Line2PostText { get; set; }
-
+ public string ScreenType { get; set; }
}
-}
+}
\ No newline at end of file