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