commit 09/09/2025
This commit is contained in:
4
src/barix/BarixConnection.kt
Normal file
4
src/barix/BarixConnection.kt
Normal file
@@ -0,0 +1,4 @@
|
||||
package barix
|
||||
|
||||
class BarixConnection {
|
||||
}
|
||||
101
src/barix/TCP_Barix_Command_Server.kt
Normal file
101
src/barix/TCP_Barix_Command_Server.kt
Normal file
@@ -0,0 +1,101 @@
|
||||
package barix
|
||||
|
||||
import codes.Somecodes.Companion.ValidString
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.tinylog.Logger
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.util.function.Consumer
|
||||
|
||||
@Suppress("unused")
|
||||
class TCP_Barix_Command_Server {
|
||||
private var tcpserver: ServerSocket? = null
|
||||
private var job: Job? = null
|
||||
private val socketMap = mutableMapOf<String, Socket>()
|
||||
|
||||
/**
|
||||
* Start TCP Command Server
|
||||
* @param port The port number to listen on (default is 5001)
|
||||
* @param cb A callback function that will be called when a valid command is received
|
||||
* @return true if successful
|
||||
*/
|
||||
fun StartTcpServer(port: Int = 5001, cb: Consumer<String>): Boolean {
|
||||
try {
|
||||
val tcp = ServerSocket(port)
|
||||
tcpserver = tcp
|
||||
job = CoroutineScope(Dispatchers.IO).launch {
|
||||
Logger.info { "TCP server started" }
|
||||
while (isActive) {
|
||||
if (tcpserver?.isClosed == true) break
|
||||
try {
|
||||
tcpserver?.accept().use { socket ->
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
if (socket != null) {
|
||||
val key : String = socket.inetAddress.hostAddress+":"+socket.port
|
||||
socketMap[key] = socket
|
||||
Logger.info { "Start communicating with $key" }
|
||||
socket.getInputStream().use { din ->
|
||||
{
|
||||
while (isActive) {
|
||||
if (din.available()>0){
|
||||
val bb = ByteArray(din.available())
|
||||
din.read(bb)
|
||||
// B4A format, 4 bytes di depan adalah size
|
||||
val str = String(bb)
|
||||
if (ValidString(str)) cb.accept(str)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Logger.info { "Finished communicating with $key" }
|
||||
socketMap.remove(key)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ex: Exception) {
|
||||
Logger.error { "Failed accepting TCP Socket, Message : ${ex.message}" }
|
||||
}
|
||||
|
||||
}
|
||||
Logger.info { "TCP server stopped" }
|
||||
}
|
||||
return true
|
||||
} catch (e: Exception) {
|
||||
Logger.error { "Failed to StartTcpServer, Message : ${e.message}" }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop TCP Command Server
|
||||
* @return true if succesful
|
||||
*/
|
||||
fun StopTcpCommand(): Boolean {
|
||||
try {
|
||||
tcpserver?.close()
|
||||
runBlocking {
|
||||
socketMap.values.forEach {
|
||||
it.close()
|
||||
}
|
||||
socketMap.clear()
|
||||
job?.join()
|
||||
}
|
||||
Logger.info { "StopTcpCommand success" }
|
||||
return true
|
||||
} catch (e: Exception) {
|
||||
Logger.error { "Failed to StopTcpServer, Message : ${e.message}" }
|
||||
} finally {
|
||||
tcpserver = null
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.function.Consumer
|
||||
import kotlin.io.path.name
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
@@ -30,6 +31,34 @@ class Somecodes {
|
||||
const val GB_threshold = MB_threshold * 1024.0
|
||||
const val TB_threshold = GB_threshold * 1024.0
|
||||
|
||||
/**
|
||||
* List all audio files (.mp3 and .wav) in the specified directory and its subdirectories.
|
||||
* Only files larger than 1KB are included.
|
||||
* @param dir The directory to search in (default is the current working directory).
|
||||
* @return A list of absolute paths to the audio files found.
|
||||
*/
|
||||
fun ListAudioFiles(dir: String = current_directory) : List<String>{
|
||||
return try{
|
||||
// find all files that ends with .mp3 or .wav
|
||||
// and find them recursively
|
||||
val p = Path.of(dir)
|
||||
if (Files.exists(p) && Files.isDirectory(p)){
|
||||
Files.walk(p)
|
||||
// cari file regular saja
|
||||
.filter { Files.isRegularFile(it)}
|
||||
// size lebih dari 1KB
|
||||
.filter { Files.size(it) > 1024}
|
||||
// extension .mp3 atau .wav
|
||||
.filter { it.name.endsWith(".mp3",true) || it.name.endsWith(".wav",true) }
|
||||
.map { it.toAbsolutePath().toString() }
|
||||
.toList()
|
||||
} else throw Exception()
|
||||
|
||||
} catch (_ : Exception){
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a size in bytes to a human-readable format.
|
||||
* @param size Size in bytes.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import codes.Somecodes
|
||||
import codes.Somecodes.Companion.ListAudioFiles
|
||||
import codes.Somecodes.Companion.ValidDate
|
||||
import codes.Somecodes.Companion.ValidFile
|
||||
import codes.Somecodes.Companion.ValidScheduleDay
|
||||
@@ -10,7 +11,9 @@ import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import content.Category
|
||||
import content.Language
|
||||
import content.ScheduleDay
|
||||
import content.VoiceType
|
||||
import database.LanguageLink
|
||||
import database.MariaDB
|
||||
import database.Soundbank
|
||||
import io.javalin.Javalin
|
||||
@@ -123,7 +126,7 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.Read_Queue_Paging()))
|
||||
}
|
||||
|
||||
"getAASPagingQueue" ->{
|
||||
"getAASQueue" ->{
|
||||
SendReply(wsMessageContext, cmd.command, MariaDB.ArrayListtoString(db.Read_Queue_Table()))
|
||||
}
|
||||
|
||||
@@ -162,11 +165,34 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("api") {
|
||||
path("VoiceType"){
|
||||
get{
|
||||
it.result(objectmapper.writeValueAsString(VoiceType.entries.map { vt -> vt.name }))
|
||||
}
|
||||
}
|
||||
path("Category") {
|
||||
get {
|
||||
it.result(objectmapper.writeValueAsString(Category.entries.map { cat -> cat.name }) )
|
||||
}
|
||||
}
|
||||
path("Language") {
|
||||
get {
|
||||
it.result(objectmapper.writeValueAsString(Language.entries.map { lang -> lang.name }) )
|
||||
}
|
||||
}
|
||||
path("ScheduleDay") {
|
||||
get {
|
||||
it.result(objectmapper.writeValueAsString(ScheduleDay.entries.map { day -> day.toString() }))
|
||||
}
|
||||
}
|
||||
path("SoundBank") {
|
||||
get("List") {
|
||||
// get soundbank list
|
||||
it.result(MariaDB.ArrayListtoString(db.SoundbankList))
|
||||
}
|
||||
get("ListFiles"){
|
||||
it.result(objectmapper.writeValueAsString(ListAudioFiles("C:\\soundbank")))
|
||||
}
|
||||
post("Add") {
|
||||
try {
|
||||
val addvalue = objectmapper.readValue(it.body(), Soundbank::class.java)
|
||||
@@ -322,6 +348,9 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
// get messagebank list
|
||||
it.result(MariaDB.ArrayListtoString(db.MessagebankList))
|
||||
}
|
||||
post("Add"){
|
||||
// TODO add new messagebank
|
||||
}
|
||||
delete("List") {
|
||||
// truncate messagebank table
|
||||
if (db.Clear_Messagebank()) {
|
||||
@@ -443,6 +472,22 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
// get language link list
|
||||
it.result(MariaDB.ArrayListtoString(db.LanguageLinkList))
|
||||
}
|
||||
post("Add"){
|
||||
// Parse JSON from request body
|
||||
val json: JsonNode = objectmapper.readTree(it.body())
|
||||
val tag = json.get("tag").asText()
|
||||
val languages = json.get("language").asText().split(";")
|
||||
if (ValidString(tag)){
|
||||
if (languages.all { xx -> Language.entries.any { yy -> yy.name== xx} }){
|
||||
if (db.LanguageLinkList.any { ll -> ll.TAG==tag }){
|
||||
val newvalue = LanguageLink(0u, tag, languages.joinToString(";"))
|
||||
if (db.Add_LanguageLink(newvalue)){
|
||||
it.result("OK")
|
||||
} else it.status(500).result("Failed to add language link to database")
|
||||
} else it.status(400).result("TAG=$tag already exists" )
|
||||
} else it.status(400).result("Contains unsupported language" )
|
||||
} else it.status(400).result("Invalid tag or language")
|
||||
}
|
||||
delete("List") {
|
||||
// truncate language link table
|
||||
if (db.Clear_LanguageLink()) {
|
||||
|
||||
Reference in New Issue
Block a user