File watcher with keep-awake state binding

This commit is contained in:
Den Delimarsky 2021-04-20 08:31:18 -07:00
parent aa46bf6076
commit a0c5f8cc59
No known key found for this signature in database
GPG Key ID: E1BE1355085F0BCF
4 changed files with 140 additions and 11 deletions

View File

@ -49,6 +49,11 @@ namespace Espresso.Shell.Core
}
}
public static bool SetNormalKeepAwake()
{
return SetAwakeState(EXECUTION_STATE.ES_CONTINUOUS);
}
public static bool SetIndefiniteKeepAwake(bool keepDisplayOn = true)
{
if (keepDisplayOn)

View File

@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
</ItemGroup>

View File

@ -0,0 +1,42 @@
using Newtonsoft.Json;
namespace Espresso.Shell.Models
{
public class EspressoSettingsModel
{
[JsonProperty("properties")]
public Properties Properties { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
}
public class Properties
{
[JsonProperty("espresso_keep_display_on")]
public KeepDisplayOn KeepDisplayOn { get; set; }
[JsonProperty("espresso_mode")]
public int Mode { get; set; }
[JsonProperty("espresso_hours")]
public Hours Hours { get; set; }
[JsonProperty("espresso_minutes")]
public Minutes Minutes { get; set; }
}
public class KeepDisplayOn
{
public bool Value { get; set; }
}
public class Hours
{
public int Value { get; set; }
}
public class Minutes
{
public int Value { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using Espresso.Shell.Core;
using Espresso.Shell.Models;
using Newtonsoft.Json;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
@ -19,9 +21,7 @@ namespace Espresso.Shell
if (!instantiated)
{
Console.WriteLine(appName + " is already running! Exiting the application.");
Console.ReadKey();
Environment.Exit(1);
ForceExit(appName + " is already running! Exiting the application.", 1);
}
Console.WriteLine("Espresso - Computer Caffeination Engine");
@ -79,6 +79,13 @@ namespace Espresso.Shell
return rootCommand.InvokeAsync(args).Result;
}
private static void ForceExit(string message, int exitCode)
{
Console.WriteLine(message);
Console.ReadKey();
Environment.Exit(exitCode);
}
private static void HandleCommandLineArguments(string config, bool displayOn, long timeLimit)
{
Console.WriteLine($"The value for --display-on is: {displayOn}");
@ -89,14 +96,21 @@ namespace Espresso.Shell
// Configuration file is used, therefore we disregard any other command-line parameter
// and instead watch for changes in the file.
FileSystemWatcher watcher = new FileSystemWatcher
try
{
Path = Path.GetDirectoryName(config),
EnableRaisingEvents = true,
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(config)
};
watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange);
var watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(config),
EnableRaisingEvents = true,
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(config)
};
watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange);
}
catch (Exception ex)
{
ForceExit($"There was a problem with the configuration file. Make sure it exists.\n{ex.Message}", 1);
}
}
else
{
@ -138,7 +152,74 @@ namespace Espresso.Shell
private static void HandleEspressoConfigChange(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Reached test!");
Console.WriteLine("Detected a file change. Reacting...");
try
{
var settings = JsonConvert.DeserializeObject<EspressoSettingsModel>(File.ReadAllText(e.FullPath));
// If the settings were successfully processed, we need to set the right mode of operation.
// INDEFINITE = 0
// TIMED = 1
switch (settings.Properties.Mode)
{
case 0:
{
// Indefinite keep awake.
bool success = APIHelper.SetIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value);
if (success)
{
Console.WriteLine($"Currently in indefinite keep awake. Display always on: {settings.Properties.KeepDisplayOn.Value}");
}
else
{
Console.WriteLine("Could not set up the state to be indefinite keep awake.");
}
break;
}
case 1:
{
// Timed keep-awake.
long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60);
Console.WriteLine($"In timed keep-awake mode. Expecting to be awake for {computedTime} seconds.");
bool success = APIHelper.SetTimedKeepAwake(computedTime, settings.Properties.KeepDisplayOn.Value);
if (success)
{
Console.WriteLine($"Finished execution of timed keep-awake.");
ResetNormalPowerState();
}
else
{
Console.WriteLine("Could not set up the state to be timed keep awake.");
}
break;
}
default:
{
ForceExit("Could not select the right mode of operation. Existing...", 1);
break;
}
}
}
catch (Exception ex)
{
ForceExit($"There was a problem reading the configuration file.\n{ex.Message}", 1);
}
}
private static void ResetNormalPowerState()
{
bool success = APIHelper.SetNormalKeepAwake();
if (success)
{
Console.WriteLine("Returned to normal keep-awake state.");
}
else
{
Console.WriteLine("Could not return to normal keep-awake state.");
}
}
}
}