Don't add trailing spaces when producing logs
[SaveMySugar/python3-savemysugar.git] / src / savemysugar / CallDistanceTransceiver.py
index f9760bb..29c3fb8 100755 (executable)
@@ -205,7 +205,7 @@ class CallDistanceTransceiver(object):
         self.end_of_message = False
 
     def log_symbol(self, distance, symbol, extra_info=""):
-        logging.info("distance: %.2f Received \"%s\" %s", distance, symbol,
+        logging.info("distance: %.2f Received \"%s\"%s", distance, symbol,
                      extra_info)
 
     def receive_character(self):
@@ -247,7 +247,7 @@ class CallDistanceTransceiver(object):
            call_distance <= self.signalspace_time.max:
             signal = self.morse_message.strip().split(' ')[-1]
             character = self.translator.signal_to_character(signal)
-            self.log_symbol(call_distance, ' ', "got \"%s\"" % character)
+            self.log_symbol(call_distance, ' ', " got \"%s\"" % character)
             self.morse_message += " "
             return
 
@@ -255,14 +255,14 @@ class CallDistanceTransceiver(object):
            call_distance <= self.wordspace_time.max:
             signal = self.morse_message.strip().split(' ')[-1]
             character = self.translator.signal_to_character(signal)
-            self.log_symbol(call_distance, '/', "got \"%s\"" % character)
+            self.log_symbol(call_distance, '/', " got \"%s\"" % character)
             self.morse_message += " / "
             return
 
         if call_distance > self.eom_time.min:
             signal = self.morse_message.strip().split(' ')[-1]
             character = self.translator.signal_to_character(signal)
-            self.log_symbol(call_distance, 'EOM', "got \"%s\"" % character)
+            self.log_symbol(call_distance, 'EOM', " got \"%s\"" % character)
             self.end_of_message = True
             self.previous_ring_time = -1
             self.previous_call_time = -1
@@ -383,34 +383,63 @@ class CallDistanceTransceiver(object):
 
 def test_send_receive():
     logging.basicConfig(level=logging.DEBUG)
-    call_setup_time = 2
-    call_setup_uncertainty = 0.4
-    ring_time = 1
-    ring_uncertainty = 0.3
+    call_setup_time_min = 0
+    call_setup_time_max = 0.01
+    ring_time_min = 0
+    ring_time_max = 0
+
+    import random
 
     class DummyModem(object):
-        """Always receive a '.' and then a '/', which result in 'E '."""
+        """Always receive a '.', a '/' and then EOM, which results in 'E '."""
 
         def __init__(self):
             self.ring_count = 0
 
+            # Take trasmission times from a transceiver
+            self.transceiver = None
+
+            random.seed(None)
+
         def send_command(self, command):
             pass
 
         def get_response(self, response):
-            if self.ring_count % 2:
+            # pylint: disable=unused-argument
+
+            setup_time = random.uniform(self.transceiver.call_setup_time_min,
+                                        self.transceiver.call_setup_time_max)
+
+            if self.ring_count == 0:
+                # dummy ring
+                pass
+            elif self.ring_count == 1:
                 # received a '.'
-                time.sleep(call_setup_time + (ring_time + ring_uncertainty))
-            else:
+                time.sleep(setup_time + self.transceiver.dot_time.dist)
+            elif self.ring_count == 2:
                 # received a '/'
-                time.sleep(call_setup_time + (ring_time + ring_uncertainty) * 4)
+                time.sleep(setup_time + self.transceiver.wordspace_time.dist)
+            else:
+                # received an 'EOM'
+                time.sleep(setup_time + self.transceiver.eom_time.dist)
 
             self.ring_count += 1
+            self.ring_count %= 4
+
+    modem = DummyModem()
+
+    xcv = CallDistanceTransceiver(modem,
+                                  call_setup_time_min, call_setup_time_max,
+                                  ring_time_min, ring_time_max, True)
+
+    modem.transceiver = xcv
 
-    xcv = CallDistanceTransceiver(DummyModem(), call_setup_time,
-                                  call_setup_uncertainty, ring_time,
-                                  ring_uncertainty)
-    xcv.receive_loop()
+    while True:
+        xcv.receive_loop()
+        modem.ring_count = 0
+        print()
+        print("Message received!")
+        print("\"%s\"" % xcv.get_text(), flush=True)
 
 
 if __name__ == "__main__":