StephaneLenclud@236: using Ear = SharpLib.Ear; StephaneLenclud@236: using SharpLib.Ear; StephaneLenclud@236: using System; StephaneLenclud@236: using System.Collections.Generic; StephaneLenclud@253: using System.Diagnostics; StephaneLenclud@236: using System.Linq; StephaneLenclud@236: using System.Text; StephaneLenclud@236: using System.Threading.Tasks; StephaneLenclud@236: using System.Runtime.Serialization; StephaneLenclud@250: using System.Windows.Forms; StephaneLenclud@262: using CodeProject.Dialog; StephaneLenclud@236: StephaneLenclud@236: namespace SharpDisplayManager StephaneLenclud@236: { StephaneLenclud@236: [DataContract] StephaneLenclud@236: [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")] StephaneLenclud@236: class ActionHarmonyCommand : Ear.Action StephaneLenclud@236: { StephaneLenclud@236: [DataMember] StephaneLenclud@236: public string DeviceId { get; set; } = ""; StephaneLenclud@236: StephaneLenclud@236: [DataMember] StephaneLenclud@257: public string Command { get; set; } = ""; StephaneLenclud@236: StephaneLenclud@250: [DataMember] StephaneLenclud@250: [AttributeObjectProperty( StephaneLenclud@250: Id = "Harmony.Command.SelectCommand", StephaneLenclud@250: Name = "Select command", StephaneLenclud@250: Description = "Click to select a command." StephaneLenclud@250: )] StephaneLenclud@250: public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" }; StephaneLenclud@250: StephaneLenclud@236: /// StephaneLenclud@236: /// StephaneLenclud@236: /// StephaneLenclud@236: /// StephaneLenclud@264: public override string BriefBase() StephaneLenclud@236: { StephaneLenclud@238: string brief="Harmony: "; StephaneLenclud@236: StephaneLenclud@236: if (Program.HarmonyConfig != null) StephaneLenclud@236: { StephaneLenclud@236: //What if the device ID is not there anymore? StephaneLenclud@236: brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId); StephaneLenclud@236: } StephaneLenclud@236: else StephaneLenclud@236: { StephaneLenclud@236: //No config found just show the device ID then. StephaneLenclud@236: brief += DeviceId; StephaneLenclud@236: } StephaneLenclud@236: StephaneLenclud@257: // TODO: Fetch function label from command StephaneLenclud@257: brief += " do " + Command; StephaneLenclud@236: StephaneLenclud@236: return brief; StephaneLenclud@236: } StephaneLenclud@236: StephaneLenclud@250: StephaneLenclud@250: protected override void DoConstruct() StephaneLenclud@250: { StephaneLenclud@250: base.DoConstruct(); StephaneLenclud@250: StephaneLenclud@250: if (SelectCommand == null) StephaneLenclud@250: { StephaneLenclud@250: SelectCommand = new PropertyButton { Text = "None"}; StephaneLenclud@250: } StephaneLenclud@250: SelectCommand.ClickEventHandler = ClickEventHandler; StephaneLenclud@250: } StephaneLenclud@250: StephaneLenclud@236: /// StephaneLenclud@236: /// StephaneLenclud@236: /// StephaneLenclud@258: protected override async Task DoExecute() StephaneLenclud@236: { StephaneLenclud@236: if (Program.HarmonyClient!=null) StephaneLenclud@236: { StephaneLenclud@258: // Send our command and wait for it async StephaneLenclud@258: await Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command); StephaneLenclud@236: } StephaneLenclud@236: else StephaneLenclud@236: { StephaneLenclud@253: Trace.WriteLine("WARNING: No Harmony client connection."); StephaneLenclud@256: } StephaneLenclud@236: } StephaneLenclud@236: StephaneLenclud@250: StephaneLenclud@250: /// StephaneLenclud@250: /// StephaneLenclud@250: /// StephaneLenclud@250: /// StephaneLenclud@250: public override bool IsValid() StephaneLenclud@250: { StephaneLenclud@250: if (Program.HarmonyConfig != null) StephaneLenclud@250: { StephaneLenclud@250: foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices) StephaneLenclud@250: { StephaneLenclud@250: if (d.Id.Equals(DeviceId)) StephaneLenclud@250: { StephaneLenclud@250: foreach (HarmonyHub.ControlGroup cg in d.ControlGroups) StephaneLenclud@250: { StephaneLenclud@250: foreach (HarmonyHub.Function f in cg.Functions) StephaneLenclud@250: { StephaneLenclud@257: if (f.Action.Command.Equals(Command)) StephaneLenclud@250: { StephaneLenclud@250: //We found our device and our function StephaneLenclud@250: return true; StephaneLenclud@250: } StephaneLenclud@250: } StephaneLenclud@250: } StephaneLenclud@250: } StephaneLenclud@250: } StephaneLenclud@250: } StephaneLenclud@250: StephaneLenclud@250: return false; StephaneLenclud@250: } StephaneLenclud@250: StephaneLenclud@257: /// StephaneLenclud@257: /// StephaneLenclud@257: /// StephaneLenclud@257: /// StephaneLenclud@257: /// StephaneLenclud@250: void ClickEventHandler(object sender, EventArgs e) StephaneLenclud@250: { StephaneLenclud@262: if (Program.HarmonyConfig == null) StephaneLenclud@262: { StephaneLenclud@262: ErrBox.Show("No Harmony Hub configuration!"); StephaneLenclud@262: return; StephaneLenclud@262: } StephaneLenclud@262: StephaneLenclud@250: FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand(); StephaneLenclud@250: DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg); StephaneLenclud@250: if (res == DialogResult.OK) StephaneLenclud@250: { StephaneLenclud@250: DeviceId = dlg.DeviceId; StephaneLenclud@257: Command = dlg.Command; StephaneLenclud@250: SelectCommand.Text = Brief(); StephaneLenclud@250: //Tell observer the object itself changed StephaneLenclud@250: OnPropertyChanged("Brief"); StephaneLenclud@250: } StephaneLenclud@250: StephaneLenclud@250: } StephaneLenclud@250: StephaneLenclud@236: } StephaneLenclud@236: }