summaryrefslogtreecommitdiffstats
path: root/gui/odr-dabmux-gui.py
blob: abfd124040fa93493037847d59d421368593102f (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
#!/usr/bin/python
#
#   Copyright (C) 2015
#   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/>.

from bottle import route, run, template, static_file
import socket
import json
import collections

class Service(object):
    def __init__(self, name, ptree):
        self.name = name
        self.label = ptree['label']
        if 'shortlabel' in ptree:
            self.shortlabel = ptree['shortlabel']
        else:
            self.shortlabel = ""
        self.srvid = ptree['id']


class Subchannel(object):
    def __init__(self, name, ptree):
        self.name = name
        for fieldname in ['type',
                "inputfile",
                "zmq-buffer",
                "zmq-prebuffering",
                "bitrate",
                "id",
                "protection",
                "encryption",
                "secret-key",
                "public-key",
                "encoder-key"]:
            if fieldname in ptree:
                setattr(self, fieldname.replace("-", "_"), ptree[fieldname])
            else:
                setattr(self, fieldname.replace("-", "_"), "")

class Component(object):
    def __init__(self, name, ptree):
        self.name = name
        for fieldname in ['label', 'shortlabel', 'service',
                'subchannel', 'figtype']:
            if fieldname in ptree:
                setattr(self, fieldname.replace("-", "_"), ptree[fieldname])
            else:
                setattr(self, fieldname.replace("-", "_"), "")

def get_mgmt_ptree():
    HOST = 'localhost'
    PORT = 12720
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.sendall(b'getptree\n')
    server_info = s.recv(32768)
    config_info = s.recv(32768)
    s.close()

    server_version = json.loads(server_info.decode())['service']
    config = json.loads(config_info.decode())

    return {'version': server_version, 'config': config}

@route('/config')
def config():
    return get_mgmt_ptree()

@route('/')
def index():
    ptree = get_mgmt_ptree()
    version = ptree['version']
    srv_pt = ptree['config']['services']
    services = [Service(name, srv_pt[name]) for name in srv_pt]

    sub_pt = ptree['config']['subchannels']
    subchannels = [Subchannel(name, sub_pt[name]) for name in sub_pt]

    comp_pt = ptree['config']['components']
    components = [Component(name, comp_pt[name]) for name in comp_pt]

    return template('index',
            version=version,
            services=services,
            components=components,
            subchannels=subchannels)


@route('/static/<filename:path>')
def send_static(filename):
    return static_file(filename, root='./static')

run(host='localhost', port=8000, debug=True, reloader=True)