You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB
Python

import yaml
class MQTT_ROS_Config:
"""
MQTT_ROS_Config configuration class
"""
# class variables
sectionName='MQTT_ROS'
options={
'msg_format': (str,True),
'MQTTClientNamePub': (str,True),
'MQTTClientNameSub': (str,True),
'host': (str,True),
'port': (int,True),
'keepalive': (int,True),
'willTopic':(str,True),
'lwt':(str, True),
'willRetain':(bool,False),
'willTopicQOS':(int,True),
'Flight_Information_topicToMqtt': (str,False),
'Fly_Formation_topicToMqtt': (str,False),
'Fly_Formation_topicToMqtt_QOS':(int,False),
'ROSClientNamePub': (str,False),
'ROSClientNameSub': (str,False),
'ROStopicName_Flight_Information': (str,False),
'ROStopicName_Fly_Formation': (str,False)}
#constructor
def __init__(self, inFileName):
#read YAML config and get EV3 section
infile=open(inFileName,'r')
ymlcfg=yaml.safe_load(infile)
infile.close()
eccfg=ymlcfg.get(self.sectionName,None)
if eccfg is None: raise Exception('Missing {} section in cfg file'.format(self.sectionName))
#iterate over options
for opt in self.options:
if opt in eccfg:
optval=eccfg[opt]
#verify parameter type
if type(optval) != self.options[opt][0]:
raise Exception('Parameter "{}" has wrong type'.format(opt))
#create attributes on the fly
setattr(self,opt,optval)
else:
if self.options[opt][1]:
raise Exception('Missing mandatory parameter "{}"'.format(opt))
else:
setattr(self,opt,None)
#string representation for class data
def __str__(self):
return str(yaml.dump(self.__dict__,default_flow_style=False))
if __name__ == '__main__':
cfg=MQTT_ROS_Config("mqttConfig.yml")
print(cfg)