def gpioConfig(self, loggingContext, tle): gpio = GpioConfiguration() gpio.port = tle.attrib['port'] portName = gpio.port gpio.portNumber = allEverGpios.getPort(gpio.port) if 'alias' in tle.attrib: gpio.alias = tle.attrib['alias'] s = tle.find('default') if s != None: loggingContext2 = "{lc:s} gpio port '{p:s}', 'default'".format( lc=loggingContext, p=portName) gpio.default_setting = self.settingConfig(loggingContext2, s) else: loggingContext2 = "{lc:s} gpio port '{p:s}' ".format( lc=loggingContext, p=portName) errorManager.append("{lc:s}: gpio has no default setting.".format( lc=loggingContext2)) s = tle.find('active') if s != None: loggingContext2 = "{lc:s} gpio port '{p:s}', 'active'".format( lc=loggingContext, p=portName) gpio.active_setting = self.settingConfig(loggingContext2, s) else: loggingContext2 = "{lc:s} gpio port '{p:s}'".format( lc=loggingContext, p=portName) errorManager.append("{lc:s}: gpio has no active setting.".format( lc=loggingContext2)) return gpio
def configure(self): try: self.tree = ET.parse(self.filename) except ET.ParseError as e: errorManager.append("error reading config file '{name:s}'".format( name=self.filename) + '\n' + '{error:s}'.format(error=str(e)) + '\n' + 'Correct xml file and restart.') return except IOError as e: errorManager.append( "error reading config file '{name:s}', '{error:s}'".format( name=self.filename, error=str(e))) return root = self.tree.getroot() # # config files may evolve over time. Check the version tag for this. # version = None if 'version' in root.attrib: version = root.attrib['version'] else: print("configuration, no 'version' attribute available.") print("assume version=1.0.") version = '1.0' if version == '1.0': self.configDelegate = ConfigManager_1_0(self.tree) else: print("configuration, unknown 'version' attribute {version:s}.". format(version=version)) sys.exit(10) self.configDelegate.configure()
def _check_module_name(self): if debug: print("_check_module_name") names = [] for adapter in self.adapters: if adapter.name in names: errorManager.append( "name used multiple times, use of unique names needed " + adapter.name) else: names.append(adapter.name)
def _check_module_parameters(self): if debug: print("_check_module_parameters") for adapter in self.adapters: for p in adapter.mandatoryParameters: if debug: print("check parameter ", p) if p not in adapter.parameters: errorManager.append( "adapter: {a:s} mandatory parameter '{name:s}' missing in configuration." .format(a=adapter.name, name=p))
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # loggingContext = "adapter '[{a:s}]'".format(a=self.name ) for tle in child: if 'zone' == tle.tag: _from = None _to = None _value = None if 'from' in tle.attrib: _from = tle.attrib['from'] else: errorManager.append("{lc:s}: no 'from' in adc:zone".format( lc=loggingContext )) if 'to' in tle.attrib: _to = tle.attrib['to'] else: errorManager.append("{lc:s}: no 'to' in adc:zone".format( lc=loggingContext )) if 'value' in tle.attrib: _value = tle.attrib['value'] else: errorManager.append("{lc:s}: no 'value' in adc:zone".format( lc=loggingContext )) if _from == None: continue if _to == None: continue if _value == None: continue try: _from = int( _from) _to = int( _to) except Exception: errorManager.append("{lc:s}: no 'from', 'to' must be int values in adc:zone".format( lc=loggingContext )) continue if _to < _from: errorManager.append("{lc:s}: no 'from' > 'to' adc:zone".format( lc=loggingContext )) continue for i in range(_from, _to+1): self.mapping[i] = _value
def settingConfig(self, loggingContext, xmlSetting): setting = GpioSetting() setting.dir = None if 'dir' in xmlSetting.attrib: setting.dir = xmlSetting.attrib['dir'] else: errorManager.append( "{lc:s}, no dir-attribute.".format(lc=loggingContext)) return if 'pull' in xmlSetting.attrib: setting.pull = xmlSetting.attrib['pull'] if 'default' in xmlSetting.attrib: setting.default = xmlSetting.attrib['default'] return setting
def __init__(self, portmappingFilename): if True: try: tree = ET.parse(portmappingFilename) except ET.ParseError as e: errorManager.append( "error reading gpio config file '{name:s}'".format( name=portmappingFilename) + '\n' + '{error:s}'.format(error=str(e)) + '\n' + 'Correct xml file and restart.') return except IOError as e: errorManager.append( "error reading config file '{name:s}', '{error:s}'".format( name=self.portmappingFilename, error=str(e))) return root = tree.getroot() # # config files may evolve over time. Check the version tag for this. # version = None if 'version' in root.attrib: version = root.attrib['version'] if version == '1.0': pass else: print( "configuration, unknown 'version' attribute {version:s}.". format(version=version)) sys.exit(10) self.gpios = {} for child in root: if debug: print('configure search modules', child.tag, child.attrib) if 'port' == child.tag: portNumber = child.attrib['gpioNumber'] name = child.attrib['name'] self.gpios[name] = int(portNumber)
def _check_gpio_alias(self): """ check mandatory gpio-alias""" for adapter in self.adapters: if len(adapter.mandatoryAlias) > 0: ma = [] for g in adapter.gpios: if g.alias != None: if g.alias in ma: errorManager.append( "adapter: {a:s} Alias not unique: {p:s}". format(a=adapter.name, p=g.alias)) else: ma.append(g.alias) for m in adapter.mandatoryAlias: if m in ma: pass else: errorManager.append( "adapter: {a:s} Alias missing: {p:s}".format( a=adapter.name, p=m))
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # loggingContext = "adapter '[{a:s}]'".format(a=self.name ) # look for extension tag (new from 2017) for tle in child: if 'extension' == tle.tag: child= tle break for tle in child: if 'io' == tle.tag: if 'id' in tle.attrib: _id = tle.attrib['id'] if self._isPin(_id): if 'dir' in tle.attrib: _dir = tle.attrib['dir'] self._setPinDir( _id, _dir ) else: errorManager.append("{lc:s}: no dir in mcp23s17:io".format( lc=loggingContext )) if 'pullup' in tle.attrib: _pullup = tle.attrib['pullup'] self._setPinPullup( _id, _pullup ) else: errorManager.append("{lc:s}: id unknown".format( lc=loggingContext )) else: errorManager.append("{lc:s}: no id in mcp23s17:io".format( lc=loggingContext )) # # print self.pins # # process data # dynamically add methods to self. # for pin in self._getPins(): x = self._getPin(pin) # print pin, x if x['dir'] == self.OUT: # # bind function to class. # see http://www.ianlewis.org/en/dynamically-adding-method-classes-or-class-instanc # # a input function corresponds to an output pin if debug: print ( "define input function", "input" + pin, " func ", x['ifunc'] ) method = MethodType(x['ifunc'], self, type(self)) setattr(self, "input" + pin, method ) if x['dir'] == self.IN: # a output function corresponds to an input pin if debug: print ( "define value output function", "output" + pin) #method = MethodType(self.pins[pin]['ifunc'], self, type(self)) setattr(self, "output" + pin, self._sendValue )
def configure(self): root = self.tree.getroot() nAdapter = 0 loggingContext = '' for child in root: if debug: print('configure search modules', child.tag, child.attrib) found = False if 'description' == child.tag: self.description = child.text found = True if 'adapter' == child.tag: className = child.attrib['class'] if 'name' in child.attrib: name = child.attrib['name'] loggingContext = "adapter '{a:s}'".format(a=name) else: loggingContext = "adapter '[{a:s}]'".format( a=str(nAdapter)) sections = className.split('.') _path = '.'.join(sections[0:len(sections) - 1]) _class = sections[len(sections) - 1] if debug: print('path = ', _path) print('class = ', _class) try: iStatement = "import {p:s}".format(p=_path, c=_class) if debug: print('import statement', iStatement) # exec( iStatement , globals(), locals() ) exec(iStatement) #print ('import statement', "success") except ImportError as e: print('----------', e) errorManager.append( "{lc:s}: no adapter class '{cn:s}' (ImportError)". format(lc=loggingContext, cn=className)) continue lAdapter = None try: cStatement = "{p:s}.{c:s}()".format(p=_path, c=_class) # print 'class statement' if debug: print('cStatement ', cStatement) lAdapter = eval(cStatement, globals(), locals()) #print ('cStatement', "success", lAdapter) except AttributeError as e: print('----------', e) errorManager.append( "{lc:s}: no adapter class '{cn:s}' (AttributeError)". format(lc=loggingContext, cn=className)) continue # could be more elegant: lAdapter.__class__ # print( str( lAdapter.__class__ )) lAdapter.className = className if debug: print('configure adapter') self.adapterConfig(lAdapter, loggingContext, child) self.adapters.append(lAdapter) if debug: print("adapter complete") nAdapter += 1 found = True if not (found): errorManager.append( "{lc:s}: unknown tag below root '{cn:s}'".format( lc=loggingContext, cn=child.tag))
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # input_broadcasts = [] output_broadcasts = [] loggingContext = "adapter '[{a:s}]'".format(a=self.name ) for tle in child: if 'remote' == tle.tag: if 'type' in tle.attrib: _type = tle.attrib['type'] if _type == 'forward': for tle2 in tle: if 'broadcast' == tle2.tag: _broadcast_name = tle2.attrib['name'] input_broadcasts.append( _broadcast_name) else: errorManager.append("{lc:s}: no name in remout:input.broadcast".format( lc=loggingContext )) elif _type == 'receive': for tle2 in tle: if 'broadcast' == tle2.tag: _broadcast_name = tle2.attrib['name'] output_broadcasts.append( _broadcast_name) else: errorManager.append("{lc:s}: no name in remout:output.broadcast".format( lc=loggingContext )) else: errorManager.append("{lc:s}: wrong type in remote (forward,receive): {type:s}".format( lc=loggingContext,type=_type )) else: errorManager.append("{lc:s}: no type in remote".format( lc=loggingContext )) # # bind function to class. # see http://www.ianlewis.org/en/dynamically-adding-method-classes-or-class-instanc # # Dynamic bindings do not work on input side (unfortunately) if False: for _name in input_broadcasts: # set the input mechanism for the adapter if debug: print ( "define input function", "input_" + _name , " func ", self._input ) method = MethodType(self._input, self, type(self)) setattr(self, "input_" + _name, method ) for _name in output_broadcasts: # set the output mechanism for the adapter if debug: print ( "define output function", "output_" + _name , " func ", self._output ) method = MethodType(self._output, self, type(self)) setattr(self, "output_" + _name, method ) if debug: for c in inspect.getmembers(self, inspect.ismethod ): print("debug methods", c) # # register the functionName and scratchName on the framework # inputs = [] outputs = [] # registry from input keys to event names self.reg = {} # ni = 0 for _name in input_broadcasts: # set the input mechanism for the adapter _is = configuration.InputSetting ("input_{n:03d}".format(n=ni)) _is.scratchNames.append(_name) inputs.append(_is) self.reg["{n:03d}".format(n=ni)] = _name ni += 1 for _name in output_broadcasts: # set the output mechanism for the adapter _os = configuration.OutputSetting ("output_"+ _name) _os.scratchNames.append(_name) outputs.append(_os) self.addInputs(inputs) self.addOutputs(outputs)
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # adapter_input_values = [] adapter_output_values = [] loggingContext = "adapter '[{a:s}]'".format(a=self.name ) foundConfig = False _topic = None _variable = None for _extension in child: if 'extension' == _extension.tag: for _mqtt in _extension: if 'mqtt' == _mqtt.tag: if foundConfig: errorManager.append("{lc:s}: more than one 'mqtt'-config for adapter.".format( lc=loggingContext )) foundConfig = True for ps in _mqtt: if 'publish' == ps.tag: if 'topic' in ps.attrib: _topic = ps.attrib['topic'] else: errorManager.append("{lc:s}: no 'topic'-attribute in <publish>".format( lc=loggingContext )) if 'variable' in ps.attrib: _variable = ps.attrib['variable'] else: _variable = _topic errorManager.appendWarning("{lc:s}: no 'variable'-attribute in <publish>".format( lc=loggingContext )) self.publishConfig.append( ( _topic, _variable)) if 'subscribe' == ps.tag: if 'topic' in ps.attrib: _topic = ps.attrib['topic'] else: errorManager.append("{lc:s}: no 'topic'-attribute in <publish>".format( lc=loggingContext )) if 'variable' in ps.attrib: _variable = ps.attrib['variable'] else: _variable = _topic errorManager.appendWarning("{lc:s}: no 'variable'-attribute in <publish>".format( lc=loggingContext )) self.subscribeConfig.append( ( _topic, _variable) ) if not foundConfig: errorManager.append("{lc:s}: no 'mqtt'-config for adapter.".format( lc=loggingContext )) # if debug: print ( self.publishConfig ) print ( self.subscribeConfig ) # # process data # dynamically add methods to self and register variables here. # for p in self.subscribeConfig: _topic = p[0] _variable = p[1] methodName = "output_" + _variable setattr(self, methodName, self._sendValue ) value = configuration.OutputSetting(methodName) value.scratchNames.append( _variable) adapter_output_values.append(value) for p in self.publishConfig: _topic = p[0] _variable = p[1] methodName = "input_" + _variable self._add_receiveValueMethod( self.__class__, methodName ) value = configuration.InputSetting(methodName) value.scratchNames.append( _variable) adapter_input_values.append(value) self.addInputValues(adapter_input_values) self.addOutputValues(adapter_output_values)
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # loggingContext = "adapter '[{a:s}]'".format(a=self.name) for tle in child: if 'extension' == tle.tag: child = tle break for tle in child: if 'zone' == tle.tag: _from = None _to = None _value = None if 'from' in tle.attrib: _from = tle.attrib['from'] else: errorManager.append("{lc:s}: no 'from' in adc:zone".format( lc=loggingContext)) if 'to' in tle.attrib: _to = tle.attrib['to'] else: errorManager.append("{lc:s}: no 'to' in adc:zone".format( lc=loggingContext)) if 'value' in tle.attrib: _value = tle.attrib['value'] else: errorManager.append( "{lc:s}: no 'value' in adc:zone".format( lc=loggingContext)) if _from == None: continue if _to == None: continue if _value == None: continue try: _from = int(_from) _to = int(_to) except Exception: errorManager.append( "{lc:s}: no 'from', 'to' must be int values in adc:zone" .format(lc=loggingContext)) continue if _to < _from: errorManager.append( "{lc:s}: no 'from' > 'to' adc:zone".format( lc=loggingContext)) continue for i in range(_from, _to + 1): self.mapping[i] = _value
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # input_broadcasts = [] output_broadcasts = [] loggingContext = "adapter '[{a:s}]'".format(a=self.name) # look for extension tag (new from 2017) for tle in child: if 'extension' == tle.tag: child = tle break for tle in child: if 'remote' == tle.tag: if 'type' in tle.attrib: _type = tle.attrib['type'] if _type == 'forward': for tle2 in tle: if 'broadcast' == tle2.tag: _broadcast_name = tle2.attrib['name'] input_broadcasts.append(_broadcast_name) else: errorManager.append( "{lc:s}: no name in remout:input.broadcast" .format(lc=loggingContext)) elif _type == 'receive': for tle2 in tle: if 'broadcast' == tle2.tag: _broadcast_name = tle2.attrib['name'] output_broadcasts.append(_broadcast_name) else: errorManager.append( "{lc:s}: no name in remout:output.broadcast" .format(lc=loggingContext)) else: errorManager.append( "{lc:s}: wrong type in remote (forward,receive): {type:s}" .format(lc=loggingContext, type=_type)) else: errorManager.append( "{lc:s}: no type in remote".format(lc=loggingContext)) # # bind function to class. # see http://www.ianlewis.org/en/dynamically-adding-method-classes-or-class-instanc # # Dynamic bindings do not work on input side (unfortunately) if False: for _name in input_broadcasts: # set the input mechanism for the adapter if debug: print("define input function", "input_" + _name, " func ", self._input) method = MethodType(self._input, self, type(self)) setattr(self, "input_" + _name, method) for _name in output_broadcasts: # set the output mechanism for the adapter if debug: print("define output function", "output_" + _name, " func ", self._output) method = MethodType(self._output, self, type(self)) setattr(self, "output_" + _name, method) if debug: for c in inspect.getmembers(self, inspect.ismethod): print("debug methods", c) # # register the functionName and scratchName on the framework # inputs = [] outputs = [] # registry from input keys to event names self.reg = {} # ni = 0 for _name in input_broadcasts: # set the input mechanism for the adapter _is = configuration.InputSetting("input_{n:03d}".format(n=ni)) _is.scratchNames.append(_name) inputs.append(_is) self.reg["{n:03d}".format(n=ni)] = _name ni += 1 for _name in output_broadcasts: # set the output mechanism for the adapter _os = configuration.OutputSetting("output_" + _name) _os.scratchNames.append(_name) outputs.append(_os) self.addInputs(inputs) self.addOutputs(outputs)
def getPort(self, portName): if portName in self.gpios: return self.gpios[portName] errorManager.append( "gpio name in config file not known: {pn:s}".format(pn=portName)) return None
def adapterConfig(self, adapter, loggingContext, child): if debug: print("adapterConfig", child.tag) if 'name' in child.attrib: # set name and force registration adapter.setName(child.attrib['name']) else: errorManager.append('no name given for adapter config, class = ', child.attrib['class']) # # give the adapter a chance to dynamically create methods # if needed when method 'setXMLConfig' is available # moduleMethods = AdapterMethods(adapter) adapter_input_values = [] adapter_inputs = [] adapter_output_values = [] adapter_outputs = [] # # set parameters first, so they are available in xml config # for tle in child: if 'parameter' == tle.tag: if not ('name' in tle.attrib): errorManager.append( "{lc:s}: no name attribute for parameter".format( lc=loggingContext)) continue if not ('value' in tle.attrib): errorManager.append( "{lc:s}: no value attribute for parameter".format( lc=loggingContext)) continue if debug: print("parameter", tle.attrib['name'], tle.attrib['value']) name = str(tle.attrib['name']) value = str(tle.attrib['value']) adapter.parameters[name] = value if moduleMethods.hasMethod("setXMLConfig"): # # the setXMLConfig-method receives the complete adapter xml. # specific extensions should be kept inside an <extension>-element # this is not done for older adapters before 2017 # foundExtension = False for _tle in child: if 'extension' == _tle.tag: foundExtension = True break if not foundExtension: errorManager.appendWarning( "{lc:s} Adapter extension should be encapsulated in <extension>-tag" .format(lc=loggingContext)) adapter.setXMLConfig(child) # there could be methods dynamically added, so rerun AdapterMethods moduleMethods = AdapterMethods(adapter) if debug: for x in moduleMethods.moduleMethods: print(x) for tle in child: if 'description' == tle.tag: adapter.description = tle.text elif 'gpio' == tle.tag: gpio = self.gpioConfig(loggingContext, tle) adapter.gpios.append(gpio) elif 'input' == tle.tag: if not ('name' in tle.attrib): errorManager.append( "{lc:s}: no name attribute for input method".format( lc=loggingContext)) continue methodName = tle.attrib['name'] if moduleMethods.hasMethod(methodName): inp = InputSetting(methodName) foundBroadcast = False for comm in tle: if comm.tag == 'broadcast': name = comm.attrib['name'] inp.scratchNames.append(name) foundBroadcast = True else: errorManager.append( "{lc:s}: input tag needs 'broadcast', but found {n:s}" .format(lc=loggingContext, n=comm.tag)) if not (foundBroadcast): errorManager.append( "{lc:s}: input tag needs 'broadcast', but not found" .format(lc=loggingContext)) adapter_inputs.append(inp) else: errorManager.append( "{lc:s}, method name '{n:s}' for input method missing". format(lc=loggingContext, n=methodName)) continue elif 'output' == tle.tag: if not ('name' in tle.attrib): errorManager.append( "{lc:s} no name attribute for output method".format( lc=loggingContext)) continue methodName = tle.attrib['name'] if moduleMethods.hasMethod(methodName): out = OutputSetting(methodName) foundBroadcast = False for comm in tle: if comm.tag == 'broadcast': name = comm.attrib['name'] out.scratchNames.append(name) foundBroadcast = True if not (foundBroadcast): errorManager.append( "{lc:s}: output tag needs 'broadcast', but not found" .format(lc=loggingContext)) adapter_outputs.append(out) else: errorManager.append( "{lc:s} method name '{n:s}' for broadcast not available (check adapter python code)" .format(lc=loggingContext, n=methodName)) continue elif 'input_value' == tle.tag: if not ('name' in tle.attrib): errorManager.append( "{lc:s}: no name attribute for input_value method". format(lc=loggingContext)) continue methodName = tle.attrib['name'] if moduleMethods.hasMethod(methodName): value = InputSetting(methodName) adapter_input_values.append(value) for comm in tle: if comm.tag == 'variable': n = comm.attrib['name'] value.scratchNames.append(n) else: errorManager.append( "{lc:s}: input_value with unexpected child '{c:s}', variable expected" .format(c=comm.tag, lc=loggingContext)) else: if debug: print("these are the well known adapter methods", moduleMethods.moduleMethods) errorManager.append( "{lc:s}: unknown input_value name '{command:s}' (check adapter python code)" .format(lc=loggingContext, command=methodName)) elif 'output_value' == tle.tag: if not ('name' in tle.attrib): errorManager.append( "{lc:s}: no name attribute for output_value method". format(lc=loggingContext)) continue methodName = tle.attrib['name'] if moduleMethods.hasMethod(methodName): value = OutputSetting(methodName) adapter_output_values.append(value) for comm in tle: if comm.tag == 'sensor': n = comm.attrib['name'] value.scratchNames.append(n) else: errorManager.append( "{lc:s}: output_value with unexpected child '{c:s}', sensor expected" .format(c=comm.tag, lc=loggingContext)) else: errorManager.append( "{lc:s}: unknown output_value name '{command:s}' (check adapter python code)" .format(lc=loggingContext, command=methodName)) else: # if adapter provides a 'setXmlConfig-Method, pass the current node to the adapter and let it grab # whatever needed. if moduleMethods.hasMethod("setXMLConfig"): pass else: errorManager.append("{lc:s}: unknown tag '{tag:s}'".format( lc=loggingContext, tag=tle.tag)) adapter.addInputs(adapter_inputs) adapter.addInputValues(adapter_input_values) adapter.addOutputs(adapter_outputs) adapter.addOutputValues(adapter_output_values)
def getPort(self, portName): if portName in self.gpios: return self.gpios[portName] errorManager.append("gpio name in config file not known: %s", portName) return None
def _check_module_input_output(self): if debug: print("_check_module_input_output") for adapter in self.adapters: for p in adapter.inputs: if debug: print("check input ", p.name) for o in adapter.input_values: if p.name == o.name: errorManager.append( "adapter: {a:s} input name '{name:s}' must not be used as input value name." .format(a=adapter.name, name=p.name)) for o in adapter.outputs: if p.name == o.name: errorManager.append( "adapter: {a:s} input name '{name:s}' must not be used as output name." .format(a=adapter.name, name=p.name)) for o in adapter.output_values: if p.name == o.name: errorManager.append( "adapter: {a:s} input name '{name:s}' must not be used as output value name." .format(a=adapter.name, name=p.name)) for p in adapter.input_values: if debug: print("check input_values ", p.name) for o in adapter.outputs: if p.name == o.name: errorManager.append( "adapter: {a:s} input value name '{name:s}' must not be used as output name." .format(a=adapter.name, name=p.name)) for o in adapter.output_values: if p.name == o.name: errorManager.append( "adapter: {a:s} input value name '{name:s}' must not be used as output value name." .format(a=adapter.name, name=p.name)) for p in adapter.outputs: if debug: print("check outputs ", p.name) for o in adapter.output_values: if p.name == o.name: errorManager.append( "adapter: {a:s} input value name '{name:s}' must not be used as output value name." .format(a=adapter.name, name=p.name))
def setXMLConfig(self, child): # # read configuration from xml # the xml config is only needed to allow configuration to access this method here. # lircd_conf_file = self.parameters['lircd.conf'] codes = [] state = 0 try: fi = open(lircd_conf_file, 'r') for line in fi: line = line.rstrip() if debug: print(line) if state == 0: if re.search(r"begin codes", line): state = 1 elif state == 1: if re.search(r"end codes", line): state = 2 else: if "" == line.strip(): continue if debug: print("state 1", line) m = re.match( r"[ \t]*([A-Za-z][A-Za-z0-9_]*)(|[ \t]+.*)", line) if m: code = m.group(1) codes.append(code) fi.close() except Exception as e: err = "{n:s}: error reading config file {e:s}".format(n=self.name, e=str(e)) logger.error(err) errorManager.append(err) return logger.debug("{n:s}: codes are {c:s}".format(n=self.name, c=str(codes))) # # process data # dynamically add methods to self and register send methods here. # adapter_output = [] for code in codes: methodName = "output_" + code + "_down" setattr(self, methodName, self._sendValue) value = configuration.OutputSetting(methodName) value.scratchNames.append(methodName) adapter_output.append(value) for code in codes: methodName = "output_" + code + "_up" setattr(self, methodName, self._sendValue) value = configuration.OutputSetting(methodName) value.scratchNames.append(methodName) adapter_output.append(value) self.addOutputs(adapter_output)
def setXMLConfig(self, child): # print "setXMLConfig" # # read configuration from xml # loggingContext = "adapter '[{a:s}]'".format(a=self.name ) # bitwise encoded pin usage self._analogs = 0 self._inputs = 0 self._inputsp = 0 self._outputs = 0 self._pwms = 0 for tle in child: if 'io' == tle.tag: if 'id' in tle.attrib: _id = tle.attrib['id'] _dir = tle.attrib['dir'] _pullup=None if 'pullup' in tle.attrib: _pullup = tle.attrib['pullup'] options = self.io_options[_id]['options'] if _dir in options: if _dir == 'void': pass elif _dir == 'in' and _pullup == 'on': self._inputsp |= ( 1 << self.io_options[_id]['pos']) if debug: print("register ", "output" + _id ) setattr(self, "output" + _id, self._sendValue ) pass elif _dir == 'in': self._inputs |= ( 1 << self.io_options[_id]['pos']) if debug: print("register ", "output" + _id ) setattr(self, "output" + _id, self._sendValue ) pass elif _dir == 'out': self._outputs |= ( 1 << self.io_options[_id]['pos']) method = MethodType(self.io_options[_id]['ifunc'], self, type(self)) if debug: print("register ", "input" + _id ) setattr(self, "input" + _id, method ) pass elif _dir == 'pwm': self._pwms |= ( 1 << self.io_options[_id]['pos']) method = MethodType(self.io_options[_id]['pfunc'], self, type(self)) if debug: print("register ", "input" + _id ) setattr(self, "input" + _id, method ) pass else: errorManager.append("{lc:s}: invalid dir in arduino_uno:io".format( lc=loggingContext )) continue else: errorManager.append("{lc:s}: id missing arduino_uno:io".format( lc=loggingContext )) continue if 'analog' == tle.tag: if 'id' in tle.attrib: _id = tle.attrib['id'] _dir = tle.attrib['dir'] options = self.analog_options[_id]['options'] if _dir in options: if _dir == 'void': pass elif _dir == 'in': self._analogs |= ( 1 << self.analog_options[_id]['pos']) if debug: print("register ", "outputAD" + _id ) setattr(self, "outputAD" + _id, self._sendValue ) pass else: errorManager.append("{lc:s}: invalid dir in arduino_uno:analog".format( lc=loggingContext )) continue else: errorManager.append("{lc:s}: id missing arduino_uno:analog".format( lc=loggingContext )) continue