var socket = io('http://127.0.0.1:3002');
const backend = "http://127.0.0.1:3001";
var lastAlarm="No alarms";
socket.on('msg', function(data) {
lastAlarm = JSON.stringify(data);
document.getElementById('icon_alarm').style = "font-size: 2em; color: red;";
});
function addMessage(message) {
document.getElementById('sensorvalue').value = message;
}
$( document ).ready(function() {
// Delete automatic generated buttons in grids
$(".custom_table > div").remove();
// Attach listeners for popus
$("#icon_alarm").click( function() {
$('#alarm_popup_dsc').val(lastAlarm);
$('#alarm_popup').css('display','flex');
document.getElementById('icon_alarm').style = "font-size: 2em; color: black;";
});
$("#alarm_manual").click( function() {
});
setTimeout(function(){
function editAlarm(alarmId){
$.ajax({
url: backend+"/alarms/"+alarmId.target.value,
success: function(result) {
$( "#alarm_edit_popup_dsc" ).val(JSON.stringify(result));
$('#alarm_edit_popup').css('display','flex');
},
dataType: 'json'
});
}
$("#alarm_table_table_body").find('tr').each(function(){
var trow = $(this);
if(trow.index() === 0){
trow.prepend('<th><button id="alarm_filter" class="button btn btn-primary m-1" value="">Filter</button></th>');
$("#alarm_filter").click( function() {
alert("filter");
});
}else{
alarmId = trow[0].firstElementChild.innerHTML;
trow.prepend(`<td><button id="alarm_edit_${alarmId}" class="button btn btn-primary m-1" value=${alarmId}>Edit</button></td>`);
$("#alarm_edit_"+alarmId).click( function(alarmId) {
editAlarm(alarmId)
});
}
});
}, 1000);
// Attach modals
// Attach modal user
function addUser() {
let obj = JSON.stringify({
"username": $( "#user_popup_name" ).val(),
"email": $( "#user_popup_email" ).val(),
"organisationId": $( "#user_popup_organisationId" ).val()
});
$.ajax({
type: 'POST',
url: backend+"/users",
data: obj,
success: function(data) { location.reload(true); },
error: function(e) { alert(e); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
$( "#user_create" ).button().on("click", function() {
$('#user_popup').css('display','flex');
});
// action for the button within popup
$( "#user_popup_create" ).button().on("click", function() {
addUser();
});
// switch off default hide behaviour
$( "#user_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#user_popup_cancel" ).button().on("click", function() {
$("#user_popup_cancel").closest('form').find("input[type=text], textarea").val("");
$('#user_popup').css('display','none');
});
// Attach modal organisation
function addOrganisation() {
let name = $( "#organisation_popup_name" ).val();
let obj = JSON.stringify({
"name": name
});
$.ajax({
type: 'POST',
url: backend+"/organisations",
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
$( "#organisation_create" ).button().on("click", function() {
$('#organisation_popup').css('display','flex');
});
// action for the button within popup
$( "#organisation_popup_create" ).button().on("click", function() {
addOrganisation();
});
// switch off default hide behaviour
$( "#organisation_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#organisation_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#organisation_popup').css('display','none');
});
// Attach modal machine
function addMachine() {
let obj = JSON.stringify({
"name": $( "#machine_popup_name" ).val(),
"_did": $( "#machine_popup_did" ).val(),
"organisationId": $( "#machine_popup_organisationId" ).val(),
"failureId": $( "#machine_popup_failureId" ).val()
});
$.ajax({
type: 'POST',
url: backend+"/machines",
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
$( "#machine_create" ).button().on("click", function() {
$('#machine_popup').css('display','flex');
});
// action for the button within popup
$( "#machine_popup_create" ).button().on("click", function() {
addMachine();
});
// switch off default hide behaviour
$( "#machine_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#machine_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#machine_popup').css('display','none');
});
// Attach modal failure
function addFailure() {
let obj = JSON.stringify({
"value": parseInt($( "#failure_popup_value" ).val()),
"msg": $( "#failure_popup_msg" ).val()
});
$.ajax({
type: 'POST',
url: backend+"/failures",
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
$( "#failure_create" ).button().on("click", function() {
$('#failure_popup').css('display','flex');
});
// action for the button within popup
$( "#failure_popup_create" ).button().on("click", function() {
addFailure();
});
// switch off default hide behaviour
$( "#failure_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#failure_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#failure_popup').css('display','none');
});
// Attach modal Interventions
function addIntervention() {
let obj = JSON.stringify({
"description": $( "#intervention_popup_dsc" ).val(),
"status": $( "#intervention_popup_status" ).val(),
"timestamp": new Date()
});
$.ajax({
type: 'POST',
url: backend+"/interventions",
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
$( "#intervention_create" ).button().on("click", function() {
$('#intervention_popup').css('display','flex');
});
// action for the button within popup
$( "#intervention_popup_create" ).button().on("click", function() {
addIntervention();
});
// switch off default hide behaviour
$( "#intervention_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#intervention_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#intervention_popup').css('display','none');
});
// Attach modal Alarm
function acceptAlarm() {
let obj = $( "#alarm_popup_dsc" ).val();
let id = JSON.parse($( "#alarm_popup_dsc" ).val()).id;
// let obj = JSON.stringify({
// "description": $( "#alarm_popup_dsc" ).val(),
// "status": $( "#alarm_popup_comment" ).val()
// });
$.ajax({
type: 'PATCH',
url: backend+"/alarms/"+id,
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
function dismissAlarm() {
let obj = $( "#alarm_popup_dsc" ).val();
let id = JSON.parse($( "#alarm_popup_dsc" ).val()).id;
// let obj = JSON.stringify({
// "description": $( "#alarm_popup_dsc" ).val(),
// "status": $( "#alarm_popup_comment" ).val()
// });
$.ajax({
type: 'PATCH',
url: backend+"/alarms/"+id,
data: obj,
success: function(data) { location.reload(true); },
contentType: "application/json",
dataType: 'json'
});
}
// popup for button
// action for the button within popup
$( "#alarm_popup_accept" ).button().on("click", function() {
acceptAlarm();
$('#alarm_popup').css('display','none');
});
$( "#alarm_popup_dismiss" ).button().on("click", function() {
dismissAlarm();
$('#alarm_popup').css('display','none');
});
$( "#alarm_edit_popup_accept" ).button().on("click", function() {
acceptAlarm();
$('#alarm_edit_popup').css('display','none');
});
$( "#alarm_edit_popup_dismiss" ).button().on("click", function() {
dismissAlarm();
$('#alarm_edit_popup').css('display','none');
});
// switch off default hide behaviour
$( "#alarm_popup" ).removeAttr("onclick");
$( "#alarm_edit_popup" ).removeAttr("onclick");
// assign hide to cancel
$( "#alarm_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#alarm_popup').css('display','none');
});
$( "#alarm_edit_popup_cancel" ).button().on("click", function() {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#alarm_edit_popup').css('display','none');
});
});