Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Tue, 30 Aug 2016 03:07:59 +0200
changeset 257 3f1d16d233dc
parent 256 51b86efdc448
child 258 e237c2e33545
permissions -rw-r--r--
Using Harmony Function Action Command.
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@257
    22
        public string Command { 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@257
    51
            // TODO: Fetch function label from command
StephaneLenclud@257
    52
            brief += " do " + Command;
StephaneLenclud@236
    53
StephaneLenclud@236
    54
            return brief;
StephaneLenclud@236
    55
        }
StephaneLenclud@236
    56
StephaneLenclud@250
    57
StephaneLenclud@250
    58
        protected override void DoConstruct()
StephaneLenclud@250
    59
        {
StephaneLenclud@250
    60
            base.DoConstruct();
StephaneLenclud@250
    61
StephaneLenclud@250
    62
            if (SelectCommand == null)
StephaneLenclud@250
    63
            {
StephaneLenclud@250
    64
                SelectCommand = new PropertyButton { Text = "None"};
StephaneLenclud@250
    65
            }
StephaneLenclud@250
    66
            SelectCommand.ClickEventHandler = ClickEventHandler;
StephaneLenclud@250
    67
        }
StephaneLenclud@250
    68
StephaneLenclud@236
    69
        /// <summary>
StephaneLenclud@236
    70
        /// 
StephaneLenclud@236
    71
        /// </summary>
StephaneLenclud@236
    72
        protected override void DoExecute()
StephaneLenclud@236
    73
        {
StephaneLenclud@236
    74
            //Fire and forget our command
StephaneLenclud@236
    75
            //TODO: check if the harmony client connection is opened
StephaneLenclud@236
    76
            if (Program.HarmonyClient!=null)
StephaneLenclud@236
    77
            {
StephaneLenclud@256
    78
                // Wait synchronously for now until we figure out how we could do async stuff in EAR.
StephaneLenclud@256
    79
                // TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed.
StephaneLenclud@257
    80
                Task<bool> task = Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);
StephaneLenclud@257
    81
                bool result = task.Result; //Synchronously waiting for result
StephaneLenclud@257
    82
                Trace.WriteLine("ActionHarmonyCommand.DoExecute result: " + result.ToString());
StephaneLenclud@236
    83
            }
StephaneLenclud@236
    84
            else
StephaneLenclud@236
    85
            {
StephaneLenclud@253
    86
                Trace.WriteLine("WARNING: No Harmony client connection.");
StephaneLenclud@256
    87
            }            
StephaneLenclud@236
    88
        }
StephaneLenclud@236
    89
StephaneLenclud@250
    90
StephaneLenclud@250
    91
        /// <summary>
StephaneLenclud@250
    92
        /// 
StephaneLenclud@250
    93
        /// </summary>
StephaneLenclud@250
    94
        /// <returns></returns>
StephaneLenclud@250
    95
        public override bool IsValid()
StephaneLenclud@250
    96
        {
StephaneLenclud@250
    97
            if (Program.HarmonyConfig != null)
StephaneLenclud@250
    98
            {
StephaneLenclud@250
    99
                foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices)
StephaneLenclud@250
   100
                {
StephaneLenclud@250
   101
                    if (d.Id.Equals(DeviceId))
StephaneLenclud@250
   102
                    {
StephaneLenclud@250
   103
                        foreach (HarmonyHub.ControlGroup cg in d.ControlGroups)
StephaneLenclud@250
   104
                        {
StephaneLenclud@250
   105
                            foreach (HarmonyHub.Function f in cg.Functions)
StephaneLenclud@250
   106
                            {
StephaneLenclud@257
   107
                                if (f.Action.Command.Equals(Command))
StephaneLenclud@250
   108
                                {
StephaneLenclud@250
   109
                                    //We found our device and our function
StephaneLenclud@250
   110
                                    return true;
StephaneLenclud@250
   111
                                }
StephaneLenclud@250
   112
                            }
StephaneLenclud@250
   113
                        }
StephaneLenclud@250
   114
                    }
StephaneLenclud@250
   115
                }
StephaneLenclud@250
   116
            }
StephaneLenclud@250
   117
StephaneLenclud@250
   118
            return false;
StephaneLenclud@250
   119
        }
StephaneLenclud@250
   120
StephaneLenclud@257
   121
        /// <summary>
StephaneLenclud@257
   122
        /// 
StephaneLenclud@257
   123
        /// </summary>
StephaneLenclud@257
   124
        /// <param name="sender"></param>
StephaneLenclud@257
   125
        /// <param name="e"></param>
StephaneLenclud@250
   126
        void ClickEventHandler(object sender, EventArgs e)
StephaneLenclud@250
   127
        {
StephaneLenclud@250
   128
            FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
StephaneLenclud@250
   129
            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
StephaneLenclud@250
   130
            if (res == DialogResult.OK)
StephaneLenclud@250
   131
            {
StephaneLenclud@250
   132
                DeviceId = dlg.DeviceId;
StephaneLenclud@257
   133
                Command = dlg.Command;
StephaneLenclud@250
   134
                SelectCommand.Text = Brief();
StephaneLenclud@250
   135
                //Tell observer the object itself changed
StephaneLenclud@250
   136
                OnPropertyChanged("Brief");
StephaneLenclud@250
   137
            }
StephaneLenclud@250
   138
StephaneLenclud@250
   139
        }
StephaneLenclud@250
   140
StephaneLenclud@236
   141
    }
StephaneLenclud@236
   142
}