-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 state(self, st):
- print("emitting state %d" % st)
- self.curstate = st
-
- @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.state(ST_TRANSITION)
- def finish():
- self.state(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.state(ST_ACTIVE)
- return self.curstate
-
- def unuse(self):
- self.usecount -= 1
- if not self.usecount:
- self.state(0)
- else:
- print("%d users left" % self.usecount)
- return False