Skip to content

Commit ae385e0

Browse files
committed
- Fixing Remote Control Server strter window colors
Trying to make Remote Control Server more stable
1 parent fb4e9dc commit ae385e0

File tree

4 files changed

+91
-69
lines changed

4 files changed

+91
-69
lines changed

pyradio/edit.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def _show_title(self):
500500
self._win.chgat(0, x, 4, curses.color_pair(12))
501501
else:
502502
self._win.chgat(0, x, 2, curses.color_pair(12))
503-
self._win.refresh()
503+
self._refresh()
504504

505505
def _show_buttons(self):
506506
sid = 3
@@ -1301,7 +1301,7 @@ def show(self, parent=None):
13011301
if parent:
13021302
self._parent = parent
13031303
self._get_window()
1304-
self._win.bkgdset(' ', curses.color_pair(12))
1304+
self._win.bkgdset(' ', curses.color_pair(3))
13051305
self._win.erase()
13061306
self._win.box()
13071307
self._show_title()
@@ -1328,12 +1328,6 @@ def show(self, parent=None):
13281328
self._editor.set_global_functions(self._global_functions)
13291329
self._editor._paste_mode = False
13301330
self._editor.chars_to_accept = [ str(x) for x in range(0, 10)]
1331-
self._editor.set_local_functions(
1332-
local_functions = {
1333-
ord('j'): self._toggle_selection,
1334-
ord('k'): self._toggle_selection
1335-
}
1336-
)
13371331
self._editor.string = self._the_port
13381332

13391333

@@ -1349,33 +1343,33 @@ def show(self, parent=None):
13491343

13501344

13511345
try:
1352-
self._win.addstr(7, 3, '─' * (self.maxX - 6), curses.color_pair(12))
1346+
self._win.addstr(7, 3, '─' * (self.maxX - 6), curses.color_pair(3))
13531347
except:
1354-
self._win.addstr(7, 3, '─'.encode('utf-8') * (self.maxX - 6), curses.color_pair(12))
1355-
self._win.addstr(7, int((self.maxX - 6) / 2), ' Help ', curses.color_pair(4))
1356-
self._win.addstr(8, 8, 'j', curses.color_pair(4))
1348+
self._win.addstr(7, 3, '─'.encode('utf-8') * (self.maxX - 6), curses.color_pair(3))
1349+
self._win.addstr(7, int((self.maxX - 6) / 2), ' Help ', curses.color_pair(11))
1350+
self._win.addstr(8, 8, 'j', curses.color_pair(11))
13571351
self._win.addstr(', ', curses.color_pair(10))
1358-
self._win.addstr('k', curses.color_pair(4))
1352+
self._win.addstr('k', curses.color_pair(11))
13591353
self._win.addstr(', ', curses.color_pair(10))
1360-
self._win.addstr('Up', curses.color_pair(4))
1354+
self._win.addstr('Up', curses.color_pair(11))
13611355
self._win.addstr(', ', curses.color_pair(10))
1362-
self._win.addstr('Down', curses.color_pair(4))
1356+
self._win.addstr('Down', curses.color_pair(11))
13631357
self._win.addstr(8, 30, 'Change selection.', curses.color_pair(10))
13641358

1365-
self._win.addstr(9, 8, 'h', curses.color_pair(4))
1359+
self._win.addstr(9, 8, 'h', curses.color_pair(11))
13661360
self._win.addstr(', ', curses.color_pair(10))
1367-
self._win.addstr('l', curses.color_pair(4))
1361+
self._win.addstr('l', curses.color_pair(11))
13681362
self._win.addstr(', ', curses.color_pair(10))
1369-
self._win.addstr('Left', curses.color_pair(4))
1363+
self._win.addstr('Left', curses.color_pair(11))
13701364
self._win.addstr(', ', curses.color_pair(10))
1371-
self._win.addstr('Right', curses.color_pair(4))
1365+
self._win.addstr('Right', curses.color_pair(11))
13721366

