commit 07/02/2026
This commit is contained in:
@@ -7,17 +7,15 @@ import org.tinylog.Logger
|
||||
import java.io.DataInputStream
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.function.Consumer
|
||||
|
||||
@Suppress("unused")
|
||||
class TCP_Barix_Command_Server {
|
||||
lateinit var tcpserver: ServerSocket
|
||||
lateinit var job: Job
|
||||
private val socketMap = mutableMapOf<String, Socket>()
|
||||
|
||||
private val regex = """STATUSBARIX;(\d+);(\d+)(;(\d+))?"""
|
||||
private val pattern = Regex(regex)
|
||||
//private val regex = """STATUSBARIX;(\d+);(\d+)(;(\d+))?"""
|
||||
//private val pattern = Regex(regex)
|
||||
|
||||
/**
|
||||
* Start TCP Command Server
|
||||
@@ -43,6 +41,7 @@ class TCP_Barix_Command_Server {
|
||||
try{
|
||||
|
||||
val din = DataInputStream(socket.getInputStream())
|
||||
var VuZeroCounter = 0L
|
||||
while (isActive) {
|
||||
|
||||
val bb = ByteArray(128)
|
||||
@@ -60,25 +59,56 @@ class TCP_Barix_Command_Server {
|
||||
Logger.error { "Error reading length from Streamer Output with IP $key, Message : ${ex.message}" }
|
||||
continue
|
||||
}
|
||||
val str = String(bb,4, stringlength).trim()
|
||||
var str = String(bb,4, stringlength).trim()
|
||||
if (str.isBlank()) continue
|
||||
if (!str.startsWith("STATUSBARIX")) continue
|
||||
if (str.endsWith("@")) str = str.removeSuffix("@")
|
||||
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,
|
||||
statusdata.isNullOrEmpty() // barix tidak ada statusdata , Q-AG1 ada
|
||||
)
|
||||
//Logger.info { "Received valid command from $key : $status" }
|
||||
cb.accept(status)
|
||||
} ?: run {
|
||||
Logger.warn { "Invalid command format from $key : $str" }
|
||||
// Valid command from StreamerOutput is in format $"STATUSBARIX;VU;BuffRemain;StatusData"$
|
||||
// Valid command from Barix is in format $"STATUSBARIX;VU;BuffRemain"$
|
||||
val values = str.split(";")
|
||||
if (values.size<3) continue
|
||||
if ("STATUSBARIX" != values[0]) continue
|
||||
val vu = values[1].toIntOrNull() ?: continue
|
||||
|
||||
val buffremain = values[2].toIntOrNull() ?: continue
|
||||
var status: BarixStatus
|
||||
when(values.size){
|
||||
3 ->{
|
||||
// mode barix
|
||||
// kadang vu stuck tidak di 0 saat idle,
|
||||
// jadi kalau vu <512 selama 10 kali berturut2
|
||||
// dan buffer lebih dari 16000, anggap idle
|
||||
if ((vu < 512) && (buffremain>=16000)){
|
||||
VuZeroCounter++
|
||||
} else {
|
||||
VuZeroCounter = 0
|
||||
}
|
||||
// statusdata = isplaying = , if VuZeroCounter >=10 then idle (0) else playing (1)
|
||||
val statusdata = if (VuZeroCounter>=10) 0 else 1
|
||||
status = BarixStatus(
|
||||
socket.inetAddress.hostAddress,
|
||||
vu,
|
||||
buffremain,
|
||||
statusdata,
|
||||
true
|
||||
)
|
||||
}
|
||||
4 ->{
|
||||
// mode Q-AG1
|
||||
val statusdata = values[3].toIntOrNull() ?: 0
|
||||
status = BarixStatus(
|
||||
socket.inetAddress.hostAddress,
|
||||
vu,
|
||||
buffremain,
|
||||
statusdata,
|
||||
false
|
||||
)
|
||||
}
|
||||
else -> continue
|
||||
}
|
||||
cb.accept(status)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user