implement a redshift device
[~helmut/onoff.git] / dbus_service.py
index 41a6546..bbc652c 100755 (executable)
@@ -8,7 +8,8 @@ import dbus.service
 from dbus.mainloop.glib import DBusGMainLoop
 from gi.repository import GObject
 
-from onoff.common import ST_ACTIVE, ST_TRANSITION
+import onoff.common
+import onoff.process
 
 logger = logging.getLogger("dbus_service")
 
@@ -16,20 +17,20 @@ class OnoffControl(dbus.service.Object):
     domain = "de.subdivi.onoff"
     path = "/de/subdivi/onoff"
 
-    def __init__(self, bus, num):
+    def __init__(self, bus, name, device):
         busname = dbus.service.BusName(self.domain, bus=bus)
-        dbus.service.Object.__init__(self, busname, "%s/%d" % (self.path, num))
+        dbus.service.Object.__init__(self, busname, "%s/%s" % (self.path, name))
+        self.device = device
+        device.notify.add(self.changestate)
         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
+        return self.device.state
 
     @dbus.service.method(domain, in_signature="q", out_signature="q")
     def activatetime(self, duration):
@@ -52,16 +53,13 @@ class OnoffControl(dbus.service.Object):
     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
+            self.device.activate()
+        return self.device.state
 
     def unuse(self):
         self.usecount -= 1
         if not self.usecount:
-            self.changestate(0)
+            self.device.deactivate()
         else:
             logger.debug("%d users left", self.usecount)
         return False
@@ -72,7 +70,9 @@ def main():
     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)
+    OnoffControl(bus, "redshift", dev)
     GObject.MainLoop().run()
 
 if __name__ == "__main__":