-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
125 lines (110 loc) · 3.75 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
# Copyright (C) 2011-2021 Brendan Le Foll <[email protected]>
#
# 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 2 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/>.
import sys
import time
import xbmc
import xbmcgui
import xbmcaddon
from dbus_control import DbusControl
Addon = xbmcaddon.Addon(id='service.madeo.rs232server')
__language__ = Addon.getLocalizedString
#volume control variables
music = False
video = False
#to avoid xbmc start with ampon but never switching off
#because power is wrong
FIRSTRUN = False
#in order to not stop playing as soon as video/playlist ends
IDLE = 0
DIDPLAY = False
NOTIFYDONE = False
class Caller:
def __init__(self):
#for notify mode 1
self.NOTIFYDONE = False
self.POWER = False
self.control = DbusControl()
self.control.startAzur()
self.control.startLgtv()
def send_azur_cmd(self, msg, repeat):
self.control.getAmpIface().send_cmd(msg, repeat, False)
def send_lgtv_cmd(self, msg, repeat):
#self.control.getLgtvIface().send_cmd(msg, repeat, False)
pass
def poweron(self, audioonly=False):
try:
self.send_azur_cmd('poweron', 1)
self.send_lgtv_cmd('poweron', 1)
if audioonly:
# need to set enerscreenoff when tv is on
self.send_lgtv_cmd('enerscreenoff', 1)
self.POWER = 2
else:
self.POWER = True
except:
self.connectionError()
def poweroff(self):
try:
self.send_azur_cmd('poweroff', 1)
self.POWER = False
except:
self.connectionError()
def errorMessage(self, phrase):
dialog = xbmcgui.Dialog()
dialog.ok(__language__(30101), phrase)
def connectionError(self):
if (bool(Addon.getSetting('notify'))) and (self.NOTIFYDONE is False):
self.NOTIFYDONE = True
self.errorMessage(__language__(30100))
def powerStatus(self):
return self.POWER
caller = Caller()
#xbmc.executeJSONRPC("{\"jsonrpc\": \"2.0\", \"method\": \"Application.SetVolume\", \"params\": { \"volume\": 100 }, \"id\": 1}")
player = xbmc.Player()
monitor = xbmc.Monitor()
while not monitor.abortRequested():
# if xbmc is playing
if (player.isPlaying() == 1):
IDLE = 0
DIDPLAY = True
lastcheck = time.time()
# check play type
if (player.isPlayingAudio()):
music = True
if (caller.powerStatus() is False):
caller.poweron(not bool(Addon.getSetting('musicon')))
if (video is True):
caller.send_azur_cmd('voldown', int(Addon.getSetting('voldiff')))
video = False
elif (player.isPlayingVideo()):
video = True
if (caller.powerStatus() is False) or (caller.powerStatus() == 2):
caller.poweron()
if (music is True):
caller.send_azur_cmd('volup', int(Addon.getSetting('voldiff')))
caller.send_lgtv_cmd('poweron', 1)
if (Addon.getSetting('muteonvid')):
caller.send_lgtv_cmd('mute', 1)
music = False
#poweroff if hasn't played/idle for defined timeout
elif (xbmc.getGlobalIdleTime() > int(Addon.getSetting('timeout'))) and (caller.powerStatus() is True or FIRSTRUN is True):
if (IDLE == 0) and (DIDPLAY is False):
caller.poweroff()
FIRSTRUN = False
IDLE = 1
elif(DIDPLAY == True) and ((time.time() - lastcheck) > int(Addon.getSetting('timeout'))):
DIDPLAY = False;
# small sleep
xbmc.sleep(1000)