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