Use the user variables in the screen matching feature

This commit is contained in:
Namhyeon Go 2024-10-03 22:27:26 +09:00
parent 1a8c93627a
commit c157116680
6 changed files with 114 additions and 13 deletions

View File

@ -75,7 +75,7 @@ namespace WelsonJS.Service
try
{
clamAvConenctionString = this.parent.GetSettingsFileHandler().Read("CLAMAV_HOST", "Service");
clamAvConenctionString = this.parent.GetSettingsHandler().Read("CLAMAV_HOST", "Service");
}
catch (Exception ex)
{

View File

@ -27,7 +27,7 @@ namespace WelsonJS.Service
try
{
serverAddress = _parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
serverAddress = _parent.GetSettingsHandler().Read("GRPC_HOST", "Service");
if (String.IsNullOrEmpty(serverAddress))
{
throw new Exception("The server address could not be empty.");

View File

@ -160,8 +160,8 @@ public class ScreenMatch
string screen_time_params;
try
{
screen_time_mode = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_MODE", "Service");
screen_time_params = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_PARAMS", "Service");
screen_time_mode = this.parent.GetSettingsHandler().Read("SCREEN_TIME_MODE", "Service");
screen_time_params = this.parent.GetSettingsHandler().Read("SCREEN_TIME_PARAMS", "Service");
}
catch (Exception ex)
{
@ -312,7 +312,21 @@ public class ScreenMatch
foreach (var file in files)
{
string filename = Path.GetFileName(file);
Bitmap bitmap = new Bitmap(file)
string realpath;
string altpath = parent.GetUserVariablesHandler().GetValue(filename);
if (!String.IsNullOrEmpty(altpath))
{
realpath = altpath;
parent.Log($"Use the alternative image: {realpath}");
}
else
{
realpath = file;
parent.Log($"Use the default image: {realpath}");
}
Bitmap bitmap = new Bitmap(realpath)
{
Tag = filename
};

View File

@ -45,7 +45,7 @@ namespace WelsonJS.Service
private string scriptFilePath;
private string scriptText;
private ScriptControl scriptControl;
private readonly string logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
private readonly string logFilePath = Path.Combine(Path.GetTempPath(), "welsonjs_service.log");
private readonly string appName = "WelsonJS";
private string[] args;
private bool disabledHeartbeat = false;
@ -53,7 +53,8 @@ namespace WelsonJS.Service
private bool disabledFileMonitor = false;
private ScreenMatch screenMatcher;
private FileEventMonitor fileEventMonitor;
private IniFile settingsFileHandler;
private IniFile settingsHandler;
private UserVariables userVariablesHandler;
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
@ -95,6 +96,10 @@ namespace WelsonJS.Service
}
}
// load the user variables
userVariablesHandler = new UserVariables(this);
userVariablesHandler.Load();
// set timers
timers = new List<Timer>();
@ -118,11 +123,11 @@ namespace WelsonJS.Service
{
try
{
settingsFileHandler = new IniFile(settingsFilePath);
settingsHandler = new IniFile(settingsFilePath);
}
catch (Exception)
{
settingsFileHandler = null;
settingsHandler = null;
}
}
else
@ -131,7 +136,7 @@ namespace WelsonJS.Service
}
// read configrations from settings.ini
if (settingsFileHandler != null)
if (settingsHandler != null)
{
string[] configNames = new string[]
{
@ -143,7 +148,7 @@ namespace WelsonJS.Service
{
try
{
if ("true" == GetSettingsFileHandler().Read(configName, "Service"))
if ("true" == GetSettingsHandler().Read(configName, "Service"))
{
switch (configName)
{
@ -211,9 +216,14 @@ namespace WelsonJS.Service
Log(appName + " Service Loaded");
}
public IniFile GetSettingsFileHandler()
public IniFile GetSettingsHandler()
{
return settingsFileHandler;
return settingsHandler;
}
public UserVariables GetUserVariablesHandler()
{
return userVariablesHandler;
}
internal void TestStartupAndStop()

View File

@ -0,0 +1,76 @@
using System.Collections.Generic;
using System;
using System.IO;
using System.ServiceProcess;
namespace WelsonJS.Service
{
public class UserVariables
{
private ServiceMain parent;
private Dictionary<string, string> userVariables;
private readonly string envFilePath = Path.Combine(Path.GetTempPath(), "welsonjs_default.env");
public UserVariables(ServiceBase parent)
{
this.parent = (ServiceMain)parent;
}
// Load user-defined variables from the temporary folder in .env format
public void Load()
{
if (File.Exists(envFilePath))
{
try
{
string fileContent = File.ReadAllText(envFilePath);
// Split based on new line characters
string[] keyValuePairs = fileContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
userVariables = new Dictionary<string, string>();
foreach (string pair in keyValuePairs)
{
// Split by the first occurrence of '='
int indexOfEquals = pair.IndexOf('=');
if (indexOfEquals != -1)
{
string key = pair.Substring(0, indexOfEquals).Trim();
string value = pair.Substring(indexOfEquals + 1).Trim();
// Remove surrounding quotes if present
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2); // Remove the first and last character
}
// Unescape double quotes in the value
value = value.Replace("\\\"", "\"");
userVariables[key] = value;
}
else
{
parent.Log($"Error parsing line: '{pair}'.");
}
}
}
catch (Exception ex)
{
parent.Log($"Error loading variable file: {ex.Message}");
userVariables = new Dictionary<string, string>();
}
}
else
{
userVariables = new Dictionary<string, string>();
}
}
public string GetValue(string name)
{
userVariables.TryGetValue(name, out string value);
return value;
}
}
}

View File

@ -203,6 +203,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenMatch.cs" />
<Compile Include="Model\ScreenMatchResult.cs" />
<Compile Include="UserVariables.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="MSScriptControl">