Server/Actions/ActionHarmonyCommand.cs
author Stephane Lenclud
Mon, 22 Aug 2016 13:20:54 +0200
changeset 252 59ea5cb46258
parent 250 b2121d03f6f0
child 253 2dae7a163fff
permissions -rw-r--r--
Trying to clean up our Harmony handling.
     1 using Ear = SharpLib.Ear;
     2 using SharpLib.Ear;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 using System.Runtime.Serialization;
     9 using System.Windows.Forms;
    10 
    11 namespace SharpDisplayManager
    12 {
    13     [DataContract]
    14     [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
    15     class ActionHarmonyCommand : Ear.Action
    16     {
    17         [DataMember]
    18         public string DeviceId { get; set; } = "";
    19 
    20         [DataMember]
    21         public string FunctionName { get; set; } = "";
    22 
    23         [DataMember]
    24         [AttributeObjectProperty(
    25         Id = "Harmony.Command.SelectCommand",
    26         Name = "Select command",
    27         Description = "Click to select a command."
    28         )]
    29         public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
    30 
    31         /// <summary>
    32         /// 
    33         /// </summary>
    34         /// <returns></returns>
    35         public override string Brief()
    36         {
    37             string brief="Harmony: ";
    38 
    39             if (Program.HarmonyConfig != null)
    40             {
    41                 //What if the device ID is not there anymore?
    42                 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
    43             }
    44             else
    45             {
    46                 //No config found just show the device ID then.
    47                 brief += DeviceId;
    48             }
    49 
    50             brief += " do " + FunctionName;
    51 
    52             return brief;
    53         }
    54 
    55 
    56         protected override void DoConstruct()
    57         {
    58             base.DoConstruct();
    59 
    60             if (SelectCommand == null)
    61             {
    62                 SelectCommand = new PropertyButton { Text = "None"};
    63             }
    64             SelectCommand.ClickEventHandler = ClickEventHandler;
    65         }
    66 
    67         /// <summary>
    68         /// 
    69         /// </summary>
    70         protected override void DoExecute()
    71         {
    72             //Fire and forget our command
    73             //TODO: check if the harmony client connection is opened
    74             if (Program.HarmonyClient!=null)
    75             {
    76                 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
    77             }
    78             else
    79             {
    80                 Console.WriteLine("WARNING: No Harmony client connection.");
    81             }
    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.Name.Equals(FunctionName))
   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 
   117         void ClickEventHandler(object sender, EventArgs e)
   118         {
   119             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   120             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   121             if (res == DialogResult.OK)
   122             {
   123                 DeviceId = dlg.DeviceId;
   124                 FunctionName = dlg.FunctionName;
   125                 SelectCommand.Text = Brief();
   126                 //Tell observer the object itself changed
   127                 OnPropertyChanged("Brief");
   128             }
   129 
   130         }
   131 
   132     }
   133 }