Skip to content

Commit

Permalink
fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Maassoft committed Jun 20, 2021
1 parent fc134b1 commit fa63d8d
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 43 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>3.2.1.0</ApplicationVersion>
<ApplicationVersion>3.2.2.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
Expand Down
29 changes: 1 addition & 28 deletions ColorControl/ColorDataConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NvAPIWrapper.Display;
using NvAPIWrapper.Native.Display;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
Expand All @@ -22,33 +21,7 @@ public override IDictionary<string, object> Serialize(object obj, JavaScriptSeri

public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
var format = ColorDataFormat.RGB;
var dynamicRange = ColorDataDynamicRange.VESA;
var colorDepth = ColorDataDepth.BPC8;
var colorimetry = ColorDataColorimetry.Auto;
var selectionPolicy = ColorDataSelectionPolicy.User;
object value;
if (dictionary.TryGetValue("ColorFormat", out value))
{
format = (ColorDataFormat)Enum.ToObject(typeof(ColorDataFormat), value);
}
if (dictionary.TryGetValue("ColorDepth", out value))
{
colorDepth = (ColorDataDepth)Enum.ToObject(typeof(ColorDataDepth), value);
}
if (dictionary.TryGetValue("Colorimetry", out value))
{
colorimetry = (ColorDataColorimetry)Enum.ToObject(typeof(ColorDataColorimetry), value);
}
if (dictionary.TryGetValue("DynamicRange", out value))
{
dynamicRange = (ColorDataDynamicRange)Enum.ToObject(typeof(ColorDataDynamicRange), value);
}
if (dictionary.TryGetValue("SelectionPolicy", out value))
{
selectionPolicy = (ColorDataSelectionPolicy)Enum.ToObject(typeof(ColorDataSelectionPolicy), value);
}
return new ColorData(format, dynamicRange: dynamicRange, colorimetry: colorimetry, colorDepth: colorDepth, colorSelectionPolicy: selectionPolicy, desktopColorDepth: ColorDataDesktopDepth.Default);
return NvPreset.GenerateColorData(dictionary);
}
}
}
75 changes: 73 additions & 2 deletions ColorControl/LgDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public class InvokableAction
public decimal MaxValue { get; set; }
}

public enum PowerState
{
Unknown,
Active,
Power_Off,
Suspend,
Active_Standby
}

public enum PowerOffSource
{
Unknown,
App,
Manually
}

protected static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

public string Name { get; private set; }
Expand All @@ -35,10 +51,17 @@ public class InvokableAction
public bool PowerOffOnStandby { get; set; }
public bool PowerSwitchOnScreenSaver { get; set; }

[JsonIgnore]
public PowerState CurrentState { get; set; }

[JsonIgnore]
private LgTvApi _lgTvApi;
[JsonIgnore]
private bool _justWokeUp;
[JsonIgnore]
public PowerOffSource PoweredOffBy { get; private set; }

public bool PoweredOffViaApp { get; private set; }

[JsonIgnore]
private List<InvokableAction> _invokableActions = new List<InvokableAction>();
Expand Down Expand Up @@ -87,15 +110,18 @@ private void AddGenericPictureAction(string name, Type type = null, decimal minV
{
Name = name,
Function = new Func<Dictionary<string, string>, bool>(GenericPictureAction),
EnumType = type
EnumType = type,
MinValue = minValue,
MaxValue = maxValue
};

_invokableActions.Add(action);
}

public override string ToString()
{
return (IsDummy ? string.Empty : (IsCustom ? "Custom: " : "Auto detect: ")) + $"{Name}" + (!string.IsNullOrEmpty(IpAddress) ? $" ({IpAddress})" : string.Empty);
//return (IsDummy ? string.Empty : (IsCustom ? "Custom: " : "Auto detect: ")) + $"{Name}" + (!string.IsNullOrEmpty(IpAddress) ? $" ({IpAddress})" : string.Empty);
return $"{(IsDummy ? string.Empty : (IsCustom ? "Custom: " : "Auto detect: "))}{Name}{(!string.IsNullOrEmpty(IpAddress) ? ", " + IpAddress : string.Empty)}";
}

public async Task<bool> Connect(int retries = 3)
Expand All @@ -114,6 +140,14 @@ public async Task<bool> Connect(int retries = 3)
{
ModelName = info.modelName;
}

//await _lgTvApi.SubscribeVolume(VolumeChanged);
await _lgTvApi.SubscribePowerState(PowerStateChanged);

