-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
569bb31
commit ff2f05a
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import dbus | ||
from dbus.mainloop.glib import DBusGMainLoop | ||
import gi.repository.GLib as GLib | ||
import time | ||
import subprocess | ||
|
||
dbus_loop = DBusGMainLoop(set_as_default=True) | ||
bus = dbus.SystemBus() | ||
obj = None | ||
iface = None | ||
|
||
def properties_changed_handler(interface_name, changed_properties, invalidated_properties): | ||
if "Dhcp6PdState" in changed_properties: | ||
new_state = changed_properties["Dhcp6PdState"] | ||
print(f"Dhcp6PdState changed to: {new_state}") | ||
if new_state == "running": # Check for string equality | ||
try: | ||
subprocess.run(["sudo", "service", "dhcpcd", "restart"], check=True) | ||
print("Successfully restarted dhcpcd service.") | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error restarting dhcpcd service: {e}") | ||
elif new_state == "stopped": | ||
try: | ||
subprocess.run(["sudo", "cp", "/etc/dhcpcd.conf.orig", "/etc/dhcpcd.conf"], check=True) | ||
print("Successfully restarted dhcpcd service.") | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error restarting dhcpcd service: {e}") | ||
|
||
def connect_to_signal(): | ||
global obj, iface | ||
try: | ||
obj = bus.get_object('io.openthread.BorderRouter.wpan0', '/io/openthread/BorderRouter/wpan0') | ||
iface = dbus.Interface(obj, 'org.freedesktop.DBus.Properties') | ||
obj.connect_to_signal("PropertiesChanged", properties_changed_handler, dbus_interface=iface.dbus_interface) | ||
print("Connected to D-Bus signal.") | ||
except dbus.DBusException as ex: | ||
print(f"Error connecting to D-Bus: {ex}") | ||
obj = None | ||
iface = None | ||
|
||
|
||
def check_and_reconnect(): | ||
if obj is None: | ||
connect_to_signal() | ||
|
||
def main(): | ||
connect_to_signal() | ||
|
||
loop = GLib.MainLoop() | ||
|
||
GLib.timeout_add_seconds(5, check_and_reconnect) # Check every 5 seconds | ||
|
||
loop.run() | ||
|
||
if __name__ == '__main__': | ||
main() |