-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
122 lines (114 loc) · 4.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Diagnostics;
using HueLightDimmer.Models;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Reflection;
namespace HueLightDimmer
{
internal class Program
{
static async Task Main(string[] args)
{
var config = await Config.FromConfigFile();
if (args.Contains("a"))
{
HideConsole();
}
else
{
AskForAutostart();
}
bool running = false;
var savedLightData = new Dictionary<string, HueLightingData>();
while (true)
{
var processFound = Process.GetProcesses().Any(x => x.ProcessName.Contains(config.ProcessName, StringComparison.OrdinalIgnoreCase));
if (!processFound && running)
{
if (config.RevertOnStop)
{
try
{
await HueCommunicationHelper.ApplyCustomLightingAsync(config, savedLightData);
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to revert light changes");
}
}
running = false;
}
else if (processFound && !running)
{
try
{
var objectData = await HueCommunicationHelper.GetHueObjectsAsync(config);
savedLightData = objectData.ToDictionary(k => k.Key, v => v.Value.GetLightingData());
await HueCommunicationHelper.ApplyProcessRunningLightingAsync(config);
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to apply light changes");
}
running = true;
}
#if DEBUG
Logger.Debug("Application is running: " + running);
#endif
await Task.Delay(1_000 * config.UpdateRate);
}
}
#region
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private static void HideConsole()
{
try
{
var cWin = GetConsoleWindow();
ShowWindow(cWin, 0);
}
catch (Exception ex)
{
Logger.Error(ex, "Unable to hide console window");
}
}
private static void AskForAutostart()
{
try
{
Logger.Debug("Grabbing registy keys to check for autostart...");
var registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
var current = registryKey!.OpenSubKey("HueLightDimmer");
Logger.Info("Grabbed registy keys");
if (registryKey!.GetValue("HueLightDimmer") is null)
{
var res = Utils.AskQuestionBool("Program is currently not starting automatically, enable this feature?");
if (res)
{
Logger.Debug("Seting application to start automatically");
registryKey!.SetValue("HueLightDimmer", $"{Assembly.GetExecutingAssembly().Location} /a");
Logger.Info("Set application to not start automatically");
}
}
else
{
var res = Utils.AskQuestionBool("Program is currently starting automatically, disable this feature?");
if (res)
{
Logger.Debug("Setting application to not start automatically");
registryKey.DeleteValue("HueLightDimmer");
Logger.Info("Set application to start automatically, to undo this simply manually start this program or remove the autostart via taskmanager");
}
}
}
catch (Exception ex)
{
Logger.Error(ex, "Unable to work with registy to check autostartup");
}
}
#endregion
}
}