CallDistanceTransceiver.py: consider possible delays in transmit_symbol()
authorAntonio Ospite <ao2@ao2.it>
Sun, 3 Jan 2016 22:16:44 +0000 (23:16 +0100)
committerAntonio Ospite <ao2@ao2.it>
Mon, 4 Jan 2016 10:13:30 +0000 (11:13 +0100)
The ATH command can take some time, resulting in a positive delay; or
the first time.sleep() may terminate earlier resulting in a negative
delay.

In either case compensate for these delays in the second sleep, in order
to try to diverge as little possible from the total nominal symbol
transmission distance.

src/savemysugar/CallDistanceTransceiver.py

index 9c53ba2..15fcdae 100755 (executable)
@@ -232,11 +232,25 @@ 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.
+        time_before = time.time()
         self.modem.send_command("ATDT" + destination_number + ";")
         time.sleep(self.call_setup_time_max)
         self.modem.send_command("ATH")
         self.modem.get_response()
         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)
+        time_after = time.time()
+
+        # Account for possible delays in order to be as adherent as
+        # possible to the nominal total symbol transmission distance.
+        delay = (time_after - time_before) - self.call_setup_time_max
+        logging.debug("Delay %.2f", delay)
+
+        remaining_sleep_time = sleep_time - delay
+        if remaining_sleep_time < 0:
+            remaining_sleep_time = 0
+
+        logging.debug("Should sleep %.2f. Will sleep %.2f", sleep_time,
+                      remaining_sleep_time)
+        time.sleep(remaining_sleep_time)
 
     def transmit(self, message, destination_number):
         morse_message = self.translator.text_to_morse(message)
 
     def transmit(self, message, destination_number):
         morse_message = self.translator.text_to_morse(message)