Server/FormMain.cs
changeset 233 2b9541e54f7d
parent 232 5a739e2e5255
child 234 0c75dec19d39
     1.1 --- a/Server/FormMain.cs	Sat Aug 13 23:26:54 2016 +0200
     1.2 +++ b/Server/FormMain.cs	Mon Aug 15 12:11:26 2016 +0200
     1.3 @@ -233,6 +233,9 @@
     1.4              OnWndProc += iCecManager.OnWndProc;
     1.5              ResetCec();
     1.6  
     1.7 +            //Harmony
     1.8 +            ResetHarmony();
     1.9 +
    1.10              //Setup Events
    1.11              PopulateEventsTreeView();
    1.12  
    1.13 @@ -1265,6 +1268,10 @@
    1.14                  comboBoxOpticalDrives.SelectedIndex = 0;
    1.15              }
    1.16  
    1.17 +            //Harmony settings
    1.18 +            iCheckBoxHarmonyEnabled.Checked = Properties.Settings.Default.HarmonyEnabled;
    1.19 +            iTextBoxHarmonyHubAddress.Text = Properties.Settings.Default.HarmonyHubAddress;
    1.20 +
    1.21              //CEC settings
    1.22              checkBoxCecEnabled.Checked = Properties.Settings.Default.CecEnabled;
    1.23              comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
    1.24 @@ -2693,6 +2700,27 @@
    1.25          /// <summary>
    1.26          /// 
    1.27          /// </summary>
    1.28 +        private async void ResetHarmony()
    1.29 +        {
    1.30 +            // ConnectAsync already if we have an existing session cookie
    1.31 +            if (Properties.Settings.Default.HarmonyEnabled && File.Exists("SessionToken"))
    1.32 +            {
    1.33 +
    1.34 +                iButtonHarmonyConnect.Enabled = false;
    1.35 +                try
    1.36 +                {
    1.37 +                    await ConnectHarmonyAsync();
    1.38 +                }
    1.39 +                finally
    1.40 +                {
    1.41 +                    iButtonHarmonyConnect.Enabled = true;
    1.42 +                }
    1.43 +            }
    1.44 +        }
    1.45 +
    1.46 +        /// <summary>
    1.47 +        /// 
    1.48 +        /// </summary>
    1.49          private void SetupCecLogLevel()
    1.50          {
    1.51              //Setup log level
    1.52 @@ -3061,5 +3089,106 @@
    1.53              //Make sure our event tree never looses focus
    1.54              ((TreeView) sender).Focus();
    1.55          }
    1.56 +
    1.57 +        private async void iButtonHarmonyConnect_Click(object sender, EventArgs e)
    1.58 +        {
    1.59 +            //Save hub address
    1.60 +            Properties.Settings.Default.HarmonyHubAddress = iTextBoxHarmonyHubAddress.Text;
    1.61 +            Properties.Settings.Default.Save();
    1.62 +
    1.63 +            iButtonHarmonyConnect.Enabled = false;
    1.64 +            try
    1.65 +            {
    1.66 +                await ConnectHarmonyAsync();
    1.67 +            }
    1.68 +            catch (Exception)
    1.69 +            {
    1.70 +                iButtonHarmonyConnect.Enabled = true;
    1.71 +            }
    1.72 +
    1.73 +        }
    1.74 +
    1.75 +
    1.76 +        private async Task ConnectHarmonyAsync()
    1.77 +        {
    1.78 +            Console.WriteLine("Harmony: Connecting... ");
    1.79 +            //First create our client and login
    1.80 +            if (File.Exists("SessionToken"))
    1.81 +            {
    1.82 +                var sessionToken = File.ReadAllText("SessionToken");
    1.83 +                Console.WriteLine("Harmony: Reusing token: {0}", sessionToken);
    1.84 +                Program.HarmonyClient = HarmonyHub.HarmonyClient.Create(iTextBoxHarmonyHubAddress.Text, sessionToken);
    1.85 +            }
    1.86 +            else
    1.87 +            {
    1.88 +                if (string.IsNullOrEmpty(iTextBoxLogitechPassword.Text))
    1.89 +                {
    1.90 +                    Console.WriteLine("Harmony: Credentials missing!");
    1.91 +                    return;
    1.92 +                }
    1.93 +
    1.94 +                Console.WriteLine("Harmony: Authenticating with Logitech servers...");
    1.95 +                Program.HarmonyClient = await HarmonyHub.HarmonyClient.Create(iTextBoxHarmonyHubAddress.Text, iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text);
    1.96 +                File.WriteAllText("SessionToken", Program.HarmonyClient.Token);
    1.97 +            }
    1.98 +
    1.99 +            Console.WriteLine("Harmony: Fetching Harmony Hub configuration...");
   1.100 +
   1.101 +            //Fetch our config
   1.102 +            var harmonyConfig = await Program.HarmonyClient.GetConfigAsync();
   1.103 +            PopulateTreeViewHarmony(harmonyConfig);
   1.104 +
   1.105 +            Console.WriteLine("Harmony: Ready");
   1.106 +        }
   1.107 +
   1.108 +        /// <summary>
   1.109 +        /// 
   1.110 +        /// </summary>
   1.111 +        /// <param name="aConfig"></param>
   1.112 +        private void PopulateTreeViewHarmony(HarmonyHub.Entities.Response.Config aConfig)
   1.113 +        {
   1.114 +            iTreeViewHarmony.Nodes.Clear();
   1.115 +            //Add our devices
   1.116 +            foreach (HarmonyHub.Entities.Response.Device device in aConfig.Devices)
   1.117 +            {
   1.118 +                TreeNode deviceNode = iTreeViewHarmony.Nodes.Add(device.Id, $"{device.Label} ({device.DeviceTypeDisplayName}/{device.Model})");
   1.119 +                deviceNode.Tag = device;
   1.120 +
   1.121 +                foreach (HarmonyHub.Entities.Response.ControlGroup cg in device.ControlGroups)
   1.122 +                {
   1.123 +                    TreeNode cgNode = deviceNode.Nodes.Add(cg.Name);
   1.124 +                    cgNode.Tag = cg;
   1.125 +
   1.126 +                    foreach (HarmonyHub.Entities.Response.Function f in cg.Functions)
   1.127 +                    {
   1.128 +                        TreeNode fNode = cgNode.Nodes.Add(f.Name);
   1.129 +                        fNode.Tag = f;
   1.130 +                    }
   1.131 +                }
   1.132 +            }
   1.133 +
   1.134 +            //treeViewConfig.ExpandAll();
   1.135 +        }
   1.136 +
   1.137 +        private void iCheckBoxHarmonyEnabled_CheckedChanged(object sender, EventArgs e)
   1.138 +        {
   1.139 +            Properties.Settings.Default.HarmonyEnabled = iCheckBoxHarmonyEnabled.Checked;
   1.140 +            Properties.Settings.Default.Save();
   1.141 +        }
   1.142 +
   1.143 +        private async void iTreeViewHarmony_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
   1.144 +        {
   1.145 +            //Upon function node double click we execute it
   1.146 +            var tag = e.Node.Tag as HarmonyHub.Entities.Response.Function;
   1.147 +            if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Entities.Response.Device)
   1.148 +            {
   1.149 +                HarmonyHub.Entities.Response.Function f = tag;
   1.150 +                HarmonyHub.Entities.Response.Device d = (HarmonyHub.Entities.Response.Device)e.Node.Parent.Parent.Tag;
   1.151 +
   1.152 +                Console.WriteLine($"Harmony: Sending {f.Name} to {d.Label}...");
   1.153 +
   1.154 +                await Program.HarmonyClient.SendCommandAsync(d.Id, f.Name);
   1.155 +            }
   1.156 +        }
   1.157      }
   1.158  }