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 FunctionName { 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 brief += " do " + FunctionName;
57 protected override void DoConstruct()
61 if (SelectCommand == null)
63 SelectCommand = new PropertyButton { Text = "None"};
65 SelectCommand.ClickEventHandler = ClickEventHandler;
71 protected override void DoExecute()
73 //Fire and forget our command
74 //TODO: check if the harmony client connection is opened
75 if (Program.HarmonyClient!=null)
77 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
81 Trace.WriteLine("WARNING: No Harmony client connection.");
90 /// <returns></returns>
91 public override bool IsValid()
93 if (Program.HarmonyConfig != null)
95 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
97 if (d.Id.Equals(DeviceId))
99 foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
101 foreach (HarmonyHub.Function f in cg.Functions)
103 if (f.Name.Equals(FunctionName))
105 //We found our device and our function
118 void ClickEventHandler(object sender, EventArgs e)
120 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
121 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
122 if (res == DialogResult.OK)
124 DeviceId = dlg.DeviceId;
125 FunctionName = dlg.FunctionName;
126 SelectCommand.Text = Brief();
127 //Tell observer the object itself changed
128 OnPropertyChanged("Brief");