[chronojump] RFID is readed now form an Arduino board



commit c118f19c2356064cefaafabd3d63966490c441a2
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon May 8 20:27:18 2017 +0200

    RFID is readed now form an Arduino board

 chronojump.csproj     |    1 +
 glade/app1.glade      |   75 ++++++++--------------------
 src/Makefile.am       |    1 +
 src/gui/chronojump.cs |   25 ++++++----
 src/gui/networks.cs   |  131 ++++++++++++++++++++++++++++++++++++-------------
 5 files changed, 133 insertions(+), 100 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index 5b77d0e..e70ad50 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -1174,6 +1174,7 @@
     <Compile Include="src\forceSensor.cs" />
     <Compile Include="src\sqlite\forceSensor.cs" />
     <Compile Include="src\gui\trigger.cs" />
+    <Compile Include="src\rfid.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
diff --git a/glade/app1.glade b/glade/app1.glade
index 569516d..0219f60 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -1239,6 +1239,9 @@
                                                             <placeholder/>
                                                             </child>
                                                             <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
                                                             <widget class="GtkLabel" 
id="label_start_selector_jumps">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -14404,7 +14407,15 @@ Concentric</property>
                                                   </packing>
                                                 </child>
                                                 <child>
-                                                  <placeholder/>
+                                                  <widget class="GtkLabel" id="label_rfid">
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">False</property>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">True</property>
+                                                    <property name="fill">True</property>
+                                                    <property name="position">2</property>
+                                                  </packing>
                                                 </child>
                                               </widget>
                                               <packing>
@@ -14418,56 +14429,6 @@ Concentric</property>
                                                 <property name="can_focus">False</property>
                                                 <property name="spacing">20</property>
                                                 <child>
-                                                  <widget class="GtkHBox" id="hbox_rfid">
-                                                    <property name="can_focus">False</property>
-                                                    <child>
-                                                      <widget class="GtkButton" id="button_rfid_read">
-                                                        <property name="label">Read RFID</property>
-                                                        <property name="can_focus">True</property>
-                                                        <property name="receives_default">True</property>
-                                                        <property name="relief">half</property>
-                                                        <signal name="clicked" 
handler="on_button_rfid_read_clicked" swapped="no"/>
-                                                      </widget>
-                                                      <packing>
-                                                        <property name="expand">True</property>
-                                                        <property name="fill">True</property>
-                                                        <property name="position">0</property>
-                                                      </packing>
-                                                    </child>
-                                                    <child>
-                                                      <widget class="GtkButton" id="button_rfid_start">
-                                                        <property name="label">Start RFID</property>
-                                                        <property name="visible">True</property>
-                                                        <property name="can_focus">True</property>
-                                                        <property name="receives_default">True</property>
-                                                        <property name="relief">half</property>
-                                                        <signal name="clicked" 
handler="on_button_rfid_start_clicked" swapped="no"/>
-                                                      </widget>
-                                                      <packing>
-                                                        <property name="expand">True</property>
-                                                        <property name="fill">True</property>
-                                                        <property name="position">1</property>
-                                                      </packing>
-                                                    </child>
-                                                    <child>
-                                                      <widget class="GtkLabel" id="label_rfid">
-                                                        <property name="visible">True</property>
-                                                        <property name="can_focus">False</property>
-                                                      </widget>
-                                                      <packing>
-                                                        <property name="expand">True</property>
-                                                        <property name="fill">True</property>
-                                                        <property name="position">2</property>
-                                                      </packing>
-                                                    </child>
-                                                  </widget>
-                                                  <packing>
-                                                    <property name="expand">False</property>
-                                                    <property name="fill">True</property>
-                                                    <property name="position">0</property>
-                                                  </packing>
-                                                </child>
-                                                <child>
                                                   <widget class="GtkHBox" id="hbox73">
                                                     <property name="can_focus">False</property>
                                                     <property name="spacing">20</property>
@@ -14573,12 +14534,13 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">1</property>
+                                                    <property name="position">0</property>
                                                   </packing>
                                                 </child>
                                                 <child>
                                                   <widget class="GtkButton" id="button_menu_outside_menu">
                                                     <property name="width_request">50</property>
+                                                    <property name="visible">True</property>
                                                     <property name="can_focus">True</property>
                                                     <property name="can_default">True</property>
                                                     <property name="receives_default">True</property>
@@ -14599,7 +14561,7 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">2</property>
+                                                    <property name="position">1</property>
                                                   </packing>
                                                 </child>
                                                 <child>
@@ -14625,7 +14587,7 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">3</property>
+                                                    <property name="position">2</property>
                                                   </packing>
                                                 </child>
                                                 <child>
