Skip to content

Commit

Permalink
add WOL to steps, fix saving configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maassoft committed Feb 14, 2021
1 parent fd51bcc commit 7f6175f
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 75 deletions.
2 changes: 1 addition & 1 deletion ColorControl/ColorControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
95 changes: 65 additions & 30 deletions ColorControl/LgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public PnpDev SelectedDevice
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();

Expand All @@ -62,6 +67,11 @@ public LgService(string dataDir, bool allowPowerOn)
}

~LgService()
{
GlobalSave();
}

public void GlobalSave()
{
SaveConfig();
SaveRemoteControlButtons();
Expand Down Expand Up @@ -105,6 +115,11 @@ public List<LgPreset> GetRemoteControlButtons()
return _remoteControlButtons;
}

public Dictionary<string, Action> GetInvokableActions()
{
return _invokableActions;
}

private List<LgPreset> GenerateDefaultRemoteControlButtons()
{
var list = new List<LgPreset>();
Expand Down Expand Up @@ -151,13 +166,13 @@ private LgPreset GeneratePreset(string name, string step = null, string appId =
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);
Expand Down Expand Up @@ -244,6 +259,17 @@ public async Task<bool> ApplyPreset(LgPreset preset, bool reconnect = false)
{
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))
Expand Down Expand Up @@ -317,13 +343,19 @@ private async Task ExecuteSteps(LgTvApi api, LgPreset preset)
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);
}
}
}
Expand Down Expand Up @@ -383,17 +415,22 @@ internal async Task<bool> PowerOff()
return true;
}

internal void WakeSelectedDevice(string macAddress = null)
internal void WakeSelectedDevice()
{
WakeSelectedDevice(SelectedDevice?.MacAddress ?? Config.PreferredMacAddress);
}

internal void WakeSelectedDevice(string macAddress)
{
macAddress = macAddress == null ? SelectedDevice?.MacAddress : macAddress;
macAddress = string.IsNullOrEmpty(macAddress) ? SelectedDevice?.MacAddress : macAddress;
if (macAddress != null)
{
WOL.WakeFunction(macAddress, !Config.UseAlternateWol);
_justWokeUp = true;
}
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");
}
}

Expand All @@ -405,31 +442,29 @@ internal void WakeAfterResume()
}
}

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;
// };
//}

await Task.Delay(wakeDelay);
WakeSelectedDevice();
await Task.Delay(connectDelay);
await ConnectToSelectedDevice();
}
catch (Exception e)
{
Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString());
}
});
//if (wakeDelay == 0)
//{
// if (await ConnectToSelectedDevice())
// {
// Logger.Debug("Already connected, no wake needed?");
// return;
// };
//}

await Task.Delay(wakeDelay);
WakeSelectedDevice();
await Task.Delay(connectDelay);
return await ConnectToSelectedDevice();
}
catch (Exception e)
{
Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString());
return false;
}
}
}
}
46 changes: 34 additions & 12 deletions ColorControl/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7f6175f

Please sign in to comment.