[chronojump] Compujump client manages RFID change on server



commit efc3bba8e2926114ba813236f48b40488e54fc6f
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Jul 3 19:51:46 2017 +0200

    Compujump client manages RFID change on server
    
    Assuming server will have soon an RFIDhistory table (personId, rfid)
    that will not allow to use an older RFID of another person.
    So a missing rfid wristband could not be used on the future.

 .../chronopicConnection-1-6-2-and-older.dia        |  Bin 5250 -> 5250 bytes
 .../chronopicConnection-1-6-2-and-older.png        |  Bin 0 -> 99719 bytes
 .../chronopicConnection-1-6-3.dia                  |  Bin 4439 -> 4439 bytes
 diagrams/processes/chronopicConnection-1-6-3.png   |  Bin 0 -> 37529 bytes
 .../{classes => processes}/encoder-threads.dia     |  Bin 5213 -> 5213 bytes
 diagrams/processes/rfid-local-read.dia             |  Bin 0 -> 3572 bytes
 diagrams/processes/rfid-local-read.png             |  Bin 0 -> 57160 bytes
 src/gui/chronojump.cs                              |    3 +-
 src/gui/networks.cs                                |  107 ++++++++++++--------
 src/person.cs                                      |   22 +++-
 src/sqlite/person.cs                               |    3 +-
 11 files changed, 87 insertions(+), 48 deletions(-)