//await _lgTvApi.SetInput("HDMI_1");
//await Task.Delay(2000);
//await _lgTvApi.SetConfig("com.palm.app.settings.enableHdmiPcLabel", true);
//await _lgTvApi.SetInput("HDMI_2");
}
return _lgTvApi != null;
}
Expand All @@ -125,6 +159,40 @@ public async Task<bool> Connect(int retries = 3)
}
}

public bool VolumeChanged(dynamic payload)
{
return true;
}
public bool PowerStateChanged(dynamic payload)
{
Logger.Debug($"[{Name}] Power state change: " + JsonConvert.SerializeObject(payload));

var state = ((string)payload.state).Replace(' ', '_');

PowerState newState;
if (Enum.TryParse(state, out newState))
{
CurrentState = newState;

if (CurrentState == PowerState.Active)
{
if (payload.processing == null)
{
PoweredOffViaApp = false;
}
}
else {
PoweredOffBy = PoweredOffViaApp ? PowerOffSource.App : PowerOffSource.Manually;
}
}
else
{
CurrentState = PowerState.Unknown;
Logger.Warn($"Unknown power state: {state}");
}
return true;
}

public bool IsConnected()
{
return !_lgTvApi?.ConnectionClosed ?? false;
Expand Down Expand Up @@ -303,7 +371,10 @@ internal async Task<bool> PowerOff()
return false;
}

PoweredOffViaApp = true;

await _lgTvApi.TurnOff();

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions ColorControl/LgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ internal void WakeAfterStartupOrResume(PowerOnOffState state = PowerOnOffState.S
state == PowerOnOffState.ScreenSaver && d.PowerSwitchOnScreenSaver);
foreach (var device in wakeDevices)
{
if (device.PoweredOffBy == LgDevice.PowerOffSource.Manually)
{
Logger.Debug($"[{device.Name}]: device was manually powered off by user, not powering on");
continue;
}

device.DisposeConnection();
var _ = device.WakeAndConnectWithRetries(Config.PowerOnRetries);
}
Expand Down
3 changes: 2 additions & 1 deletion ColorControl/MainForm.Designer.cs

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

18 changes: 16 additions & 2 deletions ColorControl/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public partial class MainForm : Form
private StartUpParams StartUpParams { get; }

private AmdService _amdService;
private bool _skipResize;

public MainForm(AppContext appContext)
{
Expand Down Expand Up @@ -541,6 +542,11 @@ private void MainForm_FormClosed(object sender, FormClosedEventArgs e)

private void MainForm_Resize(object sender, EventArgs e)
{
if (_skipResize)
{
return;
}

if (WindowState == FormWindowState.Minimized)
{
if (_config.MinimizeToTray)
Expand Down Expand Up @@ -578,8 +584,16 @@ private void LoadConfig()
Utils.RegisterShortcut(Handle, SHORTCUTID_SCREENSAVER, _config.ScreenSaverShortcut);
}

Width = _config.FormWidth;
Height = _config.FormHeight;
_skipResize = true;
try
{
Width = _config.FormWidth;
Height = _config.FormHeight;
}
finally
{
_skipResize = false;
}
}

private void SaveConfig()
Expand Down
13 changes: 9 additions & 4 deletions ColorControl/NvPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public NvPreset(NvPreset preset): this()
displayName = preset.displayName;

var colorData = preset.colorData;
this.colorData = new ColorData(colorData.ColorFormat, dynamicRange: colorData.DynamicRange, colorDepth: colorData.ColorDepth, colorSelectionPolicy: colorData.SelectionPolicy);
this.colorData = new ColorData(colorData.ColorFormat, dynamicRange: colorData.DynamicRange, colorDepth: colorData.ColorDepth, colorSelectionPolicy: ColorDataSelectionPolicy.User);
applyColorData = preset.applyColorData;

applyHDR = preset.applyHDR;
Expand Down Expand Up @@ -206,11 +206,16 @@ public static ColorData GenerateColorData(IDictionary<string, object> dictionary
{
dynamicRange = (ColorDataDynamicRange)Enum.ToObject(typeof(ColorDataDynamicRange), value);
}
if (dictionary.TryGetValue("SelectionPolicy", out value))
if (dictionary.TryGetValue("SelectionPolicy", out _))
{
selectionPolicy = (ColorDataSelectionPolicy)Enum.ToObject(typeof(ColorDataSelectionPolicy), value);
selectionPolicy =
colorDepth == ColorDataDepth.Default &&
format >= ColorDataFormat.Default &&
colorimetry >= ColorDataColorimetry.Default &&
dynamicRange == ColorDataDynamicRange.Auto ?
ColorDataSelectionPolicy.BestQuality : selectionPolicy;
}
return new ColorData(format, dynamicRange: dynamicRange, colorimetry: colorimetry, colorDepth: colorDepth, colorSelectionPolicy: selectionPolicy);
return new ColorData(format, dynamicRange: dynamicRange, colorimetry: colorimetry, colorDepth: colorDepth, colorSelectionPolicy: selectionPolicy, desktopColorDepth: ColorDataDesktopDepth.Default);
}

}
Expand Down
4 changes: 2 additions & 2 deletions ColorControl/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 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("3.2.1.0")]
[assembly: AssemblyFileVersion("3.2.1.0")]
[assembly: AssemblyVersion("3.2.2.0")]
[assembly: AssemblyFileVersion("3.2.2.0")]
25 changes: 24 additions & 1 deletion ColorControl/lgtv/LgTvApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public async Task<int> GetVolume()
}
public async Task<IEnumerable<ExternalInput>> GetInputList()
{
var requestMessage = new RequestMessage("input","ssap://tv/getExternalInputList");
var requestMessage = new RequestMessage("input","ssap://tv/getExternalInputList");
var results = await _connection.SendCommandAsync(requestMessage);
var l = new List<ExternalInput>();
foreach (var result in results)
Expand All @@ -326,6 +326,20 @@ public async Task SetInput(string id)
await _connection.SendCommandAsync(requestMessage);
}

