communicator.py 40.1 KB
Newer Older
1
import os
2
import time
3
4
import codecs
import urllib
5
import logging
6
7
8
import tempfile
import simplejson

9
from modules.base.parsexml import parsexml
10
11
12
from modules.communication.liquidsoap.playerclient import LiquidSoapPlayerClient
from modules.communication.liquidsoap.recorderclient import LiquidSoapRecorderClient
from modules.communication.liquidsoap.initthread import LiquidSoapInitThread
13
14
from modules.communication.mail.mail import AuraMailer

15
16
from libraries.enum.consolecolor import TerminalColors
from libraries.exceptions.auraexceptions import LQConnectionError
17
from libraries.database.broadcasts import TrackService
18
from libraries.exceptions.exception_logger import ExceptionLogger
19
from libraries.enum.scheduleentrytype import ScheduleEntryType
20

21

22
class LiquidSoapCommunicator(ExceptionLogger):
23
    client = None
24
    lqcr = None
25
    logger = None
26
    transaction = 0
27
    channels = None
28
    scheduler = None
29
    error_data = None
30
31
    auramailer = None
    aborttransaction = False
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
32
    connection_attempts = 0
33
34

    # ------------------------------------------------------------------------------------------ #
35
    def __init__(self, config, logger=True):
36
        """
37
38
        Constructor
        """
39
        self.config = config
40
41
        self.logger = logging.getLogger("AuraEngine")

42
43
        self.client = LiquidSoapPlayerClient(config, "engine.sock")
        # self.lqcr = LiquidSoapRecorderClient(config, "record.sock")
44

45
        errors_file = self.config.get("install_dir") + "/errormessages/controller_error.js"
46
47
48
        f = open(errors_file)
        self.error_data = simplejson.load(f)
        f.close()
49

50
        self.auramailer = AuraMailer(self.config)
51

52
53
    # ------------------------------------------------------------------------------------------ #
    def set_volume(self, mixernumber, volume):
54
        return self.__send_lqc_command__(self.client, "mixer", "volume", mixernumber, volume)
55
56
57

    # ------------------------------------------------------------------------------------------ #
    def get_active_mixer(self):
58
59
60
61
        """
        get active mixer in liquidsoap server
        :return:
        """
62
63
        activeinputs = []

64
        # enable more control over the connection
65
        self.enable_transaction()
66

67
        inputs = self.get_all_channels()
68
69
70

        cnt = 0
        for input in inputs:
71
            status = self.__get_mixer_status__(cnt)
72
73
74
75
76
77

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

            cnt = cnt + 1

78
        self.disable_transaction()
79
80
81

        return activeinputs

82
83
84
85
86
87
    # ------------------------------------------------------------------------------------------ #
    def get_active_channel(self):
        """
        gets active channel from programme
        :return:
        """
88
89
        active_entry = self.scheduler.get_active_entry()
        if active_entry is None:
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
90
            return ""
91
        return active_entry.type
92

93
94
    # ------------------------------------------------------------------------------------------ #
    def get_mixer_status(self):
95
96
        inputstate = {}

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
97
        self.enable_transaction()
98

99
        inputs = self.get_all_channels()
100
101
102

        cnt = 0
        for input in inputs:
103
            inputstate[input] = self.__get_mixer_status__(cnt)
104
105
            cnt = cnt + 1

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
106
        self.disable_transaction()
107
108
109

        return inputstate

110
111
112
113
114
115
116
117
    # ------------------------------------------------------------------------------------------ #
    def get_recorder_status(self):
        self.enable_transaction(self.lqcr)
        recorder_state = self.__send_lqc_command__(self.lqcr, "record", "status")
        self.disable_transaction(self.lqcr)

        return recorder_state

118
119
120
121
122
123
124
125
126
    # ------------------------------------------------------------------------------------------ #
    def http_start_stop(self, start):
        if start:
            cmd = "start"
        else:
            cmd = "stop"

        try:
            self.enable_transaction()
127
            self.__send_lqc_command__(self.client, "http", cmd)
