Server/FormMain.cs
changeset 256 51b86efdc448
parent 255 c57b8ac80fc6
child 257 3f1d16d233dc
     1.1 --- a/Server/FormMain.cs	Thu Aug 25 13:34:05 2016 +0200
     1.2 +++ b/Server/FormMain.cs	Mon Aug 29 17:36:02 2016 +0200
     1.3 @@ -2982,6 +2982,18 @@
     1.4          }
     1.5  
     1.6          /// <summary>
     1.7 +        /// Called whenever we loose connection with our HarmonyHub.
     1.8 +        /// </summary>
     1.9 +        /// <param name="aRequestWasCancelled"></param>
    1.10 +        private void HarmonyConnectionClosedByServer(object aSender, bool aRequestWasCancelled)
    1.11 +        {
    1.12 +            //Try reconnect then
    1.13 +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
    1.14 +            ResetHarmonyAsync();
    1.15 +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
    1.16 +        }
    1.17 +
    1.18 +        /// <summary>
    1.19          /// 
    1.20          /// </summary>
    1.21          /// <returns></returns>
    1.22 @@ -2997,16 +3009,24 @@
    1.23              Program.HarmonyConfig = null;
    1.24  
    1.25              Trace.WriteLine("Harmony: Connecting... ");
    1.26 +            //First create our client and login
    1.27              Program.HarmonyClient = new HarmonyHub.Client(iTextBoxHarmonyHubAddress.Text);
    1.28 -            //First create our client and login
    1.29 +            Program.HarmonyClient.OnConnectionClosedByServer += HarmonyConnectionClosedByServer;
    1.30 +            
    1.31              if (File.Exists("SessionToken") && !aForceAuth)
    1.32              {
    1.33                  var sessionToken = File.ReadAllText("SessionToken");
    1.34                  Trace.WriteLine("Harmony: Reusing token: {0}", sessionToken);
    1.35 -                await Program.HarmonyClient.OpenAsync(sessionToken);
    1.36 +                await Program.HarmonyClient.TryOpenAsync(sessionToken);
    1.37              }
    1.38 -            else
    1.39 +
    1.40 +            if (!Program.HarmonyClient.IsReady)
    1.41              {
    1.42 +                //We failed to connect using our token
    1.43 +                //Delete it then
    1.44 +                File.Delete("SessionToken");
    1.45 +
    1.46 +                //Then try connect using our password
    1.47                  if (string.IsNullOrEmpty(iTextBoxLogitechPassword.Text))
    1.48                  {
    1.49                      Trace.WriteLine("Harmony: Credentials missing!");
    1.50 @@ -3014,18 +3034,13 @@
    1.51                  }
    1.52  
    1.53                  Trace.WriteLine("Harmony: Authenticating with Logitech servers...");
    1.54 -                await Program.HarmonyClient.OpenAsync(iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text);
    1.55 +                await Program.HarmonyClient.TryOpenAsync(iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text);
    1.56                  File.WriteAllText("SessionToken", Program.HarmonyClient.Token);
    1.57              }
    1.58  
    1.59 -            Trace.WriteLine("Harmony: Fetching Harmony Hub configuration...");
    1.60 -
    1.61              //Fetch our config
    1.62              Program.HarmonyConfig = await Program.HarmonyClient.GetConfigAsync();
    1.63              PopulateTreeViewHarmony(Program.HarmonyConfig);
    1.64 -
    1.65 -            Trace.WriteLine("Harmony: Ready");
    1.66 -
    1.67              //Make sure harmony command actions are showing device name instead of device id
    1.68              PopulateEventsTreeView();
    1.69          }