Skip to content

Commit

Permalink
File watcher with keep-awake state binding
Browse files Browse the repository at this point in the history
  • Loading branch information
dend committed Apr 20, 2021
1 parent aa46bf6 commit a0c5f8c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/modules/espresso/Espresso.Shell/Core/APIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private static bool SetAwakeState(EXECUTION_STATE state)
}
}

public static bool SetNormalKeepAwake()
{
return SetAwakeState(EXECUTION_STATE.ES_CONTINUOUS);
}

public static bool SetIndefiniteKeepAwake(bool keepDisplayOn = true)
{
if (keepDisplayOn)
Expand Down
1 change: 1 addition & 0 deletions src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}

}
103 changes: 92 additions & 11 deletions src/modules/espresso/Espresso.Shell/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Espresso.Shell.Core;
using Espresso.Shell.Models;
using Newtonsoft.Json;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
Expand All @@ -19,9 +21,7 @@ static int Main(string[] args)

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");
Expand Down Expand Up @@ -79,6 +79,13 @@ static int Main(string[] args)
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}");
Expand All @@ -89,14 +96,21 @@ private static void HandleCommandLineArguments(string config, bool displayOn, lo
// 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
{
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)
{
Path = Path.GetDirectoryName(config),
EnableRaisingEvents = true,
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(config)
};
watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange);
ForceExit($"There was a problem with the configuration file. Make sure it exists.\n{ex.Message}", 1);
}
}
else
{
Expand Down Expand Up @@ -138,7 +152,74 @@ private static void HandleCommandLineArguments(string config, bool displayOn, lo

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.");
}
}
}
}

0 comments on commit a0c5f8c

Please sign in to comment.