-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterlude_action.py
82 lines (67 loc) · 3.81 KB
/
interlude_action.py
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
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
__author__ = "HP Envy"
__date__ = "$Nov 20, 2014 3:29:19 PM$"
from rootio.config import *
import plivohelper
import json
from os import listdir
# from media.community_media import CommunityMedia
class InterludeAction:
def __init__(self, category_id, program):
self.__category_id = category_id
self.program = program
self.__media = []
self.__media_index = 0
self.__media_expected_to_stop = False
self.__call_answer_info = None
self.__call_handler = self.program.radio_station.call_handler
self.program.radio_station.logger.info("Done initialising Interlude action for program")
def start(self):
self.__load_media()
if len(self.__media) > 0:
self.program.set_running_action(self)
self.__request_call()
else:
self.program.radio_station.logger.info("Interlude has 0 actions, station wont be called")
def stop(self):
self.__media_expected_to_stop = True
self.__stop_media()
def notify_call_answered(self, answer_info):
self.program.radio_station.logger.info(
"Interlude action for {0} received answer notification".format(self.program.name))
self.__call_answer_info = answer_info
self.__play_media(self.__call_answer_info['Channel-Call-UUID'], self.__media_index)
self.__listen_for_media_play_stop()
def __load_media(self): # load the media to be played
community_media = CommunityMedia(self.__argument, self.program.radio_station.id)
self.__media = community_media.get_media_files()
self.program.radio_station.logger.info(
"loaded media for {0} interlude action {1}".format(self.program.name, self.__media))
def __request_call(self):
raw_result = self.__call_handler.call(self, self.program.radio_station.station.transmitter_phone.number, 'play',
self.__argument, self.duration)
def __play_media(self, call_uuid, media_index):
logical_index = media_index % len(self.__media)
self.program.radio_station.logger.info(
"Playing media {0} at position {1}".format(self.__media[logical_index], media_index))
result = self.__call_handler.play(call_uuid, self.__media[logical_index])
print 'result of play ' + self.__media[logical_index] + ' is ' + result
def __stop_media(self): # stop the media being played by the player
result = self.__call_handler.stop_play(self.__call_answer_info['Channel-Call-UUID'], self.__media_index)
print 'result of stop play is ' + result
def notify_media_play_stop(self, media_stop_info):
if media_stop_info["Media-Bug-Target"] == self.__media[
self.__media_index % len(self.__media)]: # its our media stopping, not some other media
if self.__media_index >= len(self.__media) * 1 and self.__hangup_on_complete: # all media has played
self.program.radio_station.logger.info(
"Played all media thrice in Interlude action for {0}, hanging up".format(self.program.name))
result = self.__call_handler.hangup(self.__call_answer_info['Channel-Call-UUID'])
# deregister for media bug stop events
print "result of hangup is " + result
elif not self.__media_expected_to_stop:
self.__media_index = self.__media_index + 1
self.__play_media(self.__call_answer_info['Channel-Call-UUID'], self.__media_index)
def __listen_for_media_play_stop(self):
self.__call_handler.register_for_media_playback_stop(self, self.__call_answer_info['Caller-Destination-Number'])