Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Mon, 29 Aug 2016 17:36:02 +0200
changeset 256 51b86efdc448
parent 253 2dae7a163fff
child 257 3f1d16d233dc
permissions -rw-r--r--
Published V.1.0.5.
Providing Harmony Hub reconnect.
     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 FunctionName { 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             brief += " do " + FunctionName;
    52 
    53             return brief;
    54         }
    55 
    56 
    57         protected override void DoConstruct()
    58         {
    59             base.DoConstruct();
    60 
    61             if (SelectCommand == null)
    62             {
    63                 SelectCommand = new PropertyButton { Text = "None"};
    64             }
    65             SelectCommand.ClickEventHandler = ClickEventHandler;
    66         }
    67 
    68         /// <summary>
    69         /// 
    70         /// </summary>
    71         protected override void DoExecute()
    72         {
    73             //Fire and forget our command
    74             //TODO: check if the harmony client connection is opened
    75             if (Program.HarmonyClient!=null)
    76             {
    77                 // Wait synchronously for now until we figure out how we could do async stuff in EAR.
    78                 // TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed.
    79                 Program.HarmonyClient.TrySendCommandAsync(DeviceId, FunctionName).Wait(10*1000);
    80             }
    81             else
    82             {
    83                 Trace.WriteLine("WARNING: No Harmony client connection.");
    84             }            
    85         }
    86 
    87 
    88         /// <summary>
    89         /// 
    90         /// </summary>
    91         /// <returns></returns>
    92         public override bool IsValid()
    93         {
    94             if (Program.HarmonyConfig != null)
    95             {
    96                 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
    97                 {
    98                     if (d.Id.Equals(DeviceId))
    99                     {
   100                         foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
   101                         {
   102                             foreach (HarmonyHub.Function f in cg.Functions)
   103                             {
   104                                 if (f.Name.Equals(FunctionName))
   105                                 {
   106                                     //We found our device and our function
   107                                     return true;
   108                                 }
   109                             }
   110                         }
   111                     }
   112                 }
   113             }
   114 
   115             return false;
   116         }
   117 
   118 
   119         void ClickEventHandler(object sender, EventArgs e)
   120         {
   121             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   122             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   123             if (res == DialogResult.OK)
   124             {
   125                 DeviceId = dlg.DeviceId;
   126                 FunctionName = dlg.FunctionName;
   127                 SelectCommand.Text = Brief();
   128                 //Tell observer the object itself changed
   129                 OnPropertyChanged("Brief");
   130             }
   131 
   132         }
   133 
   134     }
   135 }