Published V.1.0.5.
Providing Harmony Hub reconnect.
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 // Wait synchronously for now until we figure out how we could do async stuff in EAR.
78 // TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed.
79 Program.HarmonyClient.TrySendCommandAsync(DeviceId, FunctionName).Wait(10*1000);
83 Trace.WriteLine("WARNING: No Harmony client connection.");
91 /// <returns></returns>
92 public override bool IsValid()
94 if (Program.HarmonyConfig != null)
96 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
98 if (d.Id.Equals(DeviceId))
100 foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
102 foreach (HarmonyHub.Function f in cg.Functions)
104 if (f.Name.Equals(FunctionName))
106 //We found our device and our function
119 void ClickEventHandler(object sender, EventArgs e)
121 FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
122 DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
123 if (res == DialogResult.OK)
125 DeviceId = dlg.DeviceId;
126 FunctionName = dlg.FunctionName;
127 SelectCommand.Text = Brief();
128 //Tell observer the object itself changed
129 OnPropertyChanged("Brief");