1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
import optparse
import os
import sys
import dbus
import dbus.service
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
BUS_NAME = 'org.bluez'
ALERT_INTERFACE = 'org.bluez.Alert1'
ALERT_AGENT_INTERFACE = 'org.bluez.AlertAgent1'
BLUEZ_OBJECT_PATH = '/org/bluez'
TEST_OBJECT_PATH = '/org/bluez/test'
class AlertAgent(dbus.service.Object):
def __init__(self, bus, object_path, alert, mainloop):
dbus.service.Object.__init__(self, bus, object_path)
self.alert = alert
self.mainloop = mainloop
@dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='',
out_signature='')
def MuteOnce(self):
print('method MuteOnce() was called')
self.alert.NewAlert('ringer', 1, 'not active')
@dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='s',
out_signature='')
def SetRinger(self, mode):
print('method SetRinger(%s) was called' % mode)
self.alert.NewAlert('ringer', 1, mode)
@dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='',
out_signature='')
def Release(self):
print('method Release() was called')
self.mainloop.quit()
def print_command_line(options):
if not options.verbose:
return False
print('-w: ' + str(options.wait))
if options.times:
print('-t: ' + str(options.times))
if options.register:
print('-r: ' + options.register)
else:
print('-r: ' + str(None))
if options.new_alert:
print('-n:')
for i in options.new_alert:
print(' ' + i[0] + ', ' + i[1] + ', ' + i[2])
else:
print('-n: ' + str(None))
if options.unread_alert:
print('-u:')
for i in options.unread_alert:
print(' ' + i[0] + ', ' + i[1])
else:
print('-u: ' + str(None))
print()
return True
def read_count(param):
try:
return int(param)
except ValueError:
print('<count> must be integer, not \"%s\"' % param)
sys.exit(1)
def new_alert(alert, params):
if not params:
return False
for param in params:
category = param[0]
count = read_count(param[1])
description = param[2]
alert.NewAlert(category, count, description)
def unread_alert(alert, params):
if not params:
return False
for param in params:
category = param[0]
count = read_count(param[1])
alert.UnreadAlert(category, count)
option_list = [
optparse.make_option('-v', None,
action = 'store_true',
default = False,
dest = 'verbose',
help = 'verbose'),
optparse.make_option('-w', None,
action = 'store_true',
default = False,
dest = 'wait',
help = 'wait for dbus events'),
optparse.make_option('-t', None,
action = 'store',
default = 1,
type = "int",
dest = 'times',
help = 'repeat UnreadAlert/NewAlert <times> times',
metavar = '<times>'),
optparse.make_option('-r', None,
action = 'store',
dest = 'register',
type = 'string',
metavar = '<category>',
help = 'register alert'),
optparse.make_option('-n', None,
action = 'append',
dest = 'new_alert',
type = 'string',
nargs = 3,
metavar = '<category> <count> <description>',
help = 'send new alert'),
optparse.make_option('-u', None,
action = 'append',
dest = 'unread_alert',
type = 'string',
nargs = 2,
metavar = '<category> <count>',
help = 'send unread alert'),
]
parser = optparse.OptionParser(option_list=option_list)
parser.disable_interspersed_args()
(options, args) = parser.parse_args()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
mainloop = GObject.MainLoop()
alert = dbus.Interface(bus.get_object(BUS_NAME, BLUEZ_OBJECT_PATH),
ALERT_INTERFACE)
alert_agent = AlertAgent(bus, TEST_OBJECT_PATH, alert, mainloop)
print_command_line(options)
if not (options.register or options.new_alert or options.unread_alert or
options.wait):
parser.print_usage()
sys.exit(1)
if options.register:
alert.RegisterAlert(options.register, TEST_OBJECT_PATH)
times = 0
while times < options.times:
times += 1
new_alert(alert, options.new_alert)
unread_alert(alert, options.unread_alert)
if not options.wait:
sys.exit(0)
try:
mainloop.run()
except:
pass
|