commit 24/09/2025

This commit is contained in:
2025-09-24 16:03:07 +07:00
parent 55f6a24cce
commit 1fcf64fd99
5 changed files with 269 additions and 30 deletions

View File

@@ -2,10 +2,15 @@ package barix
import codes.Somecodes
import com.fasterxml.jackson.databind.JsonNode
import org.tinylog.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.util.function.Consumer
@Suppress("unused")
class BarixConnection(val index: UInt, var channel: String, val ipaddress: String, val port: Int = 5002) {
@@ -57,25 +62,54 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
}
}
/**
* Check if Barix device is online
* @return true if online
*/
fun isOnline(): Boolean {
return _onlinecounter > 0
}
/**
* Check if Barix device is idle (not playing)
* @return true if idle
*/
fun isIdle() : Boolean{
return statusData == 0
}
/**
* Check if Barix device is playing
* @return true if playing
*/
fun isPlaying() : Boolean{
return statusData == 1
}
/**
* Send data to Barix device via UDP
* @param data The data to send
* @return true if successful
*/
fun SendData(data: ByteArray): Boolean {
fun SendData(data: ByteArray, cbOK: Consumer<String>, cbFail: Consumer<String>) {
if (data.isNotEmpty()) {
try {
udp.send(DatagramPacket(data, data.size, inet))
return true
} catch (e: Exception) {
Logger.error { "SendData to ${ipaddress}:${port} failed, message: ${e.message}" }
CoroutineScope(Dispatchers.IO).launch {
val bb = ByteBuffer.wrap(data)
while(bb.hasRemaining()){
try {
val chunk = ByteArray(if (bb.remaining() > 1400) 1400 else bb.remaining())
bb.get(chunk)
udp.send(DatagramPacket(chunk, chunk.size, inet))
delay(5)
} catch (e: Exception) {
cbFail.accept("SendData to $ipaddress:$port failed, message: ${e.message}")
return@launch
}
}
cbOK.accept("SendData to $channel ($ipaddress:$port) succeeded, ${data.size} bytes sent")
}
}
return false
}