add a sispmctl_device
[~helmut/onoff.git] / dbus_service.py
index 7bb0b37..94ff9b5 100755 (executable)
@@ -1,73 +1,30 @@
 #!/usr/bin/env python
 #!/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 os
+import logging
 
 import dbus
 import dbus.service
 from dbus.mainloop.glib import DBusGMainLoop
 from gi.repository import GObject
 
 
 import dbus
 import dbus.service
 from dbus.mainloop.glib import DBusGMainLoop
 from gi.repository import GObject
 
-from onoff.common import ST_ACTIVE, ST_TRANSITION
-
-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):
-        print("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):
-        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
-
-    @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, _):
-            print("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)
-        self.changestate(ST_ACTIVE)
-        return self.curstate
-
-    def unuse(self):
-        self.usecount -= 1
-        if not self.usecount:
-            self.changestate(0)
-        else:
-            print("%d users left" % self.usecount)
-        return False
-
+import onoff.common
+import onoff.dbusutils
+import onoff.process
 
 def main():
 
 def main():
+    logging.basicConfig()
+    logging.getLogger().setLevel(logging.DEBUG)
     DBusGMainLoop(set_as_default=True)
     bus = dbus.SessionBus()
     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, 1, 5)
+    onoff.dbusutils.OnoffControl(bus, "redshift", dev)
     GObject.MainLoop().run()
 
 if __name__ == "__main__":
     GObject.MainLoop().run()
 
 if __name__ == "__main__":