def _register_channels(self): inputs = DOORPI.config(self._conf + '/inputs', [], function='keys') logger.info('%s has %s inputs', self.fullname, len(inputs)) for input_name in inputs: if input_name in self._inputs: raise InputNameAlreadyExistsException() input_id = DOORPI.generate_id(prefix='Input_') self._inputs[input_id] = SingleChannel( id=input_id, name=input_name, technical_name=DOORPI.config( '%s/inputs/%s/technical_name' % (self._conf, input_name), input_name), initial_value=DOORPI.config( '%s/inputs/%s/initial_value' % (self._conf, input_name), False), log=DOORPI.config( '%s/inputs/%s/log' % (self._conf, input_name), True), ) outputs = DOORPI.config(self._conf + '/outputs', [], function='keys') logger.info('%s has %s outputs', self.fullname, len(inputs)) for output_name in outputs: if output_name in self._outputs: raise OutputNameAlreadyExistsException() output_id = DOORPI.generate_id(prefix='Output_') self._outputs[output_id] = SingleChannel( id=output_id, name=output_name, technical_name=DOORPI.config( '%s/outputs/%s/technical_name' % (self._conf, output_name), output_name), initial_value=DOORPI.config( '%s/outputs/%s/initial_value' % (self._conf, output_name), False), log=DOORPI.config( '%s/outputs/%s/log' % (self._conf, output_name), True), high_level=DOORPI.config( '%s/outputs/%s/high_level' % (self._conf, output_name), True), low_level=DOORPI.config( '%s/outputs/%s/low_level' % (self._conf, output_name), False), high_by_event=DOORPI.config( '%s/outputs/%s/high_by_event' % (self._conf, output_name), None), low_by_event=DOORPI.config( '%s/outputs/%s/low_by_event' % (self._conf, output_name), None), )
def __init__(self, callback, id=None, **kwargs): self._id = id or DOORPI.generate_id(prefix='Action_') self._callback = callback self._kwargs = kwargs if len(self.__class__.__bases__) is 0: self.action_name = str(callback) else: self.action_name = self.__class__.__name__
class InterfaceBaseClass(object): _id = DOORPI.generate_id(prefix='Interface_') _conf = None _name = '' _destroyed = False @property def id(self): return self._id @property def is_destroyed(self): return self._destroyed @property def fullname(self): return "[%s] %s (%s.%s)" % (self._id, self._name, self.module_name, self.class_name) @property def name(self): return self._name @property def module_name(self): return self.__class__.__module__ @property def class_name(self): return self.__class__.__name__ @property def interface_info(self): return { 'class_name': self.class_name, 'module_name': self.module_name, 'interface_name': self.name, 'interface_id': self.id #'channel' : self.last_key } def interface_name_variations(self, prefix='', postfix=''): return [ "", "%s%s%s" % (prefix, self.name, postfix), "%s%s%s" % (prefix, self.module_name, postfix), "%s%s.%s%s" % (prefix, self.module_name, self.name, postfix) ] def __init__(self, name, config_path): self._name = name self._conf = config_path self._register_destroy_action() def start(self): logger.debug('%s starting', self.fullname) def stop(self): logger.debug('%s stopping', self.fullname) DOORPI.events.unregister_source(self._id) def _register_destroy_action(self, stop_function=None): if stop_function is None: stop_function = self.stop return DOORPI.events.register_action( InterfaceStopAction(stop_function), 'OnShutdown')
def fire_event_synchron(self, event_source, event_name, kwargs=None): if self._destroy and event_source != __name__: return False log = self.log_for_event(event_name) event_fire_id = DOORPI.generate_id(prefix='Event_') if kwargs is None: kwargs = {} kwargs.update({ 'last_fired': time.time(), 'last_fired_from': event_source, 'event_fire_id': event_fire_id }) message = '' if event_name not in self._events.keys(): message = 'unknown event' elif event_source not in self._events[event_name].sources: message = 'unknown source for this event' elif len(self._events[event_name].actions) == 0: message = 'no actions for this event' if message != '': message = '[%s] %s - skip fire event %s from %s' % ( event_fire_id, message, event_name, event_source) if log: logger.info(message) return message if log: logger.debug( "[%s] fire for event %s from %s this actions %s with kwargs %s", event_fire_id, event_name, event_source, self._events[event_name].actions, kwargs) for action_id in self._events[event_name].actions: if action_id not in self._actions.keys(): logger.error( '[%s] missing action reference for action_id %s by event %s', event_fire_id, action_id, event_name) continue if log: logger.debug("[%s] try to fire action %s", event_fire_id, self._actions[action_id]) try: result = self._actions[action_id].run(**kwargs) if not result and log: logger.warning('[%s] action %s returns %s', event_fire_id, self._actions[action_id], result) if self._actions[action_id].single_fire_action is True: self.unregister_action(action_id) except SystemExit as exp: logger.info( '[%s] Detected SystemExit and shutdown DoorPi (Message: %s)', event_fire_id, exp) DOORPI.stop() except KeyboardInterrupt as exp: logger.info( "[%s] Detected KeyboardInterrupt and shutdown DoorPi (Message: %s)", event_fire_id, exp) DOORPI.stop() except: logger.exception( "[%s] error while fire action %s for event_name %s", event_fire_id, self._actions[action_id], event_name) if log: logger.debug("[%s] finished fire_event for event %s from %s", event_fire_id, event_name, event_source) return True