Published v1.3.4.0
authorStephaneLenclud
Wed, 05 Oct 2016 15:02:09 +0200
changeset 2705dcd226539d4
parent 269 8e32e51a2edd
child 271 24aec939b286
Published v1.3.4.0
Harmony connection retries.
Server/FormMain.Designer.cs
Server/FormMain.cs
Server/FormMain.resx
Server/SharpDisplayManager.csproj
Server/packages.config
     1.1 --- a/Server/FormMain.Designer.cs	Wed Sep 28 22:31:27 2016 +0200
     1.2 +++ b/Server/FormMain.Designer.cs	Wed Oct 05 15:02:09 2016 +0200
     1.3 @@ -44,7 +44,7 @@
     1.4              this.marqueeLabelTop = new SharpDisplayManager.MarqueeLabel();
     1.5              this.marqueeLabelBottom = new SharpDisplayManager.MarqueeLabel();
     1.6              this.fontDialog = new System.Windows.Forms.FontDialog();
     1.7 -            this.timer = new System.Windows.Forms.Timer(this.components);
     1.8 +            this.iTimerDisplay = new System.Windows.Forms.Timer(this.components);
     1.9              this.statusStrip = new System.Windows.Forms.StatusStrip();
    1.10              this.toolStripStatusLabelConnect = new System.Windows.Forms.ToolStripStatusLabel();
    1.11              this.toolStripStatusLabelSpring = new System.Windows.Forms.ToolStripStatusLabel();
    1.12 @@ -227,10 +227,10 @@
    1.13              this.marqueeLabelBottom.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    1.14              this.marqueeLabelBottom.UseCompatibleTextRendering = true;
    1.15              // 
    1.16 -            // timer
    1.17 +            // iTimerDisplay
    1.18              // 
    1.19 -            this.timer.Interval = 50;
    1.20 -            this.timer.Tick += new System.EventHandler(this.timer_Tick);
    1.21 +            this.iTimerDisplay.Interval = 50;
    1.22 +            this.iTimerDisplay.Tick += new System.EventHandler(this.timer_Tick);
    1.23              // 
    1.24              // statusStrip
    1.25              // 
    1.26 @@ -1352,7 +1352,7 @@
    1.27          #endregion
    1.28  
    1.29          private System.Windows.Forms.FontDialog fontDialog;
    1.30 -        private System.Windows.Forms.Timer timer;
    1.31 +        private System.Windows.Forms.Timer iTimerDisplay;
    1.32          private System.Windows.Forms.StatusStrip statusStrip;
    1.33          private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelConnect;
    1.34          private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelFps;
     2.1 --- a/Server/FormMain.cs	Wed Sep 28 22:31:27 2016 +0200
     2.2 +++ b/Server/FormMain.cs	Wed Oct 05 15:02:09 2016 +0200
     2.3 @@ -1033,7 +1033,12 @@
     2.4  
     2.5          }
     2.6  
     2.7 -        //This is our timer tick responsible to perform our render
     2.8 +        /// <summary>
     2.9 +        /// This is our timer tick responsible to perform our render
    2.10 +        /// TODO: Use a threading timer instead of a Windows form timer.
    2.11 +        /// </summary>
    2.12 +        /// <param name="sender"></param>
    2.13 +        /// <param name="e"></param>
    2.14          private void timer_Tick(object sender, EventArgs e)
    2.15          {
    2.16              //Update our animations
    2.17 @@ -1095,7 +1100,7 @@
    2.18  
    2.19              //Compute instant FPS
    2.20              toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " +
    2.21 -                                           (1000/timer.Interval).ToString() + " FPS";
    2.22 +                                           (1000/iTimerDisplay.Interval).ToString() + " FPS";
    2.23  
    2.24              LastTickTime = NewTickTime;
    2.25  
    2.26 @@ -1257,7 +1262,7 @@
    2.27              maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
    2.28              maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
    2.29              comboBoxDisplayType.SelectedIndex = cds.DisplayType;
    2.30 -            timer.Interval = cds.TimerInterval;
    2.31 +            iTimerDisplay.Interval = cds.TimerInterval;
    2.32              maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
    2.33              textBoxScrollLoopSeparator.Text = cds.Separator;
    2.34              //
    2.35 @@ -1563,27 +1568,27 @@
    2.36          private void StartTimer()
    2.37          {
    2.38              LastTickTime = DateTime.Now; //Reset timer to prevent jump
    2.39 -            timer.Enabled = true;
    2.40 +            iTimerDisplay.Enabled = true;
    2.41              UpdateSuspendButton();
    2.42          }
    2.43  
    2.44          private void StopTimer()
    2.45          {
    2.46              LastTickTime = DateTime.Now; //Reset timer to prevent jump
    2.47 -            timer.Enabled = false;
    2.48 +            iTimerDisplay.Enabled = false;
    2.49              UpdateSuspendButton();
    2.50          }
    2.51  
    2.52          private void ToggleTimer()
    2.53          {
    2.54              LastTickTime = DateTime.Now; //Reset timer to prevent jump
    2.55 -            timer.Enabled = !timer.Enabled;
    2.56 +            iTimerDisplay.Enabled = !iTimerDisplay.Enabled;
    2.57              UpdateSuspendButton();
    2.58          }
    2.59  
    2.60          private void UpdateSuspendButton()
    2.61          {
    2.62 -            if (!timer.Enabled)
    2.63 +            if (!iTimerDisplay.Enabled)
    2.64              {
    2.65                  buttonSuspend.Text = "Run";
    2.66              }
    2.67 @@ -2301,8 +2306,8 @@
    2.68  
    2.69                  if (interval > 0)
    2.70                  {
    2.71 -                    timer.Interval = interval;
    2.72 -                    cds.TimerInterval = timer.Interval;
    2.73 +                    iTimerDisplay.Interval = interval;
    2.74 +                    cds.TimerInterval = iTimerDisplay.Interval;
    2.75                      Properties.Settings.Default.Save();
    2.76                  }
    2.77              }
    2.78 @@ -3062,6 +3067,11 @@
    2.79  #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
    2.80          }
    2.81  
    2.82 +
    2.83 +
    2.84 +        int iHarmonyReconnectTries = 0;
    2.85 +        const int KHarmonyMaxReconnectTries = 10;
    2.86 +
    2.87          /// <summary>
    2.88          /// 
    2.89          /// </summary>
    2.90 @@ -3073,9 +3083,12 @@
    2.91                  await Program.HarmonyClient.CloseAsync();
    2.92              }
    2.93  
    2.94 +            bool success = false;
    2.95 +
    2.96              //Reset Harmony client & config
    2.97              Program.HarmonyClient = null;
    2.98              Program.HarmonyConfig = null;
    2.99 +            iTreeViewHarmony.Nodes.Clear();
   2.100  
   2.101              Trace.WriteLine("Harmony: Connecting... ");
   2.102              //First create our client and login
   2.103 @@ -3087,13 +3100,17 @@
   2.104              if (!string.IsNullOrEmpty(authToken) && !aForceAuth)
   2.105              {
   2.106                  Trace.WriteLine("Harmony: Reusing token: {0}", authToken);
   2.107 -                await Program.HarmonyClient.TryOpenAsync(authToken);
   2.108 +                success = await Program.HarmonyClient.TryOpenAsync(authToken);
   2.109              }
   2.110  
   2.111 -            if (!Program.HarmonyClient.IsReady)
   2.112 +            if (!Program.HarmonyClient.IsReady || !success 
   2.113 +                // Only first failure triggers new Harmony server AUTH
   2.114 +                // That's to avoid calling upon Logitech servers too often
   2.115 +                && iHarmonyReconnectTries == 0 )
   2.116              {
   2.117                  //We failed to connect using our token
   2.118                  //Delete it then
   2.119 +                Trace.WriteLine("Harmony: Reseting authentication token!");
   2.120                  Properties.Settings.Default.LogitechAuthToken = "";
   2.121                  Properties.Settings.Default.Save();
   2.122  
   2.123 @@ -3105,18 +3122,65 @@
   2.124                  }
   2.125  
   2.126                  Trace.WriteLine("Harmony: Authenticating with Logitech servers...");
   2.127 -                await Program.HarmonyClient.TryOpenAsync(iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text);
   2.128 +                success = await Program.HarmonyClient.TryOpenAsync(iTextBoxLogitechUserName.Text, iTextBoxLogitechPassword.Text);
   2.129                  //Persist our authentication token in our setting
   2.130 -                Properties.Settings.Default.LogitechAuthToken = Program.HarmonyClient.Token;
   2.131 -                Properties.Settings.Default.Save();
   2.132 +                if (success)
   2.133 +                {
   2.134 +                    Trace.WriteLine("Harmony: Saving authentication token.");
   2.135 +                    Properties.Settings.Default.LogitechAuthToken = Program.HarmonyClient.Token;
   2.136 +                    Properties.Settings.Default.Save();
   2.137 +                }
   2.138              }
   2.139 -
   2.140 -            //Fetch our config
   2.141 -            Program.HarmonyConfig = await Program.HarmonyClient.GetConfigAsync();
   2.142 -            PopulateTreeViewHarmony(Program.HarmonyConfig);
   2.143 -
   2.144 -            //Make sure harmony command actions are showing device name instead of device id
   2.145 -            PopulateTreeViewEvents(CurrentEarObject());
   2.146 +           
   2.147 +            // I've seen this failing with "Policy lookup failed on server".
   2.148 +            Program.HarmonyConfig = await Program.HarmonyClient.TryGetConfigAsync();
   2.149 +            if (Program.HarmonyConfig == null)
   2.150 +            {
   2.151 +                success = false;
   2.152 +            }
   2.153 +            else
   2.154 +            {
   2.155 +                // So we now have our Harmony Configuration
   2.156 +                PopulateTreeViewHarmony(Program.HarmonyConfig);
   2.157 +                // Make sure harmony command actions are showing device name instead of device id
   2.158 +                PopulateTreeViewEvents(CurrentEarObject());
   2.159 +            }
   2.160 +
   2.161 +            // TODO: Consider putting the retry logic one level higher in ResetHarmonyAsync
   2.162 +            if (!success)
   2.163 +            {
   2.164 +                // See if we need to keep trying 
   2.165 +                if (iHarmonyReconnectTries < KHarmonyMaxReconnectTries)
   2.166 +                {
   2.167 +                    iHarmonyReconnectTries++;
   2.168 +                    Trace.WriteLine("Harmony: Failed to connect, try again: " + iHarmonyReconnectTries);
   2.169 +                    await ConnectHarmonyAsync();
   2.170 +                }
   2.171 +                else
   2.172 +                {
   2.173 +                    Trace.WriteLine("Harmony: Failed to connect, giving up!");
   2.174 +                    iHarmonyReconnectTries = 0;
   2.175 +                    // TODO: Could use a data member as timer rather than a new instance.
   2.176 +                    // Try that again in 5 minutes then.
   2.177 +                    // Using Windows Form timer to make sure we run in the UI thread.
   2.178 +                    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
   2.179 +                    timer.Tick += async delegate (object sender, EventArgs e)
   2.180 +                    {
   2.181 +                        // Stop our timer first as we won't need it anymore
   2.182 +                        (sender as System.Windows.Forms.Timer).Stop();
   2.183 +                        // Then try to connect again
   2.184 +                        await ConnectHarmonyAsync();
   2.185 +                    };
   2.186 +                    timer.Interval = 300000;
   2.187 +                    timer.Start();
   2.188 +                }
   2.189 +            }
   2.190 +            else
   2.191 +            {
   2.192 +                // We are connected with a valid Harmony Configuration
   2.193 +                // Reset our tries counter then    
   2.194 +                iHarmonyReconnectTries = 0;
   2.195 +            }
   2.196          }
   2.197  
   2.198          /// <summary>
     3.1 --- a/Server/FormMain.resx	Wed Sep 28 22:31:27 2016 +0200
     3.2 +++ b/Server/FormMain.resx	Wed Oct 05 15:02:09 2016 +0200
     3.3 @@ -120,7 +120,7 @@
     3.4    <metadata name="fontDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     3.5      <value>17, 17</value>
     3.6    </metadata>
     3.7 -  <metadata name="timer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     3.8 +  <metadata name="iTimerDisplay.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     3.9      <value>126, 17</value>
    3.10    </metadata>
    3.11    <metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     4.1 --- a/Server/SharpDisplayManager.csproj	Wed Sep 28 22:31:27 2016 +0200
     4.2 +++ b/Server/SharpDisplayManager.csproj	Wed Oct 05 15:02:09 2016 +0200
     4.3 @@ -34,7 +34,7 @@
     4.4      <WebPage>index.htm</WebPage>
     4.5      <OpenBrowserOnPublish>false</OpenBrowserOnPublish>
     4.6      <ApplicationRevision>0</ApplicationRevision>
     4.7 -    <ApplicationVersion>1.3.2.0</ApplicationVersion>
     4.8 +    <ApplicationVersion>1.3.4.0</ApplicationVersion>
     4.9      <UseApplicationTrust>false</UseApplicationTrust>
    4.10      <CreateDesktopShortcut>true</CreateDesktopShortcut>
    4.11      <PublishWizardCompleted>true</PublishWizardCompleted>
    4.12 @@ -112,7 +112,7 @@
    4.13        <Private>True</Private>
    4.14      </Reference>
    4.15      <Reference Include="HarmonyHub, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
    4.16 -      <HintPath>..\packages\SharpLibHarmony.0.6.3\lib\net451\HarmonyHub.dll</HintPath>
    4.17 +      <HintPath>..\packages\SharpLibHarmony.0.6.5\lib\net451\HarmonyHub.dll</HintPath>
    4.18        <Private>True</Private>
    4.19      </Reference>
    4.20      <Reference Include="LibCecSharp, Version=2.2.0.0, Culture=neutral, processorArchitecture=x86">
     5.1 --- a/Server/packages.config	Wed Sep 28 22:31:27 2016 +0200
     5.2 +++ b/Server/packages.config	Wed Oct 05 15:02:09 2016 +0200
     5.3 @@ -4,7 +4,7 @@
     5.4    <package id="Loamen.agsXMPP" version="1.3.1" targetFramework="net46" />
     5.5    <package id="NAudio" version="1.7.3" targetFramework="net45" />
     5.6    <package id="SharpLibDisplay" version="0.2.6" targetFramework="net46" />
     5.7 -  <package id="SharpLibHarmony" version="0.6.3" targetFramework="net46" />
     5.8 +  <package id="SharpLibHarmony" version="0.6.5" targetFramework="net46" />
     5.9    <package id="SharpLibHid" version="1.4.2" targetFramework="net46" />
    5.10    <package id="SharpLibNotification" version="0.0.1" targetFramework="net46" />
    5.11    <package id="SharpLibWin32" version="0.0.9" targetFramework="net46" />