-
Notifications
You must be signed in to change notification settings - Fork 20
/
zynthian_config_handler.py
241 lines (178 loc) · 6.45 KB
/
zynthian_config_handler.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
# -*- coding: utf-8 -*-
#********************************************************************
# ZYNTHIAN PROJECT: Zynthian Web Configurator
#
# Zynthian Configuration Handler base class
#
# Copyright (C) 2017 Fernando Moyano <jofemodo@zynthian.org>
#
#********************************************************************
#
# 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 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.
#
# For a full copy of the GNU General Public License see the LICENSE.txt file.
#
#********************************************************************
import os
import re
import sys
import liblo
import logging
import tornado.web
from time import sleep
from subprocess import check_output
import zynconf
#Avoid unwanted debug messages from zynconf module
zynconf_logger = logging.getLogger('zynconf')
zynconf_logger.setLevel(logging.INFO)
#------------------------------------------------------------------------------
# Zynthian-UI OSC Address
#------------------------------------------------------------------------------
zynthian_ui_osc_addr = liblo.Address('localhost',1370,liblo.UDP)
#------------------------------------------------------------------------------
# Zynthian Basic Handler
#------------------------------------------------------------------------------
class ZynthianBasicHandler(tornado.web.RequestHandler):
reboot_flag = False
restart_ui_flag = False
restart_webconf_flag = False
reload_wiring_layout_flag = False
reload_midi_config_flag = False
reload_key_binding_flag = False
restart_ui_flag_fpath = "/tmp/zynthian_restart_ui"
restart_webconf_flag_fpath = "/tmp/zynthian_restart_webconf"
reboot_flag_fpath = "/tmp/zynthian_reboot"
def get_current_user(self):
return self.get_secure_cookie("user", max_age_days=5200)
def prepare(self):
zynconf.load_config()
zynconf.load_midi_config()
self.read_reboot_flag()
self.genjson=False
try:
if self.get_query_argument("json"):
self.genjson=True
except:
pass
def on_finish(self):
if self.restart_webconf_flag:
self.restart_webconf()
def render(self, tpl, **kwargs):
info = {
'host_name': self.request.host,
'reboot_flag': self.reboot_flag
}
# If MOD-UI is enabled, add access URI to info
if self.is_service_active("mod-ui"):
info['modui_uri']="http://{}:8888".format(self.request.host)
# If VNC Server is enabled, add access URI to info
if self.is_service_active("novnc0"):
info['novnc0_uri']="http://{}:6080/vnc.html".format(self.request.host)
if self.is_service_active("novnc1"):
info['novnc1_uri']="http://{}:6081/vnc.html".format(self.request.host)
# Restore scroll position
info['scrollTop'] = int(float(self.get_argument('_scrollTop', '0')))
super().render(tpl, info=info, **kwargs)
@tornado.web.authenticated
def get(self, body, title, config, errors=None):
#logging.debug(config)
if self.reboot_flag:
self.persist_reboot_flag()
if self.restart_ui_flag:
self.restart_ui()
else:
if self.reload_wiring_layout_flag:
self.reload_wiring_layout()
if self.reload_midi_config_flag:
self.reload_midi_config()
if self.reload_key_binding_flag:
self.reload_key_binding()
if self.genjson:
self.write(config)
else:
self.render("config.html", body=body, config=config, title=title, errors=errors)
def is_service_active(self, service):
return zynconf.is_service_active(service)
def power_off(self):
try:
if self.is_service_active("zynthian"):
liblo.send(zynthian_ui_osc_addr, "/CUIA/POWER_OFF")
sleep(5)
system("killall -SIGQUIT zynthian_gui.py; sleep 5; poweroff")
except Exception as e:
logging.error("Power Off: {}".format(e))
def reboot(self):
try:
self.reboot_ui_flag = False
if os.path.isfile(self.reboot_ui_flag_fpath):
os.remove(self.reboot_ui_flag_fpath)
if self.is_service_active("zynthian"):
liblo.send(zynthian_ui_osc_addr, "/CUIA/REBOOT")
sleep(5)
system("killall -SIGINT zynthian_gui.py; sleep 5; reboot")
except Exception as e:
logging.error("Reboot: {}".format(e))
def restart_ui(self):
try:
check_output("systemctl restart zynthian", shell=True)
self.restart_ui_flag = False
if os.path.isfile(self.restart_ui_flag_fpath):
os.remove(self.restart_ui_flag_fpath)
except Exception as e:
logging.error("Restarting UI: %s" % e)
def restart_webconf(self):
try:
check_output("systemctl restart zynthian-webconf", shell=True)
self.restart_webconf_flag = False
if os.path.isfile(self.restart_webconf_flag_fpath):
os.remove(self.restart_webconf_flag_fpath)
except Exception as e:
logging.error("Restarting Webconf: %s" % e)
def reload_wiring_layout(self):
liblo.send(zynthian_ui_osc_addr, "/CUIA/RELOAD_WIRING_LAYOUT")
self.reload_wiring_layout_flag = False
def reload_midi_config(self):
liblo.send(zynthian_ui_osc_addr, "/CUIA/RELOAD_MIDI_CONFIG")
self.reload_midi_config_flag = False
def reload_key_binding(self):
liblo.send(zynthian_ui_osc_addr, "/CUIA/RELOAD_KEY_BINDING")
self.reload_key_binding_flag = False
def persist_update_sys_flag(self):
check_output("touch /zynthian_update_sys", shell=True)
def persist_reboot_flag(self):
check_output("touch /tmp/zynthian_reboot", shell=True)
def read_reboot_flag(self):
self.reboot_flag = os.path.exists("/tmp/zynthian_reboot")
@classmethod
def update_sys(cls):
try:
zynconf.update_sys()
if os.path.isfile("/zynthian_update_sys"):
os.remove("/zynthian_update_sys")
except Exception as e:
logging.error("Updating System Config: %s" % e)
#------------------------------------------------------------------------------
# Zynthian Config Handler
#------------------------------------------------------------------------------
class ZynthianConfigHandler(ZynthianBasicHandler):
@tornado.web.authenticated
def get(self, title, config, errors=None):
super().get("config_block.html", title, config, errors)
def update_config(self, config):
sconfig={}
for vn in config:
if vn[0]!='_':
sconfig[vn]=config[vn][0]
zynconf.save_config(sconfig, updsys=True)
def config_env(self, config):
for vn in config:
if vn[0]!='_':
os.environ[vn]=config[vn][0]