-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarantulaTel.py
243 lines (208 loc) · 8.67 KB
/
tarantulaTel.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
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
128
129
130
131
132
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#-------------------------------------------------------------------------------
# Name: tarantulaTel
# Purpose: Wrapper for the telnet interface to tarantula
#
# Author: Robert Walker
#
# Created: 10/11/2013
# Copyright: (c) Robert Walker 2013
# Licence: GPLv3
#-------------------------------------------------------------------------------
import socket
import telnet as tel
import datetime as dt
import xml.etree.cElementTree as et
from math import floor
import device
host = 'strm1.ystv.york.ac.uk'
port = 9815
NoEvent = et.fromstring('<MCEvent></MCEvent>')
class Tarantula(tel.Telnet, device.Device):
## nextLiveId = NoEvent
nextLiveShow = NoEvent
currentShow = NoEvent
nextShow = NoEvent
def isLiveScheduled(self):
"""
Returns True if a live show is scheduled in the next
24 hours, False otherwise"""
if self.nextLiveShow is not NoEvent:
return True
else:
return False
def isLive(self):
"""
Returns True if a live show is currently in progress,
False if otherwise."""
try:
if self.currentShow.find('targetdevice').text == 'LiveShow':
return True
else:
return False
except AttributeError:
return False
def setShowData(self, type_, element):
if type_ == 'live':
self.nextLiveShow = element
elif type_ == 'current':
self.currentShow = element
elif type_ == 'next':
self.nextShow = element
else:
raise TypeError('Unrecognised show type')
def getShowData(self, showType, data = None):
if showType == 'live':
if data is None:
return self.nextLiveShow
else:
try:
return self.nextLiveShow.find(data).text
except AttributeError:
return None
elif showType == 'current':
if data is None:
return self.currentShow
else:
try:
return self.currentShow.find(data).text
except AttributeError:
return None
elif showType == 'next':
if data is None:
return self.nextShow
else:
try:
return self.nextShow.find(data).text
except AttributeError:
return None
else:
raise TypeError('Unrecognised show type')
def getShowId(self, type_):
if type_ not in ['live', 'current', 'next']:
raise TypeError('Unrecognised show type')
return self.getShowData(type_).find('eventid').text
def stopLive(self):
self.open()
self.write('<Action><ActionType>Trigger</ActionType>' +
'<channel>Default</channel>' +
'<eventid>{}</eventid></Action>\r\n'.format(
self.getNextLiveId()))
events = self.read_until('\r\n', 1)
events = self.read_until('\r\n', 1)
self.close()
def delayLive(self, length = None, option = None):
"""
Send the command to tarantula to delay the live show and
play something to fill the time
int length: time (in frames) to delay/fill
str option: what to fill the time with (currently 'Lazy mode' or
'Clock'.)"""
length = int(length) * 60 * 25
try:
tarantula.open()
tarantula.write('<Action><ActionType>UpdatePlaylist</Acti' +
'onType><channel>Default</channel><specialac' +
'tion>next</specialaction></Action>\r\n')
events = tarantula.read_until('\r\n', 1)
events = tarantula.read_until('\r\n', 1)
if '200 SUCCESS' in events:
events = events.strip('200 SUCCESS\r\n')
root = et.XML(events)
for event in root.findall('MCEvent'):
if event.find('targetdevice').text == 'LiveShow':
time = event.find('time').text
else:
raise NoEventError
tarantula.write('<Action><ActionType>Shunt</ActionType>' +
'<channel>Default</channel>' +
'<starttime>{}</starttime>'.format(time) +
'<length>{}</length></Action>\r\n'.format(
length))
events = tarantula.read_until('\r\n', 1)
if option == 'Lazy mode':
tarantula.write('<Action><ActionType>Add</ActionType>' +
'<MCEvent><action>1</action>' +
'<channel>Default</channel>' +
'<duration>{}</duration>'.format(length) +
'<type>fixed</type><actiondata />' +
'<targetdevice>Lazy Scheduler</targetdevice>' +
'<time>{}</time>'.format(time) +
'<childevents /></MCEvent></Action>\r\n')
event = tarantula.read_until('\r\n', 1)
elif option == 'Clock':
tarantula.write('<Action><ActionType>Add</ActionType>' +
'<MCEvent><action>1</action>' +
'<channel>Default</channel>' +
'<duration>{}</duration>'.format(length) +
'<type>fixed</type><actiondata>' +
'<graphicname>Clock</graphicname>' +
'<hostlayer>1</hostlayer></actiondata>' +
'<targetdevice>GFX Helper</targetdevice>' +
'<time>{}</time>'.format(time) +
'<childevents /></MCEvent></Action>\r\n')
event = tarantula.read_until('\r\n', 1)
else:
pass # Shouldn't really happen...
tarantula.close()
except (socket.timeout, socket.socket):
return False
if event == '200 SUCCESS':
return True # Woo, alls good :)
else:
return False
# eh, will worry about this later
def updateShowData(self):
time = dt.datetime.now() + dt.timedelta(minutes = 1)
time = time.isoformat(' ')
self.open()
self.write('<Action><ActionType>UpdatePlaylist</Acti' +
'onType><channel>Default</channel>' +
'<starttime>{}</starttime>'.format(time) +
'<length>86400</length></Action>\r\n')
events = self.read_until('\r\n', 1)
events = self.read_until('\r\n', 1)
if '200 SUCCESS' in events:
events = events.strip('200 SUCCESS\r\n')
self.close()
try:
root = et.XML(events)
for event in root.findall('MCEvent'):
if (event.find('targetdevice').text == 'LiveShow' or
event.find('targetdevice').text == 'Live Show'):
self.setShowData('live', event)
break
else:
self.setShowData('live', NoEvent)
except et.ParseError:
self.setShowData('live', NoEvent)
for type_ in ['next', 'current']:
self.open()
self.write('<Action><ActionType>UpdatePlaylist</Acti' +
'onType><channel>Default</channel><specialac' +
'tion>{}</specialaction></Action>\r\n'.format(
type_))
events = self.read_until('\r\n')
events = self.read_until('\r\n')
if '200 SUCCESS' in events:
events = events.strip('200 SUCCESS\r\n')
self.close()
if events == '':
self.setShowData(type_, NoEvent)
try:
root = et.XML(events)
for event in root.findall('MCEvent'):
self.setShowData(type_, event)
break
else:
self.setShowData(type_, NoEvent)
except et.ParseError:
self.setShowData(type_, NoEvent)
def __init__(self, host = host, port = port):
tel.Telnet.__init__(self, host = host, port = port)
self.name = 'tarantula'
def main():
tarantula = Tarantula()
tarantula.updateShowData()
print tarantula.getShowData('current', 'description')
if __name__ == '__main__':
main()