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