1 using Ear = SharpLib.Ear;
4 using System.Collections.Generic;
5 using System.Diagnostics;
8 using System.Threading.Tasks;
9 using System.Runtime.Serialization;
10 using System.Windows.Forms;
12 namespace SharpDisplayManager
15 [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
16 class ActionHarmonyCommand : Ear.Action
19 public string DeviceId { get; set; } = "";
22 public string Command { get; set; } = "";
25 [AttributeObjectProperty(
26 Id = "Harmony.Command.SelectCommand",
27 Name = "Select command",
28 Description = "Click to select a command."
30 public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
35 /// <returns></returns>
36 public override string Brief()
38 string brief="Harmony: ";
40 if (Program.HarmonyConfig != null)
42 //What if the device ID is not there anymore?
43 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
47 //No config found just show the device ID then.
51 // TODO: Fetch function label from command
52 brief += " do " + Command;
58 protected override void DoConstruct()
62 if (SelectCommand == null)
64 SelectCommand = new PropertyButton { Text = "None"};
66 SelectCommand.ClickEventHandler = ClickEventHandler;
72 protected override async Task DoExecute()
74 if (Program.HarmonyClient!=null)
76 // Send our command and wait for it async
77 await Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);
81 Trace.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.Action.Command.Equals(Command))
104 //We found our device and our function
119 /// <param name="sender"></param>
120 /// <param name="e"></param>
121 void ClickEventHandler(object sender, EventArgs e)
123 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
124 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
125 if (res == DialogResult.OK)
127 DeviceId = dlg.DeviceId;
128 Command = dlg.Command;
129 SelectCommand.Text = Brief();
130 //Tell observer the object itself changed
131 OnPropertyChanged("Brief");