refactor UI backend calls to python endpoints

This commit is contained in:
Jokob @NetAlertX
2026-01-10 03:06:02 +00:00
parent 6aa4e13b54
commit d849583dd5
33 changed files with 2186 additions and 313 deletions

View File

@@ -1,6 +1,6 @@
function getApiBase()
{
apiBase = getSetting("BACKEND_API_URL");
let apiBase = getSetting("BACKEND_API_URL");
if(apiBase == "")
{

View File

@@ -686,26 +686,43 @@ function numberArrayFromString(data)
}
// -----------------------------------------------------------------------------
function saveData(functionName, id, value) {
// Update network parent/child relationship (network tree)
function updateNetworkLeaf(leafMac, parentMac) {
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const url = `${apiBase}/device/${leafMac}/update-column`;
$.ajax({
method: "GET",
url: "php/server/devices.php",
data: { action: functionName, id: id, value:value },
success: function(data) {
if(sanitize(data) == 'OK')
{
showMessage("Saved")
// Remove navigation prompt "Are you sure you want to leave..."
window.onbeforeunload = null;
} else
{
showMessage("ERROR")
}
method: "POST",
url: url,
headers: { "Authorization": `Bearer ${apiToken}` },
data: JSON.stringify({ columnName: "devParentMAC", columnValue: parentMac }),
contentType: "application/json",
success: function(response) {
if(response.success) {
showMessage("Saved");
// Remove navigation prompt "Are you sure you want to leave..."
window.onbeforeunload = null;
} else {
showMessage("ERROR: " + (response.error || "Unknown error"));
}
},
error: function(xhr, status, error) {
console.error("Error updating network leaf:", status, error);
showMessage("ERROR: " + (xhr.responseJSON?.error || error));
}
});
}
// -----------------------------------------------------------------------------
// Legacy function wrapper for backward compatibility
function saveData(functionName, id, value) {
if (functionName === 'updateNetworkLeaf') {
updateNetworkLeaf(id, value);
} else {
console.warn("saveData called with unknown functionName:", functionName);
showMessage("ERROR: Unknown function");
}
}

View File

@@ -32,27 +32,62 @@ function renderList(
// remove first item containing the SQL query
options.shift();
const apiUrl = `php/server/dbHelper.php?action=read&rawSql=${btoa(encodeURIComponent(sqlQuery))}`;
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const url = `${apiBase}/dbquery/read`;
$.get(apiUrl, function (sqlOptionsData) {
// Parse the returned SQL data
const sqlOption = JSON.parse(sqlOptionsData);
// Unicode-safe base64 encoding
const base64Sql = btoa(unescape(encodeURIComponent(sqlQuery)));
// Concatenate options from SQL query with the supplied options
options = options.concat(sqlOption);
$.ajax({
url,
method: "POST",
headers: { "Authorization": `Bearer ${apiToken}` },
data: JSON.stringify({ rawSql: base64Sql }),
contentType: "application/json",
success: function(data) {
console.log("SQL query response:", data);
// Process the combined options
setTimeout(() => {
processDataCallback(
options,
valuesArray,
targetField,
transformers,
placeholder
);
}, 1);
// Parse the returned SQL data
let sqlOption = [];
if (data && data.success && data.results) {
sqlOption = data.results;
} else if (Array.isArray(data)) {
// Fallback for direct array response
sqlOption = data;
} else {
console.warn("Unexpected response format:", data);
}
// Concatenate options from SQL query with the supplied options
options = options.concat(sqlOption);
console.log("Combined options:", options);
// Process the combined options
setTimeout(() => {
processDataCallback(
options,
valuesArray,
targetField,
transformers,
placeholder
);
}, 1);
},
error: function(xhr, status, error) {
console.error("Error loading SQL options:", status, error, xhr.responseJSON);
// Process original options anyway
setTimeout(() => {
processDataCallback(
options,
valuesArray,
targetField,
transformers,
placeholder
);
}, 1);
}
});
} else {
// No SQL query, directly process the supplied options
@@ -85,7 +120,7 @@ function renderList(
// Check if database is locked
function checkDbLock() {
$.ajax({
url: "php/server/query_logs.php?file=db_is_locked.log",
url: "php/server/query_logs.php?file=db_is_locked.log",
type: "GET",
success: function (response) {

View File

@@ -33,13 +33,23 @@ function deleteDevice() {
// Check MAC
mac = getMac()
// Delete device
$.get('php/server/devices.php?action=deleteDevice&mac=' + mac, function (msg) {
showMessage(msg);
});
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const url = `${apiBase}/device/${mac}/delete`;
// refresh API
updateApi("devices,appevents")
$.ajax({
url,
method: "DELETE",
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
showMessage(response.success ? "Device deleted successfully" : (response.error || "Unknown error"));
updateApi("devices,appevents");
},
error: function(xhr, status, error) {
console.error("Error deleting device:", status, error);
showMessage("Error: " + (xhr.responseJSON?.error || error));
}
});
}
// -----------------------------------------------------------------------------
@@ -47,16 +57,23 @@ function deleteDeviceByMac(mac) {
// Check MAC
mac = getMac()
// alert(mac)
// return;
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const url = `${apiBase}/device/${mac}/delete`;
// Delete device
$.get('php/server/devices.php?action=deleteDevice&mac=' + mac, function (msg) {
showMessage(msg);
$.ajax({
url,
method: "DELETE",
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
showMessage(response.success ? "Device deleted successfully" : (response.error || "Unknown error"));
updateApi("devices,appevents");
},
error: function(xhr, status, error) {
console.error("Error deleting device:", status, error);
showMessage("Error: " + (xhr.responseJSON?.error || error));
}
});
// refresh API
updateApi("devices,appevents")
}

View File

@@ -443,12 +443,14 @@ function safeDecodeURIComponent(content) {
// -----------------------------------------------------------------------------
// Function to check for notifications
function checkNotification() {
const notificationEndpoint = 'php/server/utilNotification.php?action=get_unread_notifications';
const phpEndpoint = 'php/server/utilNotification.php';
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const notificationEndpoint = `${apiBase}/messaging/in-app/unread`;
$.ajax({
url: notificationEndpoint,
type: 'GET',
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
// console.log(response);
@@ -469,14 +471,13 @@ function checkNotification() {
if($("#modal-ok").is(":visible") == false)
{
showModalOK("Notification", decodedContent, function() {
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
// Mark the notification as read
$.ajax({
url: phpEndpoint,
type: 'GET',
data: {
action: 'mark_notification_as_read',
guid: oldestInterruptNotification.guid
},
url: `${apiBase}/messaging/in-app/read/${oldestInterruptNotification.guid}`,
type: 'POST',
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
console.log(response);
// After marking the notification as read, check for the next one
@@ -585,20 +586,21 @@ setInterval(checkNotification, 3000);
// User notification handling methods
// --------------------------------------------------
const phpEndpoint = 'php/server/utilNotification.php';
// --------------------------------------------------
// Write a notification
function write_notification(content, level) {
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
$.ajax({
url: phpEndpoint, // Change this to the path of your PHP script
type: 'GET',
data: {
action: 'write_notification',
url: `${apiBase}/messaging/in-app/write`,
type: 'POST',
headers: { "Authorization": `Bearer ${apiToken}` },
data: JSON.stringify({
content: content,
level: level
},
}),
contentType: "application/json",
success: function(response) {
console.log('Notification written successfully.');
},
@@ -609,53 +611,58 @@ function write_notification(content, level) {
}
// --------------------------------------------------
// Write a notification
// Mark a notification as read
function markNotificationAsRead(guid) {
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
$.ajax({
url: phpEndpoint,
type: 'GET',
data: {
action: 'mark_notification_as_read',
guid: guid
},
url: `${apiBase}/messaging/in-app/read/${guid}`,
type: 'POST',
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
console.log(response);
// Perform any further actions after marking the notification as read here
showMessage(getString("Gen_Okay"))
console.log("Mark notification response:", response);
if (response.success) {
showMessage(getString("Gen_Okay"));
// Reload the page to refresh notifications
setTimeout(() => window.location.reload(), 500);
} else {
console.error("Failed to mark notification as read:", response.error);
showMessage("Error: " + (response.error || "Unknown error"));
}
},
error: function(xhr, status, error) {
console.error("Error marking notification as read:", status, error);
console.error("Error marking notification as read:", status, error, xhr.responseJSON);
showMessage("Error: " + (xhr.responseJSON?.error || error));
},
complete: function() {
// Perform any cleanup tasks here
// Perform any cleanup tasks here
}
});
}
}
// --------------------------------------------------
// Remove a notification
function removeNotification(guid) {
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
$.ajax({
url: phpEndpoint,
type: 'GET',
data: {
action: 'remove_notification',
guid: guid
},
url: `${apiBase}/messaging/in-app/delete/${guid}`,
type: 'DELETE',
headers: { "Authorization": `Bearer ${apiToken}` },
success: function(response) {
console.log(response);
// Perform any further actions after marking the notification as read here
showMessage(getString("Gen_Okay"))
console.log(response);
// Perform any further actions after removing the notification here
showMessage(getString("Gen_Okay"))
},
error: function(xhr, status, error) {
console.error("Error removing notification:", status, error);
console.error("Error removing notification:", status, error);
},
complete: function() {
// Perform any cleanup tasks here
// Perform any cleanup tasks here
}
});
}
}

View File

@@ -378,14 +378,27 @@ function overwriteIconType()
)
`;
const apiUrl = `php/server/dbHelper.php?action=write&rawSql=${btoa(encodeURIComponent(rawSql))}`;
const apiBase = getApiBase();
const apiToken = getSetting("API_TOKEN");
const url = `${apiBase}/dbquery/write`;
$.get(apiUrl, function(response) {
if (response === 'OK') {
showMessage (response);
updateApi("devices")
} else {
showMessage (response, 3000, "modal_red");
$.ajax({
url,
method: "POST",
headers: { "Authorization": `Bearer ${apiToken}` },
data: JSON.stringify({ rawSql: btoa(unescape(encodeURIComponent(rawSql))) }),
contentType: "application/json",
success: function(response) {
if (response.success) {
showMessage("OK");
updateApi("devices");
} else {
showMessage(response.error || "Unknown error", 3000, "modal_red");
}
},
error: function(xhr, status, error) {
console.error("Error updating icons:", status, error);
showMessage("Error: " + (xhr.responseJSON?.error || error), 3000, "modal_red");
}
});
}