Using Harmony Function Action Command.
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 void DoExecute()
74 //Fire and forget our command
75 //TODO: check if the harmony client connection is opened
76 if (Program.HarmonyClient!=null)
78 // Wait synchronously for now until we figure out how we could do async stuff in EAR.
79 // TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed.
80 Task<bool> task = Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);
81 bool result = task.Result; //Synchronously waiting for result
82 Trace.WriteLine("ActionHarmonyCommand.DoExecute result: " + result.ToString());
86 Trace.WriteLine("WARNING: No Harmony client connection.");
94 /// <returns></returns>
95 public override bool IsValid()
97 if (Program.HarmonyConfig != null)
99 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
101 if (d.Id.Equals(DeviceId))
103 foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
105 foreach (HarmonyHub.Function f in cg.Functions)
107 if (f.Action.Command.Equals(Command))
109 //We found our device and our function
124 /// <param name="sender"></param>
125 /// <param name="e"></param>
126 void ClickEventHandler(object sender, EventArgs e)
128 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
129 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
130 if (res == DialogResult.OK)
132 DeviceId = dlg.DeviceId;
133 Command = dlg.Command;
134 SelectCommand.Text = Brief();
135 //Tell observer the object itself changed
136 OnPropertyChanged("Brief");