[chronojump] GetTasks from server implemented



commit 2d7037c279543b350756784b48a578974e55c7ae
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sun May 28 21:56:10 2017 +0200

    GetTasks from server implemented

 src/gui/dialogPersonPopup.cs |    8 ++-
 src/gui/networks.cs          |   14 +++--
 src/json.cs                  |  122 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 134 insertions(+), 10 deletions(-)
---
diff --git a/src/gui/dialogPersonPopup.cs b/src/gui/dialogPersonPopup.cs
index f4ad1c1..f2f234a 100644
--- a/src/gui/dialogPersonPopup.cs
+++ b/src/gui/dialogPersonPopup.cs
@@ -20,6 +20,7 @@
 
 using System;
 using System.IO; 
+using System.Collections.Generic; //List
 using Gtk;
 using Gdk;
 using Glade;
@@ -32,7 +33,7 @@ public class DialogPersonPopup
        [Widget] Gtk.Label label_rfid;
        [Widget] Gtk.TextView textview_task;
 
-       public DialogPersonPopup (int personID, string name, string rfid, string task)
+       public DialogPersonPopup (int personID, string name, string rfid, List<Task> tasks)
        {
                Glade.XML gladeXML;
                gladeXML = Glade.XML.FromAssembly (Util.GetGladePath() + "dialog_person_popup.glade", 
"dialog_person_popup", null);
@@ -61,7 +62,10 @@ public class DialogPersonPopup
                }
 
                TextBuffer tb = new TextBuffer (new TextTagTable());
-               tb.Text =  task;
+               tb.Text = "";
+               foreach(Task t in tasks)
+                       tb.Text += t.ToString() + "\n";
+
                textview_task.Buffer = tb;
        }
 
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 70ee823..1161814 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -368,12 +368,12 @@ public partial class ChronoJumpWindow
 
                bool currentPersonWasNull = (currentPerson == null);
                bool pChanged = false;
+               Json json = new Json();
                if(p.UniqueID == -1)
                {
                        LogB.Information("RFID person does not exist!!");
 
-                       Json js = new Json();
-                       p = js.GetPersonByRFID(capturedRFID);
+                       p = json.GetPersonByRFID(capturedRFID);
                        if(p.UniqueID == -1) {
                                LogB.Information("Person NOT found on server!");
                                new DialogMessage(Constants.MessageTypes.WARNING,
@@ -385,15 +385,15 @@ public partial class ChronoJumpWindow
                                currentPerson = p;
                                currentPersonSession = new PersonSession (
                                                currentPerson.UniqueID, currentSession.UniqueID, 
-                                               0, js.LastPersonByRFIDWeight,
+                                               0, json.LastPersonByRFIDWeight,
                                                Constants.SportUndefinedID, 
                                                Constants.SpeciallityUndefinedID, 
                                                Constants.LevelUndefinedID,
                                                "", false); //comments, dbconOpened
 
-                               if(js.LastPersonByRFIDImageURL != "")
+                               if(json.LastPersonByRFIDImageURL != "")
                                {
-                                       bool downloaded = js.DownloadImage(js.LastPersonByRFIDImageURL, 
currentPerson.UniqueID);
+                                       bool downloaded = json.DownloadImage(json.LastPersonByRFIDImageURL, 
currentPerson.UniqueID);
                                        if(downloaded)
                                                File.Copy(
                                                                Path.Combine(Path.GetTempPath(), 
currentPerson.UniqueID.ToString()),
@@ -424,11 +424,13 @@ public partial class ChronoJumpWindow
                                selectRowTreeView_persons(treeview_persons, rowToSelect);
                        */
 
+                       List<Task> tasks = json.GetTasks(currentPerson.UniqueID);
+
                        if(dialogPersonPopup != null)
                                dialogPersonPopup.DestroyDialog();
 
                        dialogPersonPopup = new DialogPersonPopup(
-                                       currentPerson.UniqueID, currentPerson.Name, capturedRFID, "Sample 
task");
+                                       currentPerson.UniqueID, currentPerson.Name, capturedRFID, tasks);
                }
 
                updatingRFIDGuiStuff = false;
diff --git a/src/json.cs b/src/json.cs
index cea0473..f111d29 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -80,7 +80,7 @@ public class Json
                } catch {
                        LogB.Warning("Error sending datastream");
                        this.ResultMessage = Catalog.GetString("Could not send file.") + "\n" + 
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
                                                serverUrl);
                        return false;
                }
@@ -98,7 +98,7 @@ public class Json
                } catch {
                        LogB.Warning("Error getting response");
                        this.ResultMessage = Catalog.GetString("Could not send file.") + "\n" + 
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
+                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
                                                serverUrl);
                        return false;
                }
@@ -404,6 +404,101 @@ public class Json
                return true;
        }
 
+       public List<Task> GetTasks(int personID)
+       {
+               // Create a request using a URL that can receive a post.
+               WebRequest request = WebRequest.Create (serverUrl + "/getTasks");
+
+               // 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("personId", personID.ToString());
+
+               // 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 new List<Task>();
+               }
+
+               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 new List<Task>();
+               }
+
+               string responseFromServer;
+               using (var sr = new StreamReader(response.GetResponseStream()))
+               {
+                       responseFromServer = sr.ReadToEnd();
+               }
+
+               LogB.Information("GetTasks: " + responseFromServer);
+
+               if(responseFromServer == "" || responseFromServer == "[]")
+               {
+                       LogB.Information(" Empty ");
+                       return new List<Task>();
+               }
+
+               return patheticTasksDeserialize(responseFromServer);
+       }
+       private List<Task> patheticTasksDeserialize(string responseFromServer)
+       {
+               List<Task> list = new List<Task>();
+
+               //      [[1, "one task"], [3, "another task"]]
+
+               //1) convert it to:
+               //      [1, "one task"], [3, "another task"]
+               responseFromServer = responseFromServer.Substring(1, responseFromServer.Length -2);
+
+               string [] strFull = responseFromServer.Split(new char[] {']'});
+               foreach(string str in strFull)
+               {
+                       if(str == null || str == "")
+                               continue;
+
+                       string s = str;
+                       LogB.Information("before: " + s);
+                       if(s.StartsWith(", ["))
+                               s = s.Substring(3);
+                       else
+                               s = s.Substring(1);
+
+                       //don't use this because comments can have a comma
+                       //string [] s2 = s.Split(new char[] {','});
+
+                       //get the first comma
+                       int sepPos = s.IndexOf(',');
+                       string sId = s.Substring(0, sepPos);
+                       string sComment = s.Substring(sepPos +1);
+                       sComment = sComment.Substring(2, sComment.Length -3);   //remove initial ' "' and end 
'"'
+
+                       list.Add(new Task(Convert.ToInt32(sId), sComment));
+               }
+               return list;
+       }
+
        /*
        public bool UploadEncoderData()
        {
@@ -602,3 +697,26 @@ public class UploadEncoderDataObject
                return Convert.ToInt32(Util.DivideSafeFraction(100.0 * (highest - lowest), highest));
        }
 }
+
+public class Task
+{
+       public int Id;
+       public string Comment;
+
+       public Task()
+       {
+               Id = -1;
+               Comment = "";
+       }
+
+       public Task(int id, string comment)
+       {
+               Id = id;
+               Comment = comment;
+       }
+
+       public override string ToString()
+       {
+               return Id.ToString() + ": " + Comment;
+       }
+}


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