dbus_client: handle multiple transition signals
authorHelmut Grohne <helmut@subdivi.de>
Sun, 14 Jul 2013 15:09:04 +0000 (17:09 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Sun, 14 Jul 2013 15:09:04 +0000 (17:09 +0200)
There bay be multiple changestate signals with ST_ACTIVE|ST_TRANSITION
before a signal for ST_ACTIVE.

dbus_client.py

index a675eb7..91d9f37 100755 (executable)
@@ -16,15 +16,13 @@ from gi.repository import GObject
 from onoff.common import ST_ACTIVE
 import onoff.dbusutils
 
-def wait_for_signal(proxy, signal):
+def wait_for_signal(proxy, signal, desired_state):
     loop = GObject.MainLoop()
-    state = []
     def callback(st):
-        state.append(st)
-        loop.quit()
+        if st == desired_state:
+            loop.quit()
     proxy.connect_to_signal(signal, callback)
     loop.run()
-    return state[0]
 
 def parse_introspection(xmlstring):
     parser = xml.parsers.expat.ParserCreate()
@@ -66,16 +64,16 @@ def main():
         os.close(fd)
         if st != ST_ACTIVE:
             print("state is %d waiting for signal" % st)
-            st = wait_for_signal(proxy, "changestate")
-            print("new state is %d" % st)
+            wait_for_signal(proxy, "changestate", ST_ACTIVE)
+            print("new state is actived")
         os.execvp(args.command[0], args.command)
     else:
         proxy = onoff.dbusutils.get_dbus_proxy(args)
         st = proxy.activatetime(args.duration)
         if st != ST_ACTIVE:
             print("state is %d waiting for signal" % st)
-            st = wait_for_signal(proxy, "changestate")
-            print("new state is %d" % st)
+            wait_for_signal(proxy, "changestate", ST_ACTIVE)
+            print("new state is active")
 
 if __name__ == "__main__":
     main()