service: more generic activation
authorHelmut Grohne <helmut@subdivi.de>
Sun, 16 Jun 2013 21:57:41 +0000 (23:57 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Sun, 16 Jun 2013 21:57:41 +0000 (23:57 +0200)
dbus_service.py

index 7bb0b37..f77da7e 100755 (executable)
@@ -31,19 +31,11 @@ class OnoffControl(dbus.service.Object):
     @dbus.service.method(domain, in_signature="q", out_signature="q")
     def activatetime(self, duration):
         print("called with duration %d" % duration)
-        self.usecount += 1
         GObject.timeout_add(duration * 1000, self.unuse)
-        if self.usecount > 1:
-            return self.curstate
-        self.changestate(ST_ACTIVE|ST_TRANSITION)
-        def finish():
-            self.changestate(ST_ACTIVE)
-        GObject.timeout_add(200, finish)
-        return self.curstate
+        return self.use()
 
     @dbus.service.method(domain, in_signature="hq", out_signature="q")
     def activatefd(self, fd, duration):
-        self.usecount += 1
         fd = fd.take()
         print("called with fd %d and duration %d" % (fd, duration))
         def callback(fd, _):
@@ -52,7 +44,15 @@ class OnoffControl(dbus.service.Object):
             GObject.timeout_add(duration * 1000, self.unuse)
             return False
         GObject.io_add_watch(fd, GObject.IO_HUP|GObject.IO_ERR, callback)
-        self.changestate(ST_ACTIVE)
+        return self.use()
+
+    def use(self):
+        self.usecount += 1
+        if self.usecount <= 1:
+            self.changestate(ST_ACTIVE|ST_TRANSITION)
+            def finish():
+                self.changestate(ST_ACTIVE)
+            GObject.timeout_add(200, finish)
         return self.curstate
 
     def unuse(self):