-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
65 lines (47 loc) · 1.59 KB
/
example.py
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
"""
A really basic example of how to connect, receive and send using the MistConnection.
"""
__copyright__ = 'Thinnect Inc. 2021'
__license__ = 'MIT'
import argparse
import time
from mistconnection.connection import Connection
from mistconnection.message import Message
import logging
# Print any received messages
def receive(message):
print(message)
if __name__ == '__main__':
# Enable logging, but disable it for pika, which is very chatty
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("pika").propagate = False
parser = argparse.ArgumentParser(description='Example setup of a mistconnection.')
parser.add_argument('--amqp', default='amqps://user:[email protected]',
help='The AMQP connection string to use')
parser.add_argument('--exchange', default='mistx',
help='The exchange to use for messaging.')
args = parser.parse_args()
conn = Connection(args.amqp, args.exchange, 0x1122334455667788, None)
# Register a receiver for AMID 0001
conn.register_receiver(0x0001, receive)
# Start connecting to the server
conn.connect()
# Listen for any messages until interrupted
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
# Send a message when interrupted
m = Message()
m.destination = 0x1234567812345678
m.payload = b"\00\11\22\33"
conn.send(m)
# Listen for any responses
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
# Shut it down
conn.close()