[chronojump] Networks GetTask to 3.1, but get exercises temporarily disabled



commit cd5141896918352dfd5056f651a7756eea1bf033
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Aug 12 23:05:27 2021 +0200

    Networks GetTask to 3.1, but get exercises temporarily disabled

 src/gui/dialogPersonPopup.cs | 14 ++++++---
 src/gui/networks.cs          | 43 +++++++++++++++++--------
 src/json/compujump.cs        | 40 +++++------------------
 src/json/compujumpOther.cs   | 75 +++++++++++++++++++++++++++++++++++++++-----
 src/json/json.cs             |  3 +-
 5 files changed, 115 insertions(+), 60 deletions(-)
---
diff --git a/src/gui/dialogPersonPopup.cs b/src/gui/dialogPersonPopup.cs
index 929273f90..4ca7af8a4 100644
--- a/src/gui/dialogPersonPopup.cs
+++ b/src/gui/dialogPersonPopup.cs
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * Copyright (C) 2016-2020   Xavier de Blas <xaviblas gmail com> 
+ * Copyright (C) 2016-2021   Xavier de Blas <xaviblas gmail com>
  */
 
 using System;
@@ -131,7 +131,7 @@ public class DialogPersonPopup
                list_buttons_start = new List<Gtk.Button>();
                list_buttons_done = new List<Gtk.Button>();
                list_buttons_done_id = new List<int>();
-               taskActive = new Task();
+               taskActive = null;
                Fake_button_start_task = new Gtk.Button();
                Fake_button_person_logout = new Gtk.Button();
                Fake_button_person_autologout_changed = new Gtk.Button();
@@ -142,21 +142,25 @@ public class DialogPersonPopup
                pixbuf = new Pixbuf (null, Util.GetImagePath(false) + "image_capture_big.png");
 
                Gtk.HBox hboxRow;
