[modules/publicip] add default route monitor

(re)add a separate thread that monitors the default route and updates
the module immediately, if the default route changes.

fixes #909
This commit is contained in:
tobi-wan-kenobi 2022-08-05 14:30:50 +02:00
parent 062506f467
commit a7dba79664

View file

@ -53,6 +53,18 @@ class Module(core.module.Module):
# By default show: <ip> (<2 letter country code>) # By default show: <ip> (<2 letter country code>)
self._format = self.parameter("format", "{ip} ({country_code})") self._format = self.parameter("format", "{ip} ({country_code})")
self.__monitor = threading.Thread(target=self.monitor, args=())
self.__monitor.start()
def monitor(self):
current_default_route = None
default_route = None
while threading.main_thread().is_alive():
current_default_route = netifaces.gateways()["default"][2]
if current_default_route != default_route:
self.update()
time.sleep(1)
def publicip(self, widget): def publicip(self, widget):
if widget.get("public_ip") == None: if widget.get("public_ip") == None:
return "n/a" return "n/a"
@ -70,17 +82,8 @@ class Module(core.module.Module):
def update(self): def update(self):
widget = self.widget() widget = self.widget()
self.__current_default_route = netifaces.gateways()["default"][2]
try: try:
# Updates public ip information if a change to default route is detected
if self.__current_default_route != self.__previous_default_route:
# Sets __previous_default_route in preparation for next change check
self.__previous_default_route = self.__current_default_route
# Refresh location information
util.location.reset() util.location.reset()
# Fetch fresh location information # Fetch fresh location information
__info = util.location.location_info() __info = util.location.location_info()