add WOL to steps, fix saving configs

This commit is contained in:
Vincent Maas committed 2021-02-14 15:17:30 +01:00
1 parent fd51bcca99
commit 7f6175f8ef
5 files changed
+175 -74

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@
<ProductName>ColorControl</ProductName>
<PublisherName>Maassoft</PublisherName>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.5.5.0</ApplicationVersion>
<ApplicationVersion>1.6.0.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
+64 -29
View File
@@ -42,10 +42,15 @@ namespace ColorControl
private List<LgPreset> _remoteControlButtons;
private JavaScriptSerializer _JsonDeserializer = new JavaScriptSerializer();
private Dictionary<string, Action> _invokableActions = new Dictionary<string, Action>();
public LgService(string dataDir, bool allowPowerOn)
{
_dataDir = dataDir;
_allowPowerOn = allowPowerOn;
_invokableActions.Add("WOL", new Action(WakeSelectedDevice));
LoadConfig();
LoadRemoteControlButtons();
@@ -62,6 +67,11 @@ namespace ColorControl
}
~LgService()
{
GlobalSave();
}
public void GlobalSave()
{
SaveConfig();
SaveRemoteControlButtons();
@@ -105,6 +115,11 @@ namespace ColorControl
return _remoteControlButtons;
}
public Dictionary<string, Action> GetInvokableActions()
{
return _invokableActions;
}
private List<LgPreset> GenerateDefaultRemoteControlButtons()
{
var list = new List<LgPreset>();
@@ -151,13 +166,13 @@ namespace ColorControl
return preset;
}
public void SaveConfig()
private void SaveConfig()
{
var json = JsonConvert.SerializeObject(Config);
File.WriteAllText(_configFilename, json);
}
public void SaveRemoteControlButtons()
private void SaveRemoteControlButtons()
{
var json = JsonConvert.SerializeObject(_remoteControlButtons);
File.WriteAllText(_rcButtonsFilename, json);
@@ -244,6 +259,17 @@ namespace ColorControl
{
var hasApp = !string.IsNullOrEmpty(preset.appId);
var hasWOL = preset.steps.Any(s => s.Equals("WOL", StringComparison.OrdinalIgnoreCase));
if (hasWOL)
{
var connected = await WakeAndConnectToSelectedDevice(0);
if (!connected)
{
return false;
}
}
for (var tries = 0; tries <= 1; tries++)
{
if (!await Connected(reconnect || tries == 1))
@@ -317,13 +343,19 @@ namespace ColorControl
foreach (var step in preset.steps)
{
var keySpec = step.Split(':');
var key = keySpec[0].ToUpper();
if (_invokableActions.ContainsKey(key))
{
continue;
}
if (keySpec.Length == 2)
{
SendKey(mouse, keySpec[0], int.Parse(keySpec[1]));
SendKey(mouse, key, int.Parse(keySpec[1]));
}
else
{
SendKey(mouse, keySpec[0]);
SendKey(mouse, key);
}
}
}
@@ -383,9 +415,14 @@ namespace ColorControl
return true;
}
internal void WakeSelectedDevice(string macAddress = null)
internal void WakeSelectedDevice()
{
macAddress = macAddress == null ? SelectedDevice?.MacAddress : macAddress;
WakeSelectedDevice(SelectedDevice?.MacAddress ?? Config.PreferredMacAddress);
}
internal void WakeSelectedDevice(string macAddress)
{
macAddress = string.IsNullOrEmpty(macAddress) ? SelectedDevice?.MacAddress : macAddress;
if (macAddress != null)
{
WOL.WakeFunction(macAddress, !Config.UseAlternateWol);
@@ -393,7 +430,7 @@ namespace ColorControl
}
else
{
Logger.Debug("Cannot wake device: no device has been selected");
Logger.Debug("Cannot wake device: no device has been selected or there is no preferred MAC-address stored in the configuration");
}
}
@@ -405,31 +442,29 @@ namespace ColorControl
}
}
private void WakeAndConnectToSelectedDevice(int wakeDelay = 5000, int connectDelay = 1000)
private async Task<bool> WakeAndConnectToSelectedDevice(int wakeDelay = 5000, int connectDelay = 1000)
{
Task.Run(async () =>
try
{
try
{
//if (wakeDelay == 0)
//{
// if (await ConnectToSelectedDevice())
// {
// Logger.Debug("Already connected, no wake needed?");
// return;
// };
//}
//if (wakeDelay == 0)
//{
// if (await ConnectToSelectedDevice())
// {
// Logger.Debug("Already connected, no wake needed?");
// return;
// };
//}
await Task.Delay(wakeDelay);
WakeSelectedDevice();
await Task.Delay(connectDelay);
await ConnectToSelectedDevice();
}
catch (Exception e)
{
Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString());
}
});
await Task.Delay(wakeDelay);
WakeSelectedDevice();
await Task.Delay(connectDelay);
return await ConnectToSelectedDevice();
}
catch (Exception e)
{
Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString());
return false;
}
}
}
}
+34 -12
View File
@@ -112,6 +112,7 @@
this.label10 = new System.Windows.Forms.Label();
this.chkFixChromeFonts = new System.Windows.Forms.CheckBox();
this.grpLGOptions = new System.Windows.Forms.GroupBox();
this.chkLgAlternateWolMechanism = new System.Windows.Forms.CheckBox();
this.btnLGTestPower = new System.Windows.Forms.Button();
this.label9 = new System.Windows.Forms.Label();
this.edtLgPowerOnAfterResumeDelay = new System.Windows.Forms.NumericUpDown();
@@ -134,12 +135,14 @@
this.lbPlugins = new System.Windows.Forms.ListBox();
this.label7 = new System.Windows.Forms.Label();
this.lblInfo = new System.Windows.Forms.Label();
this.chkLgAlternateWolMechanism = new System.Windows.Forms.CheckBox();
this.mnuLgRcButtons = new System.Windows.Forms.ToolStripMenuItem();
this.mnuLgActions = new System.Windows.Forms.ToolStripMenuItem();
this.tcMain.SuspendLayout();
this.tabNVIDIA.SuspendLayout();
this.mnuNvPresets.SuspendLayout();
this.tabAMD.SuspendLayout();
this.tabLG.SuspendLayout();
this.mnuLgButtons.SuspendLayout();
this.tabOptions.SuspendLayout();
this.grpNvidiaOptions.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pbGradient)).BeginInit();
@@ -644,15 +647,18 @@
this.btnLgAddButton.Name = "btnLgAddButton";
this.btnLgAddButton.Size = new System.Drawing.Size(75, 23);
this.btnLgAddButton.TabIndex = 32;
this.btnLgAddButton.Text = "Add button";
this.btnLgAddButton.Text = "Add step";
this.btnLgAddButton.UseVisualStyleBackColor = true;
this.btnLgAddButton.Click += new System.EventHandler(this.btnLgAddButton_Click);
//
// mnuLgButtons
//
this.mnuLgButtons.ImageScalingSize = new System.Drawing.Size(20, 20);
this.mnuLgButtons.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.mnuLgRcButtons,
this.mnuLgActions});
this.mnuLgButtons.Name = "mnuLgButtons";
this.mnuLgButtons.Size = new System.Drawing.Size(61, 4);
this.mnuLgButtons.Size = new System.Drawing.Size(181, 70);
//
// label3
//
@@ -1044,6 +1050,17 @@
this.grpLGOptions.TabStop = false;
this.grpLGOptions.Text = "LG controller";
//
// chkLgAlternateWolMechanism
//
this.chkLgAlternateWolMechanism.AutoSize = true;
this.chkLgAlternateWolMechanism.Location = new System.Drawing.Point(158, 72);
this.chkLgAlternateWolMechanism.Name = "chkLgAlternateWolMechanism";
this.chkLgAlternateWolMechanism.Size = new System.Drawing.Size(173, 17);
this.chkLgAlternateWolMechanism.TabIndex = 7;
this.chkLgAlternateWolMechanism.Text = "Use alternate WOL mechanism";
this.chkLgAlternateWolMechanism.UseVisualStyleBackColor = true;
this.chkLgAlternateWolMechanism.CheckedChanged += new System.EventHandler(this.chkLgAlternateWolMechanism_CheckedChanged);
//
// btnLGTestPower
//
this.btnLGTestPower.Location = new System.Drawing.Point(9, 68);
@@ -1297,16 +1314,17 @@
this.lblInfo.TabIndex = 1;
this.lblInfo.Text = "Info";
//
// chkLgAlternateWolMechanism
// mnuLgRcButtons
//
this.chkLgAlternateWolMechanism.AutoSize = true;
this.chkLgAlternateWolMechanism.Location = new System.Drawing.Point(158, 72);
this.chkLgAlternateWolMechanism.Name = "chkLgAlternateWolMechanism";
this.chkLgAlternateWolMechanism.Size = new System.Drawing.Size(173, 17);
this.chkLgAlternateWolMechanism.TabIndex = 7;
this.chkLgAlternateWolMechanism.Text = "Use alternate WOL mechanism";
this.chkLgAlternateWolMechanism.UseVisualStyleBackColor = true;
this.chkLgAlternateWolMechanism.CheckedChanged += new System.EventHandler(this.chkLgAlternateWolMechanism_CheckedChanged);
this.mnuLgRcButtons.Name = "mnuLgRcButtons";
this.mnuLgRcButtons.Size = new System.Drawing.Size(180, 22);
this.mnuLgRcButtons.Text = "Buttons";
//
// mnuLgActions
//
this.mnuLgActions.Name = "mnuLgActions";
this.mnuLgActions.Size = new System.Drawing.Size(180, 22);
this.mnuLgActions.Text = "Actions";
//
// MainForm
//
@@ -1320,6 +1338,7 @@
this.Name = "MainForm";
this.Text = "ColorControl";
this.Activated += new System.EventHandler(this.MainForm_Activated);
this.Deactivate += new System.EventHandler(this.MainForm_Deactivate);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
this.Shown += new System.EventHandler(this.MainForm_Shown);
@@ -1332,6 +1351,7 @@
this.tabAMD.PerformLayout();
this.tabLG.ResumeLayout(false);
this.tabLG.PerformLayout();
this.mnuLgButtons.ResumeLayout(false);
this.tabOptions.ResumeLayout(false);
this.grpNvidiaOptions.ResumeLayout(false);
this.grpNvidiaOptions.PerformLayout();
@@ -1463,6 +1483,8 @@
private System.Windows.Forms.Button btnLGRemoteControl;
private System.Windows.Forms.ToolStripMenuItem miNvPresetApplyOnStartup;
private System.Windows.Forms.CheckBox chkLgAlternateWolMechanism;
private System.Windows.Forms.ToolStripMenuItem mnuLgRcButtons;
private System.Windows.Forms.ToolStripMenuItem mnuLgActions;
}
}
+74 -30
View File
@@ -176,6 +176,32 @@ namespace ColorControl
edtLgPowerOnAfterResumeDelay.Value = _lgService.Config.PowerOnDelayAfterResume;
edtLgDeviceFilter.Text = _lgService.Config.DeviceSearchKey;
chkLgAlternateWolMechanism.Checked = _lgService.Config.UseAlternateWol;
var values = Enum.GetValues(typeof(ButtonType));
foreach (var button in values)
{
var text = button.ToString();
if (text[0] == '_')
{
text = text.Substring(1);
}
var item = mnuLgRcButtons.DropDownItems.Add(text);
item.Click += miLgAddButton_Click;
}
var actions = _lgService.GetInvokableActions();
foreach (var keyValuePair in actions)
{
var text = keyValuePair.Key;
var item = mnuLgActions.DropDownItems.Add(text);
item.Click += miLgAddAction_Click;
}
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged);
//SystemEvents.SessionEnded += new SessionEndedEventHandler(SessionEnded);
}
catch (Exception e)
{
@@ -184,24 +210,6 @@ namespace ColorControl
InitInfo();
var values = Enum.GetValues(typeof(ButtonType));
foreach (var button in values)
{
var text = button.ToString();
if (text[0] == '_')
{
text = text.Substring(1);
}
var item = mnuLgButtons.Items.Add(text);
item.Click += miLgAddButton_Click;
}
if (_lgService != null)
{
//SystemEvents.SessionEnded += new SessionEndedEventHandler(SessionEnded);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged);
}
_restartDetector = new RestartDetector();
//Scale(new SizeF(1.25F, 1.25F));
@@ -531,11 +539,8 @@ namespace ColorControl
UserExit = false;
return;
}
SaveNvPresets();
SaveLgPresets();
SaveConfig();
GlobalSave();
if (SystemShutdown && _lgService.Config.PowerOffOnShutdown)
{
@@ -560,16 +565,40 @@ namespace ColorControl
}
}
private void GlobalSave()
{
SaveNvPresets();
SaveLgPresets();
_lgService?.GlobalSave();
SaveConfig();
}
private void SaveNvPresets()
{
var json = _JsonSerializer.Serialize(_presets);
File.WriteAllText(_nvPresetsFilename, json);
try
{
var json = _JsonSerializer.Serialize(_presets);
File.WriteAllText(_nvPresetsFilename, json);
}
catch (Exception e)
{
Logger.Error(e.ToLogString());
}
}
private void SaveLgPresets()
{
var json = _JsonSerializer.Serialize(_lgPresets);
File.WriteAllText(_lgPresetsFilename, json);
try
{
var json = _JsonSerializer.Serialize(_lgPresets);
File.WriteAllText(_lgPresetsFilename, json);
}
catch (Exception e)
{
Logger.Error(e.ToLogString());
}
}
private void lvNvPresets_SelectedIndexChanged(object sender, EventArgs e)
@@ -1812,12 +1841,22 @@ namespace ColorControl
{
var item = sender as ToolStripItem;
var button = item.Text;
AddToLgSteps(item.Text);
}
private void miLgAddAction_Click(object sender, EventArgs e)
{
var item = sender as ToolStripItem;
AddToLgSteps(item.Text);
}
private void AddToLgSteps(string step)
{
var text = edtStepsLg.Text;
if (string.IsNullOrWhiteSpace(text))
{
text = button;
text = step;
}
else
{
@@ -1828,11 +1867,11 @@ namespace ColorControl
}
if (pos == text.Length)
{
text += ", " + button;
text += ", " + step;
}
else
{
text = text.Substring(0, pos + 1) + " " + button + ", " + text.Substring(pos + 1).Trim();
text = text.Substring(0, pos + 1) + " " + step + ", " + text.Substring(pos + 1).Trim();
}
}
edtStepsLg.Text = text.Trim();
@@ -2080,5 +2119,10 @@ Do you want to continue?";
{
_lgService.Config.UseAlternateWol = chkLgAlternateWolMechanism.Checked;
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
GlobalSave();
}
}
}
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.5.0")]
[assembly: AssemblyFileVersion("1.5.5.0")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.6.0.0")]