From 7100cf826d70924cb6b4fa92db4ff388e558d39d Mon Sep 17 00:00:00 2001 From: rdkartono Date: Thu, 28 Aug 2025 12:08:16 +0700 Subject: [PATCH] commit 28/08/2025 --- src/Main.kt | 43 +++++ src/database/MariaDB.kt | 364 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 407 insertions(+) diff --git a/src/Main.kt b/src/Main.kt index 865b54a..ef14507 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -2,6 +2,11 @@ import audio.AudioPlayer import com.sun.jna.Platform import content.ContentCache import database.MariaDB +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import org.tinylog.Logger import oshi.util.GlobalConfig import web.WebApp @@ -17,6 +22,44 @@ fun main() { audioPlayer.InitAudio(1) val content = ContentCache() val db = MariaDB() + CoroutineScope(Dispatchers.Default).launch { + while (isActive){ + delay(1000) + val broadcastzones = db.GetBroadcastZones() + // baca dulu queue paging, prioritas 1 + db.Read_Queue_Paging().forEach { + // cek apakah queue paging ada broadcast zone nya + if (it.BroadcastZones.isNotBlank()) { + val zz = it.BroadcastZones.split(";") + // cek apakah semua target broadcast zone dari queue paging ada di dalam database broadcast zones + if (zz.all { z -> broadcastzones.any { bz -> bz.equals(z) } }){ + // semua target broadcast zone valid, sekarang cek apakah semua target broadcast zone idle + + } else { + // ada broadcast zone yang tidak valid, delete from queue paging + db.Delete_Queue_Paging_by_index(it.index) + //TODO check log message yang benar + db.Add_Log("AAS", "Cancelled paging message with index ${it.index} due to invalid broadcast zone") + } + } else { + // invalid broadcast zone, delete from queue paging + db.Delete_Queue_Paging_by_index(it.index) + // TODO check log message yang benar + db.Add_Log("AAS", "Cancelled paging message with index ${it.index} due to empty broadcast zone") + } + } + // baca kemudian queue table, prioritas 2 + db.Read_Queue_Table().forEach { + if (it.BroadcastZones.isNotBlank()) { + + } else { + // invalid broadcast zone, delete from queue table + db.Delete_Queue_Table_by_index(it.index) + } + } + } + + } val web = WebApp(3030, listOf( Pair("admin", "password"), diff --git a/src/database/MariaDB.kt b/src/database/MariaDB.kt index 816524b..a3b4a64 100644 --- a/src/database/MariaDB.kt +++ b/src/database/MariaDB.kt @@ -105,6 +105,34 @@ class MariaDB ( connected = false } + /** + * Add Log to database + * @param machine The machine name or identifier. + * @param description The description of the log entry. + */ + fun Add_Log(machine: String, description: String){ + val currentDate = java.time.LocalDate.now() + val currentTime = java.time.LocalTime.now().withNano(0) // remove nanoseconds for cleaner format + val dateString = currentDate.format(java.time.format.DateTimeFormatter.ofPattern("dd/MM/yyyy")) + val timeString = currentTime.format(java.time.format.DateTimeFormatter.ofPattern("HH:mm:ss")) + + try { + val statement = connection?.prepareStatement("INSERT INTO log (datenya, timenya, machine, description) VALUES (?, ?, ?, ?)") + statement?.setString(1, dateString) + statement?.setString(2, timeString) + statement?.setString(3, machine) + statement?.setString(4, description) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Log added: [$dateString $timeString] [$machine] $description" as Any) + } else { + Logger.warn("No log entry added for: [$dateString $timeString] [$machine] $description" as Any) + } + } catch (e : Exception) { + Logger.error("Error adding log entry: ${e.message}" as Any) + } + } + /** * Get All Log from database * @param consumer A Consumer that will receive the list of logs @@ -213,6 +241,43 @@ class MariaDB ( } } + /** + * Deletes a schedulebank entry by its index. + * @param index The index of the schedulebank entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_Schedulebank_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM schedulebank WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from schedulebank with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from schedulebank with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from schedulebank with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the schedulebank table in the database and the local list. + */ + fun Clear_Schedulebank() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE schedulebank") + Logger.info("Schedulebank table cleared" as Any) + SchedulebankList.clear() + } catch (e : Exception) { + Logger.error("Error clearing schedulebank table: ${e.message}" as Any) + } + } + /** * Reloads the language link list from the database. */ @@ -234,6 +299,43 @@ class MariaDB ( } } + /** + * Deletes a language link entry by its index. + * @param index The index of the language link entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_LanguageLink_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM languagelink WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from languagelink with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from languagelink with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from languagelink with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the language link table in the database and the local list. + */ + fun Clear_LanguageLink() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE languagelink") + Logger.info("LanguageLink table cleared" as Any) + LanguageLinkList.clear() + } catch (e : Exception) { + Logger.error("Error clearing LanguageLink table: ${e.message}" as Any) + } + } + /** * Reloads the soundbank list from the database. */ @@ -260,6 +362,43 @@ class MariaDB ( } } + /** + * Deletes a soundbank entry by its index. + * @param index The index of the soundbank entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_Soundbank_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM soundbank WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from soundbank with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from soundbank with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from soundbank with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the soundbank table in the database and the local list. + */ + fun Clear_Soundbank() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE soundbank") + Logger.info("Soundbank table cleared" as Any) + SoundbankList.clear() + } catch (e : Exception) { + Logger.error("Error clearing soundbank table: ${e.message}" as Any) + } + } + /** * Reloads the messagebank list from the database. */ @@ -286,4 +425,229 @@ class MariaDB ( } } + /** + * Deletes a messagebank entry by its index. + * @param index The index of the messagebank entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_Messagebank_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM messagebank WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from messagebank with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from messagebank with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from messagebank with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the messagebank table in the database and the local list. + */ + fun Clear_Messagebank() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE messagebank") + Logger.info("Messagebank table cleared" as Any) + MessagebankList.clear() + } catch (e : Exception) { + Logger.error("Error clearing messagebank table: ${e.message}" as Any) + } + } + + /** + * Reads all entries from the queue_table in the database. + * @return A list of QueueTable entries. + */ + fun Read_Queue_Table() : List { + val queueList = ArrayList() + try { + val statement = connection?.createStatement() + val resultSet = statement?.executeQuery("SELECT * FROM queue_table") + while (resultSet?.next() == true) { + val queueTable = QueueTable( + resultSet.getLong("index").toUInt(), + resultSet.getString("Date_Time"), + resultSet.getString("Source"), + resultSet.getString("Type"), + resultSet.getString("Message"), + resultSet.getString("SB_TAGS"), + resultSet.getString("BroadcastZones"), + resultSet.getInt("Repeat").toUInt(), + resultSet.getString("Language") + ) + queueList.add(queueTable) + } + } catch (e : Exception) { + Logger.error("Error fetching queue table: ${e.message}" as Any) + } + return queueList + } + + /** + * Deletes a queue_table entry by its index. + * @param index The index of the queue_table entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_Queue_Table_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM queue_table WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from queue_table with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from queue_table with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from queue_table with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the queue_table in the database. + */ + fun Clear_Queue_Table() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE queue_table") + Logger.info("Queue table cleared" as Any) + } catch (e : Exception) { + Logger.error("Error clearing queue table: ${e.message}" as Any) + } + } + + /** + * Reads all entries from the queue_paging in the database. + * @return A list of QueuePaging entries. + */ + fun Read_Queue_Paging() : List{ + val queueList = ArrayList() + try { + val statement = connection?.createStatement() + val resultSet = statement?.executeQuery("SELECT * FROM queue_paging") + while (resultSet?.next() == true) { + val queuePaging = QueuePaging( + resultSet.getLong("index").toUInt(), + resultSet.getString("Date_Time"), + resultSet.getString("Source"), + resultSet.getString("Type"), + resultSet.getString("Message"), + resultSet.getString("SB_TAGS"), + ) + queueList.add(queuePaging) + } + } catch (e : Exception) { + Logger.error("Error fetching queue paging: ${e.message}" as Any) + } + return queueList + } + + /** + * Deletes a queue_paging entry by its index. + * @param index The index of the queue_paging entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_Queue_Paging_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM queue_paging WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from queue_paging with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from queue_paging with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from queue_paging with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the queue_paging in the database. + */ + fun Clear_Queue_Paging(){ + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE queue_paging") + Logger.info("Queue paging table cleared" as Any) + } catch (e : Exception) { + Logger.error("Error clearing queue paging table: ${e.message}" as Any) + } + } + + /** + * Get All Broadcast Zones from database + * @return A list of BroadcastZones entries. + */ + fun GetBroadcastZones(): List { + val zonesList = ArrayList() + try { + val statement = connection?.createStatement() + val resultSet = statement?.executeQuery("SELECT * FROM broadcast_zones") + while (resultSet?.next() == true) { + val zone = BroadcastZones( + resultSet.getLong("index").toUInt(), + resultSet.getString("description"), + resultSet.getString("SoundChannel"), + resultSet.getString("Box"), + resultSet.getString("Relay") + ) + zonesList.add(zone) + } + } catch (e : Exception) { + Logger.error("Error fetching broadcast zones: ${e.message}" as Any) + } + return zonesList + } + + /** + * Deletes a broadcast_zones entry by its index. + * @param index The index of the broadcast_zones entry to delete. + * @return True if the deletion was successful, false otherwise. + */ + fun Delete_BroadcastZones_by_index(index: UInt) : Boolean { + try { + val statement = connection?.prepareStatement("DELETE FROM broadcast_zones WHERE `index` = ?") + statement?.setLong(1, index.toLong()) + val rowsAffected = statement?.executeUpdate() + if (rowsAffected != null && rowsAffected > 0) { + Logger.info("Deleted $rowsAffected row(s) from broadcast_zones with index $index" as Any) + return true + } else { + Logger.warn("No rows deleted from broadcast_zones with index $index" as Any) + } + } catch (e : Exception) { + Logger.error("Error deleting from broadcast_zones with index $index: ${e.message}" as Any) + } + return false + } + + /** + * Clears all entries from the broadcast_zones in the database. + */ + fun Clear_BroadcastZones() { + try { + val statement = connection?.createStatement() + // use TRUNCATE to reset auto increment index + statement?.executeUpdate("TRUNCATE TABLE broadcast_zones") + Logger.info("Broadcast zones table cleared" as Any) + } catch (e : Exception) { + Logger.error("Error clearing broadcast zones table: ${e.message}" as Any) + } + } + } \ No newline at end of file