[chronojump] Encoder exercise can be downloaded



commit a4eafbe41424f53304fa1d76e990a5f4eb97b878
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Jun 28 21:08:16 2017 +0200

    Encoder exercise can be downloaded

 src/encoder.cs        |    6 +++
 src/gui/encoder.cs    |   60 +++++++++++++++++------------
 src/gui/networks.cs   |   49 +++++++++++++++++++++++-
 src/json.cs           |  100 ++++++++++++++++++++++++++++++++++++++++++++----
 src/sqlite/encoder.cs |   20 ++++++---
 5 files changed, 193 insertions(+), 42 deletions(-)
---
diff --git a/src/encoder.cs b/src/encoder.cs
index 6eb7632..9f2ffba 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -918,6 +918,12 @@ public class EncoderExercise
                        return false;
        }
 
+       public override string ToString()
+       {
+               return uniqueID.ToString() + ": " + name + " (" + percentBodyWeight.ToString() + "%) " +
+                       ressistance + "," + description + "," + speed1RM.ToString();
+       }
+
        ~EncoderExercise() {}
 }
 
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 3a3c0d8..e171108 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -3417,8 +3417,15 @@ public partial class ChronoJumpWindow
        }
        
        //this is called also when an exercise is deleted to update the combo and the string []
-       protected void createEncoderComboExerciseAndAnalyze() {
+       protected void createEncoderComboExerciseAndAnalyze()
+       {
                ArrayList encoderExercises = SqliteEncoder.SelectEncoderExercises(false, -1, false);
+               if(encoderExercises.Count == 0)
+               {
+                       encoderExercisesTranslationAndBodyPWeight = new String [0];
+                       return;
+               }
+
                encoderExercisesTranslationAndBodyPWeight = new String [encoderExercises.Count];
                string [] exerciseNamesToCombo = new String [encoderExercises.Count];
                int i =0;
@@ -4020,7 +4027,7 @@ public partial class ChronoJumpWindow
                                                        "Error: An exercise named '{0}' already exists."), 
name));
                else {
                        if(adding)
-                               SqliteEncoder.InsertExercise(false, name, genericWin.SpinIntSelected, 
+                               SqliteEncoder.InsertExercise(false, -1, name, genericWin.SpinIntSelected,
                                                genericWin.Entry2Selected, genericWin.Entry3Selected,
                                                Util.ConvertToPoint(genericWin.SpinDouble2Selected)
                                                );
@@ -4030,33 +4037,38 @@ public partial class ChronoJumpWindow
                                                Util.ConvertToPoint(genericWin.SpinDouble2Selected)
                                                );
 
-                       ArrayList encoderExercises = SqliteEncoder.SelectEncoderExercises(false,-1, false);
-                       encoderExercisesTranslationAndBodyPWeight = new String [encoderExercises.Count];
-                       string [] exerciseNamesToCombo = new String [encoderExercises.Count];
-                       int i =0;
-                       foreach(EncoderExercise ex in encoderExercises) {
-                               string nameTranslated = ex.name;
-                               //do not translated user created exercises
-                               //this names are in SqliteEncoder.initializeTableEncoderExercise()
-                               if(ex.name == "Bench press" || ex.name == "Squat" || ex.name == "Jump")
-                                       nameTranslated = Catalog.GetString(ex.name);
-                               encoderExercisesTranslationAndBodyPWeight[i] = 
-                                       ex.uniqueID + ":" + ex.name + ":" + nameTranslated + ":" + 
ex.percentBodyWeight;
-                               exerciseNamesToCombo[i] = Catalog.GetString(ex.name);
-                               i++;
-                       }
-                       UtilGtk.ComboUpdate(combo_encoder_exercise_capture, exerciseNamesToCombo, "");
-                       combo_encoder_exercise_capture.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_capture, name);
-                       
-                       exerciseNamesToCombo = addAllExercisesToComboExerciseAnalyze(exerciseNamesToCombo);
-
-                       UtilGtk.ComboUpdate(combo_encoder_exercise_analyze, exerciseNamesToCombo, "");
-                       combo_encoder_exercise_analyze.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_analyze, name);
+                       updateEncoderExercisesGui(name);
 
                        genericWin.HideAndNull();
                        LogB.Information("done");
                }
        }
+
+       private void updateEncoderExercisesGui(string name)
+       {
+               ArrayList encoderExercises = SqliteEncoder.SelectEncoderExercises(false,-1, false);
+               encoderExercisesTranslationAndBodyPWeight = new String [encoderExercises.Count];
+               string [] exerciseNamesToCombo = new String [encoderExercises.Count];
+               int i =0;
+               foreach(EncoderExercise ex in encoderExercises) {
+                       string nameTranslated = ex.name;
+                       //do not translated user created exercises
+                       //this names are in SqliteEncoder.initializeTableEncoderExercise()
+                       if(ex.name == "Bench press" || ex.name == "Squat" || ex.name == "Jump")
+                               nameTranslated = Catalog.GetString(ex.name);
+                       encoderExercisesTranslationAndBodyPWeight[i] =
+                               ex.uniqueID + ":" + ex.name + ":" + nameTranslated + ":" + 
ex.percentBodyWeight;
+                       exerciseNamesToCombo[i] = Catalog.GetString(ex.name);
+                       i++;
+               }
+               UtilGtk.ComboUpdate(combo_encoder_exercise_capture, exerciseNamesToCombo, "");
+               combo_encoder_exercise_capture.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_capture, name);
+
+               exerciseNamesToCombo = addAllExercisesToComboExerciseAnalyze(exerciseNamesToCombo);
+
+               UtilGtk.ComboUpdate(combo_encoder_exercise_analyze, exerciseNamesToCombo, "");
+               combo_encoder_exercise_analyze.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_analyze, name);
+       }
        
        void on_button_encoder_exercise_delete (object o, EventArgs args) 
        {
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 23475dd..3992ce3 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -461,8 +461,7 @@ public partial class ChronoJumpWindow
                                selectRowTreeView_persons(treeview_persons, rowToSelect);
                        */
 
-                       List<Task> tasks = json.GetTasks(currentPerson.UniqueID, 
configChronojump.CompujumpStationID);
-                       showDialogPersonPopup(tasks);
+                       getTasksExercisesAndPopup();
                }
 
                updatingRFIDGuiStuff = false;
@@ -478,8 +477,52 @@ public partial class ChronoJumpWindow
                if(currentPerson == null)
                        return;
 
+               getTasksExercisesAndPopup();
+       }
+
+       private void getTasksExercisesAndPopup()
+       {
+               //1) get tasks
                Json json = new Json();
                List<Task> tasks = json.GetTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
+
+               //2) get exercises if needed
+               if(configChronojump.CompujumpStationMode == Constants.Menuitem_modes.POWERGRAVITATORY ||
+                               configChronojump.CompujumpStationMode == 
Constants.Menuitem_modes.POWERINERTIAL)
+               {
+                       ArrayList encoderExercisesOnLocal = SqliteEncoder.SelectEncoderExercises(false, -1, 
false);
+                       foreach(Task task in tasks)
+                       {
+                               bool exerciseOnLocal = false;
+                               foreach(EncoderExercise exLocal in encoderExercisesOnLocal)
+                               {
+                                       if(task.ExerciseId == exLocal.uniqueID)
+                                       {
+                                               LogB.Information("Found:" + task.ExerciseId);
+                                               exerciseOnLocal = true;
+                                               break;
+                                       }
+                               }
+                               if(! exerciseOnLocal)
+                               {
+                                       LogB.Information("Need to download this exercise:" + task.ExerciseId);
+                                       EncoderExercise exDownloaded = 
json.GetEncoderExercise(task.ExerciseId);
+                                       LogB.Information("Downloaded:" + exDownloaded.ToString());
+
+                                       //insert on database
+                                       if(exDownloaded.name != null && exDownloaded.name != "")
+                                       {
+                                               SqliteEncoder.InsertExercise(
+                                                               false, exDownloaded.uniqueID, 
exDownloaded.name, exDownloaded.percentBodyWeight,
+                                                               "", "", ""); //ressitance, description, 
speed1RM
+                                               encoderExercisesOnLocal.Add(exDownloaded);
+                                               updateEncoderExercisesGui(exDownloaded.name);
+                                       }
+                               }
+                       }
+               }
+
+               //3) show dialog
                showDialogPersonPopup(tasks);
        }
 
@@ -506,6 +549,8 @@ public partial class ChronoJumpWindow
                dialogPersonPopup.DestroyDialog();
                LogB.Information("Selected task from gui/networks.cs:" + task.ToString());
 
+               combo_encoder_exercise_capture.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_capture, task.ExerciseName);
+
                //laterality
                if(task.Laterality == "RL")
                        radio_encoder_laterality_both.Active = true;
