# HG changeset patch # User StephaneLenclud # Date 1471434086 -7200 # Node ID 6ba20e02d04f5bce296dd1a39f92e0b0c3e1938f # Parent ba14a29944c49759b98b0b741a1f79fce932d04c Updating HarmonyHub to v0.3.0. Adding untested Harmony command action. diff -r ba14a29944c4 -r 6ba20e02d04f Server/Actions/ActionDisplayMessage.cs --- a/Server/Actions/ActionDisplayMessage.cs Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/Actions/ActionDisplayMessage.cs Wed Aug 17 13:41:26 2016 +0200 @@ -1,4 +1,5 @@ -using SharpLib.Ear; +using Ear = SharpLib.Ear; +using SharpLib.Ear; using System; using System.Collections.Generic; using System.Drawing; @@ -14,7 +15,7 @@ [DataContract] [AttributeObject(Id = "Display.Message", Name = "Display Message", Description = "Shows a message on your internal display.")] - class ActionDisplayMessage : SharpLib.Ear.Action + class ActionDisplayMessage : Ear.Action { [DataMember] [AttributeObjectProperty( diff -r ba14a29944c4 -r 6ba20e02d04f Server/Actions/ActionHarmonyCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Actions/ActionHarmonyCommand.cs Wed Aug 17 13:41:26 2016 +0200 @@ -0,0 +1,76 @@ +using Ear = SharpLib.Ear; +using SharpLib.Ear; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace SharpDisplayManager +{ + [DataContract] + [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")] + class ActionHarmonyCommand : Ear.Action + { + [DataMember] + [AttributeObjectProperty( + Id = "Harmony.Command.DeviceId", + Name = "Device ID", + Description = "The ID of the device this command is associated with." + )] + public string DeviceId { get; set; } = ""; + + + [DataMember] + [AttributeObjectProperty( + Id = "Harmony.Command.FunctionName", + Name = "Function Name", + Description = "The name of the function defining this command." + )] + public string FunctionName { get; set; } = ""; + + /// + /// + /// + /// + public override string Brief() + { + string brief=""; + + if (Program.HarmonyConfig != null) + { + //What if the device ID is not there anymore? + brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId); + } + else + { + //No config found just show the device ID then. + brief += DeviceId; + } + + brief += " do " + FunctionName; + + return brief; + } + + /// + /// + /// + protected override void DoExecute() + { + //Fire and forget our command + //TODO: check if the harmony client connection is opened + if (Program.HarmonyClient!=null) + { + Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName); + } + else + { + Console.WriteLine("WARNING: No Harmony client connection."); + } + + } + + } +} diff -r ba14a29944c4 -r 6ba20e02d04f Server/FormEditObject.cs --- a/Server/FormEditObject.cs Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/FormEditObject.cs Wed Aug 17 13:41:26 2016 +0200 @@ -35,7 +35,7 @@ /// private void FormEditAction_Load(object sender, EventArgs e) { - // Populate registered actions + // Populate registered object types IEnumerable < Type > types = Reflection.GetConcreteClassesDerivedFrom(); foreach (Type type in types) { @@ -90,10 +90,10 @@ /// /// Get properties values from our generated input fields /// - private void FetchPropertiesValue(T aAction) + private void FetchPropertiesValue(T aObject) { int ctrlIndex = 0; - foreach (PropertyInfo pi in aAction.GetType().GetProperties()) + foreach (PropertyInfo pi in aObject.GetType().GetProperties()) { AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true)); @@ -109,7 +109,7 @@ continue; } - GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label + GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label ctrlIndex+=2; //Jump over the label too } @@ -118,13 +118,13 @@ /// /// Extend this function to support reading new types of properties. /// - /// - private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aAction) + /// + private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject) { if (aInfo.PropertyType == typeof(int)) { NumericUpDown ctrl=(NumericUpDown)aControl; - aInfo.SetValue(aAction,(int)ctrl.Value); + aInfo.SetValue(aObject,(int)ctrl.Value); } else if (aInfo.PropertyType.IsEnum) { @@ -134,27 +134,30 @@ enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString()); //enumValue = ((ComboBox)aControl).SelectedValue; // Set enum value - aInfo.SetValue(aAction, enumValue); + aInfo.SetValue(aObject, enumValue); } else if (aInfo.PropertyType == typeof(bool)) { CheckBox ctrl = (CheckBox)aControl; - aInfo.SetValue(aAction, ctrl.Checked); + aInfo.SetValue(aObject, ctrl.Checked); } else if (aInfo.PropertyType == typeof(string)) { TextBox ctrl = (TextBox)aControl; - aInfo.SetValue(aAction, ctrl.Text); + aInfo.SetValue(aObject, ctrl.Text); } //TODO: add support for other types here } + /// - /// + /// Create a control for the given property. /// /// - /// - private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aAction) + /// + /// + /// + private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject) { if (aInfo.PropertyType == typeof(int)) { @@ -164,7 +167,7 @@ ctrl.Minimum = Int32.Parse(aAttribute.Minimum); ctrl.Maximum = Int32.Parse(aAttribute.Maximum); ctrl.Increment = Int32.Parse(aAttribute.Increment); - ctrl.Value = (int)aInfo.GetValue(aAction); + ctrl.Value = (int)aInfo.GetValue(aObject); return ctrl; } else if (aInfo.PropertyType.IsEnum) @@ -194,7 +197,7 @@ // Instantiate our enum object enumValue = Activator.CreateInstance(aInfo.PropertyType); - enumValue = aInfo.GetValue(aAction); + enumValue = aInfo.GetValue(aObject); //Set the current item ctrl.SelectedItem = enumValue.ToString(); @@ -205,14 +208,14 @@ CheckBox ctrl = new CheckBox(); ctrl.AutoSize = true; ctrl.Text = aAttribute.Description; - ctrl.Checked = (bool)aInfo.GetValue(aAction); + ctrl.Checked = (bool)aInfo.GetValue(aObject); return ctrl; } else if (aInfo.PropertyType == typeof(string)) { TextBox ctrl = new TextBox(); ctrl.AutoSize = true; - ctrl.Text = (string)aInfo.GetValue(aAction); + ctrl.Text = (string)aInfo.GetValue(aObject); return ctrl; } //TODO: add support for other control type here @@ -253,7 +256,7 @@ /// Fields must be specified by rows from the left. /// /// - private void UpdateTableLayoutPanel(T aAction) + private void UpdateTableLayoutPanel(T aObject) { toolTip.RemoveAll(); //Debug.Print("UpdateTableLayoutPanel") @@ -269,17 +272,17 @@ iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); - if (aAction == null) + if (aObject == null) { //Just drop it return; } - //IEnumerable properties = aAction.GetType().GetProperties().Where( + //IEnumerable properties = aObject.GetType().GetProperties().Where( // prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty))); - foreach (PropertyInfo pi in aAction.GetType().GetProperties()) + foreach (PropertyInfo pi in aObject.GetType().GetProperties()) { AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true)); if (attributes.Length != 1) @@ -291,7 +294,7 @@ //Before anything we need to check if that kind of property is supported by our UI //Create the editor - Control ctrl = CreateControlForProperty(pi, attribute, aAction); + Control ctrl = CreateControlForProperty(pi, attribute, aObject); if (ctrl == null) { //Property type not supported diff -r ba14a29944c4 -r 6ba20e02d04f Server/FormMain.cs --- a/Server/FormMain.cs Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/FormMain.cs Wed Aug 17 13:41:26 2016 +0200 @@ -3065,13 +3065,23 @@ private async Task ConnectHarmonyAsync() { + if (Program.HarmonyClient != null) + { + Program.HarmonyClient.Close(); + } + + //Reset Harmony client & config + Program.HarmonyClient = null; + Program.HarmonyConfig = null; + Console.WriteLine("Harmony: Connecting... "); + Program.HarmonyClient = new HarmonyHub.Client(iTextBoxHarmonyHubAddress.Text); //First create our client and login if (File.Exists("SessionToken")) { var sessionToken = File.ReadAllText("SessionToken"); Console.WriteLine("Harmony: Reusing token: {0}", sessionToken); - Program.HarmonyClient = HarmonyHub.HarmonyClient.Create(iTextBoxHarmonyHubAddress.Text, sessionToken); + Program.HarmonyClient.Open(sessionToken); } else { @@ -3082,15 +3092,15 @@ } Console.WriteLine("Harmony: Authenticating with Logitech servers..."); - Program.HarmonyClient = await HarmonyHub.HarmonyClient.Create(iTextBoxHarmonyHubAddress.Text, iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text); + await Program.HarmonyClient.Open(iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text); File.WriteAllText("SessionToken", Program.HarmonyClient.Token); } Console.WriteLine("Harmony: Fetching Harmony Hub configuration..."); //Fetch our config - var harmonyConfig = await Program.HarmonyClient.GetConfigAsync(); - PopulateTreeViewHarmony(harmonyConfig); + Program.HarmonyConfig = await Program.HarmonyClient.GetConfigAsync(); + PopulateTreeViewHarmony(Program.HarmonyConfig); Console.WriteLine("Harmony: Ready"); } @@ -3099,21 +3109,21 @@ /// /// /// - private void PopulateTreeViewHarmony(HarmonyHub.Entities.Response.Config aConfig) + private void PopulateTreeViewHarmony(HarmonyHub.Config aConfig) { iTreeViewHarmony.Nodes.Clear(); //Add our devices - foreach (HarmonyHub.Entities.Response.Device device in aConfig.Devices) + foreach (HarmonyHub.Device device in aConfig.Devices) { TreeNode deviceNode = iTreeViewHarmony.Nodes.Add(device.Id, $"{device.Label} ({device.DeviceTypeDisplayName}/{device.Model})"); deviceNode.Tag = device; - foreach (HarmonyHub.Entities.Response.ControlGroup cg in device.ControlGroups) + foreach (HarmonyHub.ControlGroup cg in device.ControlGroups) { TreeNode cgNode = deviceNode.Nodes.Add(cg.Name); cgNode.Tag = cg; - foreach (HarmonyHub.Entities.Response.Function f in cg.Functions) + foreach (HarmonyHub.Function f in cg.Functions) { TreeNode fNode = cgNode.Nodes.Add(f.Name); fNode.Tag = f; @@ -3127,11 +3137,11 @@ private async void iTreeViewHarmony_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { //Upon function node double click we execute it - var tag = e.Node.Tag as HarmonyHub.Entities.Response.Function; - if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Entities.Response.Device) + var tag = e.Node.Tag as HarmonyHub.Function; + if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device) { - HarmonyHub.Entities.Response.Function f = tag; - HarmonyHub.Entities.Response.Device d = (HarmonyHub.Entities.Response.Device)e.Node.Parent.Parent.Tag; + HarmonyHub.Function f = tag; + HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag; Console.WriteLine($"Harmony: Sending {f.Name} to {d.Label}..."); diff -r ba14a29944c4 -r 6ba20e02d04f Server/Program.cs --- a/Server/Program.cs Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/Program.cs Wed Aug 17 13:41:26 2016 +0200 @@ -20,7 +20,6 @@ using System; using System.Windows.Forms; using System.Security.Principal; -using HarmonyHub; namespace SharpDisplayManager @@ -31,8 +30,17 @@ //That is what we want but we should enforce it somehow. public static FormMain iFormMain; - // - public static HarmonyClient HarmonyClient { get; set; } + /// + /// + /// + public static HarmonyHub.Client HarmonyClient { get; set; } + + /// + /// + /// + public static HarmonyHub.Config HarmonyConfig { get; set; } + + /// /// The main entry point for the application. /// diff -r ba14a29944c4 -r 6ba20e02d04f Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/SharpDisplayManager.csproj Wed Aug 17 13:41:26 2016 +0200 @@ -112,7 +112,7 @@ True - ..\packages\SharpLibHarmony.0.2.1\lib\net451\HarmonyHub.dll + ..\packages\SharpLibHarmony.0.3.0\lib\net451\HarmonyHub.dll True @@ -173,6 +173,7 @@ + diff -r ba14a29944c4 -r 6ba20e02d04f Server/packages.config --- a/Server/packages.config Tue Aug 16 12:59:32 2016 +0200 +++ b/Server/packages.config Wed Aug 17 13:41:26 2016 +0200 @@ -4,7 +4,7 @@ - +