Server/FormSelectHarmonyCommand.cs
author StephaneLenclud
Thu, 25 Aug 2016 13:34:05 +0200
changeset 255 c57b8ac80fc6
parent 250 b2121d03f6f0
child 257 3f1d16d233dc
permissions -rw-r--r--
Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
     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 FunctionName;
    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.Name);
    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.Name} to {d.Label}...");
    78 
    79                 await Program.HarmonyClient.SendCommandAsync(d.Id, f.Name);
    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                 FunctionName = f.Name;
   101             }
   102             else
   103             {
   104                 DeviceId = "";
   105                 FunctionName = "";
   106             }
   107 
   108         }
   109     }
   110 }