-               foreach(Task t in tasks)
+               foreach(Task tAbstract in tasks)
                {
+                       //TODO: cast depending on type, not all them encoder ;)
+                       //if POWERGRAVITATORY || POWERINERTIAL
+                       TaskEncoder t = (TaskEncoder) tAbstract;
+
                        hboxRow = new Gtk.HBox(false, 10);
 
                        if(t.Laterality == "R,L")
                        {
                                Gtk.VBox vboxRL = new Gtk.VBox(false, 4);
 
-                               Task taskCopy = new Task(t.Id, t.PersonId, t.ExerciseId, t.ExerciseName,
+                               TaskEncoder taskCopy = new TaskEncoder (t.Id, t.PersonId, t.ExerciseId, 
t.ExerciseName,
                                        t.Sets, t.Nreps, t.Load, t.Speed, t.PercentMaxSpeed,
                                        "R", t.Comment);
                                Gtk.HBox hboxStartAndLabel = createHBoxStartAndLabel(taskCopy, pixbuf);
                                vboxRL.PackStart(hboxStartAndLabel, false, false, 0);
 
-                               taskCopy = new Task(t.Id, t.PersonId, t.ExerciseId, t.ExerciseName,
+                               taskCopy = new TaskEncoder (t.Id, t.PersonId, t.ExerciseId, t.ExerciseName,
                                        t.Sets, t.Nreps, t.Load, t.Speed, t.PercentMaxSpeed,
                                        "L", t.Comment);
                                hboxStartAndLabel = createHBoxStartAndLabel(taskCopy, pixbuf);
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index b84fbe51d..8531debdf 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * Copyright (C) 2004-2020   Xavier de Blas <xaviblas gmail com> 
+ * Copyright (C) 2004-2021   Xavier de Blas <xaviblas gmail com>
  */
 
 
@@ -965,9 +965,21 @@ public partial class ChronoJumpWindow
        {
                //1) get tasks
                JsonCompujump json = new JsonCompujump(configChronojump.CompujumpDjango);
-               List<Task> tasks = json.GetTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
+               string tasksStr = json.GetTasksResponse (currentPerson.UniqueID, 
configChronojump.CompujumpStationID);
 
-               //2) get exercises and insert if needed (only on encoder)
+               List<Task> tasks = new List<Task>();
+
+               //2) deserialize tasks
+               if(tasksStr != "")
+               {
+                       if(configChronojump.CompujumpStationMode == Constants.Modes.POWERGRAVITATORY ||
+                                       configChronojump.CompujumpStationMode == 
Constants.Modes.POWERINERTIAL)
+                               tasks = new TaskDeserialize().DeserializeTaskEncoder(tasksStr);
+               }
+
+               /*
+               TODO: disabled temporarily
+               //3) get exercises and insert if needed (only on encoder)
                if(configChronojump.CompujumpStationMode == Constants.Modes.POWERGRAVITATORY ||
                                configChronojump.CompujumpStationMode == Constants.Modes.POWERINERTIAL)
                {
@@ -997,19 +1009,20 @@ public partial class ChronoJumpWindow
                                }
                        }
                }
+               */
 
-               //3) get other stationsCount
+               //4) get other stationsCount
                List<StationCount> stationsCount = 
json.GetOtherStationsWithPendingTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
 
-               //4) check if there are active Internet devices
+               //5) check if there are active Internet devices
                NetworksCheckDevices ncd = new NetworksCheckDevices();
 
-               //5) If disconnected, make check_encoder_networks_upload false and insensitive
+               //6) If disconnected, make check_encoder_networks_upload false and insensitive
                check_encoder_networks_upload.Sensitive = json.Connected;
                if(! json.Connected)
                        check_encoder_networks_upload.Active = false;
 
-               //6) show dialog
+               //7) show dialog
                showDialogPersonPopup(tasks, stationsCount, ncd.ToString(), json.Connected);
        }
 
@@ -1024,6 +1037,7 @@ public partial class ChronoJumpWindow
        }
        */
        private void showDialogPersonPopup(List<Task> tasks, List<StationCount> stationsCount, string 
networkDevices, bool serverConnected)
+       //private void showDialogPersonPopup(TaskList taskList, List<StationCount> stationsCount, string 
networkDevices, bool serverConnected)
        {
                if(dialogPersonPopup != null)
                        dialogPersonPopup.DestroyDialog();
@@ -1049,18 +1063,21 @@ public partial class ChronoJumpWindow
        private void compujumpTaskStart(object o, EventArgs args)
        {
                dialogPersonPopup.Fake_button_start_task.Clicked -= new EventHandler(compujumpTaskStart);
-               Task task = new Task();
+               Task task = null;
                if(dialogPersonPopup != null)
                {
                        task = dialogPersonPopup.TaskActive;
                }
                dialogPersonPopup.DestroyDialog();
-               LogB.Information("Selected task from gui/networks.cs:" + task.ToString());
 
+               if(task == null)
+                       return;
+
+               LogB.Information("Selected task from gui/networks.cs:" + task.ToString());
                if(configChronojump.CompujumpStationMode == Constants.Modes.RUNSINTERVALLIC)
-                       compujumpTaskStartRunInterval(task);
+                       compujumpTaskStartRunInterval((TaskEncoder) task); //TODO: use taskRunInertial
                else
-                       compujumpTaskStartEncoder(task);
+                       compujumpTaskStartEncoder((TaskEncoder) task);
        }
 
        private void on_run_interval_compujump_type_toggled(object o, EventArgs args)
@@ -1079,7 +1096,7 @@ public partial class ChronoJumpWindow
                        combo_select_runs_interval.Active = 3;
        }
 
-       private void compujumpTaskStartRunInterval(Task task)
+       private void compujumpTaskStartRunInterval(TaskEncoder task) //TODO: use taskRunInterval
        {
                if(task.ExerciseName == "5 m" || task.ExerciseName == "05 m")
                        radio_run_interval_compujump_5m.Active = true;
@@ -1093,7 +1110,7 @@ public partial class ChronoJumpWindow
                on_button_execute_test_clicked(new object(), new EventArgs());
        }
 
-       private void compujumpTaskStartEncoder(Task task)
+       private void compujumpTaskStartEncoder(TaskEncoder task)
        {
                combo_encoder_exercise_capture.Active = 
UtilGtk.ComboMakeActive(combo_encoder_exercise_capture, task.ExerciseName);
 
diff --git a/src/json/compujump.cs b/src/json/compujump.cs
index 6a8fc0e82..c42121f77 100644
--- a/src/json/compujump.cs
+++ b/src/json/compujump.cs
@@ -16,7 +16,7 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Copyright (C) 2016-2017 Carles Pina
- * Copyright (C) 2016-2020 Xavier de Blas
+ * Copyright (C) 2016-2021 Xavier de Blas
  */
 
 using System;
@@ -161,13 +161,13 @@ public class JsonCompujump : Json
                return true;
        }
 
-       public List<Task> GetTasks(int personID, int stationID)
+       public string GetTasksResponse (int personID, int stationID)
        {
                connected = false;
 
                // Create a request using a URL that can receive a post.
                if (! createWebRequest(requestType.AUTHENTICATED, "/api/v1/client/getTasks"))
-                       return new List<Task>();
+                       return "";
 
                // Set the Method property of the request to POST.
                request.Method = "POST";
@@ -183,14 +183,14 @@ public class JsonCompujump : Json
                // Writes the json object into the request dataStream
                Stream dataStream;
                if(! getWebRequestStream (request, out dataStream, "Cannot get tasks."))
-                       return new List<Task>();
+                       return "";
 
                dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
                dataStream.Close ();
 
                HttpWebResponse response;
                if(! getHttpWebResponse (request, out response, "Cannot get tasks."))
-                       return new List<Task>();
+                       return "";
 
                string responseFromServer;
                using (var sr = new StreamReader(response.GetResponseStream()))
@@ -205,36 +205,10 @@ public class JsonCompujump : Json
                if(responseFromServer == "" || responseFromServer == "[]")
                {
                        LogB.Information(" Empty ");
-                       return new List<Task>();
+                       return "";
                }
 
-               return taskDeserializeFromServer (responseFromServer);
-       }
-
-       private List<Task> taskDeserializeFromServer(string responseFromServer)
-       {
-               List<Task> tasks = new List<Task>();
-
-               JsonValue jsonTasks = JsonValue.Parse (responseFromServer);
-
-               foreach (JsonValue jsonTask in jsonTasks) {
-                       Int32 id = jsonTask ["id"];
-                       int personId = jsonTask ["player_id"];
-                       int exerciseId = jsonTask ["exercise_id"];
-                       string exerciseName = jsonTask ["exercise_name"];
-
-                       int sets = jsonTask ["sets"];
-                       int nreps = jsonTask ["nreps"];
-                       float load = jsonTask ["load"];
-                       float speed = jsonTask ["speed"];
-                       float percentMaxSpeed = jsonTask ["percent_max_speed"];
-                       string laterality = jsonTask ["laterality"];
-                       string comment = jsonTask ["comment"];
-                       tasks.Add(new Task(id, personId, exerciseId, exerciseName,
-                                               sets, nreps, load, speed, percentMaxSpeed,
-                                               laterality, comment));
-               }
-               return tasks;
+               return responseFromServer;
        }
 
        public bool UpdateTask(int taskId, int done)
diff --git a/src/json/compujumpOther.cs b/src/json/compujumpOther.cs
index e81659e29..c7ac9c249 100644
--- a/src/json/compujumpOther.cs
+++ b/src/json/compujumpOther.cs
@@ -16,19 +16,37 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Copyright (C) 2016-2017 Carles Pina
- * Copyright (C) 2016-2020 Xavier de Blas
+ * Copyright (C) 2016-2021 Xavier de Blas
  */
 
 using System;
+using System.Json;
 using System.Text;
 using System.Collections;
+using System.Collections.Generic; //List
+using System.Text.RegularExpressions; //Regex
 using Mono.Unix;
 
-
-public class Task
+public abstract class Task
 {
        public int Id;
        public int PersonId;
+       public string Comment;
+
+       protected void initialize ()
+       {
+               Id = -1;
+               Comment = "";
+       }
+
+       public override string ToString()
+       {
+               return "";
+       }
+}
+
+public class TaskEncoder : Task
+{
        public int ExerciseId;
        public string ExerciseName;
        public int Sets;
@@ -37,15 +55,13 @@ public class Task
        public float Speed;
        public float PercentMaxSpeed;
        public string Laterality;
-       public string Comment;
 
-       public Task()
+       public TaskEncoder ()
        {
-               Id = -1;
-               Comment = "";
+               initialize ();
        }
 
-       public Task(int id, int personId, int exerciseId, string exerciseName,
+       public TaskEncoder (int id, int personId, int exerciseId, string exerciseName,
                        int sets, int nreps, float load, float speed, float percentMaxSpeed,
                        string laterality, string comment)
        {
@@ -62,6 +78,31 @@ public class Task
                Comment = comment;
        }
 
+       public TaskEncoder (JsonValue jsonTask)
+       {
+               Id = jsonTask ["id"];
+               PersonId = jsonTask ["player_id"];
+               ExerciseId = jsonTask ["exercise_id"];
+               ExerciseName = jsonTask ["exercise_name"];
+
+               string NrepsStr = jsonTask ["duration"]; //eg: "15 Repetitions"
+               Match match = Regex.Match(NrepsStr, @"(\d+) Repetitions");
+               //LogB.Information(string.Format("NrepsStr: {0}, match.Groups.Count: {1}, 
match.Groups[1].Value: {2}",
+               //      NrepsStr, match.Groups.Count, match.Groups[1].Value));
+               if(match.Groups.Count == 2 && Util.IsNumber(match.Groups[1].Value, false))
+                       Nreps = Convert.ToInt32(match.Groups[1].Value);
+
+               Laterality = jsonTask ["laterality"];
+               Sets = jsonTask ["sets"];
+
+               //measurable_info
+               Load = jsonTask ["measurable_info"]["load"];
+               Speed = jsonTask ["measurable_info"]["speed"];
+               PercentMaxSpeed = jsonTask ["measurable_info"]["percent_max_speed"];
+
+               Comment = jsonTask ["comment"];
+       }
+
        public override string ToString()
        {
                string sep = "";
@@ -108,6 +149,24 @@ public class Task
        }
 }
 
+public class TaskDeserialize
+{
+       public TaskDeserialize()
+       {
+       }
+
+       public List<Task> DeserializeTaskEncoder (string responseFromServer)
+       {
+               List<Task> tasks = new List<Task>();
+
+               JsonValue jsonTasks = JsonValue.Parse (responseFromServer);
+               foreach (JsonValue jsonTask in jsonTasks) {
+                       tasks.Add(new TaskEncoder(jsonTask));
+               }
+               return tasks;
+       }
+}
+
 public class StationCount
 {
        private string stationName;
diff --git a/src/json/json.cs b/src/json/json.cs
index 64b0259e3..89be457a0 100644
--- a/src/json/json.cs
+++ b/src/json/json.cs
@@ -33,6 +33,7 @@ public class Json
 {
        public string ResultMessage;
 
+       protected Config configChronojump;
        protected bool connected; //know if server is connected. Do it when there's a change on RFID (pulse)
        protected string authToken; //authentification token. If the string is empty auth call must be done.
        protected HttpWebRequest request; //generic request (for all methods except ping)
@@ -514,7 +515,7 @@ public class Json
                JsonObject json = new JsonObject();
 
                // Use CompujumpStationID and machineId as unique identifiers
-               Config configChronojump = new Config();
+               configChronojump = new Config();
                configChronojump.Read();
                int stationID = configChronojump.CompujumpStationID;
                if (stationID <= 0) {


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