Adding Harmony command selection dialog.
1 using Ear = SharpLib.Ear;
4 using System.Collections.Generic;
7 using System.Threading.Tasks;
8 using System.Runtime.Serialization;
9 using System.Windows.Forms;
11 namespace SharpDisplayManager
14 [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
15 class ActionHarmonyCommand : Ear.Action
18 [AttributeObjectProperty(
19 Id = "Harmony.Command.DeviceId",
21 Description = "The ID of the device this command is associated with."
23 public string DeviceId { get; set; } = "";
27 [AttributeObjectProperty(
28 Id = "Harmony.Command.FunctionName",
29 Name = "Function Name",
30 Description = "The name of the function defining this command."
32 public string FunctionName { get; set; } = "";
35 [AttributeObjectProperty(
36 Id = "Harmony.Command.SelectCommand",
37 Name = "Select command",
38 Description = "Click to select a command."
40 public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
45 /// <returns></returns>
46 public override string Brief()
48 string brief="Harmony: ";
50 if (Program.HarmonyConfig != null)
52 //What if the device ID is not there anymore?
53 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
57 //No config found just show the device ID then.
61 brief += " do " + FunctionName;
67 protected override void DoConstruct()
71 if (SelectCommand == null)
73 SelectCommand = new PropertyButton { Text = "None"};
75 SelectCommand.ClickEventHandler = ClickEventHandler;
81 protected override void DoExecute()
83 //Fire and forget our command
84 //TODO: check if the harmony client connection is opened
85 if (Program.HarmonyClient!=null)
87 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
91 Console.WriteLine("WARNING: No Harmony client connection.");
100 /// <returns></returns>
101 public override bool IsValid()
103 if (Program.HarmonyConfig != null)
105 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
107 if (d.Id.Equals(DeviceId))
109 foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
111 foreach (HarmonyHub.Function f in cg.Functions)
113 if (f.Name.Equals(FunctionName))
115 //We found our device and our function
128 void ClickEventHandler(object sender, EventArgs e)
130 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
131 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
132 if (res == DialogResult.OK)
134 DeviceId = dlg.DeviceId;
135 FunctionName = dlg.FunctionName;
136 SelectCommand.Text = Brief();
137 //Tell observer the object itself changed
138 OnPropertyChanged("Brief");