-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_client.py
67 lines (55 loc) · 1.79 KB
/
mqtt_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os, shutil, toml
import paho.mqtt.client as mqtt
try:
import configparser as configparser
except ImportError:
import ConfigParser as configparser
CONFIG_INI = "config.ini"
Config = configparser.ConfigParser()
if not os.path.exists(CONFIG_INI):
shutil.copyfile(CONFIG_INI + '.default', CONFIG_INI)
Config.read(CONFIG_INI)
TOML_PATH = '/etc/snips.toml'
# Get the MQTT host and port from /etc/snips.toml.
try:
TOML = toml.load(TOML_PATH)
MQTT_ADDR_PORT = TOML['snips-common']['mqtt']
MQTT_ADDR, MQTT_PORT = MQTT_ADDR_PORT.split(':')
except (KeyError, ValueError):
MQTT_ADDR = 'localhost'
MQTT_PORT = 1883
MQTT_ADDR_PORT = "{}:{}".format(MQTT_ADDR, str(MQTT_PORT))
try:
TOML = toml.load(TOML_PATH)
MQTT_USER = TOML['snips-common']['mqtt_username']
MQTT_PASS = TOML['snips-common']['mqtt_password']
except (KeyError, ValueError):
MQTT_USER = ''
MQTT_PASS = ''
def put(topic, payload):
client = mqtt.Client("Client") # create new instance
client.username_pw_set(MQTT_USER, MQTT_PASS)
client.connect(MQTT_ADDR, int(MQTT_PORT)) # connect to broker
if isinstance(payload, str) or isinstance(payload, int) or isinstance(payload, float) or isinstance(payload, bool):
payload = [payload]
payload_count = len(payload)
for p in payload:
print("Publishing on: " + topic + " Payload: " + str(p))
msg = client.publish(topic, p)
if msg is not None:
msg.wait_for_publish()
if payload_count > 1:
time.sleep(100.0 / 1000.0)
client.disconnect()
def get_config():
return Config
def get_addr():
return MQTT_ADDR
def get_port():
return int(MQTT_PORT)
def get_user():
return MQTT_USER
def get_pass():
return MQTT_PASS
def get_addr_port():
return MQTT_ADDR_PORT