TODO: mention some of the known issues in the TODO list
[SaveMySugar/python3-savemysugar.git] / src / savemysugar / CallDistanceTransceiver.py
index 6361afa..f04c533 100755 (executable)
@@ -161,6 +161,11 @@ class MorseDistanceModulator(object):
         return distances
 
 
         return distances
 
 
+def log_symbol(distance, symbol, extra_info=""):
+    logging.info("distance: %.2f Received \"%s\"%s", distance, symbol,
+                 extra_info)
+
+
 class CallDistanceTransceiver(object):
     """Transmit Morse messages using the distance between calls.
 
 class CallDistanceTransceiver(object):
     """Transmit Morse messages using the distance between calls.
 
@@ -216,8 +221,6 @@ class CallDistanceTransceiver(object):
         self.modem = modem
         self.translator = MorseTranslator()
 
         self.modem = modem
         self.translator = MorseTranslator()
 
-        self.destination_number = ""
-
         self.call_setup_time_max = call_setup_time_max
 
         if add_inter_call_distance:
         self.call_setup_time_max = call_setup_time_max
 
         if add_inter_call_distance:
@@ -287,17 +290,13 @@ class CallDistanceTransceiver(object):
 
         self.end_of_message = False
 
 
         self.end_of_message = False
 
-    def log_symbol(self, distance, symbol, extra_info=""):
-        logging.info("distance: %.2f Received \"%s\"%s", distance, symbol,
-                     extra_info)
-
-    def receive_character(self):
+    def receive_symbol(self):
         current_ring_time = time.time()
 
         if self.previous_ring_time == -1:
             self.previous_ring_time = current_ring_time
             self.previous_call_time = current_ring_time
         current_ring_time = time.time()
 
         if self.previous_ring_time == -1:
             self.previous_ring_time = current_ring_time
             self.previous_call_time = current_ring_time
-            self.log_symbol(0, "", "(The very first ring)")
+            log_symbol(0, "", "(The very first ring)")
             return
 
         ring_distance = current_ring_time - self.previous_ring_time
             return
 
         ring_distance = current_ring_time - self.previous_ring_time
@@ -326,7 +325,7 @@ class CallDistanceTransceiver(object):
             character = self.translator.signal_to_character(signal)
             extra_info = " got \"%s\"" % character
 
             character = self.translator.signal_to_character(signal)
             extra_info = " got \"%s\"" % character
 
-        self.log_symbol(call_distance, symbol, extra_info)
+        log_symbol(call_distance, symbol, extra_info)
 
         if symbol != "EOM":
             # Add spaces around the wordspace symbol to make it easier to split
 
         if symbol != "EOM":
             # Add spaces around the wordspace symbol to make it easier to split
@@ -342,7 +341,7 @@ class CallDistanceTransceiver(object):
     def receive_loop(self):
         while not self.end_of_message:
             self.modem.get_response("RING")
     def receive_loop(self):
         while not self.end_of_message:
             self.modem.get_response("RING")
-            self.receive_character()
+            self.receive_symbol()
             logging.debug("Current message: %s", self.morse_message)
 
         self.end_of_message = False
             logging.debug("Current message: %s", self.morse_message)
 
         self.end_of_message = False
@@ -352,7 +351,7 @@ class CallDistanceTransceiver(object):
     def get_text(self):
         return self.text_message
 
     def get_text(self):
         return self.text_message
 
-    def transmit_symbol(self, symbol, sleep_time):
+    def transmit_symbol(self, destination_number, symbol, sleep_time):
         logging.info("Dial and wait %.2f = %.2f + %.2f seconds "
                      "(transmitting '%s')",
                      self.call_setup_time_max + sleep_time,
         logging.info("Dial and wait %.2f = %.2f + %.2f seconds "
                      "(transmitting '%s')",
                      self.call_setup_time_max + sleep_time,
@@ -363,15 +362,13 @@ class CallDistanceTransceiver(object):
         # Dial, then wait self.call_setup_time_max to make sure the receiver
         # gets at least one RING, and then hangup and sleep the time needed to
         # transmit a symbol.
         # Dial, then wait self.call_setup_time_max to make sure the receiver
         # gets at least one RING, and then hangup and sleep the time needed to
         # transmit a symbol.
-        self.modem.send_command("ATDT" + self.destination_number + ";")
+        self.modem.send_command("ATDT" + destination_number + ";")
         time.sleep(self.call_setup_time_max)
         self.modem.send_command("ATH")
         self.modem.get_response()
         time.sleep(sleep_time)
 
     def transmit(self, message, destination_number):
         time.sleep(self.call_setup_time_max)
         self.modem.send_command("ATH")
         self.modem.get_response()
         time.sleep(sleep_time)
 
     def transmit(self, message, destination_number):
-        self.destination_number = destination_number
-
         morse_message = self.translator.text_to_morse(message)
         distances = self.modulator.modulate(morse_message)
 
         morse_message = self.translator.text_to_morse(message)
         distances = self.modulator.modulate(morse_message)
 
@@ -384,7 +381,7 @@ class CallDistanceTransceiver(object):
                 total_sleep_time = self.call_setup_time_max + distance
                 symbol = self.modulator.distance_to_symbol(total_sleep_time)
 
                 total_sleep_time = self.call_setup_time_max + distance
                 symbol = self.modulator.distance_to_symbol(total_sleep_time)
 
-            self.transmit_symbol(symbol, distance)
+            self.transmit_symbol(destination_number, symbol, distance)
 
     def estimate_transmit_duration(self, message):
         morsemessage = self.translator.text_to_morse(message)
 
     def estimate_transmit_duration(self, message):
         morsemessage = self.translator.text_to_morse(message)
@@ -401,7 +398,7 @@ class CallDistanceTransceiver(object):
                       transmitting_time)
 
 
                       transmitting_time)
 
 
-def test_send_receive():
+def test_transmit_receive():
     logging.basicConfig(level=logging.DEBUG)
     call_setup_time_min = 0
     call_setup_time_max = 0.01
     logging.basicConfig(level=logging.DEBUG)
     call_setup_time_min = 0
     call_setup_time_max = 0.01
@@ -457,13 +454,15 @@ def test_send_receive():
 
     modem.transceiver = xcv
 
 
     modem.transceiver = xcv
 
+    xcv.transmit("CODEX PARIS", "0")
+
     while True:
     while True:
-        xcv.receive_loop()
         modem.ring_count = 0
         modem.ring_count = 0
+        xcv.receive_loop()
         print()
         print("Message received!")
         print("\"%s\"" % xcv.get_text(), flush=True)
 
 
 if __name__ == "__main__":
         print()
         print("Message received!")
         print("\"%s\"" % xcv.get_text(), flush=True)
 
 
 if __name__ == "__main__":
-    test_send_receive()
+    test_transmit_receive()