62681453798d6aa037367e626f9a17e0cb4dd45d
[SaveMySugar/python3-savemysugar.git] / src / measure_call_setup_time.py
1 #!/usr/bin/env python3
2 #
3 # measure_call_setup_time - measure time between dialing out and ringing in
4 #
5 # Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from savemysugar.cumulative_average import cumulative_average
21 from savemysugar.Modem import Modem
22 import time
23
24
25 class Ring(object):
26     time = -1
27
28
29 def on_ring():
30     if Ring.time == -1:
31         print("First ring")
32         Ring.time = time.time()
33         raise StopIteration("We just want the first ring")
34
35
36 def measure_call_setup_time(ingoing_port, outgoing_port, destination_number):
37     ingoing_modem = Modem(ingoing_port)
38     ingoing_modem.register_callback("RING", on_ring)
39
40     outgoing_modem = Modem(outgoing_port)
41
42     min_call_time = 10000
43     max_call_time = -1
44     min_call_setup_time = 10000
45     max_call_setup_time = -1
46
47     avg_call_setup_time = 0
48
49     for i in range(5):
50         print()
51         print("Attempt: %s" % (i + 1))
52         outgoing_modem.send_command("ATDT" + destination_number)
53         dial_time = time.time()
54
55         # Wait for the receiver to get a ring before terminating the call
56         try:
57             ingoing_modem.get_response_loop()
58         except (StopIteration, KeyboardInterrupt):
59             outgoing_modem.send_command("ATH")
60             outgoing_modem.get_response()
61
62         hangup_time = time.time()
63
64         # When dialing with an analog modem I noticed that if calls are
65         # separated one from another they take less time to set up, this
66         # may be due to how an analog modem works: getting it on-hook and
67         # off-hook takes quite some time.
68         inter_call_sleep = 5.2
69         print("After hangup sleeping %.2f sec..." % inter_call_sleep)
70         time.sleep(inter_call_sleep)
71
72         call_setup_time = Ring.time - dial_time
73         min_call_setup_time = min(min_call_setup_time, call_setup_time)
74         max_call_setup_time = max(max_call_setup_time, call_setup_time)
75         avg_call_setup_time = cumulative_average(avg_call_setup_time,
76                                                  i + 1,
77                                                  call_setup_time)
78         uncertainty = (max_call_setup_time - min_call_setup_time) / 2.
79         print("Call setup time: %f" % call_setup_time)
80         print("Min call setup time: %f" % min_call_setup_time)
81         print("Max call setup time: %f" % max_call_setup_time)
82         print("Average call setup time: %f" % avg_call_setup_time)
83         print("Call setup time uncertainty: %f" % uncertainty)
84         print()
85
86         call_time = hangup_time - dial_time
87         min_call_time = min(min_call_time, call_time)
88         max_call_time = max(max_call_time, call_time)
89         print("Total call time: %f" % call_time)
90         print("Min call time: %f" % min_call_time)
91         print("Max call time: %f" % max_call_time)
92         print()
93         Ring.time = -1
94
95
96 def main():
97     import sys
98     if len(sys.argv) != 4:
99         print("usage: %s" % sys.argv[0],
100               "<ingoing serial port> <outgoing serial port>",
101               "<destination number>")
102         sys.exit(1)
103
104     measure_call_setup_time(sys.argv[1], sys.argv[2], sys.argv[3])
105
106
107 if __name__ == "__main__":
108     main()