[chronojump] Updated JSON uploadEncoderData (encoder capture is send)



commit 5e8cec2c912947feef02486158cb2080945994b4
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue May 2 21:02:48 2017 +0200

    Updated JSON uploadEncoderData (encoder capture is send)

 src/gui/encoder.cs |   39 ++++++++++++++++++++++++++++++++++++++-
 src/json.cs        |   17 +++++++++++------
 src/util.cs        |   20 ++++++++++++++++++++
 3 files changed, 69 insertions(+), 7 deletions(-)
---
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index d2d36f6..754044e 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -5940,11 +5940,48 @@ public partial class ChronoJumpWindow
                                                
encoderCaptureSaveCurvesAllNoneBest(preferences.encoderAutoSaveCurve,
                                                                
Constants.GetEncoderVariablesCapture(preferences.encoderCaptureMainVariable));
 
-                                       //save the triggers now that we have an encoderSignalUniqueID
                                        if(action == encoderActions.CURVES_AC)
                                        {
+                                               //1) save the triggers now that we have an 
encoderSignalUniqueID
                                                
eCapture.SaveTriggers(Convert.ToInt32(encoderSignalUniqueID)); //dbcon is closed
                                                showTriggersAndTab();
+
+                                               //2) send the json to server
+
+                                               //get highest meanPower on set
+                                               double meanPowerHighest = 0;
+                                               foreach (EncoderCurve curve in encoderCaptureCurves)
+                                                       if(curve.MeanPowerD > meanPowerHighest)
+                                                               meanPowerHighest = curve.MeanPowerD;
+
+                                               //get reps >= 50 % of highest
+                                               int repsAbove50pBest = 0;
+                                               foreach (EncoderCurve curve in encoderCaptureCurves)
+                                                       if(curve.MeanPowerD >= meanPowerHighest / 2.0)
+                                                               repsAbove50pBest ++;
+
+                                               /*
+                                                * problems on Json by accents like "PressiĆ³ sobre banc"
+                                                * string exerciseName = 
UtilGtk.ComboGetActive(combo_encoder_exercise_capture);
+                                                * right now fixed in json.cs UploadEncoderData()
+                                                */
+                                               Json js = new Json();
+                                               bool success = js.UploadEncoderData(
+                                                               currentPerson.UniqueID,
+                                                               1,
+                                                               
UtilGtk.ComboGetActive(combo_encoder_exercise_capture),
+                                                               Util.ConvertToPoint(meanPowerHighest),
+                                                               repsAbove50pBest);
+
+                                               if(! success) {
+                                                       LogB.Error(js.ResultMessage);
+                                                       bool showInWindow = false;
+                                                       if(showInWindow)
+                                                               new DialogMessage(
+                                                                               "Chronojump",
+                                                                               
Constants.MessageTypes.WARNING,
+                                                                               js.ResultMessage);
+                                               }
                                        }
 
                                } else
diff --git a/src/json.cs b/src/json.cs
index f285c50..9e59dde 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -268,6 +268,10 @@ public class Json
 
        public bool UploadEncoderData()
        {
+               return UploadEncoderData(1, 1, "lateral", "8100", 8);
+       }
+       public bool UploadEncoderData(int personId, int machineId, string exerciseName, string 
meanPowerBestRep, int repsAbove50pBest )
+       {
                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create (serverUrl + "/uploadEncoderData");
 
@@ -275,15 +279,16 @@ public class Json
                request.Method = "POST";
 
                // Set the ContentType property of the WebRequest.
-               request.ContentType = "application/json";
+               request.ContentType = "application/json; Charset=UTF-8"; //but this is not enough, see this 
line:
+               exerciseName = Util.RemoveAccents(exerciseName);
 
                // Creates the json object
                JsonObject json = new JsonObject();
-               json.Add("personId", 1);
-               json.Add("machineId", 1);
-               json.Add("exerciseName", "Lateral");
-               json.Add("meanPowerBestRep", 8100);
-               json.Add("repsAbove50pBest", 8);
+               json.Add("personId", personId);
+               json.Add("machineId", machineId);
+               json.Add("exerciseName", exerciseName);
+               json.Add("meanPowerBestRep", meanPowerBestRep);
+               json.Add("repsAbove50pBest", repsAbove50pBest);
 
                // Converts it to a String
                String js = json.ToString();
diff --git a/src/util.cs b/src/util.cs
index 291e7cf..669038b 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -25,6 +25,7 @@ using System.Collections; //ArrayList
 using System.Collections.Generic; //List<T>
 using System.Diagnostics;      //for detect OS
 using System.IO;               //for detect OS
+using System.Globalization;    //Unicode
 
 //this class tries to be a space for methods that are used in different classes
 public class Util
@@ -301,6 +302,25 @@ public class Util
                return myStringBuilder.ToString();
        }
 
+       //to pass latin chars to JSON
+       //http://stackoverflow.com/a/249126
+       public static string RemoveAccents(string text)
+       {
+               var normalizedString = text.Normalize(NormalizationForm.FormD);
+               var stringBuilder = new StringBuilder();
+
+               foreach (var c in normalizedString)
+               {
+                       var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
+                       if (unicodeCategory != UnicodeCategory.NonSpacingMark)
+                       {
+                               stringBuilder.Append(c);
+                       }
+               }
+
+               return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
+       }
+
        public static string RemoveTilde(string myString) 
        {
                StringBuilder myStringBuilder = new StringBuilder(myString);


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