[chronojump-server] Read RFID by Arduino, and show countdown on html
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump-server] Read RFID by Arduino, and show countdown on html
- Date: Thu, 11 May 2017 16:19:36 +0000 (UTC)
commit df9f2d051032abe217726ee51c0b19b2c8def79d
Author: Xavier de Blas <xaviblas gmail com>
Date: Thu May 11 18:18:52 2017 +0200
Read RFID by Arduino, and show countdown on html
chronojump-flask/chronojump_server.py | 2 +-
chronojump-flask/rfid-csharp/RFID.cs | 185 ++++++++++++++++++++++++++++
chronojump-flask/templates/player_add.html | 32 +++++-
3 files changed, 215 insertions(+), 4 deletions(-)
---
diff --git a/chronojump-flask/chronojump_server.py b/chronojump-flask/chronojump_server.py
index fe5f7a9..ed0ed5e 100644
--- a/chronojump-flask/chronojump_server.py
+++ b/chronojump-flask/chronojump_server.py
@@ -128,7 +128,7 @@ def player_add_submit():
if os.access(rfidFile, os.W_OK):
os.remove(rfidFile)
- rfidReadedStatus = subprocess.call("/usr/local/bin/chronojump_rfid_capture_exit.py", shell=True)
+ rfidReadedStatus = subprocess.call("mono /srv/api-app/chronojump-flask/rfid-csharp/RFID.exe",
shell=True)
try:
with open(rfidFile) as f:
diff --git a/chronojump-flask/rfid-csharp/RFID.cs b/chronojump-flask/rfid-csharp/RFID.cs
new file mode 100644
index 0000000..9169a22
--- /dev/null
+++ b/chronojump-flask/rfid-csharp/RFID.cs
@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) 2017 Xavier de Blas <xaviblas gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using System.Collections.Generic; //List<T>
+using System.IO;
+using System.IO.Ports;
+using System.Threading;
+
+public class RFIDMain
+{
+ const string rfidFile = "/tmp/chronojump_rfid.txt";
+
+ public static void Main()
+ {
+ removeFile();
+
+ RFID rfid = new RFID();
+ RFID.StatusEnum status = rfid.Start();
+
+ if(status == RFID.StatusEnum.DETECTED) {
+ Console.WriteLine("Found: " + rfid.Captured);
+ write(rfid.Captured);
+ }
+ else
+ Console.WriteLine("Problems: " + status.ToString());
+ }
+
+ private static void removeFile()
+ {
+ if(File.Exists(rfidFile))
+ File.Delete(rfidFile);
+ }
+
+ private static void write(string str)
+ {
+ TextWriter writer = File.CreateText(rfidFile);
+ writer.Write(str);
+ writer.Flush();
+ writer.Close();
+ ((IDisposable)writer).Dispose();
+ }
+}
+
+public class RFID
+{
+ public enum StatusEnum { NO_PORTS, NO_RFID_PORT, NO_DETECTED, DETECTED };
+
+ public string Captured;
+ private bool stop;
+ //public event EventHandler ChangedEvent; //raised when change RFID vaues
+ private SerialPort port;
+
+ public RFID()
+ {
+ Captured = "";
+ stop = false;
+ }
+
+ public StatusEnum Start()
+ {
+ Console.WriteLine("getPorts");
+ List<string> l = getPorts(false);
+ if(l == null || l.Count == 0)
+ return StatusEnum.NO_PORTS;
+
+
+ string str = "";
+ bool found = findRFIDPort(l);
+ if(! found)
+ return StatusEnum.NO_RFID_PORT;
+
+ //don't need to open port because it's still opened
+ //port.Open();
+ Console.WriteLine("Detection starts!");
+ DateTime ts_start = DateTime.Now;
+ while(! stop)
+ {
+ //str = port.ReadLine(); //don't use this because gets waiting some stop signal
+ if (port.BytesToRead > 0)
+ {
+ str = port.ReadExisting();
+ //Console.WriteLine("No trim str" + str);
+
+ //get only the first line and trim it
+ if(str.IndexOf(Environment.NewLine) > 0)
+ str = str.Substring(0, str.IndexOf(Environment.NewLine)).Trim();
+
+ //Console.WriteLine("Yes one line and trim str" + str);
+
+ //this first line should have a ';' (mark of end of rfid)
+ if(str.IndexOf(";") > 0)
+ {
+ str = str.Substring(0, str.IndexOf(";"));
+
+ Console.WriteLine(str);
+ Captured = str;
+
+ stop = true;
+ }
+ }
+ Thread.Sleep(50);
+ TimeSpan ts = DateTime.Now.Subtract(ts_start);
+ if(ts.TotalSeconds >= 5)
+ stop = true;
+ }
+ Console.WriteLine("AT RFID.cs: STOPPED");
+ //empty buffer
+ if (port.BytesToRead > 0)
+ port.ReadExisting();
+
+ port.Close();
+
+ if(Captured == "")
+ return StatusEnum.NO_DETECTED;
+ else
+ return StatusEnum.DETECTED;
+ }
+
+ private bool findRFIDPort(List<string> l)
+ {
+ foreach(string p in l)
+ {
+ Console.WriteLine("portName: " + p);
+ port = new SerialPort(p, 9600); //for the rfid
+ port.Open();
+ Thread.Sleep(2000); //sleep to let arduino start reading. Use 3000 if NO_RFID_PORT
+
+ if (port.BytesToRead > 0){
+ Console.WriteLine("Reading at beginning of findRFIDPort: " +
port.ReadExisting());
+ }
+ //send welcome message
+ port.WriteLine("Chronojump RFID");
+
+ //don't do this because if is not Arduino-RFID can be waiting answer forever
+ //string str = port.ReadLine();
+ string str = "";
+ DateTime ts_start = DateTime.Now;
+ do {
+ if (port.BytesToRead > 0)
+ str += port.ReadExisting();
+ } while ( (DateTime.Now.Subtract(ts_start)).TotalSeconds < 2 );
+
+ Console.WriteLine("str: " + str);
+ if(str.StartsWith("YES Chronojump RFID"))
+ {
+ Console.WriteLine("Arduino RFID found on port: " + p);
+ return(true);
+ }
+ else
+ Console.WriteLine("Arduino RFID NOT found on port: " + p);
+
+ port.Close();
+ }
+ return(false);
+ }
+
+ private List<string> getPorts(bool debug)
+ {
+ //TODO: move that method here
+ //List<string> l = new List<string>(ChronopicPorts.GetPorts()); //usb-serial ports
+ List<string> l = new List<string>(Directory.GetFiles("/dev/", "ttyUSB*"));
+
+ if(debug)
+ foreach(string p in l)
+ Console.WriteLine(string.Format("port: " + p));
+
+ return l;
+ }
+}
diff --git a/chronojump-flask/templates/player_add.html b/chronojump-flask/templates/player_add.html
index ab37151..af04467 100644
--- a/chronojump-flask/templates/player_add.html
+++ b/chronojump-flask/templates/player_add.html
@@ -5,6 +5,27 @@
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/tables.css') }}">
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/input.css') }}">
<script src="{{ url_for('static',filename='js/js.js') }}"></script>
+ <script language="JavaScript" type="text/javascript">
+ <!--
+ var timer;
+ var count=2;
+ function countDown(){
+ document.getElementById('hiddenDivWait').style.display = "block";
+ document.getElementById("countDownTime").textContent = count;
+
+ if (count<=0){
+ count=3;
+ clearTimeout(timer);
+ document.getElementById('hiddenDivWait').style.display = "none";
+ document.getElementById('hiddenDivDo').style.display = "block";
+ }
+ else{
+ timer=setTimeout('countDown()',1000);
+ }
+ count--;
+ }
+ //-->
+ </script>
</head>
<body>
@@ -35,10 +56,15 @@
</tr>
<tr>
<td>
- <input type = "submit" value = "Llegeix RFID" onclick="showDiv()"
/><br>
+ <input type = "submit" value = "Llegeix RFID" onClick="countDown()"
/><br>
</td>
- <td>
- <div id="hiddenDiv" style="display:none; color:red;"
class="answer_list" >Apropi la pulsera al lector...</div>
+ <td colspan="2">
+ <div id="hiddenDivWait" style="display:none; color:red;"
class="answer_list" >
+ Esperi <span id="countDownTime">3</span> segons
+ </div>
+ <div id="hiddenDivDo" style="display:none; color:red;"
class="answer_list" >
+ Apropi la pulsera al lector...
+ </div>
</td>
</tr>
</table>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]