Multiple measurements support (volts, amperes, watts).

Drivers send dictionaries dumped in JSON format (instead of probe_id:value format).
Driver method "send_value" is renamed to "send_measurements".
This commit is contained in:
François Rossigneux 2012-12-04 14:11:11 +01:00
parent c046dd0880
commit eb9a496c8b
4 changed files with 23 additions and 16 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json
import threading import threading
import time import time
@ -90,8 +91,8 @@ class Collector:
self.timer.start() self.timer.start()
def listen(self, conf): def listen(self, conf):
"""Subscribes to ZeroMQ messages, and adds received values to the database. """Subscribes to ZeroMQ messages, and adds received measurements to the database.
Message format is "probe:value". Messages are dictionaries dumped in JSON format.
""" """
LOG.info('Collector listenig to %s' % conf.probes_endpoint) LOG.info('Collector listenig to %s' % conf.probes_endpoint)
@ -103,11 +104,11 @@ class Collector:
while True: while True:
message = subscriber.recv() message = subscriber.recv()
data = message.split(':') measurements = json.loads(message)
if len(data) == 2: if not isinstance(measurements, dict):
try: LOG.error('Bad message type (not a dict)')
self.add(data[0], float(data[1]))
except:
LOG.error('Message format error: %s' % message)
else: else:
LOG.error('Malformed message: %s' % message) try:
self.add(measurements['probe_id'], float(measurements['w']))
except KeyError:
LOG.error('Malformed message (missing required key)')

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json
from threading import Thread, Event from threading import Thread, Event
import zmq import zmq
@ -35,9 +36,10 @@ class Driver(Thread):
"""Returns true if a stop request is pending.""" """Returns true if a stop request is pending."""
return self.stop_request.is_set() return self.stop_request.is_set()
def send_value(self, probe_id, value): def send_measurements(self, probe_id, measurements):
"""Sends a message via ZeroMQ with the following format: probe_id, value.""" """Sends a message via ZeroMQ (dictionary dumped in JSON format)."""
self.publisher.send(probe_id + ':' + str(value)) measurements['probe_id'] = probe_id
self.publisher.send(json.dumps(measurements))
def subscribe(self, observer): def subscribe(self, observer):
"""Appends the observer (callback method) to the observers list.""" """Appends the observer (callback method) to the observers list."""

View File

@ -24,6 +24,9 @@ class Dummy(Driver):
"""Starts the driver thread.""" """Starts the driver thread."""
while not self.stop_request_pending(): while not self.stop_request_pending():
for probe_id in self.probe_ids: for probe_id in self.probe_ids:
value = randrange(self.min_value, self.max_value) measurements = {}
self.send_value(probe_id, value) measurements['w'] = randrange(self.min_value, self.max_value)
measurements['v'] = 230.0
measurements['a'] = measurements['w'] / measurements['v']
self.send_measurements(probe_id, measurements)
time.sleep(1) time.sleep(1)

View File

@ -44,8 +44,9 @@ class Wattsup(Driver):
except SerialException: except SerialException:
self.serial.close() self.serial.close()
self.stop() self.stop()
value = self.extract_watts(packet) measurements = {}
self.send_value(self.probe_ids[0], value) measurements['w'] = self.extract_watts(packet)
self.send_measurements(self.probe_ids[0], measurements)
def get_packet(self): def get_packet(self):
"""Returns the next packet sent by the wattmeter.""" """Returns the next packet sent by the wattmeter."""