diff --git a/src/json.cs b/src/json.cs
index bb8bc38..95f5661 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -473,9 +473,9 @@ public class Json
                        foreach(string str in strFull)
                        {
                                if(! str.StartsWith("{"))
-                                       strFull[i] = "{" + str;
+                                       strFull[i] = "{" + strFull[i];
                                if(! str.EndsWith("}"))
-                                       strFull[i] = str + "}";
+                                       strFull[i] = strFull[i] + "}";
 
                                list.Add(strFull[i]);
                                i ++;
@@ -499,17 +499,18 @@ public class Json
 
                        Int32 id = jsonTask ["id"];
                        char type = jsonTask ["type"];
+                       int exerciseId = jsonTask ["exerciseId"];
+                       string exerciseName = jsonTask ["exerciseName"];
 
                        if(type == 'F') //'F' Free
                        {
                                string comment = jsonTask ["comment"];
-                               list.Add(new Task(id, comment));
+                               list.Add(new Task(id, exerciseId, exerciseName, comment));
                        }
                        else //'P' Parametrized
                        {
                                int personId = jsonTask ["personId"];
                                int stationId = jsonTask ["stationId"];
-                               int exerciseId = jsonTask ["exerciseId"];
                                int sets = jsonTask ["sets"];
                                int nreps = jsonTask ["nreps"];
                                float load = jsonTask ["load"];
@@ -517,7 +518,7 @@ public class Json
                                float percentMaxSpeed = jsonTask ["percentMaxSpeed"];
                                string laterality = jsonTask ["laterality"];
                                string comment = jsonTask ["comment"];
-                               list.Add(new Task(id, personId, stationId, exerciseId,
+                               list.Add(new Task(id, personId, stationId, exerciseId, exerciseName,
                                                        sets, nreps, load, speed, percentMaxSpeed,
                                                        laterality, comment));
                        }
@@ -583,6 +584,83 @@ public class Json
                return true;
        }
 
+       public EncoderExercise GetEncoderExercise(int exerciseId)
+       {
+               EncoderExercise ex = new EncoderExercise();
+
+               // Create a request using a URL that can receive a post.
+               WebRequest request = WebRequest.Create (serverUrl + "/getEncoderExercise");
+
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/json; Charset=UTF-8"; //but this is not enough, see this 
line:
+
+               // Creates the json object
+               JsonObject json = new JsonObject();
+               json.Add("exerciseId", exerciseId);
+
+               // Converts it to a String
+               String js = json.ToString();
+
+               // Writes the json object into the request dataStream
+               Stream dataStream;
+               try {
+                       dataStream = request.GetRequestStream ();
+               } catch {
+                       this.ResultMessage =
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
+                               serverUrl);
+                       return ex;
+               }
+
+               dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
+
+               dataStream.Close ();
+
+               HttpWebResponse response;
+               try {
+                       response = (HttpWebResponse) request.GetResponse();
+               } catch {
+                       this.ResultMessage =
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
+                               serverUrl);
+                       return ex;
+               }
+
+               string responseFromServer;
+               using (var sr = new StreamReader(response.GetResponseStream()))
+               {
+                       responseFromServer = sr.ReadToEnd();
+               }
+
+               LogB.Information("GetEncoderExercise: " + responseFromServer);
+
+               if(responseFromServer == "")
+                       LogB.Information(" Empty "); //never happens
+               else if(responseFromServer == "[]")
+                       LogB.Information(" Empty2 "); //when rfid is not on server
+               else {
+                       ex = encoderExerciseDeserialize(responseFromServer);
+               }
+
+               return ex;
+       }
+       private EncoderExercise encoderExerciseDeserialize(string str)
+       {
+               JsonValue jsonEx = JsonValue.Parse(str);
+
+               Int32 id = jsonEx ["id"];
+               string name = jsonEx ["name"];
+               Int32 stationId = jsonEx ["stationId"];
+               int percentBodyMassDisplaced = jsonEx ["percentBodyMassDisplaced"];
+
+               return new EncoderExercise(id, name, percentBodyMassDisplaced,
+                               "", "", 0); //ressitance, description, speed1RM
+       }
+
+
        /*
        public bool UploadEncoderData()
        {
@@ -790,6 +868,7 @@ public class Task
        public int PersonId;
        public int StationId;
        public int ExerciseId;
+       public string ExerciseName;
        public int Sets;
        public int Nreps;
        public float Load;
@@ -804,15 +883,17 @@ public class Task
                Comment = "";
        }
 
-       public Task(int id, string comment)
+       public Task(int id, int exerciseId, string exerciseName, string comment)
        {
                Type = 'F'; //free
 
                Id = id;
+               ExerciseId = exerciseId;
+               ExerciseName = exerciseName;
                Comment = comment;
        }
 
-       public Task(int id, int personId, int stationId, int exerciseId,
+       public Task(int id, int personId, int stationId, int exerciseId, string exerciseName,
                        int sets, int nreps, float load, float speed, float percentMaxSpeed,
                        string laterality, string comment)
        {
@@ -822,6 +903,7 @@ public class Task
                PersonId = personId;
                StationId = stationId;
                ExerciseId = exerciseId;
+               ExerciseName = exerciseName;
                Sets = sets;
                Nreps = nreps;
                Load = load;
@@ -834,7 +916,7 @@ public class Task
        public override string ToString()
        {
                if(Type == 'F')
-                       return Id.ToString() + ": " + Comment;
+                       return ExerciseName + ": " + Comment;
                else {
                        string sep = "";
                        string str = "";
@@ -872,7 +954,7 @@ public class Task
                        {
                                str += "\n" + Comment;
                        }
-                       return Id.ToString() + ": " + str;
+                       return ExerciseName + ": " + str;
                }
        }
 }
diff --git a/src/sqlite/encoder.cs b/src/sqlite/encoder.cs
index bf1caf7..7ee492a 100644
--- a/src/sqlite/encoder.cs
+++ b/src/sqlite/encoder.cs
@@ -733,16 +733,22 @@ class SqliteEncoder : Sqlite
                        "future3 TEXT )";       //weightAngle (unused)
                dbcmd.ExecuteNonQuery();
        }
-       
-       public static void InsertExercise(bool dbconOpened, string name, int percentBodyWeight, 
+
+       //if uniqueID == -1, NULL will be used (correlative uniqueID)
+       //uniqueID != -1 when an exercise is downloaded from server on compujump and need to have the same 
uniqueID as server
+       public static void InsertExercise(bool dbconOpened, int uniqueID, string name, int percentBodyWeight,
                        string ressistance, string description, string speed1RM)         //speed1RM decimal 
point = '.'
        {
                if(! dbconOpened)
                        Sqlite.Open();
 
+               string uniqueIDStr = "NULL";
+               if(uniqueID != -1)
+                       uniqueIDStr = uniqueID.ToString();
+
                dbcmd.CommandText = "INSERT INTO " + Constants.EncoderExerciseTable +  
                                " (uniqueID, name, percentBodyWeight, ressistance, description, future1, 
future2, future3)" +
-                               " VALUES (NULL, \"" + name + "\", " + percentBodyWeight + ", \"" + 
+                               " VALUES (" + uniqueIDStr + ", \"" + name + "\", " + percentBodyWeight + ", 
\"" +
                                ressistance + "\", \"" + description + "\", \"" + speed1RM + "\", '', '')";
                LogB.SQL(dbcmd.CommandText.ToString());
                dbcmd.ExecuteNonQuery();
@@ -764,7 +770,7 @@ class SqliteEncoder : Sqlite
                
                foreach(string line in iniEncoderExercises) {
                        string [] parts = line.Split(new char[] {':'});
-                       InsertExercise(true, parts[0], Convert.ToInt32(parts[1]), parts[2], parts[3], 
parts[4]);
+                       InsertExercise(true, -1, parts[0], Convert.ToInt32(parts[1]), parts[2], parts[3], 
parts[4]);
                }
 
                addEncoderFreeExercise();
@@ -776,13 +782,13 @@ class SqliteEncoder : Sqlite
        {
                bool exists = Sqlite.Exists (true, Constants.EncoderExerciseTable, "Free");
                if(! exists)
-                       InsertExercise(true, "Free", 0, "", "", "");
+                       InsertExercise(true, -1, "Free", 0, "", "", "");
        }
        protected internal static void addEncoderJumpExercise()
        {
                bool exists = Sqlite.Exists (true, Constants.EncoderExerciseTable, "Jump");
                if(! exists)
-                       InsertExercise(true, "Jump", 100, "", "", "");
+                       InsertExercise(true, -1, "Jump", 100, "", "", "");
        }
        protected internal static void addEncoderInclinedExercises()
        {
@@ -794,7 +800,7 @@ class SqliteEncoder : Sqlite
                
                foreach(string line in iniEncoderExercises) {
                        string [] parts = line.Split(new char[] {':'});
-                       InsertExercise(true, parts[0], Convert.ToInt32(parts[1]), parts[2], parts[3], 
parts[4]);
+                       InsertExercise(true, -1, parts[0], Convert.ToInt32(parts[1]), parts[2], parts[3], 
parts[4]);
                }
        }
 


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