[chronojump] compujump logout improvements with new CompujumpAutologout class



commit 8c7d48f8676ed451f1c06b2343e9e405577d3c6b
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Mar 28 17:32:14 2018 +0200

    compujump logout improvements with new CompujumpAutologout class

 glade/app1.glade             |   42 +++++++++-
 src/gui/chronojump.cs        |   37 ++++-----
 src/gui/dialogPersonPopup.cs |    9 +-
 src/gui/encoder.cs           |   11 +++
 src/gui/networks.cs          |  185 ++++++++++++++++++++++++++++++++++++++---
 5 files changed, 240 insertions(+), 44 deletions(-)
---
diff --git a/glade/app1.glade b/glade/app1.glade
index 9b8d710..b7c0266 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -1643,6 +1643,12 @@
                                                             <placeholder/>
                                                             </child>
                                                             <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
                                                             <widget class="GtkLabel" 
id="label_start_selector_jumps">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -3772,6 +3778,17 @@ EncoderInertialCapture</property>
                                                   </packing>
                                                 </child>
                                                 <child>
+                                                  <widget class="GtkLabel" id="label_logout_seconds">
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">False</property>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">True</property>
+                                                    <property name="fill">True</property>
+                                                    <property name="position">2</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
                                                   <widget class="GtkLabel" id="label_rfid_wait">
                                                     <property name="can_focus">False</property>
                                                     <property name="label" translatable="yes">Please, 
Wait!</property>
@@ -3779,7 +3796,7 @@ EncoderInertialCapture</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">2</property>
+                                                    <property name="position">3</property>
                                                   </packing>
                                                 </child>
                                                 <child>
@@ -3919,7 +3936,7 @@ EncoderInertialCapture</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">3</property>
+                                                    <property name="position">4</property>
                                                   </packing>
                                                 </child>
                                               </widget>
@@ -17830,6 +17847,17 @@ Concentric</property>
                                                   </packing>
                                                 </child>
                                                 <child>
+                                                  <widget class="GtkLabel" id="label_logout_seconds_encoder">
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">False</property>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">2</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
                                                   <widget class="GtkLabel" id="label_rfid_encoder_wait">
                                                     <property name="can_focus">False</property>
                                                     <property name="label" translatable="yes">Please, 
wait!</property>
@@ -17837,7 +17865,7 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">2</property>
+                                                    <property name="position">3</property>
                                                   </packing>
                                                 </child>
                                                 <child>
@@ -17977,7 +18005,7 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">3</property>
+                                                    <property name="position">4</property>
                                                   </packing>
                                                 </child>
                                               </widget>
@@ -24907,6 +24935,12 @@ then click this button.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 95b97cb..d19c6c0 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -681,7 +681,6 @@ public partial class ChronoJumpWindow
                restTime = new RestTime();
                updatingRestTimes = true;
                GLib.Timeout.Add(1000, new GLib.TimeoutHandler(updateRestTimes)); //each s, better than 5s 
for don't have problems sorting data on treeview
-               timeMarginAfterCapture = DateTime.Now;
 
                // ------ Starting main window ------
 
@@ -4277,33 +4276,27 @@ public partial class ChronoJumpWindow
        }
 
        //called each second and after a test
