def configure(self): """ Configure whole upyHome components """ try: self._load_config() MODE_DEBUG['value'] = self._get_config_key(self._config, KEY_DEBUG, True) _debug('upyhome::configure') PLATFORM['value'] = self._get_config_key(self._config, KEY_PLATFORM, True) self._name = self._config[CONFIG_NAME] # Re init base cb self._init_cb(self._get_config_key(self._config, KEY_USER_CB, True)) #Configure components suscribes = self._get_config_key(self._config, KEY_SUSCRIBES) if suscribes is not None and isinstance(suscribes, list): for scb in suscribes: self._add_suscribe(self._proxy, scb) #Components CONFIG_COMPS = [(CONFIG_NET, self._config_network), (CONFIG_DIN, self._config_inputs), (CONFIG_DOUT, self._config_outputs), (CONFIG_LED, self._config_leds), (CONFIG_I2C, self._config_i2cs), (CONFIG_SPI, self._config_spis), (CONFIG_DRIVER, self._config_drivers)] for CONF in CONFIG_COMPS: self._config_comp(*CONF) except Exception as ex: log_error('{0}@upyhome::configure'.format(ex))
def _load_config(self): _debug('upyhome::_load_config') if not CONFIG_FILE in uos.listdir(): raise Exception("config file not found") cf = open(CONFIG_FILE, 'r') self._config = ujson.load(cf) cf.close()
def _exec_func(self, obj, function, args): _debug('_exec_func {0}@{1} [{2}]'.format(function, obj, args)) if hasattr(obj, function): func = getattr(obj, function) if callable(func): if args is not None: func(args) else: func() else: raise Exception('Function %s not found on %s' % (function, obj))
def _config_comp(self, key, conf_func, suppress=True): """ Configure a component """ _debug('upyhome::_config_comp::{0}'.format(key)) if key in self._config: if self._config[key]: comp_config = self._config[key] #Check if component is disabled if isinstance(comp_config, dict): if not 'disable' in comp_config: conf_func(comp_config) elif isinstance(comp_config, list): cmp_confs = [ cc for cc in comp_config if not 'disable' in cc ] conf_func(cmp_confs) if suppress: del self._config[key]
def exec(self, method, topic=None, data=None): """ Execute a comp method, mainly used from the outside world aka REPL """ _debug('exec %s %s %s' % (method, topic, data)) try: if method == 'broadcast': self._context['topic'] = topic self._push(data) else: comps = None if topic is None: comps = self._comps.values() else: if topic in self._comps: comps = [self._comps[topic]] if comps is not None: for comp in comps: self._exec_func(comp, method, data) return True except Exception as ex: log_error('{0}@upyhome::exec'.format(ex)) return False