Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Sun, 21 Aug 2016 18:35:58 +0200
changeset 250 b2121d03f6f0
parent 238 c92587ddabcd
child 251 f60cfcb98c9a
permissions -rw-r--r--
Adding Harmony command selection dialog.
     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         [AttributeObjectProperty(
    19             Id = "Harmony.Command.DeviceId",
    20             Name = "Device ID",
    21             Description = "The ID of the device this command is associated with."
    22         )]
    23         public string DeviceId { get; set; } = "";
    24 
    25 
    26         [DataMember]
    27         [AttributeObjectProperty(
    28         Id = "Harmony.Command.FunctionName",
    29         Name = "Function Name",
    30         Description = "The name of the function defining this command."
    31         )]
    32         public string FunctionName { get; set; } = "";
    33 
    34         [DataMember]
    35         [AttributeObjectProperty(
    36         Id = "Harmony.Command.SelectCommand",
    37         Name = "Select command",
    38         Description = "Click to select a command."
    39         )]
    40         public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
    41 
    42         /// <summary>
    43         /// 
    44         /// </summary>
    45         /// <returns></returns>
    46         public override string Brief()
    47         {
    48             string brief="Harmony: ";
    49 
    50             if (Program.HarmonyConfig != null)
    51             {
    52                 //What if the device ID is not there anymore?
    53                 brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
    54             }
    55             else
    56             {
    57                 //No config found just show the device ID then.
    58                 brief += DeviceId;
    59             }
    60 
    61             brief += " do " + FunctionName;
    62 
    63             return brief;
    64         }
    65 
    66 
    67         protected override void DoConstruct()
    68         {
    69             base.DoConstruct();
    70 
    71             if (SelectCommand == null)
    72             {
    73                 SelectCommand = new PropertyButton { Text = "None"};
    74             }
    75             SelectCommand.ClickEventHandler = ClickEventHandler;
    76         }
    77 
    78         /// <summary>
    79         /// 
    80         /// </summary>
    81         protected override void DoExecute()
    82         {
    83             //Fire and forget our command
    84             //TODO: check if the harmony client connection is opened
    85             if (Program.HarmonyClient!=null)
    86             {
    87                 Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
    88             }
    89             else
    90             {
    91                 Console.WriteLine("WARNING: No Harmony client connection.");
    92             }
    93             
    94         }
    95 
    96 
    97         /// <summary>
    98         /// 
    99         /// </summary>
   100         /// <returns></returns>
   101         public override bool IsValid()
   102         {
   103             if (Program.HarmonyConfig != null)
   104             {
   105                 foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
   106                 {
   107                     if (d.Id.Equals(DeviceId))
   108                     {
   109                         foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
   110                         {
   111                             foreach (HarmonyHub.Function f in cg.Functions)
   112                             {
   113                                 if (f.Name.Equals(FunctionName))
   114                                 {
   115                                     //We found our device and our function
   116                                     return true;
   117                                 }
   118                             }
   119                         }
   120                     }
   121                 }
   122             }
   123 
   124             return false;
   125         }
   126 
   127 
   128         void ClickEventHandler(object sender, EventArgs e)
   129         {
   130             FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
   131             DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
   132             if (res == DialogResult.OK)
   133             {
   134                 DeviceId = dlg.DeviceId;
   135                 FunctionName = dlg.FunctionName;
   136                 SelectCommand.Text = Brief();
   137                 //Tell observer the object itself changed
   138                 OnPropertyChanged("Brief");
   139             }
   140 
   141         }
   142 
   143     }
   144 }