From 77b399804d0b7f3bdabcf8c1adcd95beafd9c8ad Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 7 Oct 2016 17:35:54 +0200 Subject: Add RC readout in web gui --- gui/muxrc.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ gui/odr-dabmux-gui.py | 12 +++++-- gui/views/index.tpl | 15 +++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 gui/muxrc.py diff --git a/gui/muxrc.py b/gui/muxrc.py new file mode 100644 index 0000000..7644ae4 --- /dev/null +++ b/gui/muxrc.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2016 +# Matthias P. Braendli, matthias.braendli@mpb.li +# +# http://www.opendigitalradio.org +# +# This file is part of ODR-DabMux. +# +# ODR-DabMux is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# ODR-DabMux is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ODR-DabMux. If not, see . +import zmq +import json + +class RCParameter(object): + def __init__(self, param, value): + self.param = param + self.value = value + +class RCModule(object): + """Container object for RC module""" + def __init__(self, name): + self.name = name + self.parameters = [] + +class MuxRemoteControl(object): + """Interact with ODR-DabMux using the ZMQ RC""" + + def __init__(self, mux_host, mux_port=12722): + self._host = mux_host + self._port = mux_port + self._ctx = zmq.Context() + + self.module_list = [] + + def zRead(self, message_parts): + sock = zmq.Socket(self._ctx, zmq.REQ) + sock.setsockopt(zmq.LINGER, 0) + sock.connect("tcp://{}:{}".format(self._host, self._port)) + + for i, part in enumerate(message_parts): + if i == len(message_parts) - 1: + f = 0 + else: + f = zmq.SNDMORE + + print("Send {} {}".format(i, part)) + sock.send(part, flags=f) + + print("Poll") + + # use poll for timeouts: + poller = zmq.Poller() + poller.register(sock, zmq.POLLIN) + if poller.poll(5*1000): # 5s timeout in milliseconds + recv = sock.recv_multipart() + print("RX {}".format(recv)) + sock.close() + return recv + else: + raise IOError("Timeout processing ZMQ request") + + def load(self): + """Load the list of RC modules""" + module_names = self.zRead([b'list']) + + self.module_list = [] + + for name in module_names: + mod = RCModule(name) + module_params = self.zRead([b'show', name]) + + for param in module_params: + p, v = param.split(': ') + mod.parameters.append(RCParameter(p, v)) + + self.module_list.append(mod) + + def get_modules(self): + return self.module_list + diff --git a/gui/odr-dabmux-gui.py b/gui/odr-dabmux-gui.py index 85098f8..7e3cb3e 100755 --- a/gui/odr-dabmux-gui.py +++ b/gui/odr-dabmux-gui.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # -# Copyright (C) 2015 +# Copyright (C) 2016 # Matthias P. Braendli, matthias.braendli@mpb.li # # http://www.opendigitalradio.org @@ -28,6 +28,7 @@ # along with ODR-DabMux. If not, see . from muxconfig import * +from muxrc import * from bottle import route, run, template, static_file, request import json @@ -80,13 +81,15 @@ def config_json_get(): @route('/') def index(): conf.load() + rc.load() return template('index', version = conf.get_mux_version(), g = conf.get_general_options(), services = conf.get_services(), subchannels = conf.get_subchannels(), - components = conf.get_components()) + components = conf.get_components(), + rcmodules = rc.get_modules()) @route('/services') def index(): @@ -119,10 +122,13 @@ if __name__ == '__main__': parser.add_argument('--host', default='127.0.0.1', help='socket host (default: 127.0.0.1)',required=False) parser.add_argument('--port', default='8000', help='socket port (default: 8000)',required=False) parser.add_argument('--mhost', default='127.0.0.1', help='mux host (default: 127.0.0.1)',required=False) - parser.add_argument('--mport', default='12720', help='mux port (default: 12720)',required=False) + parser.add_argument('--mport', default='12720', help='mux management server port (default: 12720)',required=False) + parser.add_argument('--rcport', default='12722', help='mux zmq rc port (default: 12722)',required=False) cli_args = parser.parse_args() conf = ConfigurationHandler(cli_args.mhost, cli_args.mport) + rc = MuxRemoteControl(cli_args.mhost, cli_args.rcport) + run(host=cli_args.host, port=int(cli_args.port), debug=True, reloader=False) diff --git a/gui/views/index.tpl b/gui/views/index.tpl index c38d827..5c44c61 100644 --- a/gui/views/index.tpl +++ b/gui/views/index.tpl @@ -14,6 +14,7 @@
  • Services
  • Subchannels
  • Components
  • +
  • RC Modules
  • @@ -52,6 +53,20 @@ % end
    +
    +

    RC Modules

    +
      + % for m in rcmodules: +
    • {{m.name}} +
        + % for p in m.parameters: +
      • {{p.param}} : {{p.value}}
      • + % end +
      +
    • + % end +
    +
    -- cgit v1.2.3