aboutsummaryrefslogtreecommitdiffstats
path: root/gui/odr-dabmux-gui.py
blob: c469b352bedceabd5943b4eff93c78afcf2ff6ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#   Copyright (C) 2016
#   Matthias P. Braendli, matthias.braendli@mpb.li
#
#    http://www.opendigitalradio.org
#
#   This is a management server for ODR-DabMux, and it will become much
#   more interesting in the future.
#
#   Run this script and connect your browser to
#   http://localhost:8000 to show the currently running
#
#   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 <http://www.gnu.org/licenses/>.

import os
import argparse
import json
import cherrypy
import muxconfig
import muxrc
import jinja2

class Root:
    def __init__(self, env, conf, rc):
        self.mconf = conf
        self.mrc = rc
        self.env = env
        self.mparam = ModuleParameter(env, rc)

    def _cp_dispatch(self, vpath):
        if len(vpath) == 3:
            vpath.pop(0) # /rc/
            cherrypy.request.params['module'] = vpath.pop(0)  # /module name/
            cherrypy.request.params['param'] = vpath.pop(0) # /parameter name/
            return self.mparam

        return vpath

    @cherrypy.expose
    def config(self, config=None):
        if config == None:
            """Show the JSON ptree in a textbox for editing"""
            self.mconf.load()
            tmpl = self.env.get_template('configeditor.tpl')
            return tmpl.render(
                version = self.mconf.get_mux_version(),
                config  = json.dumps(self.mconf.get_full_configuration(), indent=4),
                message = "")
        else:
            """Record the new configuration"""
            success = self.mconf.set_full_configuration(config)
            if success:
                successmessage = "Success"
            else:
                successmessage = "Failure"
            self.mconf.load()
            return template('configeditor',
                version = self.mconf.get_mux_version(),
                config  = json.dumps(self.mconf.get_full_configuration(), indent=4),
                message = successmessage)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def config_json(self):
        """Return a application/json containing the full
        ptree of the mux"""

        self.mconf.load()
        return { 'version': self.mconf.get_mux_version(),
                 'config': self.mconf.get_full_configuration() }

    @cherrypy.expose
    def index(self):
        self.mconf.load()
        self.mrc.load()
        tmpl = self.env.get_template('index.tpl')
        return tmpl.render(
            version     = self.mconf.get_mux_version(),
            g           = self.mconf.get_general_options(),
            services    = self.mconf.get_services(),
            subchannels = self.mconf.get_subchannels(),
            components  = self.mconf.get_components(),
            rcmodules   = self.mrc.get_modules())

    @cherrypy.expose
    def services(self):
        self.mconf.load()
        tmpl = self.env.get_template('services.tpl')
        return tmpl.render(
            version     = self.mconf.get_mux_version(),
            services    = self.mconf.get_services())

    @cherrypy.expose
    def stats(self):
        self.mconf.load()
        tmpl = self.env.get_template('stats.tpl')
        return tmpl.render(
            version     = self.mconf.get_mux_version())


    @cherrypy.expose
    @cherrypy.tools.json_out()
    def stats_json(self):
        return self.mconf.get_stats_dict()

class ModuleParameter:
    def __init__(self, env, rc):
        self.mrc = rc
        self.env = env

    @cherrypy.expose
    def index(self, module, param, newvalue=None):
        if newvalue != None:
            rc.set_param_value(module, param, newvalue)
            raise cherrypy.HTTPRedirect('/#rcmodules')
        else:
            self.mrc.load()
            value = self.mrc.get_param_value(module, param)

            if param in paramObj:
                paramList = paramObj[param]
                label = paramObj["labels"][param]
            else:
                paramList = []
                label = ""

            tmpl = self.env.get_template('rcparam.tpl')
            return tmpl.render(
                module    = module,
                param     = param,
                value     = value,
                label     = label,
                list      = paramList)

if __name__ == '__main__':
    # Get configuration file in argument
    parser = argparse.ArgumentParser(description='management server for ODR-DabMux')
    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 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()

    # Instanciate mux-configuration and mux-remote-control objects
    conf = muxconfig.ConfigurationHandler(cli_args.mhost, int(cli_args.mport))
    rc = muxrc.MuxRemoteControl(cli_args.mhost, int(cli_args.rcport))

    # Import selectable paramaters values
    paramFile = open("rcparam.json")
    paramStr = paramFile.read()
    paramObj = json.loads(paramStr)

    # Start cherrypy
    env = jinja2.Environment(loader=jinja2.FileSystemLoader('views'), trim_blocks=True)
    cherrypy.config.update({'server.socket_host': cli_args.host, 'server.socket_port': int(cli_args.port),})
    appconf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './static'
        }
    }
    cherrypy.quickstart(Root(env, conf, rc), '/', appconf)