[chronojump] getEncoderExercise to getStationExercises



commit 133ae3f3c628fe3fd3baf70f3e3a15dc0eb51f6a
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Jul 10 23:32:10 2017 +0200

    getEncoderExercise to getStationExercises

 src/gui/networks.cs |   35 ++++++++-------------
 src/json.cs         |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 22 deletions(-)
---
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index e6122a3..43e70ab 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -576,38 +576,29 @@ public partial class ChronoJumpWindow
                Json json = new Json();
                List<Task> tasks = json.GetTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
 
-               //2) get exercises if needed
+               //2) get exercises and insert if needed (only on encoder)
                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)
+                       List<EncoderExercise> exRemote_list = 
json.GetStationExercises(configChronojump.CompujumpStationID);
+
+                       foreach(EncoderExercise exRemote in exRemote_list)
                        {
-                               bool exerciseOnLocal = false;
+                               bool found = false;
                                foreach(EncoderExercise exLocal in encoderExercisesOnLocal)
-                               {
-                                       if(task.ExerciseId == exLocal.uniqueID)
+                                       if(exLocal.uniqueID == exRemote.uniqueID)
                                        {
-                                               LogB.Information("Found:" + task.ExerciseId);
-                                               exerciseOnLocal = true;
+                                               found = 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);
-                                       }
+                               if(! found)
+                               {
+                                       SqliteEncoder.InsertExercise(
+                                                       false, exRemote.uniqueID, exRemote.name, 
exRemote.percentBodyWeight,
+                                                       "", "", ""); //ressitance, description, speed1RM
+                                       updateEncoderExercisesGui(exRemote.name);
                                }
                        }
                }
diff --git a/src/json.cs b/src/json.cs
index 0ba2fdc..fd3c4cd 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -615,7 +615,92 @@ public class Json
                return stations;
        }
 
+       public List<EncoderExercise> GetStationExercises(int stationId)
+       {
+               List<EncoderExercise> ex_list = new List<EncoderExercise>();
+
+               // Create a request using a URL that can receive a post.
+               WebRequest request = WebRequest.Create (serverUrl + "/getStationExercises");
+
+               // 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("stationId", stationId);
+
+               // 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_list;
+               }
+
+               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_list;
+               }
+
+               string responseFromServer;
+               using (var sr = new StreamReader(response.GetResponseStream()))
+               {
+                       responseFromServer = sr.ReadToEnd();
+               }
+
+               LogB.Information("GetStationExercises: " + responseFromServer);
+
+               if(responseFromServer == "")
+                       LogB.Information(" Empty "); //never happens
+               else if(responseFromServer == "[]")
+                       LogB.Information(" Empty2 "); //when rfid is not on server
+               else {
+                       ex_list = stationExercisesDeserialize(responseFromServer);
+               }
+
+               return ex_list;
+       }
+       private List<EncoderExercise> stationExercisesDeserialize(string str)
+       {
+               List<EncoderExercise> ex_list = new List<EncoderExercise>();
+
+               JsonValue jsonStationExercises = JsonValue.Parse (str);
+
+               foreach (JsonValue jsonSE in jsonStationExercises)
+               {
+                       Int32 id = jsonSE ["id"];
+                       string name = jsonSE ["name"];
+                       Int32 stationId = jsonSE ["stationId"];
+                       int percentBodyMassDisplaced = jsonSE ["percentBodyMassDisplaced"];
+
+                       ex_list.Add(new EncoderExercise(id, name, percentBodyMassDisplaced,
+                                       "", "", 0)); //ressitance, description, speed1RM
+               }
+               return ex_list;
+       }
+
+
+       /*
+        * Unused, now using the above methods
+        *
        public EncoderExercise GetEncoderExercise(int exerciseId)
        {
                EncoderExercise ex = new EncoderExercise();
@@ -691,6 +776,7 @@ public class Json
                return new EncoderExercise(id, name, percentBodyMassDisplaced,
                                "", "", 0); //ressitance, description, speed1RM
        }
+       */
 
        public bool UploadSprintData(int personId, Sprint sprint, double k, double vmax, double amax, double 
fmax, double pmax )
        {


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