Server/Actions/ActionHarmonyCommand.cs
author StephaneLenclud
Wed, 17 Aug 2016 13:41:26 +0200
changeset 236 6ba20e02d04f
child 238 c92587ddabcd
permissions -rw-r--r--
Updating HarmonyHub to v0.3.0.
Adding untested 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@236
     9
StephaneLenclud@236
    10
namespace SharpDisplayManager
StephaneLenclud@236
    11
{
StephaneLenclud@236
    12
    [DataContract]
StephaneLenclud@236
    13
    [AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")]
StephaneLenclud@236
    14
    class ActionHarmonyCommand : Ear.Action
StephaneLenclud@236
    15
    {
StephaneLenclud@236
    16
        [DataMember]
StephaneLenclud@236
    17
        [AttributeObjectProperty(
StephaneLenclud@236
    18
            Id = "Harmony.Command.DeviceId",
StephaneLenclud@236
    19
            Name = "Device ID",
StephaneLenclud@236
    20
            Description = "The ID of the device this command is associated with."
StephaneLenclud@236
    21
        )]
StephaneLenclud@236
    22
        public string DeviceId { get; set; } = "";
StephaneLenclud@236
    23
StephaneLenclud@236
    24
StephaneLenclud@236
    25
        [DataMember]
StephaneLenclud@236
    26
        [AttributeObjectProperty(
StephaneLenclud@236
    27
        Id = "Harmony.Command.FunctionName",
StephaneLenclud@236
    28
        Name = "Function Name",
StephaneLenclud@236
    29
        Description = "The name of the function defining this command."
StephaneLenclud@236
    30
        )]
StephaneLenclud@236
    31
        public string FunctionName { get; set; } = "";
StephaneLenclud@236
    32
StephaneLenclud@236
    33
        /// <summary>
StephaneLenclud@236
    34
        /// 
StephaneLenclud@236
    35
        /// </summary>
StephaneLenclud@236
    36
        /// <returns></returns>
StephaneLenclud@236
    37
        public override string Brief()
StephaneLenclud@236
    38
        {
StephaneLenclud@236
    39
            string brief="";
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@236
    52
            brief += " do " + FunctionName;
StephaneLenclud@236
    53
StephaneLenclud@236
    54
            return brief;
StephaneLenclud@236
    55
        }
StephaneLenclud@236
    56
StephaneLenclud@236
    57
        /// <summary>
StephaneLenclud@236
    58
        /// 
StephaneLenclud@236
    59
        /// </summary>
StephaneLenclud@236
    60
        protected override void DoExecute()
StephaneLenclud@236
    61
        {
StephaneLenclud@236
    62
            //Fire and forget our command
StephaneLenclud@236
    63
            //TODO: check if the harmony client connection is opened
StephaneLenclud@236
    64
            if (Program.HarmonyClient!=null)
StephaneLenclud@236
    65
            {
StephaneLenclud@236
    66
                Program.HarmonyClient.SendCommandAsync(DeviceId, FunctionName);
StephaneLenclud@236
    67
            }
StephaneLenclud@236
    68
            else
StephaneLenclud@236
    69
            {
StephaneLenclud@236
    70
                Console.WriteLine("WARNING: No Harmony client connection.");
StephaneLenclud@236
    71
            }
StephaneLenclud@236
    72
            
StephaneLenclud@236
    73
        }
StephaneLenclud@236
    74
StephaneLenclud@236
    75
    }
StephaneLenclud@236
    76
}