-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish_message.py
43 lines (35 loc) · 1.14 KB
/
publish_message.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
import json
import sys
import logging
import paho.mqtt.publish as mqtt_publish
import mqtt_config
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger('messages')
def publish(mtype, **payload):
payload['type'] = mtype
logger.info('publish topic=%s payload=%s', mqtt_config.TOPIC, payload)
mqtt_publish.single(mqtt_config.TOPIC,
payload=json.dumps(payload),
qos=1,
retain=True,
hostname=mqtt_config.hostname,
auth=mqtt_config.auth,
port=mqtt_config.port,
client_id='')
def repl():
while True:
command = str(raw_input('> '))
publish('action', action=command)
def main():
# logger.setLevel(logging.INFO)
if not mqtt_config.hostname:
print >> sys.stderr, 'At least one of these must be set:', ', '.join(mqtt_config.MQTT_ENV_VARS)
sys.exit(1)
action = 'test'
if len(sys.argv) > 1:
action = sys.argv[1]
publish('action', action=action)
else:
repl()
if __name__ == '__main__':
main()