def listen(function): """Subscribes to ZeroMQ messages, and adds received measurements to the database. Messages are dictionaries dumped in JSON format. """ LOG.info('Listening to %s' % cfg.CONF.probes_endpoint) context = zmq.Context.instance() subscriber = context.socket(zmq.SUB) if not cfg.CONF.watch_probe: subscriber.setsockopt(zmq.SUBSCRIBE, '') else: for probe in cfg.CONF.watch_probe: subscriber.setsockopt(zmq.SUBSCRIBE, probe + '.') for endpoint in cfg.CONF.probes_endpoint: subscriber.connect(endpoint) while True: [probe, message] = subscriber.recv_multipart() measurements = json.loads(message) if not isinstance(measurements, dict): LOG.error('Bad message type (not a dict)') elif cfg.CONF.signature_checking and \ not security.verify_signature(measurements, cfg.CONF.driver_metering_secret): LOG.error('Bad message signature') else: try: probe = measurements['probe_id'].encode('utf-8') function(probe, float(measurements['w'])) except (TypeError, ValueError): LOG.error('Malformed power consumption data: %s' % measurements['w']) except KeyError: LOG.error('Malformed message (missing required key)')
def listen(function): """Subscribes to ZeroMQ messages, and adds received measurements to the database. Messages are dictionaries dumped in JSON format. """ LOG.info('Listening to %s' % cfg.CONF.probes_endpoint) context = zmq.Context.instance() subscriber = context.socket(zmq.SUB) if not cfg.CONF.watch_probe: subscriber.setsockopt(zmq.SUBSCRIBE, '') else: for probe in cfg.CONF.watch_probe: subscriber.setsockopt(zmq.SUBSCRIBE, probe + '.') for endpoint in cfg.CONF.probes_endpoint: subscriber.connect(endpoint) while True: [probe, message] = subscriber.recv_multipart() measurements = json.loads(message) if not isinstance(measurements, dict): LOG.error('Bad message type (not a dict)') elif cfg.CONF.signature_checking and \ not security.verify_signature(measurements, cfg.CONF.driver_metering_secret): LOG.error('Bad message signature') else: try: """ Measurement format: * probe_id * timestamp: time of the measure * measure: data retrieved by probes * data_type: type of measure (power, network...) * params: additional informations """ probe = measurements['probe_id'].encode('utf-8') timestamp = measurements['timestamp'] measure = measurements['measure'] params = measurements['data_type'] data_type = params['name'] probe_names = ast.literal_eval(measurements['probes_names']) for key in measurements.keys(): if not key in ['probe_id', 'timestamp', 'measure', 'data_type', 'message_signature']: params[key] = measurements[key] function(probe, probe_names, data_type, timestamp, measure, params) except (TypeError, ValueError): raise LOG.error('Malformed data: %s' % measurements) except KeyError: LOG.error('Malformed message (missing required key)')