Delay before querying default route
This commit is contained in:
parent
9d6be61051
commit
ce2cfd961e
|
@ -10,44 +10,47 @@ PING_INTERVAL_IN_SECONDS = 80
|
||||||
class WifiKeepalive:
|
class WifiKeepalive:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.defaultRoute = self.getDefaultWifiRoute()
|
self.defaultRoute = None
|
||||||
self.initialDelay = INITIAL_DELAY_IN_SECONDS
|
self.initialDelay = INITIAL_DELAY_IN_SECONDS
|
||||||
self.pingInterval = PING_INTERVAL_IN_SECONDS
|
self.pingInterval = PING_INTERVAL_IN_SECONDS
|
||||||
self.isConnected = True
|
self.isConnected = False
|
||||||
self.pingCommand = 'ping -c 1 -W 2 -n'
|
self.pingCommand = 'ping -c 1 -W 2 -n'
|
||||||
|
self.connectedMessage = 'Connected to Wifi'
|
||||||
|
self.disconnectedMessage = 'No Wifi Connection'
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
sleep(self.initialDelay)
|
self.prepare()
|
||||||
|
|
||||||
while self.isRunning():
|
while True:
|
||||||
self.keepalive()
|
self.keepalive()
|
||||||
sleep(self.pingInterval)
|
sleep(self.pingInterval)
|
||||||
|
|
||||||
def isRunning(self):
|
def prepare(self):
|
||||||
return True
|
sleep(self.initialDelay)
|
||||||
|
self.updateDefaultWifiRoute()
|
||||||
|
|
||||||
|
if self.defaultRoute:
|
||||||
|
self.connect(preparing=True)
|
||||||
|
else:
|
||||||
|
self.disconnect(preparing=True)
|
||||||
|
|
||||||
def keepalive(self):
|
def keepalive(self):
|
||||||
if self.defaultRoute:
|
if self.defaultRoute:
|
||||||
self.pingDefaultRoute()
|
self.connect()
|
||||||
|
self.ping() or self.updateDefaultWifiRoute()
|
||||||
else:
|
else:
|
||||||
self.attemptWifiConnection()
|
self.disconnect()
|
||||||
|
self.updateDefaultWifiRoute()
|
||||||
|
|
||||||
def pingDefaultRoute(self):
|
def connect(self, preparing=False):
|
||||||
if not self.isConnected:
|
if not self.isConnected or preparing:
|
||||||
print('Connected to Wifi', flush=True)
|
print(self.connectedMessage, flush=True)
|
||||||
|
self.isConnected = True
|
||||||
|
|
||||||
self.isConnected = True
|
def disconnect(self, preparing=False):
|
||||||
isPingSuccess = self.ping()
|
if self.isConnected or preparing:
|
||||||
|
print(self.disconnectedMessage, flush=True)
|
||||||
if not isPingSuccess:
|
self.isConnected = False
|
||||||
self.defaultRoute = self.getDefaultWifiRoute()
|
|
||||||
|
|
||||||
def attemptWifiConnection(self):
|
|
||||||
if self.isConnected:
|
|
||||||
print('No Wifi Connection', flush=True)
|
|
||||||
|
|
||||||
self.isConnected = False
|
|
||||||
self.defaultRoute = self.getDefaultWifiRoute()
|
|
||||||
|
|
||||||
def ping(self):
|
def ping(self):
|
||||||
try:
|
try:
|
||||||
|
@ -57,12 +60,12 @@ class WifiKeepalive:
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def getDefaultWifiRoute(self):
|
def updateDefaultWifiRoute(self):
|
||||||
defaults = [
|
defaults = [
|
||||||
r['gateway'] for r in self.getRoutes() if self.isDefaultWifiRoute(r)
|
r['gateway'] for r in self.getRoutes() if self.isDefaultWifiRoute(r)
|
||||||
]
|
]
|
||||||
|
|
||||||
return defaults[0] if defaults else None
|
self.defaultRoute = defaults[0] if defaults else None
|
||||||
|
|
||||||
def getRoutes(self):
|
def getRoutes(self):
|
||||||
outputLines = check_output(
|
outputLines = check_output(
|
||||||
|
|
Loading…
Reference in New Issue