Server/FormSelectHarmonyCommand.cs
author StephaneLenclud
Tue, 30 Aug 2016 11:04:40 +0200
changeset 259 74a66917910a
parent 253 2dae7a163fff
child 262 c4749a27966d
permissions -rw-r--r--
Published v1.1.1.0
Fixing Harmony client reconnection to prevent exceptions.
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@250
    11
StephaneLenclud@250
    12
namespace SharpDisplayManager
StephaneLenclud@250
    13
{
StephaneLenclud@250
    14
    public partial class FormSelectHarmonyCommand : Form
StephaneLenclud@250
    15
    {
StephaneLenclud@250
    16
        public FormSelectHarmonyCommand()
StephaneLenclud@250
    17
        {
StephaneLenclud@250
    18
            InitializeComponent();
StephaneLenclud@250
    19
        }
StephaneLenclud@250
    20
StephaneLenclud@250
    21
        public string DeviceId;
StephaneLenclud@257
    22
        public string Command;
StephaneLenclud@250
    23
StephaneLenclud@250
    24
        /// <summary>
StephaneLenclud@250
    25
        /// 
StephaneLenclud@250
    26
        /// </summary>
StephaneLenclud@250
    27
        /// <param name="sender"></param>
StephaneLenclud@250
    28
        /// <param name="e"></param>
StephaneLenclud@250
    29
        private void FormSelectHarmonyCommand_Load(object sender, EventArgs e)
StephaneLenclud@250
    30
        {
StephaneLenclud@250
    31
            PopulateTreeViewHarmony(Program.HarmonyConfig);
StephaneLenclud@250
    32
        }
StephaneLenclud@250
    33
StephaneLenclud@250
    34
        /// <summary>
StephaneLenclud@250
    35
        /// 
StephaneLenclud@250
    36
        /// </summary>
StephaneLenclud@250
    37
        /// <param name="aConfig"></param>
StephaneLenclud@250
    38
        private void PopulateTreeViewHarmony(HarmonyHub.Config aConfig)
StephaneLenclud@250
    39
        {
StephaneLenclud@250
    40
            iTreeViewHarmony.Nodes.Clear();
StephaneLenclud@250
    41
            //Add our devices
StephaneLenclud@250
    42
            foreach (HarmonyHub.Device device in aConfig.Devices)
StephaneLenclud@250
    43
            {
StephaneLenclud@250
    44
                TreeNode deviceNode = iTreeViewHarmony.Nodes.Add(device.Id, $"{device.Label} ({device.DeviceTypeDisplayName}/{device.Model})");
StephaneLenclud@250
    45
                deviceNode.Tag = device;
StephaneLenclud@250
    46
StephaneLenclud@250
    47
                foreach (HarmonyHub.ControlGroup cg in device.ControlGroups)
StephaneLenclud@250
    48
                {
StephaneLenclud@250
    49
                    TreeNode cgNode = deviceNode.Nodes.Add(cg.Name);
StephaneLenclud@250
    50
                    cgNode.Tag = cg;
StephaneLenclud@250
    51
StephaneLenclud@250
    52
                    foreach (HarmonyHub.Function f in cg.Functions)
StephaneLenclud@250
    53
                    {
StephaneLenclud@257
    54
                        TreeNode fNode = cgNode.Nodes.Add(f.Label);
StephaneLenclud@250
    55
                        fNode.Tag = f;
StephaneLenclud@250
    56
                    }
StephaneLenclud@250
    57
                }
StephaneLenclud@250
    58
            }
StephaneLenclud@250
    59
StephaneLenclud@250
    60
            //treeViewConfig.ExpandAll();
StephaneLenclud@250
    61
        }
StephaneLenclud@250
    62
StephaneLenclud@250
    63
        /// <summary>
StephaneLenclud@250
    64
        /// 
StephaneLenclud@250
    65
        /// </summary>
StephaneLenclud@250
    66
        /// <param name="sender"></param>
StephaneLenclud@250
    67
        /// <param name="e"></param>
StephaneLenclud@250
    68
        private async void iTreeViewHarmony_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
StephaneLenclud@250
    69
        {
StephaneLenclud@250
    70
            //Upon function node double click we execute it, so that user can test
StephaneLenclud@250
    71
            var tag = e.Node.Tag as HarmonyHub.Function;
StephaneLenclud@250
    72
            if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device)
StephaneLenclud@250
    73
            {
StephaneLenclud@250
    74
                HarmonyHub.Function f = tag;
StephaneLenclud@250
    75
                HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag;
StephaneLenclud@250
    76
StephaneLenclud@257
    77
                Trace.WriteLine($"Harmony: Sending {f.Label} to {d.Label}...");
StephaneLenclud@250
    78
StephaneLenclud@257
    79
                await Program.HarmonyClient.TrySendKeyPressAsync(d.Id, f.Action.Command);
StephaneLenclud@250
    80
            }
StephaneLenclud@250
    81
        }
StephaneLenclud@250
    82
StephaneLenclud@250
    83
        /// <summary>
StephaneLenclud@250
    84
        /// 
StephaneLenclud@250
    85
        /// </summary>
StephaneLenclud@250
    86
        /// <param name="sender"></param>
StephaneLenclud@250
    87
        /// <param name="e"></param>
StephaneLenclud@250
    88
        private void iTreeViewHarmony_AfterSelect(object sender, TreeViewEventArgs e)
StephaneLenclud@250
    89
        {
StephaneLenclud@250
    90
            //Enable ok button if a function is selected
StephaneLenclud@250
    91
            iButtonOk.Enabled = e.Node.Tag is HarmonyHub.Function;
StephaneLenclud@250
    92
StephaneLenclud@250
    93
            // Get selected device ID and function name
StephaneLenclud@250
    94
            HarmonyHub.Function f = e.Node.Tag as HarmonyHub.Function;
StephaneLenclud@250
    95
            if (f != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device)
StephaneLenclud@250
    96
            {
StephaneLenclud@250
    97
                HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag;
StephaneLenclud@250
    98
StephaneLenclud@250
    99
                DeviceId = d.Id;
StephaneLenclud@257
   100
                Command = f.Action.Command;
StephaneLenclud@250
   101
            }
StephaneLenclud@250
   102
            else
StephaneLenclud@250
   103
            {
StephaneLenclud@250
   104
                DeviceId = "";
StephaneLenclud@257
   105
                Command = "";
StephaneLenclud@250
   106
            }
StephaneLenclud@250
   107
StephaneLenclud@250
   108
        }
StephaneLenclud@250
   109
    }
StephaneLenclud@250
   110
}