LiquidSoapCommunicator.py 3.88 KB
Newer Older
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
from modules.communication.liquidsoap.LiquidSoapPlayerClient import LiquidSoapPlayerClient

class LiquidSoapCommunicator:
    lqs = None
    debug = None

    def __init__(self, debug=False):
        # Der Liquidsoap Client
        lqs_socket = "/home/gg/PycharmProjects/aura/modules/liquidsoap/simplestmixer.sock"
        self.lqs = LiquidSoapPlayerClient(lqs_socket)
        self.debug = debug

    def switchmixernumber(self, mixernumber, activate=True):
        return self.sendLqcCommand(self.lqs, "mixer", "select", mixernumber, activate)

    def getactivemixer(self):
        activeinputs = []

        self.enableTransaction()
        self.openConn(self.lqs)

        inputs = self.__getmixerinputs__()

        cnt = 0
        for input in inputs:
            status = self.__getmixerstatus__(cnt)

            if "selected=true" in status:
                activeinputs.append(input)

            cnt = cnt + 1

        self.disableTransaction()
        self.closeConn(self.lqs)

        return activeinputs

    def getmixerstatus(self):
        inputstate = {}

        self.enableTransaction()
        self.openConn(self.lqs)

        inputs = self.__getmixerinputs__()

        cnt = 0
        for input in inputs:
            inputstate[input] = self.__getmixerstatus__(cnt)
            cnt = cnt + 1

        self.disableTransaction()
        self.closeConn(self.lqs)

        return inputstate

    def __getmixerinputs__(self):
        return self.sendLqcCommand(self.lqs, "mixer", "inputs")

    def __getmixerstatus__(self, mixernumber):
        return self.sendLqcCommand(self.lqs, "mixer", "status", mixernumber)

    # ------------------------------------------------------------------------------------------ #
    def getClient(self):
        return self.lqs

    # ------------------------------------------------------------------------------------------ #
    def sendLqcCommand(self, lqs_instance, namespace, command, *args):
        """
        Ein Kommando an Liquidsoap senden
        @type  lqs_instance: object
        @param lqs_instance: Instance of LiquidSoap Client
        @type  namespace:    string
        @param namespace:    Namespace of function
        @type  command:      string
        @param command:      Function name
        @type args:          list
        @param args:         List of parameters
        @rtype:              string
        @return:             Response from LiquidSoap
        """
        try:
            # connect
            if self.transaction == False:
                self.openConn(lqs_instance)

            # call wanted function ...
            func = getattr(lqs_instance, namespace)
            # ... and fetch the result
            result = func(command, *args)

            # say byebye
            if self.transaction == False:
                self.closeConn(lqs_instance)

        except Exception as e:
            # Verbindung gescheitert - Fehler an Client
            if command.find('record') > -1:
                raise e
                #self.fatal('02')
            else:
                raise e
                #self.fatal('01')
            print(e)
            self.notifyClient()
            # Instanz/Thread zerstören - aufrufende Funktion wird nicht weiter abgearbeitet
            del self
        else:
            # byebye = getattr(lqs_instance, 'byebye')
            # byebye()
            if self.debug:
                if(len(args) == 0):
                    print("response to lqc command [" + namespace + "." + command + "] is " + str(result))
                else:
                    print("response to lqc command [" + namespace + "." + command + " " + str(args) +"] is "+str(result))
            return result

    def enableTransaction(self):
        self.transaction = True

    def disableTransaction(self):
        self.transaction = False

    def openConn(self, socket):
        socket.connect()

    def closeConn(self, socket):
        socket.byebye()