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