1373-
self._win.addstr(10, 8, 'Enter', curses.color_pair(4))
1367+
self._win.addstr(10, 8, 'Enter', curses.color_pair(11))
13741368
self._win.addstr(', ', curses.color_pair(10))
1375-
self._win.addstr('Space', curses.color_pair(4))
1369+
self._win.addstr('Space', curses.color_pair(11))
13761370
self._win.addstr(10, 30, 'Toggle IP.', curses.color_pair(10))
13771371

1378-
self._win.addstr(11, 8, 's', curses.color_pair(4))
1372+
self._win.addstr(11, 8, 's', curses.color_pair(11))
13791373
self._win.addstr(11, 30, 'Start the Server.', curses.color_pair(10))
13801374

13811375
self._win.addstr(12, 2, 'Any other key will hide the window (not in editor).', curses.color_pair(10))
@@ -1409,16 +1403,17 @@ def _toggle_selection(self):
14091403
if self._selection == 0:
14101404
self._selection = 1
14111405
self._editor.focused = True
1412-
self._win.chgat(5, self._field_x, self._field_width, curses.color_pair(6))
1406+
self._win.chgat(4, self._field_x, self._field_width, curses.color_pair(10))
14131407
else:
14141408
if self._validate_port():
14151409
self._selection = 0
14161410
self._editor.focused = False
1417-
self._win.chgat(5, self._field_x, self._field_width, curses.color_pair(10))
1411+
self._win.chgat(4, self._field_x, self._field_width, curses.color_pair(6))
14181412
else:
14191413
self._port_number_error_message()
14201414
self._refresh()
14211415

1416+
14221417
def _validate_port(self):
14231418
if self._editor.string:
14241419
x = int(self._editor.string)
@@ -1445,7 +1440,8 @@ def keypress(self, char):
14451440
self._the_ip = 'LAN'
14461441
else:
14471442
self._the_ip = 'localhost'
1448-
self._win.addstr(5, self._field_x, self._the_ip.ljust(self._field_width), curses.color_pair(6))
1443+
self._win.addstr(4, self._field_x, self._the_ip.ljust(self._field_width), curses.color_pair(6))
1444+
self._refresh()
14491445

14501446
elif char == ord('r'):
14511447
self._the_ip = self._cnf.active_remote_control_server_ip

pyradio/log.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,12 @@ def _set_web_title(self, msg):
303303
if server:
304304
''' remote control server running '''
305305
title = None
306+
# if msg.startswith('Playing: ') or \
307+
# msg.startswith('Connecting to: ') or \
308+
# msg.startswith('Initialization: ') or \
309+
# 'abnormal' in msg:
310+
# title = msg
306311
if msg.startswith('Playing: ') or \
307-
msg.startswith('Connecting to: ') or \
308-
msg.startswith('Initialization: ') or \
309312
'abnormal' in msg:
310313
title = msg
311314
elif msg.startswith('Failed to'):

pyradio/radio.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class PyRadio(object):
226226
stop_watch_theme_thread = stop_update_notification_thread = False
227227
_watch_theme_lock = threading.Lock()
228228
_update_notify_lock = threading.Lock()
229+
_server_send_lock = threading.Lock()
229230

230231
''' editor class '''
231232
_station_editor = None
@@ -6020,9 +6021,11 @@ def keypress(self, char):
60206021
self._start_remote_control_server()
60216022
if self._remote_control_server_thread:
60226023
self._show_remote_control_server_active()
6024+
self.refreshBody()
60236025
elif ret == -1:
6026+
self._remote_control_window = None
60246027
self.ws.close_window()
6025-
self.refreshBody()
6028+
self.refreshBody()
60266029