-       DateTime timeMarginAfterCapture;
        bool updateRestTimes()
        {
                if(! updatingRestTimes)
                        return false;
 
                //Compujump manage autologout
-               if( configChronojump.Compujump && compujumpAutologout && currentPerson != null &&
-                               DateTime.Now.Subtract(currentPersonCompujumpLoginTime).TotalMinutes >= 3 && 
//login time minimum 3'
-                               restTime.CompujumpPersonNeedLogout(currentPerson.UniqueID) )                 
//3' since last executed test
+               if( currentPerson != null && configChronojump.Compujump && compujumpAutologout != null)
                {
-                       //if capturing runInterval or encoder, don't autologout
-                       if( ! networksRunIntervalCanChangePersonSQLReady ||
-                                       (eCapture != null && capturingCsharp == 
encoderCaptureProcess.CAPTURING) )
+                       if(compujumpAutologout.ShouldILogoutNow())
+                                       //restTime.CompujumpPersonNeedLogout(currentPerson.UniqueID),         
       //3' since last executed test
                        {
+                               compujumpPersonLogoutDo();
+
+                               label_logout_seconds.Text = "";
+                               label_logout_seconds_encoder.Text = "";
+                       } else {
                                /*
-                                * and set timeMarginAfterCapture variable to have a delay between
-                                * encoderCaptureProcess != CAPTURING and restTime is updated (after capture).
-                                * This will not allow to autologout just at end of capture, because it will 
be a margin of 10 seconds
-                                * and then also the restTime.CompujumpPersonNeedLogout will add 3 extra 
minutes
-                                */
-                                timeMarginAfterCapture = DateTime.Now;
-                       }
-                       else {
-                               TimeSpan span = DateTime.Now - timeMarginAfterCapture;
-                               if(span.TotalSeconds > 10)
-                                       compujumpPersonLogoutDo();
+                                * TODO: implement when it's nicer and only is displayed when 10 seconds 
remain
+                               label_logout_seconds.Text = compujumpAutologout.RemainingSeconds(false);
+                               label_logout_seconds_encoder.Text = 
compujumpAutologout.RemainingSeconds(false);
+                               */
                        }
                }
 
@@ -4703,7 +4696,8 @@ public partial class ChronoJumpWindow
                                check_run_interval_with_reaction_time.Active
                                );
 
-               networksRunIntervalCanChangePersonSQLReady = false;
+               if(compujumpAutologout != null)
+                       compujumpAutologout.StartCapturingRunInterval();
 
                //suitable for limited by tracks and time
                if(! canCaptureC)
@@ -4776,7 +4770,8 @@ public partial class ChronoJumpWindow
                //delete the temp tables if exists
                Sqlite.DeleteTempEvents("tempRunInterval");
 
-               networksRunIntervalCanChangePersonSQLReady = true;
+               if(compujumpAutologout != null)
+                       compujumpAutologout.EndCapturingRunInterval();
        }
 
        private void calculateSprintAndUpload()
diff --git a/src/gui/dialogPersonPopup.cs b/src/gui/dialogPersonPopup.cs
index 3a83c99..34b3f4d 100644
--- a/src/gui/dialogPersonPopup.cs
+++ b/src/gui/dialogPersonPopup.cs
@@ -55,7 +55,7 @@ public class DialogPersonPopup
        public bool Autologout;
 
        public DialogPersonPopup (int personID, string name, string rfid,
-                       List<Task> tasks, List<StationCount> stationsCount, bool serverConnected)
+                       List<Task> tasks, List<StationCount> stationsCount, bool serverConnected, bool 
Autologout)
        {
                Glade.XML gladeXML;
                gladeXML = Glade.XML.FromAssembly (Util.GetGladePath() + "dialog_person_popup.glade", 
"dialog_person_popup", null);
@@ -65,6 +65,7 @@ public class DialogPersonPopup
                UtilGtk.IconWindow(dialog_person_popup);
 
                Visible = true;
+               this.Autologout = Autologout;
 
                //1) Show top of the window widgets
                label_name.Text = "<b>" + name + "</b>";
@@ -111,10 +112,6 @@ public class DialogPersonPopup
                        }
                }
 
-               //set autologout by default is true
-               Autologout = true;
-               checkbutton_autologout.Active = true;
-
                //2) Show tasks stuff
                list_tasks_fixed = new List<Task>();
                list_hboxs_row = new List<Gtk.HBox>();
@@ -126,6 +123,8 @@ public class DialogPersonPopup
                Fake_button_person_logout = new Gtk.Button();
                Fake_button_person_autologout_changed = new Gtk.Button();
 
+               checkbutton_autologout.Active = Autologout;
+
                bool task_parametrized_exist = false;
                pixbuf = new Pixbuf (null, Util.GetImagePath(false) + "image_capture_big.png");
 
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 654def4..8027d6d 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -5292,6 +5292,8 @@ public partial class ChronoJumpWindow
                                deleteAllCapturedCurveFiles();
 
                                capturingCsharp = encoderCaptureProcess.CAPTURING;
