forked from devriesusna/Qualisys_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_Qualisys_sub.py
106 lines (87 loc) · 2.31 KB
/
mqtt_Qualisys_sub.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
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
# Patrick McCorkell
# March 2021
# US Naval Academy
# Robotics and Control TSD
#
from time import sleep
import paho.mqtt.client as MQTT
import logging
import logging.handlers
from datetime import datetime
from gc import collect as trash
import json
import os
from server import *
from random import randint
# #
#-----Logging Setup-----#
# #
filename=default_directory + datetime.now().strftime('qtm_mqtt_%Y%m%d_%H:%M:%s.log')
log = logging.getLogger()
log.setLevel(logging.INFO)
format = logging.Formatter('%(asctime)s : %(message)s')
file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(format)
log.addHandler(file_handler)
# #
#-------MQTT Setup------#
# #
name = clientname + str(randint(1000,9999))
client=MQTT.Client(name)
topic_prefix = 'QTM/'
# basic callback for MQTT that prints message data directly.
def print_message(client,userdata,message):
print()
print('mqtt rx:')
print(message.topic)
print(message.qos)
print(message.payload)
print(message.payload.decode())
print(message.retain)
print(client)
# A basic callback for MQTT that stores message data to a log file.
def log_message(client,userdata,message):
#log.info('message rx')
log.info(str(message.topic)+', '+str(message.payload))
# The callback that our program will use to control device.
def process(client,userdata,message):
#
# Code to use the mqtt data.
#
a = 1
def check_quit():
return 1
def setup_subscription():
check=0
try:
# Connect to the server (defined by server.py)
client.connect(server)
# Assigns the callback function when a mqtt message is received.
if (DEBUGGING):
# client.on_message=print_message
client.on_message=log_message
else:
client.on_message=process
# Subscribes to all the topics defined at top.
for i in topiclist:
client.subscribe(topic_prefix+i+'/'+'#')
print('subscribed to ' + topic_prefix+i+'/'+'#')
# Start the mqtt subscription.
client.loop_start()
log.info('mqtt subscription script started')
check=1
except:
print("didn't connect")
log.info('mqtt subscription failed')
return check
def main():
q=0
# Run the MQTT setup once.
q=setup_subscription()
# Because the subscription works on interrupt callbacks, nothing happens in main.
while(q):
trash()
sleep(1)
q=check_quit()
main()