60276030
elif char == ord('H') and self.ws.operation_mode in \
60286031
(self.ws.NORMAL_MODE, self.ws.PLAYLIST_MODE):
@@ -9263,6 +9266,7 @@ def _start_remote_control_server(self):
92639266
self._print_remote_control_server_error,
92649267
self._print_remote_control_server_dead_error,
92659268
lambda: self.log.song_title,
9269+
self._server_send_lock
92669270
)
92679271
)
92689272
'''

pyradio/server.py

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,28 @@ class PyRadioServer(object):
133133
<div id="the_blocking_box">
134134
</div>
135135
-->
136-
<div class="row text-center" onclick="js_refresh_page();" style="background: green; color: white; padding-bottom: 15px;">
137-
<h2>PyRadio Remote Control</h2>
138-
</div>
139-
<div id="title_container" class="row" style="margin: 3px; margin-top: 15px;>
140-
<div class="col-xs-12">
141-
<div id="song_title" class="alert alert-info text-center">
142-
<b>No data!</b>
136+
<a href="/html">
137+
<div class="row text-center" onclick="js_refresh_page();" style="background: green; color: white; padding-bottom: 15px;">
138+
<h2>PyRadio Remote Control</h2>
139+
</div>
140+
</a>
141+
<a href="#" onclick="js_send_simple_command('/html/title', 0);">
142+
<div id="title_container" class="row" style="margin: 3px; margin-top: 15px;>
143+
<div class="col-xs-12">
144+
<div id="song_title" class="alert alert-info text-center">
145+
<b>No data!</b>
146+
</div>
143147
</div>
144148
</div>
145-
</div>
149+
</a>
146150
<div id="all_buttons" class="row">
147151
<div class="col-xs-4 col-lg-4">
148152
<div class="text-center">
149-
<button onclick="js_send_simple_command('/html/next', 1500);" type="button" class="btn btn-warning">Play<br>Next</button>
150-
<button onclick="js_send_simple_command('/html/previous', 1500);" type="button" class="btn btn-warning">Play<br>Previous</button>
151-
<button onclick="js_send_simple_command('/html/histnext', 1500);" type="button" class="btn btn-success">Play Hist.<br> Next</button>
152-
<button onclick="js_send_simple_command('/html/histprev', 1500);" type="button" class="btn btn-success">Play Hist.<br>Previous</button>
153-
<button onclick="js_send_simple_command('/html/toggle', 1500);" type="button" class="btn btn-danger">Toggle<br>Playback</button>
153+
<button onclick="js_send_simple_command_with_stop('/html/next', 1500);" type="button" class="btn btn-warning">Play<br>Next</button>
154+
<button onclick="js_send_simple_command_with_stop('/html/previous', 1500);" type="button" class="btn btn-warning">Play<br>Previous</button>
155+
<button onclick="js_send_simple_command_with_stop('/html/histnext', 1500);" type="button" class="btn btn-success">Play Hist.<br> Next</button>
156+
<button onclick="js_send_simple_command_with_stop('/html/histprev', 1500);" type="button" class="btn btn-success">Play Hist.<br>Previous</button>
157+
<button onclick="js_send_simple_command_with_stop('/html/toggle', 1500);" type="button" class="btn btn-danger">Toggle<br>Playback</button>
154158
</div>
155159
</div>
156160
<div class="col-xs-4 col-lg-4">
@@ -225,7 +229,14 @@ class PyRadioServer(object):
225229
226230
////////////////////////////////////////////////////////////////////
227231
232+
function js_send_simple_command_with_stop(the_command, the_timeout){
233+
js_set_title("#song_title", "<b>Player is stopped!</b>", the_command);
234+
js_disable_buttons_on_stopped(true);
235+
js_send_simple_command(the_command, the_timeout);
236+
}
237+
228238
function js_send_simple_command(the_command, the_timeout){
239+
229240
$.get(the_command, function(result){
230241
// console.log(the_command, result, typeof result);
231242
//
@@ -365,7 +376,7 @@ class PyRadioServer(object):
365376
url_to_reload = window.location.href;
366377
js_fix_muted();
367378
js_fix_stopped();
368-
setTimeout(js_init_title, 2000);
379+
setTimeout(js_init_title, 500);
369380
}
370381
371382
function js_init_title(){
@@ -463,6 +474,7 @@ def start_remote_control_server(
463474
error_func,
464475
dead_func,
465476
song_title,
477+
lock
466478
):
467479
self._path = ''
468480
self.config = config
@@ -475,6 +487,7 @@ def start_remote_control_server(
475487
'''
476488
self.sel = sel
477489
self.muted = muted
490+
self.lock = lock
478491
try:
479492
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
480493
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -979,12 +992,13 @@ def send_song_title(self, msg=None):
979992
Content-Length: {}
980993
981994
'''.format(len(b_msg)).encode('utf-8')
982-
try:
983-
self.client_socket.sendall(txt + b_msg)
984-
except socket.error as e:
985-
self.error = e
986-
except AttributeError:
987-
pass
995+
with self.lock:
996+
try:
997+
self.client_socket.sendall(txt + b_msg)
998+
except socket.error as e:
999+
self.error = e
1000+
except AttributeError:
1001+
pass
9881002

9891003
def _send_raw(self, msg):
9901004
if msg.startswith('retry: '):
@@ -1008,10 +1022,11 @@ def _send_raw(self, msg):
10081022
Content-Length: {}
10091023
10101024
'''.format(len(b_msg)).encode('utf-8')
1011-
try:
1012-
self.client_socket.sendall(txt + b_msg)
1013-
except socket.error as e:
1014-
self.error = e
1025+
with self.lock:
1026+
try:
1027+
self.client_socket.sendall(txt + b_msg)
1028+
except socket.error as e:
1029+
self.error = e
10151030

