Skip to content

Commit

Permalink
options fields used where possible, configparser more compact instruc…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
segaura committed Apr 4, 2021
1 parent 5ccd0bb commit c012194
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 176 deletions.
4 changes: 3 additions & 1 deletion HPSU/HPSU.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, logger=None, driver=None, port=None, cmd=None, lg_code=None):
if not self.listCommands: #if we don't get a dict with commands

# get language, if non given, take it from the system
LANG_CODE = lg_code.upper()[0:2] if lg_code else locale.getdefaultlocale()[0].split('_')[0].upper()
LANG_CODE = lg_code[0:2] if lg_code else locale.getdefaultlocale()[0].split('_')[0].upper()
hpsuDict = {}

# read the translation file. if it doesn't exist, take the english one
Expand Down Expand Up @@ -220,6 +220,8 @@ def umConversion(self, cmd, response, verbose):
resp = str(response["resp"])
else:
resp = str(response["resp"])
# TODO replaces resp with the decoded value, major breaking change to be discussed
# meanwhile, a 'desc' field is added to the output json
#if cmd["value_code"]:
# resp=cmd["value_code"][resp]
return resp
2 changes: 0 additions & 2 deletions HPSU/cantcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import uuid
import json

SocketPort = 7060

class CanTCP(object):
sock = None
hpsu = None
Expand Down
71 changes: 21 additions & 50 deletions HPSU/plugins/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,26 @@ def __init__(self, hpsu=None, logger=None, config_file=None):
else:
sys.exit(9)

# MQTT hostname or IP
if self.config.has_option('MQTT', 'BROKER'):
self.brokerhost = self.config['MQTT']['BROKER']
else:
self.brokerhost = 'localhost'

# MQTT broker port
if self.config.has_option('MQTT', 'PORT'):
self.brokerport = int(self.config['MQTT']['PORT'])
else:
self.brokerport = 1883

# MQTT client name
if self.config.has_option('MQTT', 'CLIENTNAME'):
self.clientname = self.config['MQTT']['CLIENTNAME']
else:
self.clientname = 'rotex'
# MQTT Username
if self.config.has_option('MQTT', 'USERNAME'):
self.username = self.config['MQTT']['USERNAME']
else:
self.username = None
self.hpsu.logger.error("Username not set!!!!!")

#MQTT Password
if self.config.has_option('MQTT', "PASSWORD"):
self.password = self.config['MQTT']['PASSWORD']
else:
self.password="None"

#MQTT Prefix
if self.config.has_option('MQTT', "PREFIX"):
self.prefix = self.config['MQTT']['PREFIX']
else:
self.prefix = ""

#MQTT QOS
if self.config.has_option('MQTT', "QOS"):
self.qos = self.config['MQTT']['QOS']
else:
self.qos = "0"
# 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
logger.info("creating new mqtt client instance: " + self.clientname)
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:
Expand All @@ -88,9 +64,10 @@ 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.msgs=[]
for r in vars:
self.logger.info("connecting to broker: " + self.brokerhost + ", port: " + str(self.brokerport))
self.client.connect(self.brokerhost, port=self.brokerport)
msgs=[]
if self.prefix:
Expand All @@ -100,10 +77,4 @@ def pushValues(self, vars=None):
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()






self.client.disconnect()
Loading

0 comments on commit c012194

Please sign in to comment.