128
129
130
131
132
            self.disable_transaction()
        except LQConnectionError as e:
            # we already caught and handled this error in __send_lqc_command__, but we do not want to execute this function further
            pass

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
    # ------------------------------------------------------------------------------------------ #
    def recorder_stop(self):
        self.enable_transaction()

        for i in range(5):
            if self.config.get("rec_" + str(i)) == "y":
                self.__send_lqc_command__(self.client, "recorder_" + str(i), "stop")

        self.disable_transaction()

    # ------------------------------------------------------------------------------------------ #
    def recorder_start(self, num=-1):
        self.enable_transaction()

        if num == -1:
            self.recorder_start_all()
        else:
            self.recorder_start_one(num)

    # ------------------------------------------------------------------------------------------ #
    def recorder_start_all(self):
        for i in range(5):
            self.recorder_start_one(i)

    # ------------------------------------------------------------------------------------------ #
    def recorder_start_one(self, num):
        self.enable_transaction()
        if self.config.get("rec_" + str(num)) == "y":
            returnvalue = self.__send_lqc_command__(self.client, "recorder", str(num), "status")

            if returnvalue == "off":
                self.__send_lqc_command__(self.client, "recorder", str(num), "start")
        self.disable_transaction()
166

167
    # ------------------------------------------------------------------------------------------ #
168
    def activate(self, entry):
169
170
        active_type = self.scheduler.get_active_entry().type

171
172
        try:
            # enable transaction
173
174
            self.enable_transaction()

175
176
177
178
179
180
            if active_type == entry.type:
                # push something to active channel
                self.activate_same_channel(entry)
            else:
                # switch to another channel
                self.activate_different_channel(entry, active_type)
181

182
            # disable conn
183
            self.disable_transaction()
184

185
            # insert playlist entry
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
186
            self.insert_track_service_entry(entry)
187
188
189
        except LQConnectionError as e:
            # we already caught and handled this error in __send_lqc_command__, but we do not want to execute this function further
            pass
190

191
192
193
194
195
196
197
198
199
200
201
202
    # ------------------------------------------------------------------------------------------ #
    def activate_same_channel(self, entry, silent=False):
        if not silent:
            self.logger.info(TerminalColors.PINK.value + entry.type.value + " already active!" + TerminalColors.ENDC.value)

        # push to fs or stream
        if entry.type == ScheduleEntryType.FILESYSTEM:
            self.playlist_push(entry.source)
        if entry.type == ScheduleEntryType.STREAM:
            self.http_start_stop(True)
            self.set_http_url(entry.source)
        # nothing to do when we are live => just leave it as is
203

204
205
206
    # ------------------------------------------------------------------------------------------ #
    def activate_different_channel(self, entry, active_type):
        self.logger.info(TerminalColors.PINK.value + "LiquidSoapCommunicator is activating " + entry.type.value + " & deactivating " + active_type.value + "!" + TerminalColors.ENDC.value)
207

208
        self.activate_same_channel(entry, True)
209

210
211
212
213
214
215
        # set others to zero volume
        others = self.all_inputs_but(entry.type.value)
        for o in others:
            self.channel_volume(o, 0)
        # set active channel to wanted volume
        self.channel_volume(entry.type.value, entry.volume)
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
216
217
218

    # ------------------------------------------------------------------------------------------ #
    def insert_track_service_entry(self, schedule_entry):
219
220
221
222
        trackservice_entry = TrackService()
        trackservice_entry.playlist_id = schedule_entry.playlist_id
        trackservice_entry.entry_num = schedule_entry.entry_num
        trackservice_entry.source = schedule_entry.source
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
223

224
        trackservice_entry.store(add=True, commit=True)
225
226
227

    # ------------------------------------------------------------------------------------------ #
    def all_inputs_but(self, input_type):
228
        try:
229
            activemixer_copy = self.get_all_channels().copy()
230
231
            activemixer_copy.remove(input_type)
        except ValueError:
232
233
234
            self.logger.error("Requested channel not in channellist ")
        except AttributeError:
            self.logger.critical("Channellist is None")
235

236
237
238
239
        return activemixer_copy

    # ------------------------------------------------------------------------------------------ #
    def get_all_channels(self):
240
        if self.channels is None or len(self.channels) == 0:
241
            self.channels = self.__send_lqc_command__(self.client, "mixer", "inputs")
242
243

        return self.channels
244

245
246
247
248
249
    # ------------------------------------------------------------------------------------------ #
    def reload_channels(self):
        self.channels = None
        return self.get_all_channels()

