-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
194 lines (146 loc) · 5.99 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
# coding=utf-8
import RPi.GPIO as GPIO
import time
import sys
import os
import subprocess
import xbmc
import xbmcaddon
import xbmcgui
import xbmcvfs
from resources.lib.tools import OsRelease, KodiLib
addon = xbmcaddon.Addon(id='service.displayswitch')
addon_path = xbmcvfs.translatePath(addon.getAddonInfo('path'))
addon_profile = xbmcvfs.translatePath(addon.getAddonInfo('profile'))
LOC = addon.getLocalizedString
flash_path = '/flash/config.txt'
flash_backup = '/flash/config.txt.origin'
config_templates = os.path.join(addon_path, 'resources', 'configs')
user_config_path = os.path.join(addon_profile, 'configs')
default_config_1 = 'config.DSI'
default_config_2 = 'config.HDMI'
if addon.getSetting('default') == default_config_1:
user_config_1 = os.path.join(user_config_path, default_config_1)
else:
user_config_1 = addon.getSetting('default')
if addon.getSetting('alternate') == default_config_2:
user_config_2 = os.path.join(user_config_path, default_config_2)
else:
user_config_2 = addon.getSetting('alternate')
osr = OsRelease()
kl = KodiLib()
monitor = xbmc.Monitor()
kl.log('Device is {} on {} ({})'.format(osr.device, osr.osid, osr.project), xbmc.LOGINFO)
if osr.project != "RPi":
kl.notify(LOC(32010), LOC(32020), xbmcgui.NOTIFICATION_WARNING)
sys.exit(0)
# Prerequisites and backups
if not os.path.exists(flash_backup):
subprocess.call(['mount', '-o', 'remount,rw', '/flash'], shell=False)
xbmcvfs.copy(flash_path, flash_backup)
subprocess.call(['mount', '-o', 'remount,ro', '/flash'], shell=False)
if not os.path.exists(user_config_path):
xbmcvfs.mkdirs(user_config_path)
xbmcvfs.copy(os.path.join(config_templates, default_config_1), os.path.join(user_config_path, default_config_1))
xbmcvfs.copy(os.path.join(config_templates, default_config_2), os.path.join(user_config_path, default_config_2))
# GPIO-Port, an dem die Taste gegen GND angeschlossen ist
# GPIO 5, Pin 29 (GND ist daneben auf Pin 30)
PORT = 5
# Schwelle für Shutdown (in Sekunden), wird die Taste kürzer gedrückt, erfolgt ein Reboot
SHUTDOWN = 3
DEBOUNCE = 0.05
duration = 0
def copy_config(src):
try:
subprocess.check_call(['mount', '-o', 'remount,rw', '/flash'], shell=False)
except subprocess.CalledProcessError as e:
kl.log('Failed to remount (rw) flash: {}'.format(e.returncode))
return False
kl.log('Copy {} to flash'.format(src))
copied = xbmcvfs.copy(src, flash_path)
try:
subprocess.check_call(['mount', '-o', 'remount,ro', '/flash'], shell=False)
except subprocess.CalledProcessError as e:
kl.log('Failed to remount (ro) flash: {}'.format(e.returncode))
return copied
def execute_command(command):
if command == 'poweroff':
kl.log('initiate shutdown')
if addon.getSetting('use_default_boot').lower() == "true":
boot_config = [user_config_1, user_config_2]
idx = int(addon.getSetting('start_config'))
src = boot_config[idx]
if copy_config(src):
kl.log('Default boot configuration copied to flash')
if idx == 0:
ps = default_config_1 if addon.getSetting('default') == default_config_1 else user_config_1
else:
ps = default_config_2 if addon.getSetting('default') == default_config_2 else user_config_2
addon.setSetting('current', ps)
else:
kl.log('Couldn\'t copy default boot configuration to flash', xbmc.LOGERROR)
kl.notify(LOC(32010), LOC(32023))
xbmc.sleep(5000)
xbmc.executebuiltin('Powerdown')
elif command == 'switch':
kl.log('initiate configuration change and reboot')
curr = addon.getSetting('current')
if curr == default_config_1:
src = user_config_2
ps = default_config_2 if addon.getSetting('alternate') == default_config_2 else user_config_2
elif curr == default_config_2:
src = user_config_1
ps = default_config_1 if addon.getSetting('default') == default_config_1 else user_config_1
elif curr == user_config_1:
src = user_config_2
ps = default_config_2 if addon.getSetting('alternate') == default_config_2 else user_config_2
else:
src = user_config_1
ps = default_config_1 if addon.getSetting('default') == default_config_1 else user_config_1
if copy_config(src):
addon.setSetting('current', ps)
kl.notify(LOC(32010), LOC(32022))
xbmc.sleep(5000)
xbmc.executebuiltin('Reboot')
else:
kl.notify(LOC(32010), LOC(32021), xbmcgui.NOTIFICATION_ERROR)
else:
kl.log('unknown Command: {}'.format(command), xbmc.LOGERROR)
# Interrupt function for PORT (GPIO)
def buttonISR(pin):
global duration
if not (GPIO.input(pin)):
# Button pressed
if duration == 0: duration = time.time()
else:
# Button released
if duration > 0:
elapsed = (time.time() - duration)
duration = 0
if elapsed > SHUTDOWN:
execute_command('poweroff')
elif elapsed > DEBOUNCE:
execute_command('switch')
else:
pass
if __name__ == '__main__':
kl.log('Service started', level=xbmc.LOGINFO)
try:
if sys.argv[1]:
execute_command(sys.argv[1])
sys.exit(0)
except IndexError:
# GPIO Init, BMC-Pin, Pullup-Resistor
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PORT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(PORT, GPIO.BOTH, callback=buttonISR)
# Main service loop
while not monitor.abortRequested():
if monitor.waitForAbort(600):
break
kl.log('Service still alive')
kl.log('Abort requested, service finished', level=xbmc.LOGINFO)
GPIO.setmode(GPIO.BCM)
GPIO.remove_event_detect(PORT)
GPIO.cleanup(PORT)