Trying to clean up our Harmony handling.
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 public string DeviceId { get; set; } = "";
21 public string FunctionName { get; set; } = "";
24 [AttributeObjectProperty(
25 Id = "Harmony.Command.SelectCommand",
26 Name = "Select command",
27 Description = "Click to select a command."
29 public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
34 /// <returns></returns>
35 public override string Brief()
37 string brief="Harmony: ";
39 if (Program.HarmonyConfig != null)
41 //What if the device ID is not there anymore?
42 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
46 //No config found just show the device ID then.
50 brief += " do " + FunctionName;
56 protected override void DoConstruct()
60 if (SelectCommand == null)
62 SelectCommand = new PropertyButton { Text = "None"};
64 SelectCommand.ClickEventHandler = ClickEventHandler;
70 protected override void DoExecute()
72 //Fire and forget our command
73 //TODO: check if the harmony client connection is opened
74 if (Program.HarmonyClient!=null)
76 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
80 Console.WriteLine("WARNING: No Harmony client connection.");
89 /// <returns></returns>
90 public override bool IsValid()
92 if (Program.HarmonyConfig != null)
94 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
96 if (d.Id.Equals(DeviceId))
98 foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
100 foreach (HarmonyHub.Function f in cg.Functions)
102 if (f.Name.Equals(FunctionName))
104 //We found our device and our function
117 void ClickEventHandler(object sender, EventArgs e)
119 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
120 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
121 if (res == DialogResult.OK)
123 DeviceId = dlg.DeviceId;
124 FunctionName = dlg.FunctionName;
125 SelectCommand.Text = Brief();
126 //Tell observer the object itself changed
127 OnPropertyChanged("Brief");