1
0
mirror of https://github.com/j-a-n/raspberrymatic-addon-rmupdate.git synced 2023-10-10 11:37:40 +00:00

working version

This commit is contained in:
Jan Schneider
2017-03-26 00:26:39 +01:00
parent 63580ae33f
commit 0f3cc9b506
4 changed files with 162 additions and 115 deletions

View File

@@ -31,13 +31,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<title>RaspberryMatic Update Addon</title>
<script>
var message_timer_id = null;
var install_running = false;
function display_message(type, html, millis) {
clear_message();
$('#message').html(html);
$('#message').attr('class', 'ui ' + type + ' message visible');
$('#message-edit-bridge').html(html);
$('#message-edit-bridge').attr('class', 'ui ' + type + ' message visible');
$('#message-log').html(html);
$('#message-log').attr('class', 'ui ' + type + ' message visible');
message_timer_id = setTimeout(clear_message, millis);
}
@@ -48,25 +49,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
message_timer_id = null;
$('#message').html('');
$('#message').attr('class', 'ui message hidden');
$('#message-edit-bridge').html('');
$('#message-edit-bridge').attr('class', 'ui message hidden');
$('#message-log').html('');
$('#message-log').attr('class', 'ui message hidden');
}
function default_error_callback(xhr, ajaxOptions, thrownError) {
console.error(xhr);
err = thrownError;
try {
obj = JSON.parse(xhr.responseText);
if (obj.error != null) {
err = obj.error;
}
}
catch(e) {
}
display_message('error', 'An error occurred: ' + err, 6000000);
}
function rest(method, path, data, success_callback, error_callback) {
if (!error_callback) {
error_callback = function(xhr, ajaxOptions, thrownError) {
console.error(xhr);
err = thrownError;
try {
obj = JSON.parse(xhr.responseText);
if (obj.error != null) {
err = obj.error;
}
}
catch(e) {
}
display_message('error', 'An error occurred: ' + err, 60000);
}
error_callback = default_error_callback
}
$.ajax({
url: "rest.cgi?" + path,
@@ -78,49 +81,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
});
}
function install_firmware(version) {
$('#modal-log').modal('show');
//$('#log-content').append('Some text<br/>');
//$('#modal-log').modal('refresh');
//$('#modal-log').modal('hide');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'rest.cgi?/install_firmware', true);
xhr.send(null);
var timer;
timer = window.setInterval(function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
window.clearTimeout(timer);
//$('body').append('done <br />');
function update_install_log() {
rest("GET", "/read_install_log", null, function(data) {
$('#log-content').html(data.replace(/\n/g, '<br />'));
$('#modal-log').modal('refresh');
$('#log-content').scrollTop($('#log-content').prop("scrollHeight"));
if (install_running) {
setTimeout(update_install_log, 1000);
}
//$('body').append('state: ' + xhr.readyState + '<br />');
console.log(xhr.responseText);
$('#log-content').append(xhr.responseText + '<br/>');
//$('body').append('data: ' + xhr.responseText + '<br />');
}, 1000);
/*
var last_response_len = false;
$.ajax('rest.cgi?/install_firmware', {
xhrFields: {
onprogress: function(e) {
var this_response, response = e.currentTarget.response;
if(last_response_len === false) {
this_response = response;
last_response_len = response.length;
}
else {
this_response = response.substring(last_response_len);
last_response_len = response.length;
}
console.log(this_response);
}
}
}).done(function(data) {
console.log('Complete response = ' + data);
}).fail(function(data) {
console.log('Error: ', data);
});
console.log('Request Sent');
*/
}
function install_firmware(version) {
display_message('info', 'Installing firmware ' + version + '.', 6000000);
install_running = true;
clear_message();
$('#log-content').html('');
$('#modal-log').modal('show');
rest("POST", "/start_install_firmware", version,
function(data) {
install_running = false;
display_message('success', 'Firmware ' + version + ' successfully installed.', 6000000);
},
function(xhr, ajaxOptions, thrownError) {
install_running = false;
//$('#modal-log').modal('hide');
default_error_callback(xhr, ajaxOptions, thrownError);
}
);
setTimeout(update_install_log, 1000);
}
$(document).ready(function() {
@@ -174,13 +163,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</table>
</div>
<div id="modal-log" class="ui modal">
<div style="height:80%" id="modal-log" class="ui modal">
<i class="close icon"></i>
<div class="header">
Progress
</div>
<div id="log-content" class="content">
Line 1
<div class="content">
<div id="message-log" class="ui message hidden">
</div>
<pre style="max-height:80%; overflow-x:hidden; overflow-y:auto; white-space: pre-wrap;" id="log-content" class="content">
</pre>
</div>
</div>
</body>

View File

@@ -47,43 +47,36 @@ proc process {} {
return "\"[rmupdate::version]\""
} elseif {[lindex $path 1] == "get_firmware_info"} {
return [rmupdate::get_firmware_info]
} elseif {[lindex $path 1] == "install_firmware"} {
fconfigure stdout -buffering none
#puts "Content-Type: application/octet-stream"
puts "Content-Type: text/html; charset=utf-8"
puts "Status: 200 OK";
puts ""
flush stdout
after 1000
puts "Line 1\n"
flush stdout
after 1000
puts "Line 2\n"
flush stdout
after 1000
puts "Line 3\n"
flush stdout
after 1000
puts "Line 4\n"
flush stdout
return ""
} elseif {[lindex $path 1] == "start_install_firmware"} {
regexp {^([\d\.]+)$} $data match version
if { [info exists version] && $version != "" } {
rmupdate::install_firmware_version $version
return "\"done\""
} else {
error "Invalid version: ${data}"
}
} elseif {[lindex $path 1] == "read_install_log"} {
variable content_type "text/html"
return [rmupdate::read_install_log]
}
}
error "invalid request" "Not found" 404
}
variable content_type "application/json"
if [catch {process} result] {
set status 500
if { [info exists $errorCode] } {
set status $errorCode
}
puts "Content-Type: application/json"
puts "Content-Type: ${content_type}"
puts "Status: $status";
puts ""
set result [json_string $result]
puts -nonewline "\{\"error\":\"${result}\"\}"
} elseif {$result != ""} {
puts "Content-Type: application/json"
} else {
puts "Content-Type: ${content_type}"
puts "Status: 200 OK";
puts ""
puts -nonewline $result