[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:
parent
062506f467
commit
a7dba79664
1 changed files with 27 additions and 24 deletions
|
@ -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,34 +82,25 @@ 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
|
util.location.reset()
|
||||||
if self.__current_default_route != self.__previous_default_route:
|
# Fetch fresh location information
|
||||||
# Sets __previous_default_route in preparation for next change check
|
__info = util.location.location_info()
|
||||||
self.__previous_default_route = self.__current_default_route
|
|
||||||
|
|
||||||
# Refresh location information
|
# Contstruct coordinates string
|
||||||
util.location.reset()
|
__lat = "{:.2f}".format(__info["latitude"])
|
||||||
|
__lon = "{:.2f}".format(__info["longitude"])
|
||||||
|
__coords = __lat + "°N" + "," + " " + __lon + "°E"
|
||||||
|
|
||||||
# Fetch fresh location information
|
# Set widget values
|
||||||
__info = util.location.location_info()
|
widget.set("public_ip", __info["public_ip"])
|
||||||
|
widget.set("country_name", __info["country"])
|
||||||
|
widget.set("country_code", __info["country_code"])
|
||||||
|
widget.set("city_name", __info["city_name"])
|
||||||
|
widget.set("coordinates", __coords)
|
||||||
|
|
||||||
# Contstruct coordinates string
|
# Update widget values
|
||||||
__lat = "{:.2f}".format(__info["latitude"])
|
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
||||||
__lon = "{:.2f}".format(__info["longitude"])
|
|
||||||
__coords = __lat + "°N" + "," + " " + __lon + "°E"
|
|
||||||
|
|
||||||
# Set widget values
|
|
||||||
widget.set("public_ip", __info["public_ip"])
|
|
||||||
widget.set("country_name", __info["country"])
|
|
||||||
widget.set("country_code", __info["country_code"])
|
|
||||||
widget.set("city_name", __info["city_name"])
|
|
||||||
widget.set("coordinates", __coords)
|
|
||||||
|
|
||||||
# Update widget values
|
|
||||||
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
|
||||||
except:
|
except:
|
||||||
widget.set("public_ip", None)
|
widget.set("public_ip", None)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue