[chronojump] doing rfid. EncoderRProc.isRunning moved to ExecuteProcess



commit e29b2ed4f777a9980125509b493faf97ee7986ab
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Jan 24 17:14:28 2017 +0100

    doing rfid. EncoderRProc.isRunning moved to ExecuteProcess

 ...eadChronojump.py => chronojump_rfid_capture.py} |    0
 src/constants.cs                                   |    1 -
 src/encoderRProc.cs                                |   95 +-----------
 src/executeProcess.cs                              |  161 +++++++++++++++++++-
 src/gui/chronojump.cs                              |   11 ++-
 src/gui/networks.cs                                |   42 +++++-
 src/util.cs                                        |   13 ++
 7 files changed, 218 insertions(+), 105 deletions(-)
---
diff --git a/rfid/ReadChronojump.py b/rfid/chronojump_rfid_capture.py
old mode 100644
new mode 100755
similarity index 100%
rename from rfid/ReadChronojump.py
rename to rfid/chronojump_rfid_capture.py
diff --git a/src/constants.cs b/src/constants.cs
index a1707b3..f750f8c 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -480,7 +480,6 @@ public class Constants
        public static string FileNameLogOld = "log_chronojump_old.txt";
        
        public static string FileNameConfig = "chronojump_config.txt";
-       public static string FilePathRfid = "/tmp/chronojump_rfid.txt";
        
        //30 colors defined
        //see als UtilGtk that's not used by the server
diff --git a/src/encoderRProc.cs b/src/encoderRProc.cs
index 039586b..c40279b 100644
--- a/src/encoderRProc.cs
+++ b/src/encoderRProc.cs
@@ -48,7 +48,7 @@ public abstract class EncoderRProc
 
                bool ok = true;
                        
