-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdown_detector.py
66 lines (48 loc) · 1.8 KB
/
down_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
from config import Config
from network import Network
from notify import Notify
from log import Log
URL = "www.google.com"
class DownDetector:
CONFIG_FILE = "down_detector.yaml"
def __init__( self ):
self.config: Config = Config( self.CONFIG_FILE )
self.notify: Notify = Notify( self.config )
self.log: Log = Log( self.config )
self.network: Network = Network( self.config, self.notify, self.log )
self.current_connection_state = None
self.current_latency_state = None
self.timeout = self.config.DOWN_TIMEOUT
self.log.started()
def detect( self, url: str ) -> None:
state = self.network.is_connected( url )
if state != self.current_connection_state:
if state:
self.notify.active()
self.log.active()
self.timeout = self.config.ACTIVE_TIMEOUT
else:
self.notify.down()
self.log.down()
self.timeout = self.config.DOWN_TIMEOUT
self.current_connection_state = state
state = self.network.is_latency_high( url, self.current_latency_state )
if self.current_connection_state and state != self.current_latency_state:
if state:
# Only notify of slow connections if the connection is active
if self.current_connection_state:
self.notify.speed_slow()
self.log.speed_slow()
else:
self.notify.speed_normal()
self.log.speed_normal()
self.current_latency_state = state
time.sleep( self.timeout )
self.config.refresh()
def main():
dd = DownDetector()
while True:
dd.detect(URL)
if __name__ == '__main__':
main()