250
251
    # ------------------------------------------------------------------------------------------ #
    def __get_mixer_status__(self, mixernumber):
252
        return self.__send_lqc_command__(self.client, "mixer", "status", mixernumber)
253
254

    # ------------------------------------------------------------------------------------------ #
255
    def init_player(self):
256
257
258
259
        t = LiquidSoapInitThread()
        t.liquidsoapcommunicator = self
        t.active_entry = self.scheduler.get_active_entry()
        t.start()
260
        return "LiquidSoapInitThread started!"
261
262
263
264
265
266
267
268
269

    # ------------------------------------------------------------------------------------------ #
    def all_data(self):
        """
        Gibt Metadaten aller Kanäle als JSON-String an den Client zurück
        @rtype:   string/None
        @return:  Die Antwort des Liquidsoap-Servers

        """
270
        channels = self.__send_lqc_command__(self.client, 'list_channels')
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

        if not isinstance(channels, list):
            self.warning('01')
            self.notifyClient()
            return

        data = {}
        pdata = {}

        try:
            self.is_intern = True
            playlist_data = simplejson.loads(self.playlist_data(True))
            self.is_intern = False
        except:
            self.warning('01')
            self.notifyClient()
            return

        # Status des Playlistkanals abfragen
290
        status = self.__send_lqc_command__(self.client, 'status', 'mixer', '0')
291
292
293
294
295
296
297
298
299
300
301
302
303
304

        states = status.split(' ')
        state_data = {}

        # Die Stati in python dicts einlesen
        for state in states:
            item = state.split('=')
            try:
                state_data[item[0]] = item[1]
            except:
                self.warning('01')
                self.notifyClient()
                return

305
        remaining = self.__send_lqc_command__(self.client, 'playlist_remaining')
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
        state_data['remaining'] = remaining
        # Die Metadaten der Playlist
        pdata['state'] = state_data
        pdata['tracks'] = playlist_data
        data['playlist'] = pdata

        # Servermeldungen abschalten
        self.is_intern = True
        # die channel queues einlesen

        for channel in channels:
            data[channel] = self.channel_queue(channel, True)
        # Servermeldungen einschalten
        self.is_intern = False
        self.success('00', data)
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def ping(self):
        """
        dem Client antworten
        """
        return self.message('OK')

    # ------------------------------------------------------------------------------------------ #
    def channel_insert(self, channel, uri, pos):
        """
        Track in einen Channel einfuegen
        @type     channel: string
        @param    channel: Kanal
        @type     uri:     string
        @param    uri:     Uri - z.B. file:///my/audio/mp3
        @type     pos:     int
        @param    pos:     Die Position an der eingefügt werden soll
        @rtype:   string/None
        @return:  Die Antwort des Liquidsoap-Servers
        """
343
        message = self.__send_lqc_command__(self.client, 'insert', uri, pos, channel)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        message = message.strip()

        try:
            if int(message) > -1:
                self.success()
                return self.message(message)
        except:
            self.warning('01')
            self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_move(self, channel, fromPos, toPos):
        """
        Channel-Eintrag von Position fromPos nach Position toPos verschieben
        @type     channel: string
        @param    channel: Kanal
        @type     fromPos: int
        @param    fromPos: die Position des Eintrags, der verschoben wird
        @type     toPos:   int
        @param    toPos:   Zielposition
        @rtype:   string
        @return:  Die Antwort des Liquidsoap-Servers
        """
367
        message = self.__send_lqc_command__(self.client, 'get_queue', channel, 'secondary_queue')
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

        rids = message.strip().split(' ')

        try:
            rid = rids[int(fromPos) - 1]
        except:
            self.warning('01')
            self.notifyClient()
            return
        try:
            target = rids[int(toPos) - 1]
        except:
            self.warning('01')
            self.notifyClient()
            return

        if rids[int(fromPos) - 1] == rids[int(toPos) - 1]:
            self.warning('02')
            self.notifyClient()
            return

389
        message = self.__send_lqc_command__(self.client, 'move', rid, str(int(toPos) - 1), channel)
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
        message = message.strip()

        if message.strip().find('OK') > -1:
            self.success()
            self.notifyClient()
            return
        else:
            self.warning('03')
            self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_off(self, channel):
        """
        Channel deaktivieren
        @type     channel: string
        @param    channel: Kanal
        @rtype:   string
        @return:  Die Antwort des Liquidsoap-Servers
        """
        # internal channel name for playlist is 'common'
        if channel == 'playlist':
            channel = 'common'

