root/software/dbus_test/service.py @ 1136d406
| cd10f9a6 | David Sorber | #!/usr/bin/env python3
|
||
| 7eba70e1 | David Sorber | import time
|
||
| cd10f9a6 | David Sorber | |||
import dbus
|
||||
import dbus.service
|
||||
import dbus.mainloop.glib
|
||||
from gi.repository import GObject as gobject
|
||||
| 7eba70e1 | David Sorber | from gi.repository import GLib
|
||
| cd10f9a6 | David Sorber | |||
class Service(dbus.service.Object):
|
||||
| f94cb2af | David Sorber | def __init__(self, message):
|
||
self._message = message
|
||||
| 7eba70e1 | David Sorber | self.poke_ctr = 0
|
||
| cd10f9a6 | David Sorber | |||
| f94cb2af | David Sorber | def run(self):
|
||
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||
bus_name = dbus.service.BusName("com.example.service", dbus.SystemBus())
|
||||
dbus.service.Object.__init__(self, bus_name, "/com/example/service")
|
||||
| cd10f9a6 | David Sorber | |||
| f94cb2af | David Sorber | self._loop = gobject.MainLoop()
|
||
print("Service running...")
|
||||
self._loop.run()
|
||||
print("Service stopped")
|
||||
| cd10f9a6 | David Sorber | |||
| f94cb2af | David Sorber | @dbus.service.method("com.example.service.Message", in_signature='', out_signature='s')
|
||
def get_message(self):
|
||||
print(" sending message")
|
||||
return self._message
|
||||
| 7eba70e1 | David Sorber | |||
@dbus.service.method("com.example.service.Poke", in_signature='', out_signature='')
|
||||
def poke(self):
|
||||
print('Got a poke!')
|
||||
| a0f033b8 | David Sorber | GLib.timeout_add(1, self.poke_runner)
|
||
| 7eba70e1 | David Sorber | print('Post idle add')
|
||
def poke_runner(self):
|
||||
self.poke_ctr += 1
|
||||
print(' Poke: {:d}'.format(self.poke_ctr))
|
||||
time.sleep(3)
|
||||
print(' Poke done')
|
||||
return False
|
||||
| cd10f9a6 | David Sorber | |||
| f94cb2af | David Sorber | @dbus.service.method("com.example.service.Quit", in_signature='', out_signature='')
|
||
def quit(self):
|
||||
print(" shutting down")
|
||||
self._loop.quit()
|
||||
| cd10f9a6 | David Sorber | |||
if __name__ == "__main__":
|
||||
| f94cb2af | David Sorber | Service("This is the service").run()
|