@@ -14650,7 +14612,7 @@ Concentric</property>
                                                   <packing>
                                                     <property name="expand">True</property>
                                                     <property name="fill">True</property>
-                                                    <property name="position">4</property>
+                                                    <property name="position">3</property>
                                                   </packing>
                                                 </child>
                                               </widget>
@@ -20315,6 +20277,9 @@ then click this button.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/Makefile.am b/src/Makefile.am
index 3620d07..0d9a973 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -191,6 +191,7 @@ SOURCES = \
        log.cs\
        logB.cs\
        logConsoleCrayon.cs\
+       RFID.cs\
        serverPing.cs\
        serverEvaluator.cs\
        server.cs\
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 8657ba8..2ca1dbc 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2066,7 +2066,21 @@ public partial class ChronoJumpWindow
                LogB.Information("Bye!");
 
                updatingRestTimes = false;
-               updatingRFIDGuiStuff = false;
+
+               if(threadRFID != null && threadRFID.IsAlive)
+               {
+                       LogB.Information("Closing threadRFID");
+
+                       rfid.Stop();
+                       rfidProcessCancel = true;
+
+                       System.Threading.Thread.Sleep(250);
+
+                       if(threadRFID.IsAlive)
+                       {
+                               threadRFID.Abort();
+                       }
+               }
 
                //if capturing on the background finish it
                if(eCaptureInertialBG != null)
@@ -2082,15 +2096,6 @@ public partial class ChronoJumpWindow
                        jsPing.PingAbort();
                }
 
-               //close rfid capture process if it's working
-               if(ExecuteProcess.IsRunning(processRFIDcapture))
-               {
-                       LogB.Information("processRFIDcapture is running. Stopping");
-                       processRFIDcapture.Kill();
-               } else
-                       LogB.Information("processRFIDcapture is NOT running.");
-
-
                //printing remaining logs in the non-gtk thread
                LogB.Information("Printing non-GTK thread remaining log");
                LogB.Information(LogSync.ReadAndEmpty());
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 1c110d5..8b01721 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -28,6 +28,7 @@ using System.IO; //"File" things
 using System.Collections; //ArrayList
 using System.Collections.Generic; //List
 using System.Diagnostics; //Process
+using System.Threading;
        
 public partial class ChronoJumpWindow 
 {
@@ -39,8 +40,6 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.Alignment alignment_encoder_capture_options;
                        
        //RFID
-       [Widget] Gtk.HBox hbox_rfid;
-       [Widget] Gtk.Button button_rfid_start;
        [Widget] Gtk.Label label_rfid;
        
        //better raspberry controls
@@ -66,6 +65,13 @@ public partial class ChronoJumpWindow
 
        private enum linuxTypeEnum { NOTLINUX, LINUX, RASPBERRY, NETWORKS }
        private bool encoderUpdateTreeViewWhileCapturing = true;
+
+       static Thread threadRFID;
+       public RFID rfid;
+       private static string capturedRFID;
+       private static bool shouldUpdateRFIDGui;
+       private static bool updatingRFIDGuiStuff;
+       private bool rfidProcessCancel;
                
        Config configChronojump;
        private void configInitRead()
@@ -83,10 +89,42 @@ public partial class ChronoJumpWindow
                        //TODO: don't allow edit person on person treeview
 
                        Json.ChangeServerUrl(configChronojump.CompujumpServerURL);
+
+                       capturedRFID = "";
+                       updatingRFIDGuiStuff = false;
+                       shouldUpdateRFIDGui = false;
+                       rfidProcessCancel = false;
+
+                       rfid = new RFID();
+                       rfid.FakeButtonChange.Clicked += new EventHandler(rfidChanged);
+
+                       threadRFID = new Thread (new ThreadStart (RFIDStart));
+                       GLib.Idle.Add (new GLib.IdleHandler (pulseRFID));
+
+                       LogB.ThreadStart();
+                       threadRFID.Start();
                }
 
                configDo();
        }
+       private void RFIDStart()
+       {
+               LogB.Information("RFID Start");
+               rfid.Start();
+               //rfid.ChangedEvent += new EventHandler(this.rfidChanged);
+       }
+       private void rfidChanged(object sender, EventArgs e)
+       {
+               if(rfid.Captured != capturedRFID)
+               {
+                       LogB.Information("RFID changed to: " + rfid.Captured);
+
+                       capturedRFID = rfid.Captured;
+                       shouldUpdateRFIDGui = true;
+               } else
+                       LogB.Information("RFID doesn't change");
+       }
+
        private void configInitFromPreferences()
        {
                configChronojump = new Config();
@@ -255,7 +293,6 @@ public partial class ChronoJumpWindow
                        
                        on_load_session_accepted();
                        sensitiveGuiYesSession();
-                       rfid_start();
                }
 
                //TODO
