From 71065672a7ab2c11068623733db075fc00efbc5a Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Sun, 23 Sep 2018 18:44:58 +0200 Subject: [PATCH] some improvements --- boswatch/network/netCheck.py | 9 +++++++++ boswatch/utils/timer.py | 2 ++ bw_server.py | 5 ++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/boswatch/network/netCheck.py b/boswatch/network/netCheck.py index 2c2e991..c71d0ba 100644 --- a/boswatch/network/netCheck.py +++ b/boswatch/network/netCheck.py @@ -30,6 +30,8 @@ class NetCheck: @param timout: timout for connection check in sec. (1)""" self._hostname = hostname self._timeout = timeout + self._connectionState = False + self.checkConn() # initiate a first check def checkConn(self): """!Check the connection @@ -38,7 +40,14 @@ class NetCheck: try: urlopen(self._hostname, timeout=self._timeout) logging.debug("%s is reachable", self._hostname) + self._connectionState = True return True except: logging.warning("%s is not reachable", self._hostname) + self._connectionState = False return False + + @property + def connectionState(self): + """!Property for the last connection state from checkConn()""" + return self._connectionState diff --git a/boswatch/utils/timer.py b/boswatch/utils/timer.py index cb89dda..c9521f3 100644 --- a/boswatch/utils/timer.py +++ b/boswatch/utils/timer.py @@ -87,6 +87,8 @@ class RepeatedTimer: else: logging.warning("timer overdue! interval: %0.3f sec. - runtime: %0.3f sec.", self._interval, runTime) self._overdueCount += 1 + logging.debug("repeatedTimer thread stopped: %s", self._thread.name) + @property def restTime(self): """!Property to get remaining time till next call""" diff --git a/bw_server.py b/bw_server.py index 4814dcf..414d78b 100644 --- a/bw_server.py +++ b/bw_server.py @@ -61,8 +61,11 @@ except Exception as e: # pragma: no cover from boswatch.utils.timer import RepeatedTimer from boswatch.network.netCheck import NetCheck net = NetCheck() -test = RepeatedTimer(10, net.checkConn) +test = RepeatedTimer(3, net.checkConn) test.start() +time.sleep(10) +print(net.connectionState) +test.stop() try: header.logoToLog()