-               if(isRunning() && isResponsive(p)) {
+               if(ExecuteProcess.IsRunning(p) && ExecuteProcess.IsResponsive(p)) {
                        LogB.Debug("calling continue");
                        ok = continueProcess();
                } else {
@@ -62,97 +62,6 @@ public abstract class EncoderRProc
                return ok;
        }
 
-       protected bool isRunning() 
-       {
-               LogB.Debug("calling isRunning()");
-               if(p == null) {
-                       LogB.Debug("p == null");
-                       return false;
-               } else {
-                       if(isRunningThisProcess(p))
-                               return true;
-               }
-       
-               return false;
-       }
-
-       private bool isRunningThisProcess(Process p)
-       {
-               /*
-                * Process Id is not valid if the associated process is not running.
-                * Need to ensure that the process is running before attempting to retrieve the Id property.
-                */
-               
-               try {
-                       LogB.Debug(string.Format("last pid id {0}", p.Id));
-                       Process pid = Process.GetProcessById(p.Id);
-                       if(pid == null)
-                               return false;
-               } catch {
-                       return false;
-               }
-
-               return true;
-       }
-       /*
-        * don't use this because in linux R script can be called by:
-        * "/usr/lib/R/bin/exec/R"
-        * and it will not be found passing "R" or "*R"
-       private bool isRunningThisProcess(string name)
-       {
-               Process [] pids = Process.GetProcessesByName(name);
-               foreach (Process myPid in pids) {
-                       LogB.Debug(string.Format("pids id: {0}", myPid.Id));
-                       if (myPid.Id == Convert.ToInt32(p.Id))
-                               return true;
-               }
-               
-               return false;
-       }
-       */
-       
-       /*
-        * The process.Responding only works on GUI processes
-        * So, here we send a "ping" expecting to see the result in short time
-        *
-        * TODO: maybe is good to kill the unresponsive processes
-        */
-       private bool isResponsive(Process process)
-       {
-               Random rnd = new Random();
-               int randomInt = rnd.Next(); //eg. 1234
-               
-               string randomPingStr = "PING" + Path.Combine(Path.GetTempPath(), "chronojump" + 
randomInt.ToString() + ".txt"); 
-               //eg Linux: 'PING/tmp/chronojump1234.txt'
-               //eg Windows: 'PINGC:\Temp...\chronojump1234.txt'
-
-               if (UtilAll.IsWindows()) {
-                       //On win32 R understands backlash as an escape character and 
-                       //a file path uses Unix-like path separator '/'         
-                       randomPingStr = randomPingStr.Replace("\\","/");
-               }
-               //eg Windows: 'PINGC:/Temp.../chronojump1234.txt'
-
-               LogB.Information("Sending ping: " + randomPingStr);
-               try {
-                       process.StandardInput.WriteLine(randomPingStr);
-               } catch {
-                       LogB.Warning("Catched waiting response");
-                       return false;
-               }
-
-               //wait 250ms the response
-               System.Threading.Thread.Sleep(250);
-
-               //On Linux will be '/' on Windows '\'   
-               if(File.Exists(Path.Combine(Path.GetTempPath(), "chronojump" + randomInt.ToString() + 
".txt"))) {
-                       LogB.Information("Process is responding");
-                       return true;
-               }
-
-               LogB.Warning("Process is NOT responding");
-               return false;
-       }
 
        protected string pBinURL()
        {
@@ -231,7 +140,7 @@ public abstract class EncoderRProc
 
        public void SendEndProcess() 
        {
-               if(isRunning()) {
+               if(ExecuteProcess.IsRunning(p)) {
                        LogB.Debug("Closing R script");
                        try {
                                p.StandardInput.WriteLine("Q");
diff --git a/src/executeProcess.cs b/src/executeProcess.cs
index 0909cdd..fe3f674 100644
--- a/src/executeProcess.cs
+++ b/src/executeProcess.cs
@@ -1,8 +1,3 @@
-using System.Collections.Generic;
-using System.Diagnostics;
-using System;
-using Mono.Unix;
-
 /*
  * This file is part of ChronoJump
  *
@@ -21,8 +16,16 @@ using Mono.Unix;
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Copyright (C) 2016-2017   Carles Pina i Estany <carles pina cat>
+ * Copyright (C) 2017   Xavier de Blas <xaviblas gmail com>
  */
 
+using System.Collections.Generic;
+using System.Diagnostics;
+using System;
+using Mono.Unix;
+using System.IO;                //FILE
+
+
 /* This class executes a process and returns the result. */
 class ExecuteProcess
 {
@@ -123,4 +126,152 @@ class ExecuteProcess
 
                return new Result (stdout, stderr, exitCode);
        }
+
+       /*
+        * run a process on the background, eg: read rfid.
+        * don't call WaitForExit(), kill it on Chronojump exit
+        * returns false if there are problems calling it
+        */
+       public static bool RunAtBackground(Process process, string file_name, List<string> parameters)
+       {
+               if(! File.Exists(parameters[0]))
+               {
+                       LogB.Debug ("ExecuteProcess does not exist parameter: " + parameters[0]);
+                       return false;
+               }
+
+               ProcessStartInfo processStartInfo = new ProcessStartInfo();
+
+               processStartInfo.FileName = file_name;
+
+               string parameters_string = "";
+               foreach (string parameter in parameters)
+               {
+                       parameters_string += CommandLineEncoder.EncodeArgText (parameter) + " ";
+               }
+
+               processStartInfo.Arguments = parameters_string;
+
+               LogB.Debug ("ExecuteProcess FileName: " + processStartInfo.FileName);
+               LogB.Debug ("ExecuteProcess Arguments: " + processStartInfo.Arguments);
+
+               processStartInfo.CreateNoWindow = true;
+               processStartInfo.UseShellExecute = false;
+               processStartInfo.RedirectStandardInput = false;
+               processStartInfo.RedirectStandardError = true;
+               processStartInfo.RedirectStandardOutput = true;
+
+               process.StartInfo = processStartInfo;
+
+               try {
+                       process.Start();
+               }
+               catch(Exception e) {
+                       string errorMessage = String.Format (Catalog.GetString("Cannot start:\n" +
+                                                                              "{0}\n" +
+                                                                              "with the parameters:" +
+                                                                              "{1}\n" +
+                                                                              "Exception:\n" +
+                                                                              "{2}"),
+                                                            processStartInfo.FileName, parameters_string, 
e.Message);
+                       LogB.Warning (errorMessage);
+                       return false;
+               }
+
+               return true;
+       }
+
+       public static bool IsRunning(Process p)
+       {
+               LogB.Debug("calling Process.IsRunning()");
+               if(p == null) {
+                       LogB.Debug("p == null");
+                       return false;
+               } else {
+                       if(isRunningThisProcess(p))
+                               return true;
+               }
+
+               return false;
+       }
+
+       private static bool isRunningThisProcess(Process p)
+       {
+               /*
+                * Process Id is not valid if the associated process is not running.
+                * Need to ensure that the process is running before attempting to retrieve the Id property.
+                */
+
+               try {
+                       LogB.Debug(string.Format("last pid id {0}", p.Id));
+                       Process pid = Process.GetProcessById(p.Id);
+                       if(pid == null)
+                               return false;
+               } catch {
+                       return false;
+               }
+
+               return true;
+       }
+
+       /*
+        * don't use this because in linux R script can be called by:
+        * "/usr/lib/R/bin/exec/R"
+        * and it will not be found passing "R" or "*R"
+       private bool isRunningThisProcess(string name)
+       {
+               Process [] pids = Process.GetProcessesByName(name);
+               foreach (Process myPid in pids) {
+                       LogB.Debug(string.Format("pids id: {0}", myPid.Id));
+                       if (myPid.Id == Convert.ToInt32(p.Id))
+                               return true;
+               }
+
+               return false;
+       }
+       */
+
+       /*
+        * The process.Responding only works on GUI processes
+        * So, here we send a "ping" expecting to see the result in short time
+        *
+        * TODO: maybe is good to kill the unresponsive processes
+        */
+       public static bool IsResponsive(Process p)
+       {
+               Random rnd = new Random();
+               int randomInt = rnd.Next(); //eg. 1234
+
+               string randomPingStr = "PING" + Path.Combine(Path.GetTempPath(), "chronojump" + 
randomInt.ToString() + ".txt"); 
+               //eg Linux: 'PING/tmp/chronojump1234.txt'
+               //eg Windows: 'PINGC:\Temp...\chronojump1234.txt'
+
+               if (UtilAll.IsWindows()) {
+                       //On win32 R understands backlash as an escape character and
+                       //a file path uses Unix-like path separator '/'
+                       randomPingStr = randomPingStr.Replace("\\","/");
+               }
+               //eg Windows: 'PINGC:/Temp.../chronojump1234.txt'
+
+               LogB.Information("Sending ping: " + randomPingStr);
+               try {
+                       p.StandardInput.WriteLine(randomPingStr);
+               } catch {
+                       LogB.Warning("Catched waiting response");
+                       return false;
+               }
+
+               //wait 250ms the response
+               System.Threading.Thread.Sleep(250);
+
+               //On Linux will be '/' on Windows '\'
+               if(File.Exists(Path.Combine(Path.GetTempPath(), "chronojump" + randomInt.ToString() + 
".txt"))) {
+                       LogB.Information("Process is responding");
+                       return true;
+               }
+
+               LogB.Warning("Process is NOT responding");
+               return false;
+       }
+
 }
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 5ab0a21..36cab7f 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2277,7 +2277,8 @@ public partial class ChronoJumpWindow
        }
                
 
-       private void on_quit2_activate (object o, EventArgs args) {
+       private void on_quit2_activate (object o, EventArgs args)
+       {
                LogB.Information("Bye!");
 
                updatingRestTimes = false;
@@ -2299,6 +2300,14 @@ 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");
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index ab8890d..31bee67 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -27,6 +27,7 @@ using System.IO.Ports;
 using System.IO; //"File" things
 using System.Collections; //ArrayList
 using System.Collections.Generic; //List
+using System.Diagnostics; //Process
        
 public partial class ChronoJumpWindow 
 {
@@ -196,17 +197,49 @@ public partial class ChronoJumpWindow
        }
        void on_button_rfid_read_clicked (object o, EventArgs args)
        {
-               string filePath = Constants.FilePathRfid;
+               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)
        {
-               button_rfid_start.Sensitive = false;
+               string script_path = Util.GetRFIDCaptureScript();
 
-               string filePath = Constants.FilePathRfid;
+               if(! File.Exists(script_path))
+               {
+                       LogB.Debug ("ExecuteProcess does not exist parameter: " + script_path);
+                       label_rfid.Text = "Error starting rfid capture";
+                       return;
+               }
+
+               string filePath = Util.GetRFIDCapturedFile();
+               Util.FileDelete(filePath);
+
+
+               // ---- start process ----
+               //
+               // on Windows will be different, but at the moment RFID is only supported on Linux 
(Raspberrys)
+               // On Linux and OSX we execute Python and we pass the path to the script as a first argument
+
+               string executable = "python";         // TODO: check if ReadChronojump.py works on Python 2 
and Python 3
+
+               List<string> parameters = new List <string> ();
+               // first argument of the Python: the path to the script
+               parameters.Insert (0, script_path);
+
+
+               processRFIDcapture = new Process();
+               bool calledOk = ExecuteProcess.RunAtBackground (processRFIDcapture, executable, parameters);
+               if(calledOk) {
+                       button_rfid_start.Sensitive = false;
+                       label_rfid.Text = "...";
+               } else
+                       label_rfid.Text = "Error starting rfid capture";
+
+               // ----- process is launched
 
                //create a new FileSystemWatcher and set its properties.
                FileSystemWatcher watcher = new FileSystemWatcher();
@@ -230,7 +263,7 @@ public partial class ChronoJumpWindow
 
        private void rfid_read()
        {
-               string filePath = Constants.FilePathRfid;
+               string filePath = Util.GetRFIDCapturedFile();
 
                LogB.Information("Changed file: " +  filePath);
 
@@ -238,6 +271,5 @@ public partial class ChronoJumpWindow
                        label_rfid.Text = Util.ReadFile(filePath, true);
        }
 
-
 }
 
diff --git a/src/util.cs b/src/util.cs
index e4e7681..29b9a70 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -965,6 +965,19 @@ public class Util
        /********** end of encoder paths ************/
 
 
+       /********** start of rfid paths ************/
+
+       public static string GetRFIDCaptureScript() {
+               return Path.Combine(GetPrefixDir(), "bin/chronojump_rfid_capture.py");
+       }
+       public static string GetRFIDCapturedFile() {
+               return Path.Combine(Path.GetTempPath(), "chronojump_rfid.txt");
+       }
+
+
+       /********** end of rfid paths ************/
+
+
        public static string GetManualDir() {
                //we are on:
                //lib/chronojump/ (Unix) or bin/ (win32)


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