commit 03/09/2025
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package codes
|
package codes
|
||||||
|
|
||||||
|
import content.ScheduleDay
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
@@ -23,6 +24,7 @@ class Somecodes {
|
|||||||
val datetimeformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
|
val datetimeformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
|
||||||
val dateformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
|
val dateformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
|
||||||
val timeformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss")
|
val timeformat1: DateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss")
|
||||||
|
val timeformat2: DateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm")
|
||||||
const val KB_threshold = 1024.0
|
const val KB_threshold = 1024.0
|
||||||
const val MB_threshold = KB_threshold * 1024.0
|
const val MB_threshold = KB_threshold * 1024.0
|
||||||
const val GB_threshold = MB_threshold * 1024.0
|
const val GB_threshold = MB_threshold * 1024.0
|
||||||
@@ -165,6 +167,52 @@ class Somecodes {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a string is a valid schedule time in the format "HH:mm".
|
||||||
|
* @param value The string to check.
|
||||||
|
* @return True if the string is a valid schedule time, false otherwise.
|
||||||
|
*/
|
||||||
|
fun ValidScheduleTime(value: String): Boolean{
|
||||||
|
// format HH:mm
|
||||||
|
try {
|
||||||
|
if (ValidString(value)){
|
||||||
|
timeformat2.parse(value)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} catch (_ : Exception){
|
||||||
|
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a schedule day by its name.
|
||||||
|
* @param value The name of the schedule day to find.
|
||||||
|
* @return The name of the schedule day if found, null otherwise.
|
||||||
|
*/
|
||||||
|
fun FindScheduleDay(value: String) : String? {
|
||||||
|
val sd = ScheduleDay.entries.find { sd -> sd.name == value }
|
||||||
|
return sd?.name
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a string is a valid schedule day or a valid date.
|
||||||
|
* A valid schedule day is either one of the ScheduleDay enum names or a date in the format "dd/MM/yyyy".
|
||||||
|
* @param value The string to check.
|
||||||
|
* @return True if the string is a valid schedule day or date, false otherwise.
|
||||||
|
*/
|
||||||
|
fun ValidScheduleDay(value: String) : Boolean {
|
||||||
|
if (ValidString(value)){
|
||||||
|
// check if value is one of ScheduleDay enum name
|
||||||
|
if (FindScheduleDay(value) != null){
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// check if value is in format dd/MM/yyyy
|
||||||
|
return ValidDate(value)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
13
src/content/ScheduleDay.kt
Normal file
13
src/content/ScheduleDay.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
|
enum class ScheduleDay(val day: String) {
|
||||||
|
Sunday("Sunday"),
|
||||||
|
Monday("Monday"),
|
||||||
|
Tuesday("Tuesday"),
|
||||||
|
Wednesday("Wednesday"),
|
||||||
|
Thursday("Thursday"),
|
||||||
|
Friday("Friday"),
|
||||||
|
Saturday("Saturday"),
|
||||||
|
Everyday("Everyday")
|
||||||
|
}
|
||||||
@@ -1157,7 +1157,7 @@ class MariaDB(
|
|||||||
* Exports the messagebank table to an XLSX workbook.
|
* Exports the messagebank table to an XLSX workbook.
|
||||||
* @return An XSSFWorkbook containing the messagebank data.
|
* @return An XSSFWorkbook containing the messagebank data.
|
||||||
*/
|
*/
|
||||||
fun Export_Messagebank_XLSX(): XSSFWorkbook {
|
fun Export_Messagebank_XLSX(): XSSFWorkbook? {
|
||||||
try {
|
try {
|
||||||
val statement = connection?.createStatement()
|
val statement = connection?.createStatement()
|
||||||
val resultSet = statement?.executeQuery("SELECT * FROM messagebank")
|
val resultSet = statement?.executeQuery("SELECT * FROM messagebank")
|
||||||
@@ -1188,7 +1188,7 @@ class MariaDB(
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Logger.error { "Error exporting Messagebank, Msg: ${e.message}" }
|
Logger.error { "Error exporting Messagebank, Msg: ${e.message}" }
|
||||||
}
|
}
|
||||||
return XSSFWorkbook()
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Import_Messagebank_XLSX(workbook: XSSFWorkbook): Boolean {
|
fun Import_Messagebank_XLSX(workbook: XSSFWorkbook): Boolean {
|
||||||
|
|||||||
@@ -1,4 +1,13 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
data class ScheduleBank(var index: UInt, var Description: String, var Day: String, var Time: String, var Soundpath: String, var Repeat: UByte, var Enable: Boolean, var BroadcastZones: String, var Language: String)
|
data class ScheduleBank(
|
||||||
|
var index: UInt,
|
||||||
|
var Description: String,
|
||||||
|
var Day: String,
|
||||||
|
var Time: String,
|
||||||
|
var Soundpath: String,
|
||||||
|
var Repeat: UByte,
|
||||||
|
var Enable: Boolean,
|
||||||
|
var BroadcastZones: String,
|
||||||
|
var Language: String)
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ package web
|
|||||||
import codes.Somecodes
|
import codes.Somecodes
|
||||||
import codes.Somecodes.Companion.ValidDate
|
import codes.Somecodes.Companion.ValidDate
|
||||||
import codes.Somecodes.Companion.ValidFile
|
import codes.Somecodes.Companion.ValidFile
|
||||||
|
import codes.Somecodes.Companion.ValidScheduleDay
|
||||||
|
import codes.Somecodes.Companion.ValidScheduleTime
|
||||||
import codes.Somecodes.Companion.ValidString
|
import codes.Somecodes.Companion.ValidString
|
||||||
import com.fasterxml.jackson.databind.JsonNode
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import content.Category
|
import content.Category
|
||||||
|
import content.Language
|
||||||
|
import content.VoiceType
|
||||||
import database.MariaDB
|
import database.MariaDB
|
||||||
import database.Soundbank
|
import database.Soundbank
|
||||||
import io.javalin.Javalin
|
import io.javalin.Javalin
|
||||||
@@ -20,6 +24,7 @@ import io.javalin.apibuilder.ApiBuilder.ws
|
|||||||
import io.javalin.http.Context
|
import io.javalin.http.Context
|
||||||
import io.javalin.json.JavalinJackson
|
import io.javalin.json.JavalinJackson
|
||||||
import io.javalin.websocket.WsMessageContext
|
import io.javalin.websocket.WsMessageContext
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
@@ -38,8 +43,7 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Start() {
|
fun Start() {
|
||||||
app = Javalin.create {
|
app = Javalin.create { config ->
|
||||||
config ->
|
|
||||||
config.useVirtualThreads = true
|
config.useVirtualThreads = true
|
||||||
config.staticFiles.add("/webpage")
|
config.staticFiles.add("/webpage")
|
||||||
config.jsonMapper(JavalinJackson(jacksonObjectMapper()))
|
config.jsonMapper(JavalinJackson(jacksonObjectMapper()))
|
||||||
@@ -85,41 +89,70 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
ws.onMessage { wsMessageContext ->
|
ws.onMessage { wsMessageContext ->
|
||||||
try {
|
try {
|
||||||
val cmd = objectmapper.readValue(wsMessageContext.message(), WebsocketCommand::class.java)
|
val cmd =
|
||||||
|
objectmapper.readValue(wsMessageContext.message(), WebsocketCommand::class.java)
|
||||||
when (cmd.command) {
|
when (cmd.command) {
|
||||||
"getSystemTime" -> {
|
"getSystemTime" -> {
|
||||||
SendReply(wsMessageContext, cmd.command, LocalDateTime.now().format(Somecodes.datetimeformat1))
|
SendReply(
|
||||||
|
wsMessageContext,
|
||||||
|
cmd.command,
|
||||||
|
LocalDateTime.now().format(Somecodes.datetimeformat1)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
"getCPUStatus" -> {
|
"getCPUStatus" -> {
|
||||||
Somecodes.getCPUUsage { vv ->
|
Somecodes.getCPUUsage { vv ->
|
||||||
SendReply(wsMessageContext, cmd.command, vv)
|
SendReply(wsMessageContext, cmd.command, vv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
"getMemoryStatus" -> {
|
"getMemoryStatus" -> {
|
||||||
SendReply(wsMessageContext, cmd.command, Somecodes.getMemoryUsage())
|
SendReply(wsMessageContext, cmd.command, Somecodes.getMemoryUsage())
|
||||||
}
|
}
|
||||||
|
|
||||||
"getDiskStatus" -> {
|
"getDiskStatus" -> {
|
||||||
SendReply(wsMessageContext, cmd.command, Somecodes.getDiskUsage())
|
SendReply(wsMessageContext, cmd.command, Somecodes.getDiskUsage())
|
||||||
}
|
}
|
||||||
|
|
||||||
"getNetworkStatus" -> {
|
"getNetworkStatus" -> {
|
||||||
// TODO Get Network status
|
// TODO Get Network status
|
||||||
SendReply(wsMessageContext, cmd.command, "OK")
|
SendReply(wsMessageContext, cmd.command, "OK")
|
||||||
}
|
}
|
||||||
|
|
||||||
"getSoundBankList" -> {
|
"getSoundBankList" -> {
|
||||||
println("getSoundBankList command received")
|
println("getSoundBankList command received")
|
||||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.SoundbankList))
|
SendReply(
|
||||||
|
wsMessageContext,
|
||||||
|
cmd.command,
|
||||||
|
MariaDB.ArrayListtoString(db.SoundbankList)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
"getMessageBankList" -> {
|
"getMessageBankList" -> {
|
||||||
println("getMessageBankList command received")
|
println("getMessageBankList command received")
|
||||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.MessagebankList))
|
SendReply(
|
||||||
|
wsMessageContext,
|
||||||
|
cmd.command,
|
||||||
|
MariaDB.ArrayListtoString(db.MessagebankList)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
"getLanguageList" -> {
|
"getLanguageList" -> {
|
||||||
println("getLanguageList command received")
|
println("getLanguageList command received")
|
||||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.LanguageLinkList))
|
SendReply(
|
||||||
|
wsMessageContext,
|
||||||
|
cmd.command,
|
||||||
|
MariaDB.ArrayListtoString(db.LanguageLinkList)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
"getTimerList" -> {
|
"getTimerList" -> {
|
||||||
println("getTimerList command received")
|
println("getTimerList command received")
|
||||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.SchedulebankList))
|
SendReply(
|
||||||
|
wsMessageContext,
|
||||||
|
cmd.command,
|
||||||
|
MariaDB.ArrayListtoString(db.SchedulebankList)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
@@ -178,9 +211,12 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
if (ValidFile(addvalue.Path)) {
|
if (ValidFile(addvalue.Path)) {
|
||||||
if (db.Add_Soundbank(addvalue)) {
|
if (db.Add_Soundbank(addvalue)) {
|
||||||
it.result("OK")
|
it.result("OK")
|
||||||
} else it.status(500).result("Failed to add soundbank to database")
|
} else it.status(500)
|
||||||
} else it.status(400).result("Invalid Path, file does not exist")
|
.result("Failed to add soundbank to database")
|
||||||
} else it.status(400).result("TAG=${addvalue.TAG} already exists for the same Language=${addvalue.Language} and Category=${addvalue.Category}")
|
} else it.status(400)
|
||||||
|
.result("Invalid Path, file does not exist")
|
||||||
|
} else it.status(400)
|
||||||
|
.result("TAG=${addvalue.TAG} already exists for the same Language=${addvalue.Language} and Category=${addvalue.Category}")
|
||||||
} else it.status(400).result("Invalid Path")
|
} else it.status(400).result("Invalid Path")
|
||||||
} else it.status(400).result("Invalid Language")
|
} else it.status(400).result("Invalid Language")
|
||||||
} else it.status(400).result("Invalid Category")
|
} else it.status(400).result("Invalid Category")
|
||||||
@@ -243,8 +279,8 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
if (ValidString(_category) && _category != sb.Category) {
|
if (ValidString(_category) && _category != sb.Category) {
|
||||||
if (Category.entries.any {
|
if (Category.entries.any { cat ->
|
||||||
cat -> cat.name == _category
|
cat.name == _category
|
||||||
}) {
|
}) {
|
||||||
sb.Category = _category
|
sb.Category = _category
|
||||||
changed = true
|
changed = true
|
||||||
@@ -270,13 +306,26 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
if (db.Update_Soundbank_by_index(index, sb)) {
|
if (db.Update_Soundbank_by_index(index, sb)) {
|
||||||
it.result("OK")
|
it.result("OK")
|
||||||
} else it.status(500).result("Failed to update soundbank with index $index")
|
} else it.status(500).result("Failed to update soundbank with index $index")
|
||||||
} else it.status(400).result("Nothing has changed for soundbank with index $index")
|
} else it.status(400)
|
||||||
|
.result("Nothing has changed for soundbank with index $index")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
get("ExportXLSX") {
|
get("ExportXLSX") {
|
||||||
|
val xlsxdata = db.Export_Soundbank_XLSX()
|
||||||
|
if (xlsxdata != null) {
|
||||||
|
it.header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
|
it.header("Content-Disposition", "attachment; filename=\"soundbank.xlsx\"")
|
||||||
|
it.outputStream().use { out ->
|
||||||
|
xlsxdata.write(out)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to export soundbank to XLSX")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
post("ImportXLSX") {
|
post("ImportXLSX") {
|
||||||
val uploaded = it.uploadedFile("file")
|
val uploaded = it.uploadedFile("file")
|
||||||
@@ -284,6 +333,16 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
it.status(400).result("No file uploaded")
|
it.status(400).result("No file uploaded")
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
val xlsx = XSSFWorkbook(uploaded.content())
|
||||||
|
if (db.Import_Soundbank_XLSX(xlsx)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to import soundbank from XLSX")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
it.status(400).result("Invalid XLSX file")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
path("MessageBank") {
|
path("MessageBank") {
|
||||||
@@ -312,6 +371,100 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
patch("UpdateByIndex/{index}") {
|
||||||
|
// update messagebank by index
|
||||||
|
val index = it.pathParam("index").toUIntOrNull()
|
||||||
|
if (index == null) {
|
||||||
|
it.status(400).result("Invalid index")
|
||||||
|
} else {
|
||||||
|
val mb = db.MessagebankList.find { xx -> xx.index == index }
|
||||||
|
if (mb == null) {
|
||||||
|
it.status(404).result("Messagebank with index $index not found")
|
||||||
|
} else {
|
||||||
|
val json: JsonNode = objectmapper.readTree(it.body())
|
||||||
|
if (json.isEmpty) {
|
||||||
|
it.status(400).result("UpdateByIndex with index=$index has empty body")
|
||||||
|
} else {
|
||||||
|
val _description = json.get("Description").asText()
|
||||||
|
val _language = json.get("Language").asText()
|
||||||
|
val _ann_id = json.get("ANN_ID").asInt().toUInt()
|
||||||
|
val _voice_type = json.get("Voice_Type").asText()
|
||||||
|
val _message_detail = json.get("Message_Detail").asText()
|
||||||
|
val _message_tags = json.get("Message_TAGS").asText()
|
||||||
|
|
||||||
|
var changed = false
|
||||||
|
if (ValidString(_description) && _description != mb.Description) {
|
||||||
|
mb.Description = _description
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_language) && _language != mb.Language) {
|
||||||
|
mb.Language = _language
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (_ann_id > 0u && _ann_id != mb.ANN_ID) {
|
||||||
|
mb.ANN_ID = _ann_id
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_voice_type) && _voice_type != mb.Voice_Type) {
|
||||||
|
if (VoiceType.entries.any { vt -> vt.name == _voice_type }) {
|
||||||
|
mb.Voice_Type = _voice_type
|
||||||
|
changed = true
|
||||||
|
} else {
|
||||||
|
it.status(400).result("Invalid Voice_Type")
|
||||||
|
return@patch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ValidString(_message_detail) && _message_detail != mb.Message_Detail) {
|
||||||
|
mb.Message_Detail = _message_detail
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_message_tags) && _message_tags != mb.Message_TAGS) {
|
||||||
|
mb.Message_TAGS = _message_tags
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
if (db.Update_Messagebank_by_index(index, mb)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else it.status(500)
|
||||||
|
.result("Failed to update messagebank with index $index")
|
||||||
|
} else it.status(400)
|
||||||
|
.result("Nothing has changed for messagebank with index $index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get("ExportXLSX") {
|
||||||
|
val xlsxdata = db.Export_Messagebank_XLSX()
|
||||||
|
if (xlsxdata != null) {
|
||||||
|
it.header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
|
it.header("Content-Disposition", "attachment; filename=\"messagebank.xlsx\"")
|
||||||
|
it.outputStream().use { out ->
|
||||||
|
xlsxdata.write(out)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to export messagebank to XLSX")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
post("ImportXLSX") {
|
||||||
|
val uploaded = it.uploadedFile("file")
|
||||||
|
if (uploaded == null) {
|
||||||
|
it.status(400).result("No file uploaded")
|
||||||
|
return@post
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val xlsx = XSSFWorkbook(uploaded.content())
|
||||||
|
if (db.Import_Messagebank_XLSX(xlsx)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to import messagebank from XLSX")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
it.status(400).result("Invalid XLSX file")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
path("LanguageLink") {
|
path("LanguageLink") {
|
||||||
get("List") {
|
get("List") {
|
||||||
@@ -339,6 +492,74 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
patch("UpdateByIndex/{index}") {
|
||||||
|
// update by index
|
||||||
|
val index = it.pathParam("index").toUIntOrNull()
|
||||||
|
if (index == null) {
|
||||||
|
it.status(400).result("Invalid index")
|
||||||
|
} else {
|
||||||
|
val ll = db.LanguageLinkList.find { xx -> xx.index == index }
|
||||||
|
if (ll == null) {
|
||||||
|
it.status(404).result("Language link with index $index not found")
|
||||||
|
} else {
|
||||||
|
val json: JsonNode = objectmapper.readTree(it.body())
|
||||||
|
if (json.isEmpty) {
|
||||||
|
it.status(400).result("UpdateByIndex with index=$index has empty body")
|
||||||
|
} else {
|
||||||
|
val _tag = json.get("TAG").asText()
|
||||||
|
val _language = json.get("Language").asText()
|
||||||
|
var changed = false
|
||||||
|
if (ValidString(_language) && _language != ll.Language) {
|
||||||
|
ll.Language = _language
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_tag) && _tag != ll.TAG) {
|
||||||
|
ll.TAG = _tag
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
if (db.Update_LanguageLink_by_index(index, ll)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else it.status(500)
|
||||||
|
.result("Failed to update language link with index $index")
|
||||||
|
} else it.status(400)
|
||||||
|
.result("Nothing has changed for language link with index $index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get("ExportXLSX") {
|
||||||
|
val xlsxdata = db.Export_LanguageLink_XLSX()
|
||||||
|
if (xlsxdata != null) {
|
||||||
|
it.header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
|
it.header("Content-Disposition", "attachment; filename=\"languagelink.xlsx\"")
|
||||||
|
it.outputStream().use { out ->
|
||||||
|
xlsxdata.write(out)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to export language link to XLSX")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
post("ImportXLSX") {
|
||||||
|
val uploaded = it.uploadedFile("file")
|
||||||
|
if (uploaded == null) {
|
||||||
|
it.status(400).result("No file uploaded")
|
||||||
|
return@post
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val xlsx = XSSFWorkbook(uploaded.content())
|
||||||
|
if (db.Import_LanguageLink_XLSX(xlsx)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to import language link from XLSX")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
it.status(400).result("Invalid XLSX file")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
path("ScheduleBank") {
|
path("ScheduleBank") {
|
||||||
get("List") {
|
get("List") {
|
||||||
@@ -366,6 +587,119 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
patch("UpdateByIndex/{index}") {
|
||||||
|
// update by index
|
||||||
|
val index = it.pathParam("index").toUIntOrNull()
|
||||||
|
if (index == null) {
|
||||||
|
it.status(400).result("Invalid index")
|
||||||
|
} else {
|
||||||
|
val sb = db.SchedulebankList.find { xx -> xx.index == index }
|
||||||
|
if (sb == null) {
|
||||||
|
it.status(404).result("Schedule with index $index not found")
|
||||||
|
} else {
|
||||||
|
val json: JsonNode = objectmapper.readTree(it.body())
|
||||||
|
if (json.isEmpty) {
|
||||||
|
it.status(400).result("UpdateByIndex with index=$index has empty body")
|
||||||
|
} else {
|
||||||
|
val _description = json.get("Description").asText()
|
||||||
|
val _time = json.get("Time").asText()
|
||||||
|
val _day = json.get("Day").asText()
|
||||||
|
val _soundpath = json.get("Soundpath").asText()
|
||||||
|
val _repeat = json.get("Repeat").asInt().toUByte()
|
||||||
|
val _enable = json.get("Enable").asBoolean()
|
||||||
|
val _broadcast_zones = json.get("BroadcastZones").asText()
|
||||||
|
val _language = json.get("Language").asText()
|
||||||
|
var changed = false
|
||||||
|
if (ValidString(_description) && _description != sb.Description) {
|
||||||
|
sb.Description = _description
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_time) && _time != sb.Time) {
|
||||||
|
if (ValidScheduleTime(_time)) {
|
||||||
|
sb.Time = _time
|
||||||
|
changed = true
|
||||||
|
} else {
|
||||||
|
it.status(400).result("Invalid Time format, must be HH:mm")
|
||||||
|
return@patch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ValidString(_day) && _day != sb.Day) {
|
||||||
|
if (ValidScheduleDay(_day)) {
|
||||||
|
sb.Day = _day
|
||||||
|
changed = true
|
||||||
|
} else {
|
||||||
|
it.status(400).result("Invalid Day format")
|
||||||
|
return@patch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ValidString(_soundpath) && _soundpath != sb.Soundpath) {
|
||||||
|
sb.Soundpath = _soundpath
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (_repeat != sb.Repeat) {
|
||||||
|
sb.Repeat = _repeat
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (_enable != sb.Enable) {
|
||||||
|
sb.Enable = _enable
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_broadcast_zones) && _broadcast_zones != sb.BroadcastZones) {
|
||||||
|
sb.BroadcastZones = _broadcast_zones
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if (ValidString(_language) && _language != sb.Language) {
|
||||||
|
if (Language.entries.any{ lang -> lang.name == _language }) {
|
||||||
|
sb.Language = _language
|
||||||
|
changed = true
|
||||||
|
} else {
|
||||||
|
it.status(400).result("Invalid Language")
|
||||||
|
return@patch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
if (db.Update_Schedulebank_by_index(index, sb)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else it.status(500)
|
||||||
|
.result("Failed to update schedule with index $index")
|
||||||
|
} else it.status(400)
|
||||||
|
.result("Nothing has changed for schedule with index $index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get("ExportXLSX") {
|
||||||
|
val xlsxdata = db.Export_Schedulebank_XLSX()
|
||||||
|
if (xlsxdata != null) {
|
||||||
|
it.header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
|
it.header("Content-Disposition", "attachment; filename=\"schedulebank.xlsx\"")
|
||||||
|
it.outputStream().use { out ->
|
||||||
|
xlsxdata.write(out)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to export schedulebank to XLSX")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
post("ImportXLSX") {
|
||||||
|
val uploaded = it.uploadedFile("file")
|
||||||
|
if (uploaded == null) {
|
||||||
|
it.status(400).result("No file uploaded")
|
||||||
|
return@post
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val xlsx = XSSFWorkbook(uploaded.content())
|
||||||
|
if (db.Import_Schedulebank_XLSX(xlsx)) {
|
||||||
|
it.result("OK")
|
||||||
|
} else {
|
||||||
|
it.status(500).result("Failed to import schedulebank from XLSX")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
it.status(400).result("Invalid XLSX file")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
path("Log") {
|
path("Log") {
|
||||||
get("List/<logdate>/<logfilter>") { get1 ->
|
get("List/<logdate>/<logfilter>") { get1 ->
|
||||||
@@ -392,7 +726,6 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fun CheckUsers(ctx: Context) {
|
fun CheckUsers(ctx: Context) {
|
||||||
val user = ctx.sessionAttribute<String?>("user")
|
val user = ctx.sessionAttribute<String?>("user")
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user