Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Wed, 31 Aug 2016 17:28:30 +0200
changeset 264 4a08e1b7ba64
parent 262 c4749a27966d
permissions -rw-r--r--
EAR: Actions now support multiple iterations.
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@262
    11
using CodeProject.Dialog;
StephaneLenclud@236
    12
StephaneLenclud@236
    13
namespace SharpDisplayManager
StephaneLenclud@236
    14
{
StephaneLenclud@236
    15
    [DataContract]
StephaneLenclud@236
    16
    [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
StephaneLenclud@236
    17
    class ActionHarmonyCommand : Ear.Action
StephaneLenclud@236
    18
    {
StephaneLenclud@236
    19
        [DataMember]
StephaneLenclud@236
    20
        public string DeviceId { get; set; } = "";
StephaneLenclud@236
    21
StephaneLenclud@236
    22
        [DataMember]
StephaneLenclud@257
    23
        public string Command { get; set; } = "";
StephaneLenclud@236
    24
StephaneLenclud@250
    25
        [DataMember]
StephaneLenclud@250
    26
        [AttributeObjectProperty(
StephaneLenclud@250
    27
        Id = "Harmony.Command.SelectCommand",
StephaneLenclud@250
    28
        Name = "Select command",
StephaneLenclud@250
    29
        Description = "Click to select a command."
StephaneLenclud@250
    30
        )]
StephaneLenclud@250
    31
        public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" };
StephaneLenclud@250
    32
StephaneLenclud@236
    33
        /// <summary>
StephaneLenclud@236
    34
        /// 
StephaneLenclud@236
    35
        /// </summary>
StephaneLenclud@236
    36
        /// <returns></returns>
StephaneLenclud@264
    37
        public override string BriefBase()
StephaneLenclud@236
    38
        {
StephaneLenclud@238
    39
            string brief="Harmony: ";
StephaneLenclud@236
    40
StephaneLenclud@236
    41
            if (Program.HarmonyConfig != null)
StephaneLenclud@236
    42
            {
StephaneLenclud@236
    43
                //What if the device ID is not there anymore?
StephaneLenclud@236
    44
                brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId);
StephaneLenclud@236
    45
            }
StephaneLenclud@236
    46
            else
StephaneLenclud@236
    47
            {
StephaneLenclud@236
    48
                //No config found just show the device ID then.
StephaneLenclud@236
    49
                brief += DeviceId;
StephaneLenclud@236
    50
            }
StephaneLenclud@236
    51
StephaneLenclud@257
    52
            // TODO: Fetch function label from command
StephaneLenclud@257
    53
            brief += " do " + Command;
StephaneLenclud@236
    54
StephaneLenclud@236
    55
            return brief;
StephaneLenclud@236
    56
        }
StephaneLenclud@236
    57
StephaneLenclud@250
    58
StephaneLenclud@250
    59
        protected override void DoConstruct()
StephaneLenclud@250
    60
        {
StephaneLenclud@250
    61
            base.DoConstruct();
StephaneLenclud@250
    62
StephaneLenclud@250
    63
            if (SelectCommand == null)
StephaneLenclud@250
    64
            {
StephaneLenclud@250
    65
                SelectCommand = new PropertyButton { Text = "None"};
StephaneLenclud@250
    66
            }
StephaneLenclud@250
    67
            SelectCommand.ClickEventHandler = ClickEventHandler;
StephaneLenclud@250
    68
        }
StephaneLenclud@250
    69
StephaneLenclud@236
    70
        /// <summary>
StephaneLenclud@236
    71
        /// 
StephaneLenclud@236
    72
        /// </summary>
StephaneLenclud@258
    73
        protected override async Task DoExecute()
StephaneLenclud@236
    74
        {
StephaneLenclud@236
    75
            if (Program.HarmonyClient!=null)
StephaneLenclud@236
    76
            {
StephaneLenclud@258
    77
                // Send our command and wait for it async
StephaneLenclud@258
    78
                await Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command);               
StephaneLenclud@236
    79
            }
StephaneLenclud@236
    80
            else
StephaneLenclud@236
    81
            {
StephaneLenclud@253
    82
                Trace.WriteLine("WARNING: No Harmony client connection.");
StephaneLenclud@256
    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@257
   103
                                if (f.Action.Command.Equals(Command))
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@257
   117
        /// <summary>
StephaneLenclud@257
   118
        /// 
StephaneLenclud@257
   119
        /// </summary>
StephaneLenclud@257
   120
        /// <param name="sender"></param>
StephaneLenclud@257
   121
        /// <param name="e"></param>
StephaneLenclud@250
   122
        void ClickEventHandler(object sender, EventArgs e)
StephaneLenclud@250
   123
        {
StephaneLenclud@262
   124
            if (Program.HarmonyConfig == null)
StephaneLenclud@262
   125
            {
StephaneLenclud@262
   126
                ErrBox.Show("No Harmony Hub configuration!");
StephaneLenclud@262
   127
                return;
StephaneLenclud@262
   128
            }
StephaneLenclud@262
   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@257
   135
                Command = dlg.Command;
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
}