Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Thu, 25 Aug 2016 13:34:05 +0200
changeset 255 c57b8ac80fc6
parent 251 f60cfcb98c9a
child 256 51b86efdc448
permissions -rw-r--r--
Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
     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                 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
    78             }
    79             else
    80             {
    81                 Trace.WriteLine("WARNING: No Harmony client connection.");
    82             }
    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.Name.Equals(FunctionName))
   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 
   118         void ClickEventHandler(object sender, EventArgs e)
   119         {
   120             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   121             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   122             if (res == DialogResult.OK)
   123             {
   124                 DeviceId = dlg.DeviceId;
   125                 FunctionName = dlg.FunctionName;
   126                 SelectCommand.Text = Brief();
   127                 //Tell observer the object itself changed
   128                 OnPropertyChanged("Brief");
   129             }
   130 
   131         }
   132 
   133     }
   134 }