forked from kafisc1/service.fhemcinema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
228 lines (198 loc) · 7.36 KB
/
addon.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
# -*- coding: utf-8 -*-
# Copyright 2014 Leo Moll
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# -- Imports ------------------------------------------------
import datetime,socket,subprocess,os
import xbmc,xbmcplugin,xbmcgui,xbmcaddon
import http.client
import time
# -- Constants ----------------------------------------------
ADDON_ID = 'service.fhemcinema'
# -- Settings -----------------------------------------------
settings = xbmcaddon.Addon( id = ADDON_ID )
# -- I18n ---------------------------------------------------
language = xbmcaddon.Addon( id = ADDON_ID ).getLocalizedString
# -- Functions ----------------------------------------------
if __name__ == '__main__':
monitor = xbmc.Monitor()
# -- Classes ------------------------------------------------
class FhemHandler( xbmc.Player ):
def __init__ ( self ):
xbmc.Player.__init__( self )
self.isplayingvideo = False
self.lastStopTime = 0
self.stopCommand = ''
self.loadSettings()
def loadSettings( self ):
self.stopDelay = int( float( settings.getSetting( 'stopdelay' ) ) )
def isDayTime( self ):
try:
nowTime = datetime.datetime.now().time()
xbmc.log ( 'nowTime: {0}:{1}'.format( nowTime.hour, nowTime.minute ) )
fromTime=settings.getSetting( 'daytimefrom' )
toTime=settings.getSetting( 'daytimeto' )
tmp=fromTime.split( ':' )
t1=datetime.time( int( tmp[0] ), int( tmp[1]) )
tmp=toTime.split( ':' )
t2=datetime.time( int( tmp[0] ), int( tmp[1] ) )
if t1 < t2:
return nowTime >= t1 and nowTime <= t2
else:
return nowTime >= t2 or nowTime <= t1
except Exception as e:
# this should not really happen...
xbmc.log( 'Cinema: Exception in isDayTime: ' + str( e ), xbmc.LOGERROR )
return False
def getCommand( self, command ):
if settings.getSetting( 'daytimeenable' ) == "true":
if self.isDayTime():
return settings.getSetting(command+'dt')
return settings.getSetting(command)
def SendCommand(self,command):
endpointtype=settings.getSetting('endpointtype')
if type(endpointtype) is str and endpointtype == "FHEM":
self.SendFHEM(self.getCommand(command))
elif type(endpointtype) is str and endpointtype == "CCU":
self.SendCCU(command)
else:
xbmc.log ('Endpoint type not supported!')
def SendFHEM(self,command=''):
if type(command) is str and len(command) > 0:
xbmc.log ('Sending command to FHEM: '+command)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((settings.getSetting('hostname'), int(settings.getSetting('port'))))
s.send('\n{0}\nexit\n'.format(command))
s.close()
def SendCCU(self,command):
ccusystemvar = settings.getSetting('ccusystemvar')
createsysvar = settings.getSetting('createsysvar')
if type(ccusystemvar) is str and len(ccusystemvar) > 0:
hostname = settings.getSetting('hostname')
xbmc.log ('Sending command to CCU '+hostname+': '+command)
if command == "onstartup":
state = 1
elif command == "onshutdown":
state = 2
elif command == "onaudioplay":
state = 3
elif command == "onvideoplay":
state = 4
elif command == "onaudiostop":
state = 5
elif command == "onvideostop":
state = 6
elif command == "onaudiopause":
state = 7
elif command == "onvideopause":
state = 8
else:
state = 0
connection = http.client.HTTPConnection(hostname,8181,timeout=10)
connection.connect();
connection.set_debuglevel(9);
params = 'object svObj = dom.GetObject(ID_SYSTEM_VARIABLES).Get(\"'+ccusystemvar+'\");\n'
params = params + 'if (svObj) {\n'
params = params + ' svObj.State(' + str(state) + ');\n'
if settings.getSetting( 'createsysvar' ) == "true":
params = params + '} else {\n'
params = params + ' object svObjects = dom.GetObject(ID_SYSTEM_VARIABLES);\n'
params = params + ' svObj = dom.CreateObject(OT_VARDP);\n'
params = params + ' svObjects.Add(svObj.ID());\n'
params = params + ' svObj.Name(\"'+ccusystemvar+'\");\n'
params = params + ' svObj.ValueType(ivtInteger);\n'
params = params + ' svObj.ValueSubType(istEnum);\n'
params = params + ' svObj.DPInfo(\"\");\n'
params = params + ' svObj.ValueList(\"Unknown;Start;Shutdown;Audio playback start;Video playback start;Audio playback stop;Video playback stop;Audio playback pause;Video playback pause\");\n'
params = params + ' svObj.DPArchive(false);\n'
params = params + ' svObj.State(' + str(state) + ');\n'
params = params + ' svObj.Internal(false);\n'
params = params + ' svObj.Visible(true);\n'
params = params + ' dom.RTUpdate(0);\n'
params = params + '}'
connection.request("POST", "/kodi.exe", params);
response = connection.getresponse()
connection.close();
xbmc.log ( "CCU response code: "+str(response.status))
else:
xbmc.log ('No CCU Object configured')
def Run(self):
while not monitor.abortRequested():
if xbmc.Player().isPlaying():
if xbmc.Player().isPlayingVideo():
self.isplayingvideo = True
else:
self.isplayingvideo = False
timeElapsed = int(round(time.time())) - self.lastStopTime
if self.stopCommand != '' and timeElapsed > self.stopDelay:
self.SendCommand(self.stopCommand)
self.stopCommand = ''
xbmc.sleep(1000)
def StartUp(self):
xbmc.log('Starting up Cinema...')
self.SendCommand('onstartup')
def ShutDown(self):
self.SendCommand('onshutdown')
xbmc.log('Cinema shut down')
def onPlayBackStarted(self):
if xbmc.Player().isPlayingAudio():
self.SendCommand('onaudioplay')
else:
self.isplayingvideo = True;
self.SendCommand('onvideoplay')
self.stopCommand = ''
def onPlayBackEnded(self):
if self.isplayingvideo:
self.stopCommand = 'onvideostop'
else:
self.stopCommand = 'onaudiostop'
self.lastStopTime = int(round(time.time()))
def onPlayBackStopped(self):
if self.isplayingvideo:
self.stopCommand = 'onvideostop'
else:
self.stopCommand = 'onaudiostop'
self.lastStopTime = int(round(time.time()))
def onPlayBackPaused(self):
if xbmc.Player().isPlayingAudio():
self.SendCommand('onaudiopause')
else:
self.SendCommand('onvideopause')
self.stopCommand = ''
def onPlayBackResumed(self):
if xbmc.Player().isPlayingAudio():
self.SendCommand('onaudioplay')
else:
self.isplayingvideo = True;
self.SendCommand('onvideoplay')
self.stopCommand = ''
try:
class SettingsMonitor( xbmc.Monitor ):
def __init__( self, *args, **kwargs ):
xbmc.Monitor.__init__( self )
xbmc.log( 'SettingsMonitor - init' )
def RegisterHandler( self, handler ):
self.handler = handler
def onSettingsChanged( self ):
self.handler.loadSettings()
except:
log( 'Using Eden API - you need to restart addon for changing settings' )
# -- Main Code ----------------------------------------------
settingsMonitor = SettingsMonitor()
handler = FhemHandler()
settingsMonitor.RegisterHandler( handler )
handler.StartUp()
handler.Run()
handler.ShutDown()