summaryrefslogtreecommitdiffstats
path: root/gui/muxrc.py
blob: 3bda0468a37cf9b3093cac6d7f8e0dff2952cb42 (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
#!/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 <http://www.gnu.org/licenses/>.
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

    def get_param_value(self, module, param):
        value = self.zRead([b'get', module, param])
        if value[0] == b'fail':
            raise ValueError("Error getting param: {}".format(value[1]))
        else:
            return value[0]

    def set_param_value(self, module, param, value):
        ret = self.zRead([b'set', module, param, value])
        if ret[0] == b'fail':
            raise ValueError("Error getting param: {}".format(ret[1]))