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