public async Task SubscribeVolume(Func<dynamic, bool> callback, dynamic payload = null)
{
var requestMessage = new RequestMessage("ssap://audio/getVolume", null, "subscribe");

await _connection.SubscribeAsync(requestMessage, callback);
}

public async Task SubscribePowerState(Func<dynamic, bool> callback, dynamic payload = null)
{
var requestMessage = new RequestMessage("ssap://com.webos.service.tvpower/power/getPowerState", null, "subscribe");

await _connection.SubscribeAsync(requestMessage, callback);
}

public async Task<IEnumerable<App>> GetLaunchPoints()
{
_appList = new List<App>();
Expand Down Expand Up @@ -436,6 +450,15 @@ public async Task SetConfig(string key, string value)
await ExecuteRequest(lunauri, @params);
}

public async Task SetConfig(string key, bool value)
{
var lunauri = "luna://com.webos.service.config/setConfigs";

var @params = JObject.Parse(@"{ ""configs"": { """ + key + @""": " + value.ToString().ToLowerInvariant() + @" } }");

await ExecuteRequest(lunauri, @params);
}

private async Task ExecuteRequest(string lunauri, object @params)
{
var buttons = new[]
Expand Down
20 changes: 20 additions & 0 deletions ColorControl/lgtv/LgTvConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class LgTvApiCore : IDisposable

public event IsConnectedDelegate IsConnected;
private readonly ConcurrentDictionary<string, TaskCompletionSource<dynamic>> _tokens = new ConcurrentDictionary<string, TaskCompletionSource<dynamic>>();
private readonly ConcurrentDictionary<string, Func<dynamic, bool>> _callbacks = new ConcurrentDictionary<string, Func<dynamic, bool>>();

private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

Expand Down Expand Up @@ -135,6 +136,20 @@ public Task<dynamic> SendCommandAsync(RequestMessage message)
return SendCommandAsync(rawMessage.Id, serialized);
}

public Task<dynamic> SubscribeAsync(RequestMessage message, Func<dynamic, bool> callback)
{
var rawMessage = new RawRequestMessage(message, ++_commandCount);
var serialized = JsonConvert.SerializeObject(rawMessage, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});

_callbacks.TryAdd(rawMessage.Id, callback);

return SendCommandAsync(rawMessage.Id, serialized);
}

private void Connection_Closed(IWebSocket sender, WebSocketClosedEventArgs args)
{
MessageWebSocket webSocket = Interlocked.Exchange(ref _connection, null);
Expand Down Expand Up @@ -177,6 +192,11 @@ private void Connection_MessageReceived(MessageWebSocket sender, MessageWebSocke
// taskSource.SetCanceled();
//}
taskCompletion.TrySetResult(obj.payload);

if (_callbacks.TryGetValue(id, out Func<dynamic, bool> callback))
{
callback(obj.payload);
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions ColorControl/lgtv/RequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ public RequestMessage(string prefix, string uri)
Prefix = prefix;
Uri = uri;
}
public RequestMessage(string uri,object payload)
public RequestMessage(string uri, object payload, string type = "request")
{
Uri = uri;
Payload = JsonConvert.SerializeObject(payload);
if (payload != null)
{
Payload = JsonConvert.SerializeObject(payload);
}
Type = type;
}

public string Prefix { get; set; }
Expand Down

0 comments on commit fa63d8d

Please sign in to comment.