forked from Spanni26/pyHPSU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.py
86 lines (69 loc) · 3.08 KB
/
mqtt.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# config inf conf_file (defaults):
# [MQTT]
# BROKER = localhost
# PORT = 1883
# USERNAME =
# PASSWORD =
# CLIENTNAME = rotex_hpsu
# PREFIX = rotex
import configparser
import requests
import sys
import os
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
class export():
hpsu = None
def __init__(self, hpsu=None, logger=None, config_file=None):
self.hpsu = hpsu
self.logger = logger
self.config_file = config_file
self.config = configparser.ConfigParser()
if os.path.isfile(self.config_file):
self.config.read(self.config_file)
else:
sys.exit(os.EX_CONFIG)
# object to store entire MQTT config section
self.mqtt_config = self.config['MQTT']
self.brokerhost = self.mqtt_config.get('BROKER', 'localhost')
self.brokerport = self.mqtt_config.getint('PORT', 1883)
self.clientname = self.mqtt_config.get('CLIENTNAME', 'rotex')
self.username = self.mqtt_config.get('USERNAME', None)
if self.username is None:
self.logger.error("Username not set!!!!!")
self.password = self.mqtt_config.get('PASSWORD', "NoPasswordSpecified")
self.prefix = self.mqtt_config.get('PREFIX', "")
self.qos = self.mqtt_config.getint('QOS', 0)
# every other value implies false
self.retain = self.mqtt_config.get('RETAIN', "NOT TRUE") == "True"
# every other value implies false
self.addtimestamp = self.mqtt_config.get('ADDTIMESTAMP', "NOT TRUE") == "True"
self.logger.info("configuration parsing complete")
# no need to create a different client name every time, because it only publish
# so adding the PID at the end of the client name ensures every process have a
# different client name only for readability on broker and troubleshooting
self.clientname += "-" + str(os.getpid())
self.logger.info("creating new mqtt client instance: " + self.clientname)
self.client=mqtt.Client(self.clientname)
self.client.on_publish = self.on_publish
if self.username:
self.client.username_pw_set(self.username, password=self.password)
self.client.enable_logger()
def on_publish(self,client,userdata,mid):
self.hpsu.logger.debug("mqtt output plugin data published, mid: " + str(mid))
def pushValues(self, vars=None):
self.logger.info("connecting to broker: " + self.brokerhost + ", port: " + str(self.brokerport))
self.client.connect(self.brokerhost, port=self.brokerport)
#self.msgs=[]
for r in vars:
msgs=[]
if self.prefix:
ret=self.client.publish(self.prefix + "/" + r['name'],payload=r['resp'], qos=int(self.qos))
topic=self.prefix + "/" + r['name']
else:
ret=self.client.publish(r['name'],payload=r['resp'], qos=int(self.qos))
topic=r['name']
msg={'topic':topic,'payload':r['resp'], 'qos':self.qos, 'retain':False}
self.client.disconnect()