mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
🔒DB lock v0.4 #685 + 🔔userNotifications
This commit is contained in:
@@ -23,16 +23,19 @@ function readData(sqlQuery, processDataCallback, valuesArray, targetLocation, ta
|
||||
// Check if database is locked
|
||||
function checkDbLock() {
|
||||
$.ajax({
|
||||
url: 'php/server/dbHelper.php', // Replace with the actual path to your PHP file
|
||||
url: 'log/db_is_locked.log', // Replace with the actual path to your PHP file
|
||||
type: 'GET',
|
||||
data: { action: 'checkLock' },
|
||||
|
||||
success: function(response) {
|
||||
if (response == 1) {
|
||||
console.log('🟥 Database is locked');
|
||||
$(".header-status-locked-db").show()
|
||||
} else {
|
||||
// console.log(response);
|
||||
if (response == 0) {
|
||||
// console.log('Database is not locked');
|
||||
$(".header-status-locked-db").hide()
|
||||
|
||||
} else {
|
||||
console.log('🟥 Database is locked:');
|
||||
console.log(response);
|
||||
$(".header-status-locked-db").show()
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
@@ -42,7 +45,8 @@ function checkDbLock() {
|
||||
});
|
||||
}
|
||||
|
||||
// Start the loop
|
||||
setInterval(() => {
|
||||
checkDbLock();
|
||||
}, 1000);
|
||||
setInterval(checkDbLock(), 1000);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -192,17 +192,17 @@ function showMessage(textMessage = "", timeout = 3000, colorClass = "modal_green
|
||||
alert(textMessage);
|
||||
} else {
|
||||
// show temporary notification
|
||||
$("#notification").removeClass(); // remove all classes
|
||||
$("#notification").addClass("alert alert-dimissible notification_modal"); // add default ones
|
||||
$("#notification").addClass(colorClass); // add color modifiers
|
||||
$("#notification_modal").removeClass(); // remove all classes
|
||||
$("#notification_modal").addClass("alert alert-dimissible notification_modal"); // add default ones
|
||||
$("#notification_modal").addClass(colorClass); // add color modifiers
|
||||
|
||||
// message
|
||||
$("#alert-message").html(textMessage);
|
||||
|
||||
// timeout
|
||||
$("#notification").fadeIn(1, function () {
|
||||
$("#notification_modal").fadeIn(1, function () {
|
||||
window.setTimeout(function () {
|
||||
$("#notification").fadeOut(500);
|
||||
$("#notification_modal").fadeOut(500);
|
||||
}, timeout);
|
||||
});
|
||||
}
|
||||
@@ -240,3 +240,66 @@ $(document).ready(function () {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Backend notification Polling
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function to check for notifications
|
||||
function checkNotification() {
|
||||
const notificationEndpoint = 'php/server/utilNotification.php?action=get_unread_notifications';
|
||||
const phpEndpoint = 'php/server/utilNotification.php';
|
||||
|
||||
$.ajax({
|
||||
url: notificationEndpoint,
|
||||
type: 'GET',
|
||||
success: function(response) {
|
||||
// console.log(response);
|
||||
|
||||
if(response != "[]")
|
||||
{
|
||||
|
||||
// Find the oldest unread notification with level "interrupt"
|
||||
const oldestInterruptNotification = response.find(notification => notification.read === 0 && notification.level === "interrupt");
|
||||
|
||||
if (oldestInterruptNotification) {
|
||||
// Show modal dialog with the oldest unread notification
|
||||
|
||||
const decodedContent = JSON.parse(decodeURIComponent(oldestInterruptNotification.content));
|
||||
|
||||
showModalOK("Notification", decodedContent, function() {
|
||||
// Mark the notification as read
|
||||
$.ajax({
|
||||
url: phpEndpoint,
|
||||
type: 'GET',
|
||||
data: {
|
||||
action: 'mark_notification_as_read',
|
||||
guid: oldestInterruptNotification.guid
|
||||
},
|
||||
success: function(response) {
|
||||
console.log(response);
|
||||
// After marking the notification as read, check for the next one
|
||||
checkNotification();
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error("Error marking notification as read:", status, error);
|
||||
},
|
||||
complete:function() {
|
||||
hideSpinner();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
console.warn(`🟥 Error checking ${notificationEndpoint}`)
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Start checking for notifications periodically
|
||||
setInterval(checkNotification, 3000);
|
||||
|
||||
|
||||
|
||||
51
front/js/tests.js
Executable file
51
front/js/tests.js
Executable file
@@ -0,0 +1,51 @@
|
||||
// --------------------------------------------------
|
||||
// Check if database is locked
|
||||
function lockDatabase(delay=20) {
|
||||
$.ajax({
|
||||
url: 'php/server/dbHelper.php', // Replace with the actual path to your PHP file
|
||||
type: 'GET',
|
||||
data: { action: 'lockDatabase', delay: delay },
|
||||
success: function(response) {
|
||||
console.log('Executed');
|
||||
},
|
||||
error: function() {
|
||||
console.log('Error ocurred');
|
||||
}
|
||||
});
|
||||
|
||||
let times = delay;
|
||||
let countdownInterval = setInterval(() => {
|
||||
times--;
|
||||
console.log(`Remaining time: ${times} seconds`);
|
||||
|
||||
if (times <= 0) {
|
||||
clearInterval(countdownInterval);
|
||||
console.log('Countdown finished');
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
|
||||
function writeNotification(content, level) {
|
||||
|
||||
const phpEndpoint = 'php/server/utilNotification.php';
|
||||
|
||||
$.ajax({
|
||||
url: phpEndpoint, // Change this to the path of your PHP script
|
||||
type: 'GET',
|
||||
data: {
|
||||
action: 'write_notification',
|
||||
content: content,
|
||||
level: level
|
||||
},
|
||||
success: function(response) {
|
||||
alert('Notification written successfully.');
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error writing notification:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user