413
        channels = self.__send_lqc_command__(self.client, 'list_channels', False)
414
415

        index = channels.index(channel)
416
        message = self.__send_lqc_command__(self.client, 'deactivate', str(index))
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
        if message.find('selected=false'):
            self.success()
        else:
            self.warning('01')

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_on(self, channel):
        """
        Channel aktivieren
        @type     channel: string
        @param    channel: Kanal
        @rtype:   string
        @return:  Die Antwort des Liquidsoap-Servers
        """

        # Find channels
        if channel == 'playlist':
            channel = 'common'

438
        channels = self.__send_lqc_command__(self.client, 'list_channels', False)
439
440
441

        index = channels.index(channel)
        # a activate channel
442
        message = self.__send_lqc_command__(self.client, 'activate', str(index))
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

        if message.find('selected=true'):
            self.success()
        else:
            self.warning('01')

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_queue(self, channel, raw=False):
        """
        Channel Queue abrufen
        @type     channel: string
        @param    channel: Kanal
        @type     raw:     boolean
        @param    raw:     Wenn true, Rückgabe als Python dict Object, andernfalls als JSON-String
        @rtype:   string/dict
        @return:  Der Channel Queue
        """
        data = {}

        # queue will return request id's (rids)

466
        message = self.__send_lqc_command__(self.client, 'get_queue', channel)
467
468
469
470
471
472

        rids = message.strip().split(' ')
        data['tracks'] = []
        for rid in rids:
            if rid != '':
                # get each rids metadata
473
                metadata = self.__send_lqc_command__(self.client, 'getMetadata', rid)
474
475
476
477
478
479
480
481
482
483
                track = self._metadata_format(metadata)
                if not 'title' in track:
                    if 'location' in track:
                        track['title'] = os.path.basename(track['location'])
                    elif 'filename' in track:
                        track['title'] = os.path.basename(track['filename'])
                    else:
                        track['title'] = 'unknown'

                data['tracks'].extend([track])
484
        channels = self.__send_lqc_command__(self.client, 'list_channels')
485
486
487
488
489
490
491

        """
        now get channels state 
        self.lqc.status: ready=false volume=100% single=false selected=false remaining=0.00
        """
        try:
            index = channels.index(channel)
492
            status = self.__send_lqc_command__(self.client, 'status', 'mixer', str(index + 1))
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
            states = status.split(' ')
            state_data = {}
            for state in states:
                item = state.split('=')
                if len(item) > 1:
                    state_data[item[0]] = item[1]
        except:
            state_data = {}
            self.error('01')
            self.notifyClient()
            return

        data['state'] = state_data

        if raw:
            # return the list internal
            data['state'] = state_data
            return data
        else:
            self.success('00', data)
            self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_remove(self, channel, pos):
        """
        Channel-Eintrag löschen
        @type     channel: string
        @param    channel: Kanal
        @type     pos:     int
        @param    pos:     Position des Eintrags
        """
        # Es kann nur vom Secondary Queue gelöscht werden
        # Falls der Track im Primary Queue gelöscht werden soll, ist ein skip nötg

527
        message = self.__send_lqc_command__(self.client, 'get_queue', channel, 'secondary_queue')
528
529
530
531
532
533
534
        rids = message.strip().split(' ')
        try:
            rid = rids[int(pos) - 1]
        except:
            self.warning('02')
            self.notifyClient()
            return
535
        message = self.__send_lqc_command__(self.client, 'remove', rid, channel)
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
        if message.find('OK') > -1:
            self.success()
        else:
            self.warning('01')

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_seek(self, channel, duration):
        """
        Im aktuell spielenden Track auf dem Kanal <channel> <duration> Sekunden "vorspulen"
        @type     channel:  string
        @param    channel:  Kanal
        @type     duration: int
        @param    duration: Dauer in Sekunden
        """
        # Liquidsoap Kommando
