Преглед изворни кода

piufared updates

* send 'Q' messages for QR codes (over rs232 line)
* ratelimit messages
* go through init sequence for DIU to recognize it
clementinecomputing пре 4 година
родитељ
комит
c59015a9ac
1 измењених фајлова са 76 додато и 14 уклоњено
  1. 76 14
      busunit-PIU/piufared/piufared

+ 76 - 14
busunit-PIU/piufared/piufared

@@ -39,6 +39,8 @@ opt = {
 
   "ratelimit_mark" : 0.0,
   "ratelimit_t" : 10.0,
+  "ratelimit_n" : 40,
+  "ratelimit_count" : 0,
 
   "dev" : "/dev/ttyPIU",
   "baud" : 115200,
@@ -57,6 +59,27 @@ opt = {
 def _DT():
   return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
+# We allow `reatelimit_n` message bursts.
+# If the number of messages exceeds `ratelimit_n`, then
+# we have a moratorium on messages for `ratelimit_t` seconds.
+# Once the time watermark is exceeded, the count and time
+# watermark reset.
+#
+# Return True if we should ratelimit
+# Return False if we don't need to ratelimit
+#
+def ratelimit(_opt):
+  if time.time() > _opt["ratelimit_mark"]:
+    _opt["ratelimit_count"] = 0
+    _opt["ratelimit_mark"] = time.time() + _opt["ratelimit_t"]
+    return False
+  elif _opt["ratelimit_count"] < _opt["ratelimit_n"]:
+    _opt["ratelimit_count"] += 1
+    #_opt["ratelimit_mark"] = time.time() + _opt["ratelimit_t"]
+    return False
+  return True
+
+
 def write_state(_opt):
   with open(opt["state_fn"], "w") as ofp:
     ofp.write("mag_ts " + str(_opt["mag_ts"]) + "\nrfid_ts " + str(_opt["rfid_ts"]) + "\nqr_ts " + str(_opt["qr_ts"]) + "\n")
@@ -117,20 +140,56 @@ while True:
     have_data = True
 
   if have_data:
+
     s = "".join(buf)
     buf = []
 
     if len(s)==0:
-      if time.time() > opt["ratelimit_mark"]:
+      if not ratelimit(opt):
 
         ## DEBUG
         if opt["verbose"] > 0:
           print(_DT(), "[piufared]", "sending init msg '/?: ?=rider_ui\\r\\n")
-          os.stdout.flush()
+          sys.stdout.flush()
+
+        send_data(piu_serial, "/?: ?=rider_ui\r\n")
 
-        write_data(piu_serial, "/?: ?=rider_ui\r\n")
-        opt["ratelimit_mark"] = time.time() + opt["ratelimit_t"]
       continue
+    elif s[0:3] == "/0:":
+
+      if not ratelimit(opt):
+
+        ## DEBUG
+        if opt["verbose"] > 0:
+          print(_DT(), "[piufared]", "sending /ok: resp to '/0:'")
+          sys.stdout.flush()
+
+        send_data(piu_serial, "/ok:\r\n")
+
+    elif s[0:3] == "/1:":
+      if not ratelimit(opt):
+        ## DEBUG
+        if opt["verbose"] > 0:
+          print(_DT(), "[piufared]", "sending /ok: resp to '/1:'")
+          sys.stdout.flush()
+
+        send_data(piu_serial, "/ok:\r\n")
+
+    elif s[0:3] == "/m:":
+
+      if not ratelimit(opt):
+
+        ## DEBUG
+        if opt["verbose"] > 0:
+          print(_DT(), "[piufared]", "sending '/OK:m:09 00 03 42 5A 44 56\\r\\n'")
+          sys.stdout.flush()
+
+        # init sequence the DIU expects
+        # hard coded for now.
+        # should echo received message?
+        #
+        send_data(piu_serial, "/OK:m:09 00 03 42 5A 44 56\r\n")
+        send_data(piu_serial, "/M:^\r\n")
 
 
     if len(s) < 3: continue
@@ -151,9 +210,10 @@ while True:
     lines = fp.read().split("\n")
     for idx in range(len(lines)-1):
       line = lines[idx]
-      tok = line.split(" ")
-      if len(tok)!=2: continue
-      _ts = int(tok[0].split(":")[0])
+      tok = line.split(":")
+      if len(tok)<2: continue
+      _ts = int(tok[0])
+      tok[1] = re.sub('^ *', '', tok[1])
 
       if _ts <= opt["mag_ts"]: continue
 
@@ -168,9 +228,10 @@ while True:
     lines = fp.read().split("\n")
     for idx in range(len(lines)-1):
       line = lines[idx]
-      tok = line.split(" ")
-      if len(tok)!=2: continue
-      _ts = int(tok[0].split(":")[0])
+      tok = line.split(":")
+      if len(tok)<2: continue
+      _ts = int(tok[0])
+      tok[1] = re.sub('^ *', '', tok[1])
 
       if _ts <= opt["rfid_ts"]: continue
 
@@ -185,16 +246,17 @@ while True:
     lines = fp.read().split("\n")
     for idx in range(len(lines)-1):
       line = lines[idx]
-      tok = line.split(" ")
-      if len(tok)!=2: continue
-      _ts = int(tok[0].split(":")[0])
+      tok = line.split(":")
+      if len(tok)<2: continue
+      _ts = int(tok[0])
+      tok[1] = re.sub('^ *', '', tok[1])
 
       if _ts <= opt["qr_ts"]: continue
 
       if opt["verbose"] > 0:
         print(_DT(), "[piufared]", "sending qr", " ".join(tok[1:]))
 
-      send_data(piu_serial, "/M:" + " ".join(tok[1:]) + "\r\n")
+      send_data(piu_serial, "/Q:" + " ".join(tok[1:]) + "\r\n")
       opt["qr_ts"] = _ts
       state_change = True