def emit_service_state_event(svc_name, state): _logger.debug("emit_service_state_event(%s, %s)", svc_name, state) from pycstbox import evtmgr from dbus import DBusException # don't try to emit events for the event manager (egg and chicken problem) if svc_name == evtmgr.SERVICE_NAME: return if state not in SVC_STATE_NAMES: raise ValueError('invalid service state (%s)' % state) global _evtmgr if not _evtmgr: _evtmgr = evtmgr.get_object(evtmgr.FRAMEWORK_EVENT_CHANNEL) if not _evtmgr: _logger.error('service state event not emitted : unable to get event manager service') return False try: _evtmgr.emitEvent(SVC_EVENT_VAR_TYPE, svc_name, json.dumps({'state': state, "state_str": SVC_STATE_NAMES[state]})) except DBusException as e: # log the error but don't choke on it, since it can happen during the shutdown sequence due to some # race condition with the involved peers (f.i. the private bus daemon is already stopped when we try # to emit our stop notification) _logger.error( 'unexpected DBus error %s while sending the service state event "%s"', e, SVC_STATE_NAMES[state] ) if state == SVC_STOPPED: _logger.error('(can be safely ignored if during the global shutdown sequence)')
def _on_message(self, client, user_data, message): if self._inbound_adapter: with self._lock: now = int(time.time() * 1000) for event, channel in self._inbound_adapter.handle_message(client, user_data, message): try: publisher = self._cbx_publishers[channel] except KeyError: self._cbx_publishers[channel] = publisher = evtmgr.get_object(channel) publisher.emitFullEvent(now, event.var_type, event.var_name, event.data)
def start(self): """ Service objet runtime initialization """ self.log_info('starting svcobj for channel %s', self._channel) self._dao.open() try: svc = evtmgr.get_object(self._channel) except dbus.exceptions.DBusException as e: self.log_error(e) raise RuntimeError('evtmgr service object connection failed') else: svc.connect_to_signal("onCSTBoxEvent", self._event_signal_handler, dbus_interface=evtmgr.SERVICE_INTERFACE) self.log_info('connected to EventManager onCSTBoxEvent signal')
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the services discovery process. settings expected content: - config_path : automation scenarios configuration file """ if not logger: logger = log.getLogger('wsapi.homeautomation') _handlers_initparms['logger'] = logger _handlers_initparms['settings'] = settings logger.info('connecting to Event Manager...') evt_mgr = evtmgr.get_object(evtmgr.CONTROL_EVENT_CHANNEL) if not evt_mgr: raise Exception('cannot get event manager access for channel %s' % evtmgr.CONTROL_EVENT_CHANNEL) _handlers_initparms['events_mgr'] = evt_mgr logger.info('success')
def emit_service_state_event(svc_name, state): _logger.debug("emit_service_state_event(%s, %s)", svc_name, state) from pycstbox import evtmgr from dbus import DBusException # don't try to emit events for the event manager (egg and chicken problem) if svc_name == evtmgr.SERVICE_NAME: return if state not in SVC_STATE_NAMES: raise ValueError('invalid service state (%s)' % state) global _evtmgr if not _evtmgr: _evtmgr = evtmgr.get_object(evtmgr.FRAMEWORK_EVENT_CHANNEL) if not _evtmgr: _logger.error( 'service state event not emitted : unable to get event manager service' ) return False try: _evtmgr.emitEvent( SVC_EVENT_VAR_TYPE, svc_name, json.dumps({ 'state': state, "state_str": SVC_STATE_NAMES[state] })) except DBusException as e: # log the error but don't choke on it, since it can happen during the shutdown sequence due to some # race condition with the involved peers (f.i. the private bus daemon is already stopped when we try # to emit our stop notification) _logger.error( 'unexpected DBus error %s while sending the service state event "%s"', e, SVC_STATE_NAMES[state]) if state == SVC_STOPPED: _logger.error( '(can be safely ignored if during the global shutdown sequence)' )
def start(self): """ Starts the service object. """ self.log_info('starting...') # connect to CSTBox event channels for channel, adapter in self._outbound_adapters.iteritems(): try: svc = evtmgr.get_object(channel) except DBusException as e: msg = "cannot connect to event channel : %s (%s)" % (channel, e) self.log_error(msg) raise MQTTGatewayError(msg) else: svc.connect_to_signal("onCSTBoxEvent", self._channel_event_handler(channel), dbus_interface=evtmgr.SERVICE_INTERFACE) self.log_info('connected to event channel : %s', channel) # connect MQTT message handler and start the client self._mqttc.on_message = self._on_message self._mqttc.run() self.log_info('started.')
from pycstbox import evtmgr from pycstbox.obix import OBIXConnector, OBIXConnectorError __author__ = 'Eric PASCUAL - CSTB ([email protected])' if __name__ == '__main__': parser = cli.get_argument_parser(description="oBIX connector daemon") cli.add_config_file_option_to_parser(parser, dflt_name=OBIXConnector.DEFAULT_CONFIG_NAME) args = parser.parse_args() log_level = getattr(log, args.loglevel) dbuslib.dbus_init() em = evtmgr.get_object(evtmgr.SENSOR_EVENT_CHANNEL) try: polling_daemon = OBIXConnector(args.config_path, em, log_level) def sigterm_handler(*args, **kwargs): polling_daemon.terminate() signal.signal(signal.SIGTERM, sigterm_handler) signal.signal(signal.SIGINT, sigterm_handler) try: polling_daemon.start() except Exception as e: sys.exit(1) else: while polling_daemon.is_alive():