From a0c5f8cc59022cdb9d299ff29ac64ea0c53f9d29 Mon Sep 17 00:00:00 2001
From: Den Delimarsky <1389609+dend@users.noreply.github.com>
Date: Tue, 20 Apr 2021 08:31:18 -0700
Subject: [PATCH] File watcher with keep-awake state binding
---
.../espresso/Espresso.Shell/Core/APIHelper.cs | 5 +
.../Espresso.Shell/Espresso.Shell.csproj | 1 +
.../Models/EspressoSettingsModel.cs | 42 +++++++
.../espresso/Espresso.Shell/Program.cs | 103 ++++++++++++++++--
4 files changed, 140 insertions(+), 11 deletions(-)
create mode 100644 src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs
diff --git a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs
index a8dd28a695..1b8931df37 100644
--- a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs
+++ b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs
@@ -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)
diff --git a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj
index 0b1ad4d993..fcef0fe5b7 100644
--- a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj
+++ b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj
@@ -6,6 +6,7 @@
+
diff --git a/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs b/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs
new file mode 100644
index 0000000000..8fd358861b
--- /dev/null
+++ b/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs
@@ -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; }
+ }
+
+}
diff --git a/src/modules/espresso/Espresso.Shell/Program.cs b/src/modules/espresso/Espresso.Shell/Program.cs
index 86aed38eb6..2f3790b33f 100644
--- a/src/modules/espresso/Espresso.Shell/Program.cs
+++ b/src/modules/espresso/Espresso.Shell/Program.cs
@@ -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(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.");
+ }
}
}
}