From 86e5974f6d5cbf57a2450b9fce09964ea64d3b1f Mon Sep 17 00:00:00 2001
From: Robert Hartung <hartung@ibr.cs.tu-bs.de>
Date: Thu, 11 Jan 2018 14:32:39 +0100
Subject: [PATCH] adds latest coordinator (cleaned up)

---
 software/coordinator2.py | 94 ++++++++++++++++++++++------------------
 1 file changed, 53 insertions(+), 41 deletions(-)

diff --git a/software/coordinator2.py b/software/coordinator2.py
index b479a21..12b22e5 100644
--- a/software/coordinator2.py
+++ b/software/coordinator2.py
@@ -56,6 +56,49 @@ def calculate_checksum(*args):
         s += sum(bytes(a, 'utf-8'))
     return hex( s&0xFF )[2:].upper().zfill(2)
 
+def handle_match_tx( src_id, ts, match_tx ):
+    seq_nr = match_tx.group("seq_nr")
+    checksum = match_tx.group("checksum")
+    
+    expected_checksum = calculate_checksum( seq_nr )
+    if expected_checksum == checksum:
+        session = Session()
+        session.add(TX(serial_id = src_id, ts=ts, seq_nr = seq_nr ))
+        session.commit()
+        #logger.log("[{}] TX: #{}".format( src_id, seq_nr ))
+        return True
+    else:
+        logger.debug("Corrupt Line")
+        return False
+
+def handle_match_rx( src_id, ts, match_rx ):
+    node_id = match_rx.group("node_id")
+    seq_nr = match_rx.group("seq_nr")
+    rssi = match_rx.group('rssi')
+    lqi = match_rx.group('lqi')
+    checksum = match_rx.group("checksum")
+    expected_checksum = calculate_checksum( node_id, '|', seq_nr, '|', rssi, '|', lqi )
+
+    if expected_checksum == checksum:
+        session = Session()
+        session.add(RX(serial_id = src_id, ts=ts, seq_nr = seq_nr, node_id = node_id, rssi = rssi, lqi = lqi))
+        # TODO(rh): Add sqlite3.OperationalError
+        session.commit()
+        #logger.log("[{}] RX: #{} from {}".format( src_id, node_id, seq_nr ))
+        return True
+    else:
+        logger.debug("Corrupt Line {} != {}".format( expected_checksum, checksum) )
+        return False
+
+def handle_match_rx_corrupt( src_id, ts, match_rx_corrupt ):
+    rssi = match_rx_corrupt.group('rssi')
+    lqi = match_rx_corrupt.group('lqi')
+    payload = match_rx_corrupt.group('payload').strip()
+    logger.debug( "Corrupt: {}dBm, {}, {}".format( rssi, lqi, payload ) )
+    session = Session()
+    session.add(Corrupt(serial_id = src_id, ts=ts, rssi = rssi, lqi = lqi, payload = payload))
+    session.commit()
+    return True
 
 def handle_serial_rx(d):
     src_id = d['src']['id'][0]
@@ -65,51 +108,15 @@ def handle_serial_rx(d):
     match_ifconfig = re_ifconfig.match(line)
     if match_ifconfig is not None:
         return True
-
     match_tx = re_tx.match(line)
     if match_tx is not None:
-        seq_nr = match_tx.group("seq_nr")
-        checksum = match_tx.group("checksum")
-        
-        expected_checksum = calculate_checksum( seq_nr )
-        if expected_checksum == checksum:
-            session = Session()
-            session.add(TX(serial_id = src_id, ts=ts, seq_nr = seq_nr ))
-            session.commit()
-            #logger.log("[{}] TX: #{}".format( src_id, seq_nr ))
-            return True
-        else:
-            logger.debug("Corrupt Line")
-            return False
+        return handle_match_tx( src_id, ts, match_tx )
     match_rx = re_rx.match(line)
     if match_rx is not None:
-        node_id = match_rx.group("node_id")
-        seq_nr = match_rx.group("seq_nr")
-        rssi = match_rx.group('rssi')
-        lqi = match_rx.group('lqi')
-        checksum = match_rx.group("checksum")
-        expected_checksum = calculate_checksum( node_id, '|', seq_nr, '|', rssi, '|', lqi )
-
-        if expected_checksum == checksum:
-            session = Session()
-            session.add(RX(serial_id = src_id, ts=ts, seq_nr = seq_nr, node_id = node_id, rssi = rssi, lqi = lqi))
-            session.commit()
-            #logger.log("[{}] RX: #{} from {}".format( src_id, node_id, seq_nr ))
-            return True
-        else:
-            logger.debug("Corrupt Line {} != {}".format( expected_checksum, checksum) )
-            return False
-        return True
+        return handle_match_rx( src_id, ts, match_rx )
     match_rx_corrupt = re_rx_corrupt.match(line)
     if match_rx_corrupt is not None:
-        rssi = match_rx_corrupt.group('rssi')
-        lqi = match_rx_corrupt.group('lqi')
-        payload = match_rx_corrupt.group('payload').strip()
-        logger.debug( "Corrupt: {}dBm, {}, {}".format( rssi, lqi, payload ) )
-        session = Session()
-        session.add(Corrupt(serial_id = src_id, ts=ts, rssi = rssi, lqi = lqi, payload = payload))
-        session.commit()
-        return True
+        return handle_match_rx_corrupt( src_id, ts, match_rx_corrupt  )
     return False
 
 class PRREvalClient(Client):
@@ -130,11 +137,16 @@ class PRREvalClient(Client):
                     logger.log( "PAYLOAD={} ".format(payload) )
                     self.send_all( "payload {}\n".format( payload )  )
                     sleep( 0.5 )
+                    # TODO(rh) START ROUND:
+                    # round = Round( channel=channel, power=power, payload=payload )
+                    # session.add( round )
+                    # sessionc.commit()
                     for sender in NODES:
                         self.send({'dst': {'id': sender}, 'type': 'serial_tx', 'line': 'tx 5 200000\n'})
                         # 5 packets, 200ms -> 1 second
                         sleep( 1.5 )
-    
+                        self.send({'dst': {'id': sender}, 'type': 'serial_tx', 'line': 'ifconfig 3 set state idle\n'})
+
     def run(self):
         while True:
             try:
@@ -144,7 +156,7 @@ class PRREvalClient(Client):
                 break
 
     def set_channel(self, channel):
-        self.send_all("ifconfig 3 set state idle\n")
+        #self.send_all("ifconfig 3 set state idle\n")
         self.send_all("ifconfig 3 set channel {}\n".format( channel ))
     
     def set_txpower(self, power):
-- 
GitLab