553
        data = self.__send_lqc_command__(self.client, 'seek', duration, channel)
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

        # Resultate prüfen
        if self._check_result(data):
            self.success('00', self.lq_error['value'])
        else:
            self.warning('01')

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def channel_skip(self, channel):
        """
        Kanal skippen
        @type     channel:  string
        @param    channel:  Kanal
        """

        # Liquidsoap Kommando
572
        channels = self.__send_lqc_command__(self.client, 'list_channels')
573
574
575
576
577
578
579

        foundChannel = ''
        if not isinstance(channels, list):
            self.error('02')
        else:
            for index, item in enumerate(channels):
                if item == channel:
580
                    foundChannel = self.__send_lqc_command__(self.client, 'skip', 'mixer', str(index + 1))
581
582
583
584
585
586
587
588
589
590
591
                    break

            if foundChannel.strip().find('OK') > -1:
                self.success()
            elif len(channels) < 1:
                self.warning('01')
            else:
                self.error('03')

        self.notifyClient()

592
    # ------------------------------------------------------------------------------------------ #
593
594
595
596
597
598
599
600
    def channel_activate(self, channel, activate):
        channels = self.get_all_channels()

        try:
            index = channels.index(channel)
            if len(channel) < 1:
                self.warning('02')
        except:
601
602
            import traceback
            traceback.print_exc()
603
604
605
            self.error('03')

        else:
606
            message = self.__send_lqc_command__(self.client, "mixer", "select", index, activate)
607
608
609
            return message


610
611
612
    # ------------------------------------------------------------------------------------------ #
    def channel_volume(self, channel, volume):
        """
613
        set volume of a channel
614
        @type     channel:  string
615
        @param    channel:  Channel
616
        @type     volume:   int
617
        @param    volume:   Volume between 0 and 100
618
        """
619

620
        try:
621
            channels = self.get_all_channels()
622
            index = channels.index(channel)
623

624
            if len(channel) < 1:
625
626
                self.warning(job="channel_volume", errnum="02")
            else:
627
                message = self.__send_lqc_command__(self.client, "mixer", "volume", str(index), str(int(volume)))
628

629
630
631
632
                if message.find('volume=' + str(volume) + '%'):
                    self.success("channel_volume", errnum="00", value=str(volume))
                else:
                    self.warning("channel_volume", errnum="01")
633

634
635
636
637
                return message
        except (AttributeError, ValueError): #(LQConnectionError, AttributeError):
            self.disable_transaction(force=True)
            self.error("channel_volume", errnum="03")
638

639
640
641
642
643
    # ------------------------------------------------------------------------------------------ #
    def auraengine_state(self):
        state = self.__send_lqc_command__(self.client, "auraengine", "state")
        return state

644
645
646
647
648
649
650
651
    # ------------------------------------------------------------------------------------------ #
    def current_data(self):
        """
        Metadaten des gespielten Tracks im JSON-Format
        Beispiel: {"title": "Deserted Cities of the Heart", "filename": "/home/michel/Nas-audio/cream/the_very_best_of/17_Deserted_Cities_of_the_Heart.mp3", "source": "ch2", "on_air": "2014/07/23 23:46:37",  "rid": "2"}
        """

        # Liquidsoap Kommando
652
        message = self.__send_lqc_command__(self.client, 'currentTrack')
653
654
655

        rid = message.strip()

656
        metadata = self.__send_lqc_command__(self.client, 'getMetadata', rid)
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673

        data = self._metadata_format(metadata)

        if data:
            self.success('00', simplejson.dumps(data))
        elif rid == '':
            self.warning('01')
        else:
            self.warning('02', rid)

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def get_channel_state(self, channel):

        if channel == 'playlist':
            channel = 'common'
674

675
        channels = self.__send_lqc_command__(self.client, 'list_channels', False)
676
677
678
679
680

        index = channels.index(channel)
        state_data = {}
        try:
            index = channels.index(channel)
681
            status = self.__send_lqc_command__(self.client, 'status', 'mixer', str(index + 1))
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
            states = status.split(' ')
            for state in states:
                item = state.split('=')
                if len(item) > 1:
                    state_data[item[0]] = item[1]
        except:
            state_data = {}
            self.error('01')
            self.notifyClient()
            return
        self.success('00', simplejson.dumps(state_data))
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def help(self):
        """
        Gibt die Hilfe aus
        """
        try:
            file = open(os.path.dirname(os.path.abspath(__file__)) + '/doc/comba.hlp', 'r')
            doc = file.read()
            return self.message(doc)
        except:
            self.warning('01')
            self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def liquidsoap_help(self):
