[chronojump] Solve RFID cable disconnect



commit e8f494f9a4a20e3a4a365b2320fa504143af196b
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Jun 29 08:47:34 2017 +0200

    Solve RFID cable disconnect

 src/RFID.cs         |   20 ++++++++++++++++++--
 src/constants.cs    |    3 +++
 src/gui/networks.cs |   16 ++++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)
---
diff --git a/src/RFID.cs b/src/RFID.cs
index d45411c..9c040df 100644
--- a/src/RFID.cs
+++ b/src/RFID.cs
@@ -31,12 +31,14 @@ public class RFID
        private SerialPort port;
        private string portName;
        private Gtk.Button fakeButtonChange;
+       private Gtk.Button fakeButtonDisconnected;
 
        public RFID(string portName)
        {
                this.portName = portName;
                stop = false;
                fakeButtonChange = new Button();
+               fakeButtonDisconnected = new Button();
        }
        
        public void Start()
@@ -62,9 +64,18 @@ public class RFID
                while(! stop)
                {
                        //str = port.ReadLine(); //don't use this because gets waiting some stop signal
-                       if (port.BytesToRead > 0)
+                       str = "";
+                       try {
+                               if (port.BytesToRead > 0)
+                                       str = port.ReadExisting();
+                       } catch (System.IO.IOException) {
+                               LogB.Information("Catched reading RFID!");
+                               fakeButtonDisconnected.Click();
+                               return;
+                       }
+
+                       if(str != "")
                        {
-                               str = port.ReadExisting();
                                LogB.Information("No trim str" + str);
 
                                //get only the first line and trim it
@@ -150,4 +161,9 @@ public class RFID
        {
                get { return fakeButtonChange; }
        }
+
+       public Gtk.Button FakeButtonDisconnected
+       {
+               get { return fakeButtonDisconnected; }
+       }
 }
diff --git a/src/constants.cs b/src/constants.cs
index ce2bd94..3a96f4f 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -194,6 +194,9 @@ public class Constants
        public static string SimulatedTestsNotAllowed = Catalog.GetString("Chronopic is disconnected.") + 
"\n\n" + 
                string.Format(Catalog.GetString("If you want to simulate tests, use {0} session."), 
"SIMULATED");
 
+       public static string RFIDDisconnectedMessage = Catalog.GetString("RFID cable has been disconnected!") 
+ "\n\n" +
+               Catalog.GetString("Please, connect it and restart Chronojump.");
+
        //server
        public const string ServerPingTable = "SPing"; 
        public const string ServerEvaluatorTable = "SEvaluator"; 
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 166d7c8..8a854cb 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -81,6 +81,7 @@ public partial class ChronoJumpWindow
        public RFID rfid;
        private static string capturedRFID;
        private static bool shouldUpdateRFIDGui;
+       private static bool shouldShowRFIDDisconnected;
        private static bool updatingRFIDGuiStuff;
        private bool rfidProcessCancel;
 
@@ -136,6 +137,7 @@ public partial class ChronoJumpWindow
                        {
                                rfid = new RFID(chronopicRegister.GetRfidPortName());
                                rfid.FakeButtonChange.Clicked += new EventHandler(rfidChanged);
+                               rfid.FakeButtonDisconnected.Clicked += new EventHandler(rfidDisconnected);
 
                                threadRFID = new Thread (new ThreadStart (RFIDStart));
                                GLib.Idle.Add (new GLib.IdleHandler (pulseRFID));
@@ -168,6 +170,12 @@ public partial class ChronoJumpWindow
                        LogB.Information("RFID doesn't change");
        }
 
+       private void rfidDisconnected(object sender, EventArgs e)
+       {
+               shouldShowRFIDDisconnected = true;
+               rfidProcessCancel = true;
+       }
+
        private void configInitFromPreferences()
        {
                configChronojump = new Config();
@@ -375,6 +383,14 @@ public partial class ChronoJumpWindow
 
        private bool pulseRFID ()
        {
+               if(shouldShowRFIDDisconnected)
+               {
+                       new DialogMessage(Constants.MessageTypes.WARNING, Constants.RFIDDisconnectedMessage);
+
+                       if(dialogPersonPopup != null)
+                               dialogPersonPopup.DestroyDialog();
+               }
+
                if(! threadRFID.IsAlive || rfidProcessCancel)
                {
                        LogB.ThreadEnding();


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