StephaneLenclud@236: using Ear = SharpLib.Ear;
StephaneLenclud@236: using SharpLib.Ear;
StephaneLenclud@236: using System;
StephaneLenclud@236: using System.Collections.Generic;
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@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@236: public string FunctionName { 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@236: public override string Brief()
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@236: brief += " do " + FunctionName;
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@236: protected override void DoExecute()
StephaneLenclud@236: {
StephaneLenclud@236: //Fire and forget our command
StephaneLenclud@236: //TODO: check if the harmony client connection is opened
StephaneLenclud@236: if (Program.HarmonyClient!=null)
StephaneLenclud@236: {
StephaneLenclud@236: Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
StephaneLenclud@236: }
StephaneLenclud@236: else
StephaneLenclud@236: {
StephaneLenclud@236: Console.WriteLine("WARNING: No Harmony client connection.");
StephaneLenclud@236: }
StephaneLenclud@236:
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@250: if (f.Name.Equals(FunctionName))
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@250:
StephaneLenclud@250: void ClickEventHandler(object sender, EventArgs e)
StephaneLenclud@250: {
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@250: FunctionName = dlg.FunctionName;
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: }