710
        data = self.__send_lqc_command__(self.client, 'help')
711
712
713
714
715
716
717
718
719
720
721
722
723
        if not data:
            self.warning('01')
        else:
            self.success('00', data)
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def list_channels(self):
        """
        Channels auflisten (Simple JSON)
        """
        # Liquidsoap Kommando

724
        channels = self.__send_lqc_command__(self.client, 'list_channels')
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741

        if not isinstance(channels, list):
            self.error('02')
        elif len(channels) < 1:
            self.warning('01')
        else:
            self.success('00', channels)

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_data(self, raw=False):
        """
        Aktuelle Playlist Daten im JSON-Format
        """

        # Liquidsoap Kommando
742
        data = self.__send_lqc_command__(self.client, 'playlistData')
743
744
745
746
747
748
749
750
751
752
753
        if not raw:
            self.success('00', simplejson.loads(data))
            self.notifyClient()
        else:
            return data

    # ------------------------------------------------------------------------------------------ #
    def playlist_flush(self):
        """
        Aktuelle Playlist leeren
        """
754
        data = self.__send_lqc_command__(self.client, 'flush')
755
756
757
758
759
760
761
762
        self.success('00')
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_insert(self, uri, pos):
        """
        Track in die Playlist einfuegen
        """
763
        data = self.__send_lqc_command__(self.client, 'insert', uri, pos)
764
765
766
767
768
769
770
771
772
773
774
775
776
777
        if not self._check_result(data):
            self.warning('01')
        else:
            self.success('00')
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_load(self, uri):
        """
        Playlist laden
        @type   uri:   string
        @param  uri:   Uri der Playlist
        """
        try:
778
            xml = urllib.urlopen(uri).read().decode("utf8")
779
780
781

        except:
            try:
782
                xml = open(uri).read().decode("utf8")
783
            except:
784
                self.error("01", self.lq_error["message"])
785
786
787
788
789
                self.notifyClient()
                return

        (num, filename) = tempfile.mkstemp(suffix=".xspf")

790
        with codecs.open(filename, "w", encoding="utf8") as text_file:
791
792
793
794
795
            text_file.write(xml)

        playlist = parsexml(xml)

        if not isinstance(playlist, dict):
796
            self.error("02")
797
798
            self.notifyClient()
        else:
799
800
            self.__send_lqc_command__(self.client, "flush")
            data = self.__send_lqc_command__(self.client, "loadPlaylist", filename)
801
802

            if not self._check_result(data):
803
                self.error("01", self.lq_error["message"])
804
805
806
807

            else:
                os.remove(filename)
                self._updateEventQueue(playlist)
808
809
810
                event = {"job": "loadplaylist", "uri": uri}
                self.messenger.fire_event("loadplaylist", event, "player")
                self.success("00")
811
812
813
814
815
816
817
818
819
820
821
822

            self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_move(self, fromPos, toPos):
        """
        Playlist-Eintrag von Position fromPos nach Position toPos verschieben
        @type     fromPos: int
        @param    fromPos: die Position des Eintrags, der verschoben wird
        @type     toPos:   int
        @param    toPos:   Zielposition
        """
823
        data = self.__send_lqc_command__(self.client, "move", str(int(fromPos) + 1), str(int(toPos) + 1))
824
825

        if not self._check_result(data):
826
            self.warning("01")
827
        else:
828
            self.success("00")
829
830
831
832
833
834
835
836

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_pause(self):
        """
        Playlist pausieren
        """
837
        data = self.__send_lqc_command__(self.client, "pause")
838
839

        if not self._check_result(data):
840
            self.info("01")
841
        else:
842
            self.success("00")
843
844
845
846
847
848
849
850
851

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_stop(self):
        """
        Playlist stoppen - der Kanal wird deaktiviert
        """
        # Kanal 0 (Playlist) deaktivieren
852
        self.__send_lqc_command__(self.client, "deactivate", "0")
853

854
        data = self.__send_lqc_command__(self.client, "pause")
855
856

        if not self._check_result(data):
857
            self.info("01")
858
        else:
859
            self.success("00")
