def __init__(self): self.node = IsacNode('demo-zwave-react') green.signal(signal.SIGTERM, partial(self.sigterm_handler)) self.action_value = IsacValue(self.node, 'zwave://0xdefbc93b.power_strip001/switch_binary/1/switch', survey_last_value=False, survey_static_tags=False) self.sensor_value = IsacValue(self.node, 'zwave://0xdefbc93b.13/alarm/access_control', survey_last_value=False, survey_static_tags=False) self.sensor_value.observers += self.value_update
def __init__(self, bridges): self.isac_node = IsacNode('alidron-hue') green.signal(signal.SIGTERM, partial(self._sigterm_handler)) green.signal(signal.SIGINT, partial(self._sigterm_handler)) self.bridges = bridges self.signals = {} self.sync_signals()
def __init__(self, port): self.isac_node = IsacNode('alidron-tic') green.signal(signal.SIGTERM, partial(self._sigterm_handler)) green.signal(signal.SIGINT, partial(self._sigterm_handler)) self.ser = serial.Serial( port=port, baudrate=1200, bytesize=serial.SEVENBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1 ) self.signals = {}
else: pub.send(iv.uri) def make_value(uri): values[uri] = IsacValue(n, uri, survey_last_value=False, survey_static_tags=False) values[uri].observers += isac_update make_value('action://nucleo-sensor-demo/led/blue/toggle') make_value('action://nucleo-sensor-demo/led/blue/on') make_value('action://nucleo-sensor-demo/led/blue/off') def sigterm_handler(node): node.shutdown() sys.exit(0) green.signal(signal.SIGTERM, partial(sigterm_handler, n)) # contiki-zmtp part ctx = zmq.Context() pub = ctx.socket(zmq.PUB) pub.setsockopt(zmq.IPV6, 1) pub.connect('tcp://aaaa::600:fbff:a2df:5d20:8888') time.sleep(1) sub = ctx.socket(zmq.SUB) sub.setsockopt(zmq.IPV6, 1) sub.setsockopt(zmq.SUBSCRIBE, '') sub.connect('tcp://aaaa::600:fbff:a2df:5d20:9999')
def __init__(self, config): logger.info('Starting') self.config = config buffer_path = self.config['buffer']['path'] if not os.path.exists(os.path.dirname(buffer_path)): os.makedirs(os.path.dirname(buffer_path)) dsn = InfluxDBArchiver.make_DSN(**self.config['archiver-user']) for i in range(2): self._client = InfluxDBClient.from_DSN(dsn, password=self.config['archiver-user']['password']) try: self._client.query('SHOW MEASUREMENTS') break except InfluxDBClientError as ex: if ex.code == 401: logger.error(ex) logger.warning('Could not connect as user %s, trying as root to setup the DB', self.config['archiver-user']['username']) self._create_user() elif ex.message.startswith('database not found'): logger.warning('Could not find database %s, creating it', self.config['archiver-user']['db']) self._create_db() else: raise logger.info('Connected to DB with %s', dsn) self.isac_node = IsacNode('alidron-archiver-influxdb') green.signal(signal.SIGTERM, partial(self._sigterm_handler)) green.signal(signal.SIGINT, partial(self._sigterm_handler)) self.signals = {} query = 'SELECT * FROM /.*/ GROUP BY authority, path ORDER BY time DESC LIMIT 1' logger.debug('Doing query: %s', query) raw_data = self._client.query(query) logger.debug('Raw data: %s', pf(raw_data.items())) metadata = {} def _make_uri(meas, tags): uri_str = uricompose(scheme=meas, authority=tags['authority'], path=tags['path']) return uri_str, urisplit(uri_str) for meas_tags, fields in raw_data.items(): uri_str, uri = _make_uri(*meas_tags) if uri.scheme == 'metadata': raw_metadata = fields.next() uri_str = uri_str.replace('metadata', raw_metadata['scheme'], 1) metadata[uri_str] = {} for key, value in raw_metadata.items(): if key.startswith('d_') or key.startswith('s_') or key in ['time', 'scheme']: continue if (key.startswith('value')) and (value is None): continue if key.startswith('json_'): if value is None: metadata[uri_str][key[len('json_'):]] = None else: try: metadata[uri_str][key[len('json_'):]] = json.loads(str(value)) except ValueError: logger.error('Wrong JSON for %s at key %s: %s', uri_str, key, str(value)) continue else: metadata[uri_str][key] = value logger.debug('Read metadata for %s: %s', uri_str, metadata[uri_str]) for meas_tags, data in raw_data.items(): uri_str, uri = _make_uri(*meas_tags) if uri.scheme == 'metadata': continue last_point = data.next() try: ts = datetime.strptime(last_point['time'], '%Y-%m-%dT%H:%M:%S.%fZ') except ValueError: ts = datetime.strptime(last_point['time'], '%Y-%m-%dT%H:%M:%SZ') static_tags = {} dynamic_tags = {} for k, v in last_point.items(): if k.startswith('s_'): static_tags[k[2:]] = v elif k.startswith('d_'): dynamic_tags[k[2:]] = v logger.debug('For URI %s: %s, %s', uri_str, ts, pf(last_point)) logger.debug('Decoded tags: %s, %s', static_tags, dynamic_tags) self.signals[uri_str] = InfluxDBArchivedValue( self.isac_node, uri_str, initial_value=(self._type_from_db_to_py(last_point), ts), static_tags=static_tags, dynamic_tags=dynamic_tags, observers=Observable([self._notify]), metadata=metadata.get(uri_str, None), survey_last_value=False, survey_static_tags=False, influxdb_client=self._client, ) self.signals[uri_str].metadata_observers += self._notify_metadata green.spawn(self.signals[uri_str].survey_metadata) logger.warning('Discovered %s', uri_str) logger.warning('Done loading existing signals') self.isac_node.register_isac_value_entering(self._new_signal) signal_uris = self.isac_node.survey_value_uri('.*') map(partial(self._new_signal, ''), signal_uris)
self.network.stop() logger.info('Stopped network') self.network.destroy() logger.info('Destroyed network') self.isac_node.shutdown() logger.info('Stopped ISAC node') def sigterm_handler(alidron_ozw): logger.info('Received SIGTERM signal, exiting') alidron_ozw.shutdown() logger.info('Exiting') sys.exit(0) if __name__ == '__main__': DEVICE = sys.argv[1] isac_node = IsacNode('alidron-openzwave-controller') alidron_ozw = AlidronOZW(DEVICE, isac_node) green.signal(signal.SIGTERM, partial(sigterm_handler, alidron_ozw)) try: isac_node.serve_forever() except KeyboardInterrupt: alidron_ozw.shutdown() green.sleep(1)