10161031
def _send_text(self,
10171032
msg, alert_type='alert-info',
@@ -1044,10 +1059,11 @@ def _send_text(self,
10441059
Content-Length: {}
10451060
10461061
'''.format(len(b_msg)).encode('utf-8')
1047-
try:
1048-
self.client_socket.sendall(txt + b_msg)
1049-
except socket.error as e:
1050-
self.error = e
1062+
with self.lock:
1063+
try:
1064+
self.client_socket.sendall(txt + b_msg)
1065+
except socket.error as e:
1066+
self.error = e
10511067

10521068
def _send_html(self, msg=None, put_script=False):
10531069
f_msg = self._html + '\n'
@@ -1073,10 +1089,11 @@ def _send_html(self, msg=None, put_script=False):
10731089
Content-Length: {}
10741090
10751091
'''.format(len(b_msg)).encode('utf-8')
1076-
try:
1077-
self.client_socket.sendall(txt + b_msg)
1078-
except socket.error as e:
1079-
self.error = e
1092+
with self.lock:
1093+
try:
1094+
self.client_socket.sendall(txt + b_msg)
1095+
except socket.error as e:
1096+
self.error = e
10801097

10811098
def _get_numbers(self, comma):
10821099
if logger.isEnabledFor(logging.DEBUG):
@@ -1151,13 +1168,15 @@ def close_server(self):
11511168
s.close()
11521169
return False, e
11531170

1154-
try:
1155-
s.connect((self._bind_ip, self._bind_port))
1156-
request = "GET /quit HTTP/1.0\n\n".encode('utf-8')
1157-
s.sendall(request)
1158-
except socket.error as e:
1159-
s.close()
1160-
return False, e
1171+
1172+
with self.lock:
1173+
try:
1174+
s.connect((self._bind_ip, self._bind_port))
1175+
request = "GET /quit HTTP/1.0\n\n".encode('utf-8')
1176+
s.sendall(request)
1177+
except socket.error as e:
1178+
s.close()
1179+
return False, e
11611180

11621181
# Receive data
11631182
try:
@@ -1319,9 +1338,9 @@ def _format_html_table(
13191338
else:
13201339
t_url = url[2].format(playlist_index+1, i+1)
13211340
if sel == i:
1322-
out.append(' <td id="' + str(i+1) + '"><a style="color: white;" href="#" onclick="js_send_simple_command(\'' + t_url + '\', ' + timeout[index] + ');">' + n + '</a></td>')
1341+
out.append(' <td id="' + str(i+1) + '"><a style="color: white;" href="#" onclick="js_send_simple_command_with_stop(\'' + t_url + '\', ' + timeout[index] + ');">' + n + '</a></td>')
13231342
else:
1324-
out.append(' <td id="' + str(i+1) + '"><a href="#" onclick="js_send_simple_command(\'' + t_url + '\', ' + timeout[index] + ');">' + n + '</a></td>')
1343+
out.append(' <td id="' + str(i+1) + '"><a href="#" onclick="js_send_simple_command_with_stop(\'' + t_url + '\', ' + timeout[index] + ');">' + n + '</a></td>')
13251344
out.append(' </tr>')
13261345
out.append(' </tbody>')
13271346
out.append(' </table>')

0 commit comments

Comments
 (0)