-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmqtt_script.py
More file actions
executable file
·163 lines (138 loc) · 5.07 KB
/
Copy pathmqtt_script.py
File metadata and controls
executable file
·163 lines (138 loc) · 5.07 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import sys
import os
import time
import argparse
import json
import paho.mqtt.client as mqtt
debug_p = False
dryrun = False
def run_script(config_file, script_file):
'''
config_file holds the address of the MQTT server and login credentials
script_file JSON file with array of MQTT events to send
'''
config_data = {}
script_data = {}
# read config file
with open(config_file) as json_data_file:
try:
config_data = json.load(json_data_file)
except json.JSONDecodeError as parse_error:
print("JSON decode failed. [" + parse_error.msg + "]")
print("error in file [", config_file, "] at pos: ",
parse_error.pos, " line: ", parse_error.lineno)
sys.exit(1)
# read config file
with open(script_file) as json_data_file:
try:
script_data = json.load(json_data_file)
except json.JSONDecodeError as parse_error:
print("JSON decode failed. [" + parse_error.msg + "]")
print("error in file [", script_file, "] at pos: ",
parse_error.pos, " line: ", parse_error.lineno)
sys.exit(1)
# connect to MQTT server
host = config_data['mqtt_host']
port = config_data['mqtt_port'] if 'mqtt_port' in config_data else 8883
topic = config_data['mqtt_topic'] if 'mqtt_topic' in config_data else 'weasleyclock/#'
if debug_p:
print("connecting to host " + host + ":" + str(port) +
" topic " + topic)
# how to mqtt in python see https://pypi.org/project/paho-mqtt/
user_data = {"script": script_data, "topic": topic}
mqttc = mqtt.Client(client_id='mqtt_script_play',
clean_session=True,
userdata=user_data)
mqttc.username_pw_set(config_data['mqtt_user'],
config_data['mqtt_password'])
# create callbacks
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connected_flag = False
if port == 4883 or port == 4884 or port == 8883 or port == 8884:
mqttc.tls_set('/etc/ssl/certs/ca-certificates.crt')
mqttc.loop_start()
mqttc.connect(host, port, 60)
while not mqttc.connected_flag:
time.sleep(0.1)
send_mqtt_messages(mqttc, userdata=user_data)
time.sleep(2)
mqttc.loop_stop()
mqttc.disconnect()
print("All Done.")
def on_connect(client, userdata, flags, rc):
'''
Connect to the MQTT server, then start sending MQTT messages from our script.
'''
client.connected_flag = True
def on_message(client, userdata, message):
'''
We don't care about incoming messages. We are only sending a set
script of MQTT messages.
'''
return
def send_mqtt_messages(client, userdata):
'''
Iterate over the list of messages in the script and send them.
'''
script = userdata['script']
if debug_p:
print("send_mqtt_messages")
for msg in script:
topic = 'weasleyclock/susan'
if 'topic' in msg:
topic = msg['topic']
if 'type' in msg:
if msg['type'] == 'sleep':
t = 1
if 'time' in msg:
t = msg['time']
if debug_p:
print("SLEEP for ", t, " seconds")
time.sleep(t)
elif msg['type'] == 'range':
range_key = 'distance'
if 'range_key' in msg:
range_key = msg['range_key']
wait = 0.1
if 'sleep' in msg:
wait = msg['sleep']
(start, stop, inc) = (50, 0, -1)
if 'range' in msg:
(start, stop, inc) = msg['range']
if debug_p:
print("RANGE: ", range_key, "(", start, stop, inc, ")")
for val in range(start, stop, inc):
m = msg['msg']
m[range_key] = val
send_message(client, topic, m)
time.sleep(wait)
else:
print("Unknown message type [", msg['type'], "]")
else:
send_message(client, topic, msg['msg'])
def send_message(client, topic, message):
'''
Send MQTT message
'''
if debug_p:
print("send_message")
json_msg = json.dumps(message)
if debug_p:
print(topic, json_msg)
if not dryrun:
client.publish(topic, payload=json_msg, qos=2, retain=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="MQTT message script")
parser.add_argument('-c', '--config-file', default='/home/pi/weasleyclockd/weasleyclockd.json')
parser.add_argument('-s', '--script-file', default='/home/pi/weasleyclockd/demo_mqtt.json')
parser.add_argument('-v', '--verbose', action="store_true")
parser.add_argument('-d', '--dryrun', action="store_true")
args = parser.parse_args()
if args.verbose:
debug_p = True
if args.dryrun:
dryrun = True
run_script(config_file=args.config_file,
script_file=args.script_file)