860
861

        self.notifyClient()
862
863

    # ------------------------------------------------------------------------------------------ #
864
    def playlist_play(self, when="now"):
865
866
867
868
869
870
        """
        Playlist starten
        @type   when:   string
        @param  when:   Wenn "now" werden alle anderen Kanäle deaktiviert und geskipped
        """
        # Playlist Kanal aktivieren
871
        self.__send_lqc_command__(self.client, "activate", "0")
872

873
        if when == "now":
874
875
            # immediately skip all playing channels
            # and activate the playlist channel
876
            channels = self.__send_lqc_command__(self.client, "list_channels")
877
            if not isinstance(channels, list):
878
                self.error("03")
879
            elif len(channels) < 1:
880
                self.warning("02")
881
882
883
            else:
                # xrange
                for i in range(len(channels)):
884
                    status = self.__send_lqc_command__(self.client, "status", "mixer", str(i + 1))
885
                    if "selected=true" in status:
886
887
888
                        status = self.__send_lqc_command__(self.client, "deactivate", str(i + 1))
                        status = self.__send_lqc_command__(self.client, "skip", "mixer", str(i + 1))
                        self.__send_lqc_command__(self.client, "activate", "0")
889
890

        # send the play command
891
        data = self.__send_lqc_command__(self.client, "play")
892
        if not self._check_result(data):
893
            self.info("01")
894
        else:
895
            self.success("00")
896
897
898

        self.notifyClient()

899
900
    # ------------------------------------------------------------------------------------------ #
    def set_http_url(self, uri):
901
        return self.__send_lqc_command__(self.client, "http", "url", uri)
902

903
904
905
906
907
908
909
    # ------------------------------------------------------------------------------------------ #
    def playlist_push(self, uri):
        """
        Eine Uri in die Playlist einfügen
        @type   uri:   str
        @param  uri:   Die Uri
        """
910
        return self.__send_lqc_command__(self.client, "fs", "push", uri)
911
#        self.notifyClient()
912
913
914
915
916
917
918
919

    # ------------------------------------------------------------------------------------------ #
    def playlist_remove(self, pos):
        """
        Playlist-Eintrag löschen
        @type     pos:     int
        @param    pos:     Position des Eintrags
        """
920
        data = self.__send_lqc_command__(self.client, "remove", pos)
921
922

        if not self._check_result(data):
923
            self.info("01")
924
        else:
925
            self.success("00")
926
927
928
929
930
931
932
933
934
935

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_seek(self, duration):
        """
        Im aktuell spielenden Track auf dem der Playlist "vorspulen"
        @type     duration: int
        @param    duration: Dauer in Sekunden
        """
936
        data = self.__send_lqc_command__(self.client, "seek", duration)
937
938
939

        # Resultate prüfen
        if self._check_result(data):
940
            self.success("00", self.lq_error["value"])
941
        else:
942
            self.warning("01")
943
944
945
946
947
948
949
950

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def playlist_skip(self):
        """
        Playlist skippen
        """
951
        data = self.__send_lqc_command__(self.client, "skip")
952

953
        self.success("00")
954
955
956
957
958
959
960
961

        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def version(self):
        """
        get version
        """
962
        data = self.__send_lqc_command__(self.client, "version")
963
        self.success("00", data)
964
        self.notifyClient()
965
966

    # ------------------------------------------------------------------------------------------ #
967
968
969
970
    def uptime(self):
        """
        get uptime
        """
971
        data = self.__send_lqc_command__(self.client, "uptime")
972
        self.success("00", data)
973
974
975
976
977
978
979
        self.notifyClient()

    # ------------------------------------------------------------------------------------------ #
    def on_air(self):
        """
        get whats playing now
        """
980
        data = self.__send_lqc_command__(self.client, "on_air")
981
        self.success("00", data)
982
983
        self.notifyClient()

984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
        # ------------------------------------------------------------------------------------------ #
        def http_start_stop(self, start):
            if start:
                cmd = "start"
            else:
                cmd = "stop"

            try:
                self.enable_transaction()
                self.__send_lqc_command__(self.lqc, "http", cmd)
                self.disable_transaction()
            except LQConnectionError as e:
                # we already caught and handled this error in __send_lqc_command__, but we do not want to execute this function further
                pass

999
1000