provide a default for --busname
[~helmut/onoff.git] / dbus_service.py
index 41a6546..6fb6daf 100755 (executable)
@@ -1,78 +1,30 @@
 #!/usr/bin/env python
+"""
+A dbus service example currently providing a device called redshift, that runs
+redshift whenever it is not activated. This could be useful to temporarily
+disable redshift e.g. while watching a movie.
+"""
 
 import logging
-import os
 
 import dbus
 import dbus.service
 from dbus.mainloop.glib import DBusGMainLoop
 from gi.repository import GObject
 
-from onoff.common import ST_ACTIVE, ST_TRANSITION
-
-logger = logging.getLogger("dbus_service")
-
-class OnoffControl(dbus.service.Object):
-    domain = "de.subdivi.onoff"
-    path = "/de/subdivi/onoff"
-
-    def __init__(self, bus, num):
-        busname = dbus.service.BusName(self.domain, bus=bus)
-        dbus.service.Object.__init__(self, busname, "%s/%d" % (self.path, num))
-        self.usecount = 0
-        self.curstate = 0 # empty bitmask
-
-    @dbus.service.signal(domain, signature="q")
-    def changestate(self, st):
-        logger.debug("emitting state %d", st)
-        self.curstate = st
-
-    @dbus.service.method(domain, out_signature="q")
-    def state(self):
-        return self.curstate
-
-    @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="hq", out_signature="q")
-    def activatefd(self, fd, duration):
-        fd = fd.take()
-        logger.info("activatefd fd %d, duration %d", (fd, duration))
-        def callback(fd, _):
-            logger.info("fd %d completed", fd)
-            os.close(fd)
-            GObject.timeout_add(duration * 1000, self.unuse)
-            return False
-        GObject.io_add_watch(fd, GObject.IO_HUP|GObject.IO_ERR, callback)
-        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):
-        self.usecount -= 1
-        if not self.usecount:
-            self.changestate(0)
-        else:
-            logger.debug("%d users left", self.usecount)
-        return False
-
+import onoff.common
+import onoff.dbusutils
+import onoff.process
 
 def main():
     logging.basicConfig()
     logging.getLogger().setLevel(logging.DEBUG)
     DBusGMainLoop(set_as_default=True)
     bus = dbus.SessionBus()
-    OnoffControl(bus, 5)
+    dev = onoff.process.OnoffProcess(["redshift"], 3)
+    dev = onoff.common.InvertedDevice(dev)
+    dev = onoff.common.ThrottledDevice(dev, 5)
+    onoff.dbusutils.OnoffControl(bus, "redshift", dev)
     GObject.MainLoop().run()
 
 if __name__ == "__main__":