[IMP] hw_posbox_homepage: restart buttons added

Before, the user had to manually unplug and replug the IoT Box to reboot it.
Also, it was required to connect to the IoT box to restart Odoo service.

This improvement adds the 'Restart Odoo' and 'Reboot IoT box' buttons on the box's form view to IoT app.
They allow the user to reboot the IoT box and restart Odoo service on it remotely from the IoT app in Odoo.

task-2476576

closes odoo/odoo#101045

X-original-commit: 662b41293315ae934502c683939d9820e4ca451a
Related: odoo/enterprise#31747
Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
This commit is contained in:
Yaroslav
2022-09-26 09:36:02 +02:00
parent 79895c77bc
commit 07df48592f
2 changed files with 91 additions and 0 deletions
@@ -356,3 +356,16 @@ class IoTboxHomepage(Home):
self.clean_partition()
_logger.error('A error encountered : %s ' % e)
return Response(str(e), status=500)
@http.route('/iot_restart_odoo_or_reboot', type='json', auth='none', cors='*', csrf=False)
def iot_restart_odoo_or_reboot(self, action):
""" Reboots the IoT Box / restarts Odoo on it depending on chosen 'action' argument"""
try:
if action == 'restart_odoo':
helpers.odoo_restart(3)
else:
subprocess.call(['sudo', 'reboot'])
return 'success'
except Exception as e:
_logger.error('An error encountered : %s ', e)
return str(e)
@@ -1,6 +1,25 @@
{% extends "layout.html" %}
{% from "loading.html" import loading_block_ui %}
{% block head %}
<style>
.btn-sm-restart {
display: flex;
min-width: 100%;
justify-content: center;
}
.item-restart {
display: flex;
flex-direction: column;
margin-left: auto;
max-width: 100%;
}
.text-green-primary {
display: flex;
width: 100%;
margin-top: 30px;
margin-bottom: 30px;
justify-content: center;
}
table {
width: 100%;
border-collapse: collapse;
@@ -81,9 +100,67 @@
content.css('max-height', maxHeight);
});
});
function display_error_and_clear_interval(interval, xhrStatus, thrownError) {
/// Displays the error message and stops sending requests to the server
if (interval) {
clearInterval(interval);
}
$('.loading-block').addClass('o_hide');
$('.error-message').text(xhrStatus + ": " + thrownError);
}
function restart_odoo_or_reboot(action) {
/// Call restart method on server, then ping it until restarting is finished
/// If an error is encountered, display it and stop
$('.loading-block').removeClass('o_hide');
$('.message-title').text('Restarting');
$('.message-status').text('Please wait');
$.ajax({
url: '/iot_restart_odoo_or_reboot/',
type: 'post',
contentType: 'application/json',
data: JSON.stringify({ params: {action: action} }),
timeout: 15000,
}).done(function(data) {
if (data.result == 'success') {
const interval = setInterval(function() {
$.ajax({
url:'/',
timeout: 4000
}).done(function() {
location.reload();
}).fail(function(xhr, textStatus, thrownError) {
if (xhr.status) {
display_error_and_clear_interval(interval, xhr.status, thrownError);
}
})
}, 4000)
setTimeout(function(){
display_error_and_clear_interval(interval, '0', 'timeout');
}, 600000);
}
else {
display_error_and_clear_interval(interval, 'Error', data.result);
}
}).fail(function(xhr, textStatus, thrownError) {
display_error_and_clear_interval(null, xhr.status, thrownError);
})
}
</script>
{% endblock %}
{% block content %}
<div class="collapse item-restart">
<div class="collapsible item-restart">
<div class="title arrow-down">Restart</div>
<div class="content">
<div class="device-status">
<button class="btn btn-sm btn-sm-restart" onclick="restart_odoo_or_reboot('reboot_iot_box')">Reboot the IoT Box</button>
<button class="btn btn-sm btn-sm-restart" onclick="restart_odoo_or_reboot('restart_odoo')">Restart Odoo service</button>
</div>
</div>
</div>
</div>
<h2 class="text-center text-green">Your IoT Box is up and running</h2>
<table align="center" cellpadding="3">
<tr>
@@ -149,4 +226,5 @@
<a class="btn" style="margin-left: 10px;" href='/list_credential'>Credential</a>
{% endif %}
</div>
{{ loading_block_ui(loading_message) }}
{% endblock %}