commit 24/09/2025

This commit is contained in:
2025-09-24 11:48:36 +07:00
parent 59dd67acc8
commit 55f6a24cce
7 changed files with 156 additions and 15 deletions

View File

@@ -1,26 +1,74 @@
package barix
import codes.Somecodes
import com.fasterxml.jackson.databind.JsonNode
import org.tinylog.Logger
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetSocketAddress
@Suppress("unused")
class BarixConnection(var channel: String, val ipaddress: String, val port: Int = 5002) {
var bufferRemain: Int = 0
var statusData: Int = 0
var vu: Int = 0
class BarixConnection(val index: UInt, var channel: String, val ipaddress: String, val port: Int = 5002) {
private var _bR: Int = 0
private var _sd: Int = 0
private var _vu: Int = 0
private var _onlinecounter = 0
private val udp = DatagramSocket(0)
private val inet = InetSocketAddress(ipaddress, port)
/**
* Buffer remain in bytes
*/
var bufferRemain: Int
get() = _bR
set(value) {
_bR = value
_onlinecounter = 5
}
/**
* Status data, 0 = playback idle, 1 = playback running
*/
var statusData: Int
get() = _sd
set(value) {
_sd = if (value < 0) 0 else if (value > 1) 1 else value
_onlinecounter = 5
}
/**
* VU level 0-100
*/
var vu: Int
get() = _vu
set(value) {
_vu = if (value < 0) 0 else if (value > 100) 100 else value
_onlinecounter = 5
}
/**
* Decrement online counter, if counter reaches 0, the device is considered offline
*/
fun decrementOnlineCounter() {
if (_onlinecounter > 0) {
_onlinecounter--
}
}
fun isOnline(): Boolean {
return _onlinecounter > 0
}
/**
* Send data to Barix device via UDP
* @param data The data to send
* @return true if successful
*/
fun SendData(data: ByteArray): Boolean {
if (data.isNotEmpty()){
try{
if (data.isNotEmpty()) {
try {
udp.send(DatagramPacket(data, data.size, inet))
return true
} catch (e: Exception) {
@@ -30,4 +78,30 @@ class BarixConnection(var channel: String, val ipaddress: String, val port: Int
return false
}
/**
* Convert BarixConnection to JsonNode
* @return JsonNode representation of BarixConnection
*/
fun toJsonNode(): JsonNode {
// make json node from index, channel, ipaddress, port, bufferRemain, statusData, vu
return Somecodes.objectmapper.createObjectNode().apply {
put("index", index.toInt())
put("channel", channel)
put("ipaddress", ipaddress)
put("port", port)
put("bufferRemain", bufferRemain)
put("statusData", statusData)
put("vu", vu)
put("isOnline", isOnline())
}
}
/**
* Convert BarixConnection to JSON string
* @return JSON string representation of BarixConnection
*/
fun toJsonString(): String {
return Somecodes.toJsonString(toJsonNode())
}
}