#!/usr/bin/env python
"""
-A simpe client for an onoff device. If no parameters are given, the device
-is activated for 10 seconds. When a command is given, the device is activated,
-then the command is run. 10 seconds after the command finishes, the device is
+A simpe client for an onoff device. If no command is given, the device is
+temporarily activated. When a command is given, the device is activated, then
+the command is run. Some time after the command finishes, the device is
released.
"""
+import argparse
import os
-import sys
-import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GObject
from onoff.common import ST_ACTIVE
+import onoff.dbusutils
def wait_for_signal(proxy, signal):
loop = GObject.MainLoop()
return state[0]
def main():
+ parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
+ parser.add_argument("--duration", type=int, default=10,
+ help="how long to activate the device in seconds " +
+ "(default: %(default)d")
+ parser.add_argument("command", nargs=argparse.REMAINDER,
+ help="a command to be executed with the device being" +
+ "activated for the duration of the execution")
+ args = parser.parse_args()
DBusGMainLoop(set_as_default=True)
- bus = dbus.SessionBus()
- proxy = bus.get_object("de.subdivi.onoff0", "/de/subdivi/onoff0/redshift")
- if len(sys.argv) > 1:
- st, fd = proxy.activatefd(10)
+ proxy = onoff.dbusutils.get_dbus_proxy(args)
+ if args.command:
+ st, fd = proxy.activatefd()
fd = fd.take()
os.dup2(fd, 254)
os.close(fd)
print("state is %d waiting for signal" % st)
st = wait_for_signal(proxy, "changestate")
print("new state is %d" % st)
- os.execvp(sys.argv[1], sys.argv[1:])
+ os.execvp(args.command[0], args.command)
else:
- st = proxy.activatetime(10)
+ st = proxy.activatetime(args.duration)
if st != ST_ACTIVE:
print("state is %d waiting for signal" % st)
st = wait_for_signal(proxy, "changestate")