/** * List of voice types available * @type {string[]} */ let voiceTypes = []; /** * List of categories available * @type {string[]} */ let categories = []; /** * List of languages available * @type {string[]} */ let languages = []; /** * List of scheduled days available * @type {string[]} */ let scheduledays = [] /** * Create a list item element * @param {String} text Text Content for the list item * @param {String} className Specific class name for the list item * @returns {JQuery} */ function ListItem(text, className = "") { return $('
  • ').addClass(className).text(text); } /** * WebSocket connection * @type {WebSocket} */ let ws = null; /** * Send a command to the WebSocket server. * @param {String} command command to send * @param {String} data data to send */ function sendCommand(command, data) { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ command, data })); } } /** * Fetch API helper function * @param {string} endpoint Endpoint URL * @param {string} method Method (GET, POST, etc.) * @param {Object} headers Headers to include in the request * @param {Object} body Body of the request * @param {Function} cbOK Callback function for successful response * @param {Function} cbError Callback function for error response */ function fetchAPI(endpoint, method, headers = {}, body = null, cbOK, cbError) { let url = window.location.origin + "/api/" + endpoint; let options = { method: method, headers: headers } if (body !== null) { options.body = JSON.stringify(body); if (!options.headers['Content-Type']) { options.headers['Content-Type'] = 'application/json'; } } fetch(url, options) .then(response => { if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } return response.json(); }) .then(data => { cbOK(data); }) .catch(error => { cbError(error); }); } /** * Fetch asset file from /assets/img/* * @param {String} url the filename to fetch, relative to /assets/img/ * @param {Function} cbOK callback function on success, will receive the object URL * @param {Function} cbError callback function on error, will receive the error object */ function fetchImg(url, cbOK, cbError) { url = "/assets/img/" + url; fetch(url) .then(response => { if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } return response.blob(); }) .then(blob => { const url = URL.createObjectURL(blob); cbOK(url); }) .catch(error => { cbError(error); }); } /** * Reload voice types from server */ function getVoiceTypes() { voiceTypes = []; fetchAPI("VoiceType", "GET", {}, null, (okdata) => { // okdata is a string contains elements separated by semicolon ; if (Array.isArray(okdata)) { voiceTypes = okdata.filter(item => item.trim().length > 0); //console.log("Loaded " + voiceTypes.length + " voice types : " + voiceTypes.join(", ")); } else console.log("getVoiceTypes: okdata is not array"); }, (errdata) => { alert("Error loading voice types : " + errdata.message); }); } /** * Reload categories from server */ function getCategories() { categories = []; fetchAPI("Category", "GET", {}, null, (okdata) => { // okdata is a string contains elements separated by semicolon ; if (Array.isArray(okdata)) { categories = okdata.filter(item => item.trim().length > 0); //console.log("Loaded " + categories.length + " categories : " + categories.join(", ")); } else console.log("getCategories: okdata is not array"); }, (errdata) => { alert("Error loading categories : " + errdata.message); }); } /** * Reload languages from server */ function getLanguages() { languages = []; fetchAPI("Language", "GET", {}, null, (okdata) => { // okdata is a string contains elements separated by semicolon ; if (Array.isArray(okdata)) { languages = okdata.filter(item => item.trim().length > 0); //console.log("Loaded " + languages.length + " languages : " + languages.join(", ") ); } else console.log("getLanguages: okdata is not array"); }, (errdata) => { alert("Error loading languages : " + errdata.message); }); } /** * Reload scheduled days from server */ function getScheduledDays() { scheduledays = []; fetchAPI("ScheduleDay", "GET", {}, null, (okdata) => { // okdata is a string contains elements separated by semicolon ; if (Array.isArray(okdata)) { scheduledays = okdata.filter(item => item.trim().length > 0); //console.log("Loaded " + scheduledays.length + " scheduled days : " + scheduledays.join(", ") ); } else console.log("getScheduledDays: okdata is not array"); }, (errdata) => { alert("Error loading scheduled days : " + errdata.message); }); } /** * Clear database mechanism * @param {String} APIURL API URL endpoint * @param {String} whattoclear what to clear * @param {Function} cbOK callback function on success * @param {Function} cbError callback function on error */ function DoClear(APIURL, whattoclear, cbOK, cbError) { if (confirm(`Are you sure want to clear ${whattoclear} ? This procedure is not reversible`)) { fetchAPI(APIURL + "List", "DELETE", {}, null, (okdata) => { cbOK(okdata); }, (errdata) => { cbError(errdata); }); } } /** * Export mechanism to XLSX file * @param {String} APIURL API URL endpoint * @param {String} filename target filename * @param {Object} queryParams additional query parameters as object */ function DoExport(APIURL, filename, queryParams = {}) { // send GET request to APIURL + "ExportXLSX" // reply Content-Type is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet // reply Content-Disposition: attachment; filename=filename // Use fetch to download the XLSX file as a blob and trigger download let url = "/api/" + APIURL + "ExportXLSX"; if (queryParams && Object.keys(queryParams).length > 0) { url += "?" + new URLSearchParams(queryParams).toString(); } fetch(url, { method: "GET", headers: {} }) .then(response => { if (!response.ok) throw new Error('Network response was not ok ' + response.statusText); return response.blob(); }) .then(blob => { const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove(); window.URL.revokeObjectURL(url); }) .catch(error => { alert("Error export to " + filename + ": " + error.message); }); return; // prevent the rest of the function from running } /** * Import mechanism from XLSX file * @param {String} APIURL API URL endpoint * @param {Function} cbOK function that accept object data * @param {Function} cbError function that accept error object */ function DoImport(APIURL, cbOK, cbError) { // Open file selection dialog that accepts only .xlsx files // then upload to server using fetchAPI at "api/ImportXLSX" with POST method let fileInput = document.createElement('input'); fileInput.type = 'file'; fileInput.accept = '.xlsx'; fileInput.onchange = e => { let file = e.target.files[0]; if (file) { let formData = new FormData(); formData.append('file', file); fetch("/api/" + APIURL + "ImportXLSX", { method: 'POST', body: formData }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } return response.json(); }) .then(data => { cbOK(data); }) .catch(error => { cbError(error); }); } else { cbError(new Error("No file selected")); } }; fileInput.click(); fileInput.remove(); } let $onlineindicator = null; let $cpustatus = null; let $ramstatus = null; let $diskstatus = null; let $networkstatus = null; let $datetimetext = null; let greencircle = null; let redcircle = null; /** * App entry point */ $(document).ready(function () { document.title = "Automatic Announcement System" fetchImg('green_circle.png', (url) => { greencircle = url; }, (err) => { console.error("Error loading green_circle.png : ", err); }); fetchImg('red_circle.png', (url) => { redcircle = url; }, (err) => { console.error("Error loading red_circle.png : ", err); }); const wsURL = window.location.pathname + '/ws' if (chrome && chrome.runtime && chrome.runtime.lastError) { alert("Runtime error: " + chrome.runtime.lastError.message); return; } $onlineindicator = $('#onlineindicator'); $cpustatus = $('#cpustatus'); $ramstatus = $('#ramstatus'); $diskstatus = $('#diskstatus'); $networkstatus = $('#networkstatus'); $datetimetext = $('#datetimetext'); // reset status indicators function resetStatusIndicators() { $onlineindicator.attr('src', redcircle); $cpustatus.text("CPU : N/A"); $ramstatus.text("RAM : N/A"); $diskstatus.text("Disk : N/A"); $networkstatus.text("Network : N/A"); $datetimetext.text("Date/Time : N/A"); } resetStatusIndicators(); getVoiceTypes(); getCategories(); getLanguages(); getScheduledDays(); // Initialize WebSocket connection ws = new WebSocket(wsURL); ws.onopen = () => { console.log('WebSocket connection established'); $onlineindicator.attr('src', greencircle); }; ws.onmessage = (event) => { let rep = JSON.parse(event.data); let cmd = rep.reply let data = rep.data; if (cmd && cmd.length > 0) { switch (cmd) { case "getCPUStatus": $cpustatus.text("CPU : " + data) break; case "getMemoryStatus": $ramstatus.text("RAM : " + data) break; case "getDiskStatus": $diskstatus.text("Disk : " + data) break; case "getNetworkStatus": $networkstatus.text("Network : " + data) break; case "getSystemTime": $datetimetext.text(data) break; } } }; ws.onclose = () => { console.log('WebSocket connection closed'); resetStatusIndicators(); }; // ws.onerror = (error) => { // console.error('WebSocket error:', error); // }; setInterval(() => { sendCommand("getCPUStatus", "") sendCommand("getMemoryStatus", "") sendCommand("getDiskStatus", "") sendCommand("getNetworkStatus", "") sendCommand("getSystemTime", "") }, 1000) let sidemenu = new bootstrap.Offcanvas('#offcanvas-menu'); $('#showmenu').click(() => { sidemenu.show(); }) $('#homelink').click(() => { sidemenu.hide(); $('#content').load('overview.html', function (response, status, xhr) { if (status === "success") { console.log("Overview content loaded successfully"); } }); }); $('#soundbanklink').click(() => { sidemenu.hide(); $('#content').load('soundbank.html', function (response, status, xhr) { if (status === "success") { console.log("Soundbank content loaded successfully"); // pindah soundbank.js } else { console.error("Error loading soundbank content : ", xhr.status, xhr.statusText); } }); }) $('#messagebanklink').click(() => { sidemenu.hide(); $('#content').load('messagebank.html', function (response, status, xhr) { if (status === "success") { console.log("Messagebank content loaded successfully"); // pindah messagebank.js } else { console.error("Error loading messagebank content : ", xhr.status, xhr.statusText); } }); }) $('#languagelink').click(() => { sidemenu.hide(); $('#content').load('language.html', function (response, status, xhr) { if (status === "success") { console.log("Language content loaded successfully"); // pindah languagelink.js } else { console.error("Error loading language content : ", xhr.status, xhr.statusText); } }); }) $('#broadcastzonelink').click(() => { sidemenu.hide(); $('#content').load('broadcastzones.html', function (response, status, xhr) { if (status === "success") { console.log("Broadcast Zone content loaded successfully"); // pindah ke broadcastzones.js } else { console.error("Error loading broadcast zone content : ", xhr.status, xhr.statusText); } }); }) $('#timerlink').click(() => { sidemenu.hide(); $('#content').load('timer.html', function (response, status, xhr) { if (status === "success") { console.log("Timer content loaded successfully"); // pindah ke schedulebank.js } else { console.error("Error loading timer content : ", xhr.status, xhr.statusText); } }); }) $('#loglink').click(() => { sidemenu.hide(); $('#content').load('log.html', function (response, status, xhr) { if (status === "success") { console.log("Log content loaded successfully"); // pindah ke log.js } else { console.error("Error loading log content:", xhr.status, xhr.statusText); } }); }) $('#settinglink').click(() => { sidemenu.hide(); $('#content').load('setting.html', function (response, status, xhr) { if (status === "success") { console.log("Setting content loaded successfully"); //sendCommand("getSetting", ""); } else { console.error("Error loading setting content:", xhr.status, xhr.statusText); } }); }) $('#logoutlink').click(() => { window.location.href = "login.html" }) });