+                               if(compujumpAutologout != null)
+                                       compujumpAutologout.StartCapturingEncoder();
                        }
 
 
@@ -5897,12 +5899,18 @@ public partial class ChronoJumpWindow
                        button_encoder_capture_finish_cont.Sensitive = false;
 
                        capturingCsharp = encoderCaptureProcess.STOPPED;
+
+                       if(compujumpAutologout != null)
+                               compujumpAutologout.EndCapturingEncoder();
                } else {        //STOPPED       
                        LogB.Debug("at pulseGTKEncoderCaptureAndCurves stopped");               
                        //do curves, capturingCsharp has ended
                        updatePulsebar(encoderActions.CURVES); //activity on pulsebar
                        //LogB.Debug(" Cur:", encoderThread.ThreadState.ToString());
                        LogB.Information(" Cur:" + encoderThread.ThreadState.ToString());
+
+                       if(compujumpAutologout != null)
+                               compujumpAutologout.EndCapturingEncoder();
                }
                        
                Thread.Sleep (50);
@@ -6565,6 +6573,9 @@ public partial class ChronoJumpWindow
                        if(encoderProcessCancel) {
                                encoder_pulsebar_analyze.Text = Catalog.GetString("Cancelled");
                        } else {
+                               if(compujumpAutologout != null)
+                                       compujumpAutologout.UpdateLastEncoderAnalyzeTime();
+
                                //TODO pensar en si s'ha de fer 1er amb mida petita i despres amb gran (en el 
zoom),
                                //o si es una sola i fa alguna edicio
                                
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 1db3c8c..bec64ce 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -42,6 +42,9 @@ public partial class ChronoJumpWindow
        //RFID
        [Widget] Gtk.Label label_rfid_wait;
        [Widget] Gtk.Label label_rfid_encoder_wait;
+
+       [Widget] Gtk.Label label_logout_seconds;
+       [Widget] Gtk.Label label_logout_seconds_encoder;
        
        //better raspberry controls
        [Widget] Gtk.Box hbox_encoder_capture_extra_mass_no_raspberry;
@@ -96,8 +99,9 @@ public partial class ChronoJumpWindow
        private static DateTime startedRFIDWait;
        private bool rfidProcessCancel;
        private bool rfidIsDifferent;
-       private DateTime currentPersonCompujumpLoginTime;
-       private bool compujumpAutologout;
+       //private bool compujumpAutologout;
+       //private static CompujumpAutologout compujumpAutologout;
+       private CompujumpAutologout compujumpAutologout;
 
        DialogPersonPopup dialogPersonPopup;
                
