[chronojump] First implementation of json uploadEncoderData
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] First implementation of json uploadEncoderData
- Date: Fri, 20 Jan 2017 16:14:02 +0000 (UTC)
commit 731db55d8c4ce0a17d76e9308a55d452eada2637
Author: Xavier de Blas <xaviblas gmail com>
Date: Fri Jan 20 17:13:32 2017 +0100
First implementation of json uploadEncoderData
src/gui/chronojump.cs | 32 +++++++++++++++++++++++++-
src/json.cs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 1 deletions(-)
---
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index fdd19f3..798c464 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -6359,7 +6359,37 @@ LogB.Debug("X");
);
}
- private void on_button_carles_clicked (object o, EventArgs args) {
+ private void on_button_carles_clicked (object o, EventArgs args)
+ {
+ bool showInWindow = true;
+
+ Json js = new Json();
+ bool success = js.UploadEncoderData();
+
+ if(success) {
+ LogB.Information(js.ResultMessage);
+ if(showInWindow)
+ new DialogMessage(
+ "Chronojump",
+ Constants.MessageTypes.INFO,
+ js.ResultMessage);
+ }
+ else {
+ LogB.Error(js.ResultMessage);
+ if(showInWindow)
+ new DialogMessage(
+ "Chronojump",
+ Constants.MessageTypes.WARNING,
+ js.ResultMessage);
+ }
+
+ /*
+ new DialogMessage(
+ "Chronojump",
+ Constants.MessageTypes.INFO,
+ "Temporarily Disabled");
+ */
+
//carles stuff
}
diff --git a/src/json.cs b/src/json.cs
index acb8a29..bff1268 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -265,6 +265,65 @@ public class Json
return true;
}
+ public bool UploadEncoderData()
+ {
+ // Create a request using a URL that can receive a post.
+ WebRequest request = WebRequest.Create (serverUrl + "/uploadEncoderData");
+
+ // Set the Method property of the request to POST.
+ request.Method = "POST";
+
+ // Set the ContentType property of the WebRequest.
+ request.ContentType = "application/json";
+
+ // Creates the json object
+ JsonObject json = new JsonObject();
+ json.Add("person_id", 1);
+ json.Add("person_name", "Joan yjk");
+ json.Add("machine_id", 2);
+ json.Add("machine_name", "Bench press");
+ json.Add("mean_power", 650);
+
+ // 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 false;
+ }
+
+ dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
+
+ dataStream.Close ();
+
+ // Get the response.
+ WebResponse response;
+ try {
+ response = request.GetResponse ();
+ } catch {
+ this.ResultMessage =
+ string.Format(Catalog.GetString("You are not connected to the Internet\nor
{0} server is down."),
+ serverUrl);
+ return false;
+ }
+
+ // Display the status (will be 202, CREATED)
+ Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+
+ // Clean up the streams.
+ dataStream.Close ();
+ response.Close ();
+
+ this.ResultMessage = "Encoder data sent.";
+ return true;
+ }
+
~Json() {}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]