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