-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.py
278 lines (227 loc) · 13.1 KB
/
service.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import xbmc, xbmcaddon, xbmcgui
import json
import os
import re
import handler
import resources.lib.knClasses as knClasses
addon = xbmcaddon.Addon()
addonid = addon.getAddonInfo('id')
addonname = addon.getAddonInfo('name')
path = xbmc.translatePath(addon.getAddonInfo('path'))
version = addon.getAddonInfo('version')
loc = addon.getLocalizedString
IconDefault = os.path.join(path, 'resources', 'media', 'default.png')
IconAlert = os.path.join(path, 'resources', 'media', 'alert.png')
IconOk = os.path.join(path, 'resources', 'media', 'ok.png')
INTERVAL = 10 # More than that will make switching too fuzzy because service isn't synchronized with real time
HOME = xbmcgui.Window(10000)
SKIN = xbmc.translatePath('special://skin').split(os.sep)[-2] + '.st_notification.xml'
def jsonrpc(query):
querystring = {"jsonrpc": "2.0", "id": 1}
querystring.update(query)
return json.loads(xbmc.executeJSONRPC(json.dumps(querystring, encoding='utf-8')))
class Service(knClasses.XBMCMonitor):
def __init__(self, *args):
knClasses.XBMCMonitor.__init__(self)
self.skinPath = None
self.getSettings()
handler.notifyLog('Init Service %s %s' % (addonname, version))
self.bootstrap = True
self.timers = handler.getTimer()
handler.setTimerProperties(self.timers)
def getSettings(self):
# There seems to be a bug in kodi as sometimes changed properties wasn't read/update properly even if
# monitor signaled a change.
# Reading of settings is now outsourced to handler as a workaround.
self.__showNoticeBeforeSw = True if handler.getSetting('showNoticeBeforeSw').upper() == 'TRUE' else False
self.__useCountdownTimer = True if handler.getSetting('useCountdownTimer').upper() == 'TRUE' else False
self.__leadOffset = int(re.match('\d+', handler.getSetting('leadOffset')).group())
self.__dispMsgTime = int(re.match('\d+', handler.getSetting('dispTime')).group())*1000
self.__discardTmr = int(re.match('\d+', handler.getSetting('discardOldTmr')).group())*60
self.__confirmTmrAdded = True if handler.getSetting('confirmTmrAdded').upper() == 'TRUE' else False
self.__switchOnInit = True if handler.getSetting('switchOnInit').upper() == 'TRUE' else False
try:
self.__channel = int(re.match('\d+', handler.getSetting('channel')).group())
except AttributeError:
self.__channel = 0
self.SettingsChanged = False
@classmethod
def resetTmr(cls, date):
for prefix in ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7', 't8', 't9']:
if HOME.getProperty('%s:date' % (prefix)) == '': continue
elif HOME.getProperty('%s:date' % (prefix)) == date: handler.clearTimerProperties(prefix)
@classmethod
def channelName2channelId(cls, channelname):
query = {
"method": "PVR.GetChannels",
"params": {"channelgroupid": "alltv"},
}
res = jsonrpc(query)
if 'result' in res and 'channels' in res['result']:
res = res['result'].get('channels')
for channels in res:
if channels['label'] == channelname: return channels['channelid']
return False
@classmethod
def getPlayer(cls):
props = {'player': None, 'playerid': None, 'media': None, 'id': None}
query = {
"method": "Player.GetActivePlayers",
}
res = jsonrpc(query)
if 'result' in res and res['result']:
res = res['result'][0]
props['player'] = res['type']
props['playerid'] = res['playerid']
query = {
"method": "Player.GetItem",
"params": {"properties": ["title", "season", "episode", "file"],
"playerid": props['playerid']},
"id": "VideoGetItem"
}
res = jsonrpc(query)
if 'result' in res:
res = res['result'].get('item')
props['media'] = res['type']
if 'id' in res: props['id'] = res['id']
return props
@classmethod
def switchToChannelId(cls, playerProperties, channelId, channel):
if playerProperties['player'] == 'audio' or (playerProperties['player'] == 'video' and playerProperties['media'] != 'channel'):
# stop all other players except pvr
handler.notifyLog('player:%s media:%s @id:%s is running' %
(playerProperties['player'], playerProperties['media'], playerProperties['playerid']))
query = {
"method": "Player.Stop",
"params": {"playerid": playerProperties['playerid']},
}
res = jsonrpc(query)
if 'result' in res and res['result'] == "OK":
handler.notifyLog('Player stopped')
handler.notifyLog('Currently playing channelid %s, switch to id %s' % (playerProperties['id'], channelId))
query = {
"method": "Player.Open",
"params": {"item": {"channelid": channelId}}
}
res = jsonrpc(query)
if 'result' in res and res['result'] == 'OK':
handler.notifyLog('Switched to channel \'%s\'' % (channel))
else:
handler.notifyLog('Couldn\'t switch to channel \'%s\'' % (channel))
handler.notifyOSD(loc(30000), loc(30025) % (channel), icon=IconAlert)
def poll(self):
while not knClasses.XBMCMonitor.abortRequested(self):
if knClasses.XBMCMonitor.waitForAbort(self, INTERVAL): break
if self.SettingsChanged:
self.getSettings()
_now = time.time()
_switchInstantly = False
plrProps = self.getPlayer()
for _timer in self.timers:
if not _timer['utime']:
handler.notifyLog('Couldn\'t calculate timestamp, delete timer', xbmc.LOGERROR)
self.resetTmr(_timer['date'])
break
# delete old/discarded timers
if _timer['utime'] + self.__discardTmr < _now:
self.resetTmr(_timer['date'])
continue
if _timer['utime'] - self.__leadOffset < _now: _switchInstantly = True
if (_timer['utime'] - _now < INTERVAL + self.__dispMsgTime / 1000 + self.__leadOffset) or _switchInstantly:
chanIdTmr = self.channelName2channelId(_timer['channel'])
if chanIdTmr:
# compare with player properties, switch if necessary
if chanIdTmr == plrProps['id']:
handler.notifyLog('Channel switching unnecessary')
handler.notifyOSD(loc(30000), loc(30027) % (_timer['title'], _timer['channel']), time=self.__dispMsgTime)
else:
switchAborted = False
secs = 0
handler.notifyLog('Channel switch to %s required' % (_timer['channel']))
if _switchInstantly:
handler.notifyLog('immediate channel switching required')
handler.notifyOSD(loc(30000), loc(30027) % (_timer['title'], _timer['channel']), time=5000)
elif not self.__showNoticeBeforeSw: xbmc.sleep(self.__dispMsgTime)
elif self.__useCountdownTimer:
if os.path.exists(os.path.join(path, 'resources', 'skins', 'Default', '1080i', SKIN)):
try:
pvr = knClasses.cPvrProperties()
pvrprops = dict()
recEnabled = True
pvrprops.update(pvr.getRecordingCapabilities(
self.channelName2channelId(_timer['channel']), _timer['utime']))
if pvrprops['hastimer']: recEnabled = False
except (pvr.PvrAddTimerException, pvr.JsonExecException), e:
handler.notifyLog(str(e), xbmc.LOGERROR)
recEnabled = False
Popup = knClasses.cNotification(SKIN, path, message=loc(30035) % (_timer['title'], _timer['channel']),
timer=self.__dispMsgTime/1000, icon=_timer['icon'], recEnabled=recEnabled)
Popup.doModal()
if Popup.isCanceled: switchAborted = True
elif Popup.initRecording:
try:
pvr.setTimer(pvrprops['broadcastid'])
handler.notifyLog('Recording schedule set for %s: %s' % (_timer['channel'], _timer['title']), xbmc.LOGNOTICE)
switchAborted = True
except (pvr.PvrAddTimerException, pvr.JsonExecException), e:
handler.notifyOSD(loc(30000), loc(30036) % (_timer['title'], _timer['channel']), icon=IconAlert)
handler.notifyLog(str(e), xbmc.LOGERROR)
elif Popup.requestSwitch:
handler.notifyLog('immediate channel switch initate by user')
else:
handler.notifyLog('Window countdown completed without user action')
else:
handler.OSDProgress.create(loc(30028), loc(30026) %
(_timer['channel'], _timer['title']),
loc(30029) % (int(self.__dispMsgTime / 1000 - secs)))
while secs < self.__dispMsgTime /1000:
secs += 1
percent = int((secs * 100000) / self.__dispMsgTime)
handler.OSDProgress.update(percent, loc(30026) %
(_timer['channel'], _timer['title']),
loc(30029) % (int(self.__dispMsgTime / 1000 - secs)))
xbmc.sleep(1000)
if (handler.OSDProgress.iscanceled()):
switchAborted = True
break
handler.OSDProgress.close()
else:
idleTime = xbmc.getGlobalIdleTime()
handler.notifyOSD(loc(30000), loc(30026) %
(_timer['channel'], _timer['title']), time=self.__dispMsgTime)
while secs < self.__dispMsgTime /1000:
if idleTime > xbmc.getGlobalIdleTime():
switchAborted = True
break
xbmc.sleep(1000)
idleTime += 1
secs += 1
if switchAborted: handler.notifyOSD(loc(30000), loc(30032))
if switchAborted: handler.notifyLog('Channelswitch cancelled by user')
else:
self.bootstrap = False
self.switchToChannelId(plrProps, chanIdTmr, _timer['channel'])
self.resetTmr(_timer['date'])
if self.bootstrap and self.__switchOnInit and self.__channel > 0:
handler.notifyLog('Channelswitch on startup enabled')
query = {
"method": "PVR.GetChannels",
"params": {"channelgroupid": "alltv", "properties": ["channelnumber"]}
}
res = jsonrpc(query)
if 'result' in res:
for _channel in res['result']['channels']:
if _channel['channelnumber'] == self.__channel:
handler.notifyLog('Channelswitch on startup is enabled, switch to \'%s\'' % (_channel['label']))
handler.notifyOSD(loc(30000), loc(30013) % (_channel['label']))
self.switchToChannelId(plrProps, _channel['channelid'], _channel['label'])
self.bootstrap = False
break
self.timers = handler.getTimer()
handler.notifyLog('Service kicks off')
service = Service()
service.poll()
del service