#!/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 gi.repository import GObject
import onoff.common
+import onoff.dbusutils
import onoff.process
-logger = logging.getLogger("dbus_service")
-
-class OnoffControl(dbus.service.Object):
- domain = "de.subdivi.onoff0"
- path = "/de/subdivi/onoff0"
-
- 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="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.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)
bus = dbus.SessionBus()
dev = onoff.process.OnoffProcess(["redshift"], 3)
dev = onoff.common.InvertedDevice(dev)
- OnoffControl(bus, "redshift", dev)
+ dev = onoff.common.ThrottledDevice(dev, 1, 5)
+ onoff.dbusutils.OnoffControl(bus, "redshift", dev)
GObject.MainLoop().run()
if __name__ == "__main__":