@@ -204,8 +208,7 @@ public partial class ChronoJumpWindow
                {
                        LogB.Information("RFID changed to: " + rfid.Captured);
 
-                       if( ! networksRunIntervalCanChangePersonSQLReady ||
-                                       (eCapture != null && capturingCsharp == 
encoderCaptureProcess.CAPTURING) )
+                       if(isCompujumpCapturing ())
                        {
                                startedRFIDWait = DateTime.Now;
                                LogB.Information("... but we are on the middle of capture");
@@ -222,8 +225,7 @@ public partial class ChronoJumpWindow
        {
                if(currentSession != null && rfid.Captured == capturedRFID)
                {
-                       if( ! networksRunIntervalCanChangePersonSQLReady ||
-                                       (eCapture != null && capturingCsharp == 
encoderCaptureProcess.CAPTURING) )
+                       if(isCompujumpCapturing ())
                        {
                                startedRFIDWait = DateTime.Now;
                                LogB.Information("... but we are on the middle of capture");
@@ -483,8 +485,7 @@ public partial class ChronoJumpWindow
                        label_rfid_encoder_wait.Visible = false;
                }
 
-               if( ! networksRunIntervalCanChangePersonSQLReady ||
-                               (eCapture != null && capturingCsharp == encoderCaptureProcess.CAPTURING) )
+               if(isCompujumpCapturing ())
                        return true;
 
                //---- end of checking if we are on the middle of capture.
@@ -560,7 +561,6 @@ public partial class ChronoJumpWindow
                                        SqlitePerson.Update(pLocal);
                                }
 
-                               currentPersonCompujumpLoginTime = DateTime.Now;
                                currentPerson = pLocal;
                                insertAndAssignPersonSessionIfNeeded(json);
 
@@ -593,7 +593,6 @@ public partial class ChronoJumpWindow
                        LogB.Information("RFID person exists locally!!");
                        if(rfidIsDifferent || dialogPersonPopup == null || ! dialogPersonPopup.Visible)
                        {
-                               currentPersonCompujumpLoginTime = DateTime.Now;
                                currentPerson = pLocal;
                                insertAndAssignPersonSessionIfNeeded(json);
 
@@ -643,6 +642,8 @@ public partial class ChronoJumpWindow
 
                if(pChangedShowTasks)
                {
+                       compujumpAutologout = new CompujumpAutologout();
+
                        /*TODO:
                        int rowToSelect = myTreeViewPersons.FindRow(currentPerson.UniqueID);
                        if(rowToSelect != -1)
@@ -682,6 +683,10 @@ public partial class ChronoJumpWindow
                if(currentPerson == null)
                        return;
 
+               //update login time
+               if(compujumpAutologout != null)
+                       compujumpAutologout.UpdateLoginTime();
+
                getTasksExercisesAndPopup();
        }
 
@@ -721,8 +726,6 @@ public partial class ChronoJumpWindow
                //3) get other stationsCount
                List<StationCount> stationsCount = 
json.GetOtherStationsWithPendingTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
 
-               compujumpAutologout = true;
-
                //4) show dialog
                showDialogPersonPopup(tasks, stationsCount, json.Connected);
        }
@@ -736,7 +739,8 @@ public partial class ChronoJumpWindow
                        dialogMessageNotAtServer.on_close_button_clicked(new object(), new EventArgs());
 
                dialogPersonPopup = new DialogPersonPopup(
-                               currentPerson.UniqueID, currentPerson.Name, capturedRFID, tasks, 
stationsCount, serverConnected);
+                               currentPerson.UniqueID, currentPerson.Name, capturedRFID, tasks, 
stationsCount,
+                               serverConnected, compujumpAutologout.Active);
 
                dialogPersonPopup.Fake_button_start_task.Clicked -= new EventHandler(compujumpTaskStart);
                dialogPersonPopup.Fake_button_start_task.Clicked += new EventHandler(compujumpTaskStart);
@@ -850,7 +854,16 @@ public partial class ChronoJumpWindow
 
        private void compujumpPersonAutoLogoutChanged(object o, EventArgs args)
        {
-               compujumpAutologout = dialogPersonPopup.Autologout;
+               compujumpAutologout.Active = dialogPersonPopup.Autologout;
+       }
+
+       //are we capturing runInterval or encoder?
+       private bool isCompujumpCapturing ()
+       {
+               if(compujumpAutologout == null)
+                       return false;
+
+               return(compujumpAutologout.IsCompujumpCapturing());
        }
 
        /*
@@ -974,4 +987,148 @@ public partial class ChronoJumpWindow
                }
        }
        */
+
+}
+
+//Class for manage autologout on Compujump
+//TODO: expand this class to manage better resttime and clearer
+public class CompujumpAutologout
+{
+       public bool Active;
+       public DateTime lastEncoderAnalyzeTime;
+
+       private bool capturingRunInterval;
+       private bool capturingEncoder;
+
+       private DateTime loginTime;
+       private DateTime lastRunIntervalTime;
+       private DateTime lastEncoderCaptureTime;
+       private int logoutMinutes = 3;
+
+       public CompujumpAutologout ()
+       {
+               Active = true;
+               loginTime = DateTime.Now;
+
+               capturingRunInterval = false;
+               capturingEncoder = false;
+
+               lastRunIntervalTime = DateTime.MinValue;
+               lastEncoderCaptureTime = DateTime.MinValue;
+               lastEncoderAnalyzeTime = DateTime.MinValue;
+       }
+
+       public void UpdateLoginTime()
+       {
+               loginTime = DateTime.Now;
+       }
+
+       public void StartCapturingRunInterval()
+       {
+               capturingRunInterval = true;
+       }
+       public void EndCapturingRunInterval()
+       {
+               capturingRunInterval = false;
+               lastRunIntervalTime = DateTime.Now;
+       }
+
+       public void StartCapturingEncoder()
+       {
+               capturingEncoder = true;
+       }
+       public void EndCapturingEncoder()
+       {
+               capturingEncoder = false;
+               lastEncoderCaptureTime = DateTime.Now;
+       }
+
+       public void UpdateLastEncoderAnalyzeTime()
+       {
+               lastEncoderAnalyzeTime = DateTime.Now;
+       }
+
+       public bool IsCompujumpCapturing()
+       {
+               return (capturingRunInterval || capturingEncoder);
+       }
+
+       //mSinceLogin: time in 'm'inutes since login
+       private double mSinceLogin()
+       {
+               return DateTime.Now.Subtract(loginTime).TotalMinutes;
+       }
+       private double mSinceRunInterval()
+       {
+               return DateTime.Now.Subtract(lastRunIntervalTime).TotalMinutes;
+       }
+       private double mSinceEncoderCapture()
+       {
+               return DateTime.Now.Subtract(lastEncoderCaptureTime).TotalMinutes;
+       }
+       private double mSinceEncoderAnalyze()
+       {
+               return DateTime.Now.Subtract(lastEncoderAnalyzeTime).TotalMinutes;
+       }
+
+       //decide if use has to be autologout now
+       public bool ShouldILogoutNow()
+                       //bool moreThanThreeMinutesSinceLastCapture
+       {
+               if(IsCompujumpCapturing())
+                       return false;
+
+               if(Active &&
+                               mSinceLogin() >= logoutMinutes &&
+       //                      moreThanThreeMinutesSinceLastCapture &&
+                               mSinceRunInterval() >= logoutMinutes &&
+                               mSinceEncoderCapture() >= logoutMinutes &&
+                               mSinceEncoderAnalyze() >= logoutMinutes
+                               )
+                       return true;
+
+               return false;
+       }
+
+       //showAll is for debug, user will see only one value
+       //TODO: separate between minutes and seconds and only display when remaining 10 seconds
+       public string RemainingSeconds(bool showAll)
+       {
+               if(! Active)
+                       return "";
+
+               double elapsed = logoutMinutes - mSinceLogin();
+
+               string strAll = "Logout in (minutes): " + string.Format("login: {0}", Math.Round(elapsed, 2));
+               double maxTime = elapsed;
+
+               if(lastRunIntervalTime > DateTime.MinValue)
+               {
+                       elapsed = logoutMinutes - mSinceRunInterval();
+                       strAll += string.Format("; lastRunI: {0}", Math.Round(elapsed, 2));
+                       if(elapsed > maxTime)
+                              maxTime = elapsed;
+               }
+
+               if(lastEncoderCaptureTime > DateTime.MinValue)
+               {
+                       elapsed = logoutMinutes - mSinceEncoderCapture();
+                       strAll += string.Format("; lastECapture {0}", Math.Round(elapsed, 2));
+                       if(elapsed > maxTime)
+                              maxTime = elapsed;
+               }
+
+               if(lastEncoderAnalyzeTime > DateTime.MinValue)
+               {
+                       elapsed = logoutMinutes - mSinceEncoderAnalyze();
+                       strAll += string.Format("; lastEAnalyze: {0}", Math.Round(elapsed, 2));
+                       if(elapsed > maxTime)
+                              maxTime = elapsed;
+               }
+
+               if(showAll)
+                       return strAll;
+               else
+                       return "Logout in\n" + Math.Round(maxTime, 2) + " min.";
+       }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]