-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathudp_client.py
executable file
·49 lines (40 loc) · 1.77 KB
/
udp_client.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
#!/usr/bin/env python3
import os
import socket
import sys
# Add the Python root directory (fusion-engine-client/python/) to the import search path to enable FusionEngine imports
# if this application is being run directly out of the repository and is not installed as a pip package.
root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, root_dir)
from fusion_engine_client.utils.argument_parser import ArgumentParser
from fusion_engine_client.messages import MessagePayload
from fusion_engine_client.parsers import FusionEngineDecoder
if __name__ == "__main__":
# Parse command-line arguments.
parser = ArgumentParser(description="""\
Connect to a Point One device over UDP and print out the incoming message
contents.
When using UDP, you must configure the device to send data to your machine.
""")
parser.add_argument('-p', '--port', type=int, default=30400,
help="The FusionEngine UDP port on the data source.")
options = parser.parse_args()
# Connect to the device.
transport = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
transport.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
transport.bind(('', options.port))
# Listen for incoming data and parse FusionEngine messages.
try:
decoder = FusionEngineDecoder()
while True:
received_data = transport.recv(1024)
messages = decoder.on_data(received_data)
for header, message in messages:
if isinstance(message, MessagePayload):
print(str(message))
else:
print(f'{header.message_type} message (not supported)')
except KeyboardInterrupt:
pass
# Close the transport when finished.
transport.close()