Commit 63db906e authored by Swann Perarnau's avatar Swann Perarnau
Browse files

[refactor] Improve power message format

This commit changes the message format for the upstream API, to use a
json-encoded dictionary. While the format is not set in stone at this
point, the goal is to slowly move into a proper protocol, with
well-defined fields to the messages, and proper mechanisms to send
commands and receive notification of their completion.

The only current user of this API is the power management piece, and
this change breaks the GRM code maintained outside of this repo. We will
need to reconcile the two implementation once the message protocol gets
more stable.

Related to #1 and #6.
parent bc1b7fd2
......@@ -57,17 +57,24 @@ class CommandLineInterface(object):
The NRM should answer on the pub socket with an acknowledgment."""
command = "34.56 %g" % argv.level
# build the command as a JSON dict giving enough info. This is an
# idempotent command, so we will repeat the command if we don't get a
# timely answer.
# TODO: check that the level makes a little bit of sense in the first
# place
command = {'command': 'setpower',
'limit': argv.limit,
# now wait for an answer
msg = self.upstream_sub_socket.recv_string()
msg = self.upstream_sub_socket.recv_json()"new message: %r", msg)
fields = msg.split()
if fields[0] == "23.45":"new power: %g", float(fields[1]))
# ignore other messages
if isinstance(msg, dict) and msg.get('type') == 'power':
if msg['limit'] == argv.limit:"command received by the daemon")
def main(self):
parser = argparse.ArgumentParser()
......@@ -86,7 +93,7 @@ class CommandLineInterface(object):
parser_setpower.add_argument("-f", "--follow",
help="listen for power changes",
help="set new power limit",
from __future__ import print_function
import json
import logging
import re
import signal
......@@ -97,16 +98,24 @@ class Daemon(object):
def do_upstream_receive(self, parts):"receiving upstream message: %r", parts) = int(parts[0].split()[1])"target measure: %g",
if len(parts) != 1:
self.logger.error("unexpected msg length, droping it: %r", parts)
msg = json.loads(parts[0])
if isinstance(msg, dict) and msg.get('command') == 'setpower': = float(msg['limit'])"new target measure: %g",
def do_sensor(self):
self.machine_info = self.sensor.do_update()"current state: %r", self.machine_info)
total_power = self.machine_info['energy']['power']['total']
msg = "23.45 %g" % (total_power)
self.upstream_pub.send_string(msg)"Sending power values: %r", msg)
msg = {'type': 'power',
'total': total_power,
self.upstream_pub.send_json(msg)"sending sensor message: %r", msg)
def do_control(self):
total_power = self.machine_info['energy']['power']['total']
......@@ -154,7 +163,7 @@ class Daemon(object):
upstream_sub_filter = "34.56 "
upstream_sub_filter = ""
upstream_sub_socket.setsockopt(zmq.SUBSCRIBE, upstream_sub_filter)"downstream socket bound to: %s",
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment