Server/FormSelectHarmonyCommand.cs
author Stephane Lenclud
Fri, 02 Sep 2016 01:38:08 +0200
changeset 267 a601b377a3eb
parent 257 3f1d16d233dc
permissions -rw-r--r--
Event trigger by name now won't trigger disabled events.
StephaneLenclud@250
     1
using System;
StephaneLenclud@250
     2
using System.Collections.Generic;
StephaneLenclud@253
     3
using System.Diagnostics;
StephaneLenclud@250
     4
using System.ComponentModel;
StephaneLenclud@250
     5
using System.Data;
StephaneLenclud@250
     6
using System.Drawing;
StephaneLenclud@250
     7
using System.Linq;
StephaneLenclud@250
     8
using System.Text;
StephaneLenclud@250
     9
using System.Threading.Tasks;
StephaneLenclud@250
    10
using System.Windows.Forms;
StephaneLenclud@262
    11
using CodeProject.Dialog;
StephaneLenclud@250
    12
StephaneLenclud@250
    13
namespace SharpDisplayManager
StephaneLenclud@250
    14
{
StephaneLenclud@250
    15
    public partial class FormSelectHarmonyCommand : Form
StephaneLenclud@250
    16
    {
StephaneLenclud@250
    17
        public FormSelectHarmonyCommand()
StephaneLenclud@250
    18
        {
StephaneLenclud@250
    19
            InitializeComponent();
StephaneLenclud@250
    20
        }
StephaneLenclud@250
    21
StephaneLenclud@250
    22
        public string DeviceId;
StephaneLenclud@257
    23
        public string Command;
StephaneLenclud@250
    24
StephaneLenclud@250
    25
        /// <summary>
StephaneLenclud@250
    26
        /// 
StephaneLenclud@250
    27
        /// </summary>
StephaneLenclud@250
    28
        /// <param name="sender"></param>
StephaneLenclud@250
    29
        /// <param name="e"></param>
StephaneLenclud@250
    30
        private void FormSelectHarmonyCommand_Load(object sender, EventArgs e)
StephaneLenclud@250
    31
        {
StephaneLenclud@262
    32
            if (Program.HarmonyConfig != null)
StephaneLenclud@262
    33
            {
StephaneLenclud@262
    34
                PopulateTreeViewHarmony(Program.HarmonyConfig);
StephaneLenclud@262
    35
            }
StephaneLenclud@262
    36
            else
StephaneLenclud@262
    37
            {
StephaneLenclud@262
    38
                ErrBox.Show("No Harmony Hub configuration!");
StephaneLenclud@262
    39
                Close();
StephaneLenclud@262
    40
            }           
StephaneLenclud@250
    41
        }
StephaneLenclud@250
    42
StephaneLenclud@250
    43
        /// <summary>
StephaneLenclud@250
    44
        /// 
StephaneLenclud@250
    45
        /// </summary>
StephaneLenclud@250
    46
        /// <param name="aConfig"></param>
StephaneLenclud@250
    47
        private void PopulateTreeViewHarmony(HarmonyHub.Config aConfig)
StephaneLenclud@250
    48
        {
StephaneLenclud@250
    49
            iTreeViewHarmony.Nodes.Clear();
StephaneLenclud@250
    50
            //Add our devices
StephaneLenclud@250
    51
            foreach (HarmonyHub.Device device in aConfig.Devices)
StephaneLenclud@250
    52
            {
StephaneLenclud@250
    53
                TreeNode deviceNode = iTreeViewHarmony.Nodes.Add(device.Id, $"{device.Label} ({device.DeviceTypeDisplayName}/{device.Model})");
StephaneLenclud@250
    54
                deviceNode.Tag = device;
StephaneLenclud@250
    55
StephaneLenclud@250
    56
                foreach (HarmonyHub.ControlGroup cg in device.ControlGroups)
StephaneLenclud@250
    57
                {
StephaneLenclud@250
    58
                    TreeNode cgNode = deviceNode.Nodes.Add(cg.Name);
StephaneLenclud@250
    59
                    cgNode.Tag = cg;
StephaneLenclud@250
    60
StephaneLenclud@250
    61
                    foreach (HarmonyHub.Function f in cg.Functions)
StephaneLenclud@250
    62
                    {
StephaneLenclud@257
    63
                        TreeNode fNode = cgNode.Nodes.Add(f.Label);
StephaneLenclud@250
    64
                        fNode.Tag = f;
StephaneLenclud@250
    65
                    }
StephaneLenclud@250
    66
                }
StephaneLenclud@250
    67
            }
StephaneLenclud@250
    68
StephaneLenclud@250
    69
            //treeViewConfig.ExpandAll();
StephaneLenclud@250
    70
        }
StephaneLenclud@250
    71
StephaneLenclud@250
    72
        /// <summary>
StephaneLenclud@250
    73
        /// 
StephaneLenclud@250
    74
        /// </summary>
StephaneLenclud@250
    75
        /// <param name="sender"></param>
StephaneLenclud@250
    76
        /// <param name="e"></param>
StephaneLenclud@250
    77
        private async void iTreeViewHarmony_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
StephaneLenclud@250
    78
        {
StephaneLenclud@250
    79
            //Upon function node double click we execute it, so that user can test
StephaneLenclud@250
    80
            var tag = e.Node.Tag as HarmonyHub.Function;
StephaneLenclud@250
    81
            if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device)
StephaneLenclud@250
    82
            {
StephaneLenclud@250
    83
                HarmonyHub.Function f = tag;
StephaneLenclud@250
    84
                HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag;
StephaneLenclud@250
    85
StephaneLenclud@257
    86
                Trace.WriteLine($"Harmony: Sending {f.Label} to {d.Label}...");
StephaneLenclud@250
    87
StephaneLenclud@257
    88
                await Program.HarmonyClient.TrySendKeyPressAsync(d.Id, f.Action.Command);
StephaneLenclud@250
    89
            }
StephaneLenclud@250
    90
        }
StephaneLenclud@250
    91
StephaneLenclud@250
    92
        /// <summary>
StephaneLenclud@250
    93
        /// 
StephaneLenclud@250
    94
        /// </summary>
StephaneLenclud@250
    95
        /// <param name="sender"></param>
StephaneLenclud@250
    96
        /// <param name="e"></param>
StephaneLenclud@250
    97
        private void iTreeViewHarmony_AfterSelect(object sender, TreeViewEventArgs e)
StephaneLenclud@250
    98
        {
StephaneLenclud@250
    99
            //Enable ok button if a function is selected
StephaneLenclud@250
   100
            iButtonOk.Enabled = e.Node.Tag is HarmonyHub.Function;
StephaneLenclud@250
   101
StephaneLenclud@250
   102
            // Get selected device ID and function name
StephaneLenclud@250
   103
            HarmonyHub.Function f = e.Node.Tag as HarmonyHub.Function;
StephaneLenclud@250
   104
            if (f != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device)
StephaneLenclud@250
   105
            {
StephaneLenclud@250
   106
                HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag;
StephaneLenclud@250
   107
StephaneLenclud@250
   108
                DeviceId = d.Id;
StephaneLenclud@257
   109
                Command = f.Action.Command;
StephaneLenclud@250
   110
            }
StephaneLenclud@250
   111
            else
StephaneLenclud@250
   112
            {
StephaneLenclud@250
   113
                DeviceId = "";
StephaneLenclud@257
   114
                Command = "";
StephaneLenclud@250
   115
            }
StephaneLenclud@250
   116
StephaneLenclud@250
   117
        }
StephaneLenclud@250
   118
    }
StephaneLenclud@250
   119
}