commit 23/09/2025

This commit is contained in:
2025-09-23 16:13:32 +07:00
parent 85ccf05634
commit 59dd67acc8
4 changed files with 89 additions and 9 deletions

View File

@@ -1,12 +1,7 @@
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 kotlinx.coroutines.*
import org.tinylog.Logger
import java.net.ServerSocket
import java.net.Socket
@@ -18,13 +13,15 @@ class TCP_Barix_Command_Server {
private var job: Job? = null
private val socketMap = mutableMapOf<String, Socket>()
private val regex: String = "\\$\\\"STATUSBARIX;(\\d+);(\\d+);?(\\d)?\\\"\\$"
private val pattern = Regex(regex)
/**
* 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 {
fun StartTcpServer(port: Int = 5001, cb: Consumer<BarixStatus>): Boolean {
try {
val tcp = ServerSocket(port)
tcpserver = tcp
@@ -48,7 +45,22 @@ class TCP_Barix_Command_Server {
din.read(bb)
// B4A format, 4 bytes di depan adalah size
val str = String(bb)
if (ValidString(str)) cb.accept(str)
if (ValidString(str)) {
// Valid command from Barix is in format $"STATUSBARIX;VU;BuffRemain;StatusData"$
pattern.find(str)?.let { matchResult ->
val (vu, buffremain, statusdata) = matchResult.destructured
val status = BarixStatus(
socket.inetAddress.hostAddress,
vu.toInt(),
buffremain.toInt(),
statusdata.toIntOrNull() ?: 0
)
Logger.info { "Received valid command from $key : $status" }
cb.accept(status)
} ?: run {
Logger.warn { "Invalid command format from $key : $str" }
}
}
}
}
}