-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver_demo_5.py
More file actions
64 lines (56 loc) · 1.5 KB
/
server_demo_5.py
File metadata and controls
64 lines (56 loc) · 1.5 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# server_demo_5.py
# Objective:
# Test that a server program can emit an integer using pydbus.
# Uses the session bus
#
# Use gdbus to monitor the signals being emitted.
# $ gdbus monitor --session --dest org.example.demo.test
#
# Ian Stewart
# 2019-11-22
#
# Importing...
import sys
if int(sys.version[0]) < 3: sys.exit("Please use python 3. Exiting...")
# Importing...
from pydbus import SessionBus # SystemBus
from pydbus.generic import signal
from gi.repository import GLib
import time
import random
# Variables / Constants / Instantiation...
bus = SessionBus() # SystemBus
BUS = "org.example.demo.test"
loop = GLib.MainLoop()
INTERVAL = 2
message_count = 0
class DBusService_XML():
"""
DBus Service XML definition.
type="i" for integer, "s" string, "d" double, "as" list of string data.
"""
dbus = """
<node>
<interface name="{}">
<signal name="integer_signal">
<arg type="i"/>
</signal>
</interface>
</node>
""".format(BUS)
integer_signal = signal()
def timer():
"Emit a random integer each call."
random_integer = random.randint(0, 100)
print("Random integer emitted: {}".format(random_integer))
emit.integer_signal(random_integer)
return True
if __name__ == "__main__":
print("Starting Server Demo 5...")
emit = DBusService_XML()
bus.publish(BUS, emit)
GLib.timeout_add_seconds(interval=INTERVAL, function=timer)
loop.run()