@@ -275,7 +312,7 @@ public partial class ChronoJumpWindow
                        //time, gym, ...
 
                        //show rfid
-                       hbox_rfid.Visible = true;
+                       label_rfid.Visible = true;
 
                        //to test display, just make sensitive the top controls, but beware there's no 
session yet and no person
                        notebook_sup.Sensitive = true;
@@ -284,48 +321,43 @@ public partial class ChronoJumpWindow
                }
                */
 
-               hbox_rfid.Visible = (UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX);
+               label_rfid.Visible = (UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX);
        }
 
-       //rfid
-       private void rfid_test() {
-               Networks networks = new Networks();
-               networks.Test();
-       }
-       void on_button_rfid_read_clicked (object o, EventArgs args)
+       private bool pulseRFID ()
        {
-               string filePath = Util.GetRFIDCapturedFile();
-
-               if(Util.FileExists(filePath))
-                       label_rfid.Text = Util.ReadFile(filePath, true);
-       }
-       
-       static bool updatingRFIDGuiStuff;
-       static string updatingRFIDGuiStuffNewRFID;
-       //called each second and after a test
-       bool updateRFIDGuiStuff()
-       {
-               if(! updatingRFIDGuiStuff)
+               if(! threadRFID.IsAlive || rfidProcessCancel)
+               {
+                       LogB.ThreadEnding();
+                       LogB.ThreadEnded();
                        return false;
+               }
+
+               //don't allow this method to be called again until ended
+               //Note RFID detection can send many cards (the same) per second
+               if(updatingRFIDGuiStuff)
+                       return true;
 
-               if(updatingRFIDGuiStuffNewRFID == "")
+               if(! shouldUpdateRFIDGui)
                        return true;
 
-               string rfid = updatingRFIDGuiStuffNewRFID;
-               updatingRFIDGuiStuffNewRFID = ""; //to not be called again //TODO: ensure this is not called 
two times
+               shouldUpdateRFIDGui = false;
+               updatingRFIDGuiStuff = true;
 
-               label_rfid.Text = rfid; //take care, maybe cannot read label stuff on a WATCHER
-               Person p = SqlitePerson.SelectByRFID(rfid);
+               //TODO: this pulseRFID need only the GTK stuff, not the rest
+               label_rfid.Text = capturedRFID; //GTK
+
+               Person p = SqlitePerson.SelectByRFID(capturedRFID);
                if(p.UniqueID == -1)
                {
                        LogB.Information("RFID person does not exist!!");
 
                        Json js = new Json();
-                       p = js.GetPersonByRFID(rfid);
+                       p = js.GetPersonByRFID(capturedRFID);
                        if(p.UniqueID == -1) {
                                LogB.Information("Person NOT found on server!");
                                new DialogMessage(Constants.MessageTypes.WARNING,
-                                               "Aquesta pulsera o jugador no es troba identificada al 
servidor");
+                                               "Aquesta pulsera o jugador no es troba identificada al 
servidor"); //GTK
                        }
                        else {
                                LogB.Information("Person found on server!");
@@ -338,22 +370,52 @@ public partial class ChronoJumpWindow
                                                Constants.SpeciallityUndefinedID, 
                                                Constants.LevelUndefinedID,
                                                "", false); //comments, dbconOpened
-                               person_added();
+                               person_added(); //GTK
                        }
                }
                else
                        LogB.Information("RFID person exists!!");
 
+               updatingRFIDGuiStuff = false;
+
+               Thread.Sleep (100);
+               LogB.Information(" threadRFID:" + threadRFID.ThreadState.ToString());
+
                return true;
        }
 
 
-       Process processRFIDcapture;
+       /*
+        *
+        * This code uses a watcher to see changes on a filename
+        * is thought to be run on a raspberry connected to rfid
+        * with a Python program reading rfid and changing that file.
+        *
+        * Now is not used anymore because new code connects by USB to an Arduino that has the RFID
+        */
+
+       /*
+       static string updatingRFIDGuiStuffNewRFID;
+
+       private void rfid_test() {
+               Networks networks = new Networks();
+               networks.Test();
+       }
+
+       void on_button_rfid_read_clicked (object o, EventArgs args)
+       {
+               string filePath = Util.GetRFIDCapturedFile();
+
+               if(Util.FileExists(filePath))
+                       label_rfid.Text = Util.ReadFile(filePath, true);
+       }
+
+       //Process processRFIDcapture;
        void on_button_rfid_start_clicked (object o, EventArgs args)
        {
                rfid_start();
        }
-               
+
        private void rfid_start()
        {
                string script_path = Util.GetRFIDCaptureScript();
@@ -443,6 +505,5 @@ public partial class ChronoJumpWindow
                        }
                }
        }
-
+       */
 }
-


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