---
diff --git a/diagrams/processes/chronopicConnection-1-6-2-and-older.png 
b/diagrams/processes/chronopicConnection-1-6-2-and-older.png
new file mode 100644
index 0000000..14c8493
Binary files /dev/null and b/diagrams/processes/chronopicConnection-1-6-2-and-older.png differ
diff --git a/diagrams/processes/chronopicConnection-1-6-3.png 
b/diagrams/processes/chronopicConnection-1-6-3.png
new file mode 100644
index 0000000..676a6da
Binary files /dev/null and b/diagrams/processes/chronopicConnection-1-6-3.png differ
diff --git a/diagrams/processes/rfid-local-read.dia b/diagrams/processes/rfid-local-read.dia
new file mode 100644
index 0000000..5e5c64b
Binary files /dev/null and b/diagrams/processes/rfid-local-read.dia differ
diff --git a/diagrams/processes/rfid-local-read.png b/diagrams/processes/rfid-local-read.png
new file mode 100644
index 0000000..cd6f26c
Binary files /dev/null and b/diagrams/processes/rfid-local-read.png differ
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 8d8a9ab..5ae5c13 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2486,13 +2486,14 @@ public partial class ChronoJumpWindow
                if (personAddModifyWin.CurrentPerson != null)
                {
                        currentPerson = personAddModifyWin.CurrentPerson;
+                       currentPersonSession = SqlitePersonSession.Select(currentPerson.UniqueID, 
currentSession.UniqueID);
+
                        person_added();
                }
        }
 
        private void person_added ()
        {
-               currentPersonSession = SqlitePersonSession.Select(currentPerson.UniqueID, 
currentSession.UniqueID);
                label_person_change();
                myTreeViewPersons.Add(currentPerson.UniqueID.ToString(), currentPerson.Name);
 
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 806bd50..8055291 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -413,17 +413,21 @@ public partial class ChronoJumpWindow
                label_rfid_contacts.Text = capturedRFID; //GTK
                label_rfid_encoder.Text = capturedRFID; //GTK
 
-               Person p = SqlitePerson.SelectByRFID(capturedRFID);
+               /*
+                * This method is shown on diagrams/processes/rfid-local-read.dia
+                */
+
+               Person pLocal = SqlitePerson.SelectByRFID(capturedRFID);
 
                bool currentPersonWasNull = (currentPerson == null);
-               bool pChanged = false;
+               bool pChangedShowTasks = false;
                Json json = new Json();
-               if(p.UniqueID == -1)
+               if(pLocal.UniqueID == -1)
                {
-                       LogB.Information("RFID person does not exist!!");
+                       LogB.Information("RFID person does not exist locally!!");
 
-                       p = json.GetPersonByRFID(capturedRFID);
-                       if(p.UniqueID == -1) {
+                       Person pServer = json.GetPersonByRFID(capturedRFID);
+                       if(pServer.UniqueID == -1) {
                                LogB.Information("Person NOT found on server!");
                                new DialogMessage(Constants.MessageTypes.WARNING,
                                                "Aquesta pulsera o jugador no es troba identificada al 
servidor"); //GTK
@@ -431,54 +435,60 @@ public partial class ChronoJumpWindow
                        else {
                                LogB.Information("Person found on server!");
 
-                               currentPerson = p;
-                               currentPersonSession = new PersonSession (
-                                               currentPerson.UniqueID, currentSession.UniqueID, 
-                                               json.LastPersonByRFIDHeight, json.LastPersonByRFIDWeight,
-                                               Constants.SportUndefinedID, 
-                                               Constants.SpeciallityUndefinedID, 
-                                               Constants.LevelUndefinedID,
-                                               "", false); //comments, dbconOpened
+                               //personID exists at local DB?
+                               //check if this uniqueID already exists on local database (would mean RFID 
changed on server)
+                               bool newPerson = false;
+                               pLocal = SqlitePerson.Select(false, pServer.UniqueID);
+
+                               if(pLocal.UniqueID == -1) {
+                                       newPerson = true;
+                               } else {
+                                       /*
+                                        * id exists, RFID has changed. Changed locally
+                                        * Note server don't allow having an rfid of a previous person. Must 
be historically unique.
+                                        */
+
+                                       pLocal.Future1 = pServer.Future1;
+                                       SqlitePerson.Update(pLocal);
+                               }
+
+                               currentPerson = pLocal;
+                               insertAndAssignPersonSessionIfNeeded(json);
 
-                               if(json.LastPersonByRFIDImageURL != "")
+                               if(newPerson)
                                {
-                                       bool downloaded = json.DownloadImage(json.LastPersonByRFIDImageURL, 
currentPerson.UniqueID);
-                                       if(downloaded)
-                                               File.Copy(
-                                                               Path.Combine(Path.GetTempPath(), 
currentPerson.UniqueID.ToString()),
-                                                               Util.GetPhotoFileName(false, 
currentPerson.UniqueID),
-                                                               true); //overwrite
-                               }
+                                       if(json.LastPersonByRFIDImageURL != "")
+                                       {
+                                               bool downloaded = 
json.DownloadImage(json.LastPersonByRFIDImageURL, currentPerson.UniqueID);
+                                               if(downloaded)
+                                                       File.Copy(
+                                                                       Path.Combine(Path.GetTempPath(), 
currentPerson.UniqueID.ToString()),
+                                                                       Util.GetPhotoFileName(false, 
currentPerson.UniqueID),
+                                                                       true); //overwrite
+                                       }
 
-                               person_added(); //GTK
-                               pChanged = true;
+                                       person_added(); //GTK
+                               } else {
+                                       personChanged(); //GTK
+                                       label_person_change();
+                               }
+                               pChangedShowTasks = true;
                        }
                }
                else {
-                       LogB.Information("RFID person exists!!");
-                       currentPerson = p;
-
-                       PersonSession ps = SqlitePersonSession.Select(false, p.UniqueID, 
currentSession.UniqueID);
-                       if(ps == null)
-                               currentPersonSession = new PersonSession (
-                                               p.UniqueID, currentSession.UniqueID,
-                                               json.LastPersonByRFIDHeight, json.LastPersonByRFIDWeight,
-                                               Constants.SportUndefinedID,
-                                               Constants.SpeciallityUndefinedID,
-                                               Constants.LevelUndefinedID,
-                                               "", false); //comments, dbconOpened
-                       else
-                               currentPersonSession = ps;
+                       LogB.Information("RFID person exists locally!!");
+                       currentPerson = pLocal;
+                       insertAndAssignPersonSessionIfNeeded(json);
 
                        personChanged(); //GTK
                        label_person_change();
-                       pChanged = true;
+                       pChangedShowTasks = true;
                }
 
                if(currentPersonWasNull)
                        sensitiveGuiYesPerson();
 
-               if(pChanged)
+               if(pChangedShowTasks)
                {
                        /*TODO:
                        int rowToSelect = myTreeViewPersons.FindRow(currentPerson.UniqueID);
@@ -492,11 +502,26 @@ public partial class ChronoJumpWindow
                updatingRFIDGuiStuff = false;
 
                Thread.Sleep (100);
-               LogB.Information(" threadRFID:" + threadRFID.ThreadState.ToString());
+               //LogB.Information(" threadRFID:" + threadRFID.ThreadState.ToString());
 
                return true;
        }
 
+       private void insertAndAssignPersonSessionIfNeeded(Json json)
+       {
+               PersonSession ps = SqlitePersonSession.Select(false, currentPerson.UniqueID, 
currentSession.UniqueID);
+               if(ps == null)
+                       currentPersonSession = new PersonSession (
+                                       currentPerson.UniqueID, currentSession.UniqueID,
+                                       json.LastPersonByRFIDHeight, json.LastPersonByRFIDWeight,
+                                       Constants.SportUndefinedID,
+                                       Constants.SpeciallityUndefinedID,
+                                       Constants.LevelUndefinedID,
+                                       "", false); //comments, dbconOpened
+               else
+                       currentPersonSession = ps;
+       }
+
        private void on_button_person_popup_clicked (object o, EventArgs args)
        {
                if(currentPerson == null)
diff --git a/src/person.cs b/src/person.cs
index 0681316..577c097 100644
--- a/src/person.cs
+++ b/src/person.cs
@@ -56,10 +56,16 @@ public class Person {
                this.description = "";
                this.future1 = rfid;
                this.serverUniqueID = Constants.ServerUndefinedID;
-               
-               SqlitePerson.Insert(false,  
-                               uniqueID.ToString(), name, sex, dateBorn, race, countryID,
-                               description, future1, serverUniqueID);
+
+               /*
+                * Before insertion check that uniqueID exists locally
+                * can happen when there are rfid changes on server
+                */
+               Person personTemp = SqlitePerson.Select(false, uniqueID);
+               if(personTemp.UniqueID == -1) //does not exist
+                       SqlitePerson.Insert(false,
+                                       uniqueID.ToString(), name, sex, dateBorn, race, countryID,
+                                       description, future1, serverUniqueID);
        }
 
        //suitable when we load a person from the database for being the current Person
@@ -186,7 +192,13 @@ public class Person {
                get { return description; }
                set { description = value; }
        }
-       
+
+       //rfid
+       public string Future1 {
+               get { return future1; }
+               set { future1 = value; }
+       }
+
        public int ServerUniqueID {
                get { return serverUniqueID; }
                set { serverUniqueID = value; }
diff --git a/src/sqlite/person.cs b/src/sqlite/person.cs
index 9f59fe5..aa25145 100644
--- a/src/sqlite/person.cs
+++ b/src/sqlite/person.cs
@@ -46,7 +46,7 @@ class SqlitePerson : Sqlite
                        "race INT, " + 
                        "countryID INT, " + 
                        "description TEXT, " +  
-                       "future1 TEXT, " +      
+                       "future1 TEXT, " + //rfid
                        "future2 TEXT, " +      
                        "serverUniqueID INT ) ";
                dbcmd.ExecuteNonQuery();
@@ -580,6 +580,7 @@ finishForeach:
                        "\", race = " + myPerson.Race +
                        ", countryID = " + myPerson.CountryID +
                        ", description = \"" + myPerson.Description +
+                       "\", future1 = \"" + myPerson.Future1 +                         //rfid
                        "\", serverUniqueID = " + myPerson.ServerUniqueID +
                        " WHERE uniqueID == " + myPerson.UniqueID;
                LogB.SQL(dbcmd.CommandText.ToString());


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