Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Tue, 30 Aug 2016 03:07:59 +0200
changeset 257 3f1d16d233dc
parent 256 51b86efdc448
child 258 e237c2e33545
permissions -rw-r--r--
Using Harmony Function Action Command.
     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 void DoExecute()
    73         {
    74             //Fire and forget our command
    75             //TODO: check if the harmony client connection is opened
    76             if (Program.HarmonyClient!=null)
    77             {
    78                 // Wait synchronously for now until we figure out how we could do async stuff in EAR.
    79                 // TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed.
    80                 Task<bool> task = Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);
    81                 bool result = task.Result; //Synchronously waiting for result
    82                 Trace.WriteLine("ActionHarmonyCommand.DoExecute result: " + result.ToString());
    83             }
    84             else
    85             {
    86                 Trace.WriteLine("WARNING: No Harmony client connection.");
    87             }            
    88         }
    89 
    90 
    91         /// <summary>
    92         /// 
    93         /// </summary>
    94         /// <returns></returns>
    95         public override bool IsValid()
    96         {
    97             if (Program.HarmonyConfig != null)
    98             {
    99                 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
   100                 {
   101                     if (d.Id.Equals(DeviceId))
   102                     {
   103                         foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
   104                         {
   105                             foreach (HarmonyHub.Function f in cg.Functions)
   106                             {
   107                                 if (f.Action.Command.Equals(Command))
   108                                 {
   109                                     //We found our device and our function
   110                                     return true;
   111                                 }
   112                             }
   113                         }
   114                     }
   115                 }
   116             }
   117 
   118             return false;
   119         }
   120 
   121         /// <summary>
   122         /// 
   123         /// </summary>
   124         /// <param name="sender"></param>
   125         /// <param name="e"></param>
   126         void ClickEventHandler(object sender, EventArgs e)
   127         {
   128             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   129             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   130             if (res == DialogResult.OK)
   131             {
   132                 DeviceId = dlg.DeviceId;
   133                 Command = dlg.Command;
   134                 SelectCommand.Text = Brief();
   135                 //Tell observer the object itself changed
   136                 OnPropertyChanged("Brief");
   137             }
   138 
   139         }
   140 
   141     }
   142 }