move OnoffControl to onoff.dbusutils
authorHelmut Grohne <helmut@subdivi.de>
Fri, 28 Jun 2013 09:35:58 +0000 (11:35 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Fri, 28 Jun 2013 09:35:58 +0000 (11:35 +0200)
What's left in dbus_service.py now is mainly the configuration part.
Maybe configuring onoff should happen by writing a python script for
now?

dbus_service.py
onoff/dbusutils.py

index 3c0c267..6fb6daf 100755 (executable)
@@ -16,60 +16,6 @@ import onoff.common
 import onoff.dbusutils
 import onoff.process
 
-logger = logging.getLogger("dbus_service")
-
-class OnoffControl(dbus.service.Object):
-    domain = "de.subdivi.onoff0"
-    path = onoff.dbusutils.object_prefix
-
-    def __init__(self, bus, name, device):
-        busname = dbus.service.BusName(self.domain, bus=bus)
-        dbus.service.Object.__init__(self, busname, "%s/%s" % (self.path, name))
-        self.device = device
-        device.notify.add(self.changestate)
-        self.usecount = 0
-
-    @dbus.service.signal(domain, signature="q")
-    def changestate(self, st):
-        logger.debug("emitting state %d", st)
-
-    @dbus.service.method(domain, out_signature="q")
-    def state(self):
-        return self.device.state
-
-    @dbus.service.method(domain, in_signature="q", out_signature="q")
-    def activatetime(self, duration):
-        logger.info("activatetime %d", duration)
-        GObject.timeout_add(duration * 1000, self.unuse)
-        return self.use()
-
-    @dbus.service.method(domain, in_signature="", out_signature="qh")
-    def activatefd(self):
-        logger.info("activatefd")
-        notifyfd, retfd = onoff.dbusutils.socketpair()
-        def callback(fd, _):
-            logger.info("fd %d completed", fd.fileno())
-            fd.close()
-            self.unuse()
-            return False
-        GObject.io_add_watch(notifyfd, GObject.IO_HUP|GObject.IO_ERR, callback)
-        return (self.use(), retfd)
-
-    def use(self):
-        self.usecount += 1
-        if self.usecount <= 1:
-            self.device.activate()
-        return self.device.state
-
-    def unuse(self):
-        self.usecount -= 1
-        if not self.usecount:
-            self.device.deactivate()
-        else:
-            logger.debug("%d users left", self.usecount)
-        return False
-
-
 def main():
     logging.basicConfig()
     logging.getLogger().setLevel(logging.DEBUG)
@@ -78,7 +24,7 @@ def main():
     dev = onoff.process.OnoffProcess(["redshift"], 3)
     dev = onoff.common.InvertedDevice(dev)
     dev = onoff.common.ThrottledDevice(dev, 5)
-    OnoffControl(bus, "redshift", dev)
+    onoff.dbusutils.OnoffControl(bus, "redshift", dev)
     GObject.MainLoop().run()
 
 if __name__ == "__main__":
index 27ee35d..6d1d837 100644 (file)
@@ -1,7 +1,12 @@
 import argparse
+import logging
 import socket
 
 import dbus
+import dbus.service
+from gi.repository import GObject
+
+logger = logging.getLogger("onoff.dbusutils")
 
 object_prefix = "/de/subdivi/onoff0"
 
@@ -39,3 +44,61 @@ def socketpair():
     s3 = dbus.types.UnixFd(s2)
     s2.close()
     return s1, s3
+
+class OnoffControl(dbus.service.Object):
+    domain = "de.subdivi.onoff0"
+    path = object_prefix
+
+    def __init__(self, bus, name, device):
+        """
+        @type bus: dbus.Bus
+        @type name: str
+        @type device: OnoffDevice
+        """
+        busname = dbus.service.BusName(self.domain, bus=bus)
+        dbus.service.Object.__init__(self, busname, "%s/%s" % (self.path, name))
+        self.device = device
+        self.usecount = 0
+        device.notify.add(self.changestate)
+
+    @dbus.service.signal(domain, signature="q")
+    def changestate(self, state):
+        logger.debug("emitting state %d", state)
+
+    @dbus.service.method(domain, out_signature="q")
+    def state(self):
+        return self.device.state
+
+    @dbus.service.method(domain, in_signature="q", out_signature="q")
+    def activatetime(self, duration):
+        """Activate the device for a given number of seconds."""
+        logger.info("activatetime %d", duration)
+        GObject.timeout_add(duration * 1000, self.unuse)
+        return self.use()
+
+    @dbus.service.method(domain, in_signature="", out_signature="qh")
+    def activatefd(self):
+        """Activate a device until the returned file descriptor is closed."""
+        logger.info("activatefd")
+        notifyfd, retfd = socketpair()
+        def callback(fd, _):
+            logger.info("fd %d completed", fd.fileno())
+            fd.close()
+            self.unuse()
+            return False
+        GObject.io_add_watch(notifyfd, GObject.IO_HUP|GObject.IO_ERR, callback)
+        return (self.use(), retfd)
+
+    def use(self):
+        self.usecount += 1
+        if self.usecount <= 1:
+            self.device.activate()
+        return self.device.state
+
+    def unuse(self):
+        self.usecount -= 1
+        if not self.usecount:
+            self.device.deactivate()
+        else:
+            logger.debug("%d users left", self.usecount)
+        return False