Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262 c4749a27966d
parent 258 e237c2e33545
child 264 4a08e1b7ba64
permissions -rw-r--r--
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
     1 using Ear = SharpLib.Ear;
     2 using SharpLib.Ear;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Diagnostics;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Runtime.Serialization;
    10 using System.Windows.Forms;
    11 using CodeProject.Dialog;
    12 
    13 namespace SharpDisplayManager
    14 {
    15     [DataContract]
    16     [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
    17     class ActionHarmonyCommand : Ear.Action
    18     {
    19         [DataMember]
    20         public string DeviceId { get; set; } = "";
    21 
    22         [DataMember]
    23         public string Command { get; set; } = "";
    24 
    25         [DataMember]
    26         [AttributeObjectProperty(
    27         Id = "Harmony.Command.SelectCommand",
    28         Name = "Select command",
    29         Description = "Click to select a command."
    30         )]
    31         public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
    32 
    33         /// <summary>
    34         /// 
    35         /// </summary>
    36         /// <returns></returns>
    37         public override string Brief()
    38         {
    39             string brief="Harmony: ";
    40 
    41             if (Program.HarmonyConfig != null)
    42             {
    43                 //What if the device ID is not there anymore?
    44                 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
    45             }
    46             else
    47             {
    48                 //No config found just show the device ID then.
    49                 brief += DeviceId;
    50             }
    51 
    52             // TODO: Fetch function label from command
    53             brief += " do " + Command;
    54 
    55             return brief;
    56         }
    57 
    58 
    59         protected override void DoConstruct()
    60         {
    61             base.DoConstruct();
    62 
    63             if (SelectCommand == null)
    64             {
    65                 SelectCommand = new PropertyButton { Text = "None"};
    66             }
    67             SelectCommand.ClickEventHandler = ClickEventHandler;
    68         }
    69 
    70         /// <summary>
    71         /// 
    72         /// </summary>
    73         protected override async Task DoExecute()
    74         {
    75             if (Program.HarmonyClient!=null)
    76             {
    77                 // Send our command and wait for it async
    78                 await Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);               
    79             }
    80             else
    81             {
    82                 Trace.WriteLine("WARNING: No Harmony client connection.");
    83             }            
    84         }
    85 
    86 
    87         /// <summary>
    88         /// 
    89         /// </summary>
    90         /// <returns></returns>
    91         public override bool IsValid()
    92         {
    93             if (Program.HarmonyConfig != null)
    94             {
    95                 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
    96                 {
    97                     if (d.Id.Equals(DeviceId))
    98                     {
    99                         foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
   100                         {
   101                             foreach (HarmonyHub.Function f in cg.Functions)
   102                             {
   103                                 if (f.Action.Command.Equals(Command))
   104                                 {
   105                                     //We found our device and our function
   106                                     return true;
   107                                 }
   108                             }
   109                         }
   110                     }
   111                 }
   112             }
   113 
   114             return false;
   115         }
   116 
   117         /// <summary>
   118         /// 
   119         /// </summary>
   120         /// <param name="sender"></param>
   121         /// <param name="e"></param>
   122         void ClickEventHandler(object sender, EventArgs e)
   123         {
   124             if (Program.HarmonyConfig == null)
   125             {
   126                 ErrBox.Show("No Harmony Hub configuration!");
   127                 return;
   128             }
   129 
   130             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   131             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   132             if (res == DialogResult.OK)
   133             {
   134                 DeviceId = dlg.DeviceId;
   135                 Command = dlg.Command;
   136                 SelectCommand.Text = Brief();
   137                 //Tell observer the object itself changed
   138                 OnPropertyChanged("Brief");
   139             }
   140 
   141         }
   142 
   143     }
   144 }