commit 08/10/2025
BarixConnection Activate Deactivate Relays
This commit is contained in:
@@ -6,9 +6,11 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import org.tinylog.Logger
|
||||
import java.net.DatagramPacket
|
||||
import java.net.DatagramSocket
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Socket
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.function.Consumer
|
||||
|
||||
@@ -21,6 +23,7 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
private val udp = DatagramSocket(0)
|
||||
private val inet = InetSocketAddress(ipaddress, port)
|
||||
private val maxUDPsize = 1000
|
||||
private var _tcp: Socket? = null
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,6 +56,15 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
_onlinecounter = 5
|
||||
}
|
||||
|
||||
/**
|
||||
* TCP command socket for communication with this Barix device
|
||||
*/
|
||||
var commandsocket: Socket?
|
||||
get() = _tcp
|
||||
set(value){
|
||||
_tcp = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement online counter, if counter reaches 0, the device is considered offline
|
||||
*/
|
||||
@@ -142,4 +154,76 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
fun toJsonString(): String {
|
||||
return Somecodes.toJsonString(toJsonNode())
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate relay on Barix device
|
||||
* @param relays The relay numbers to activate (1-8)
|
||||
* @return true if successful
|
||||
*/
|
||||
fun ActivateRelay(vararg relays: Int){
|
||||
val command = StringBuilder("RELAY;")
|
||||
var binary = 0
|
||||
relays.forEach {
|
||||
if (it in 1..8) {
|
||||
binary = binary or (1 shl (it - 1))
|
||||
}
|
||||
}
|
||||
command.append(binary.toString()).append("@")
|
||||
SendCommand(command.toString())
|
||||
}
|
||||
|
||||
fun ActivateRelay(relays: List<Int>){
|
||||
val command = StringBuilder("RELAY;")
|
||||
var binary = 0
|
||||
relays.forEach {
|
||||
if (it in 1..8) {
|
||||
binary = binary or (1 shl (it - 1))
|
||||
}
|
||||
}
|
||||
command.append(binary.toString()).append("@")
|
||||
SendCommand(command.toString())
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate relay on Barix device
|
||||
*/
|
||||
fun DeactivateRelay(){
|
||||
SendCommand("RELAY;0@")
|
||||
}
|
||||
|
||||
/**
|
||||
* Send command to Barix device
|
||||
* @param command The command to send
|
||||
* @return true if successful
|
||||
*/
|
||||
fun SendCommand(command: String): Boolean {
|
||||
try {
|
||||
if (_tcp!=null){
|
||||
if (!_tcp!!.isClosed){
|
||||
val bb = command.toByteArray()
|
||||
val size = bb.size + 4
|
||||
val b4 = byteArrayOf(
|
||||
(size shr 24 and 0xFF).toByte(),
|
||||
(size shr 16 and 0xFF).toByte(),
|
||||
(size shr 8 and 0xFF).toByte(),
|
||||
(size and 0xFF).toByte()
|
||||
)
|
||||
val out = _tcp!!.getOutputStream()
|
||||
out.write(b4)
|
||||
out.write(bb)
|
||||
out.flush()
|
||||
Logger.info { "SendCommand to $ipaddress : $command" }
|
||||
return true
|
||||
}else {
|
||||
Logger.error { "Socket to $ipaddress is not connected" }
|
||||
}
|
||||
} else {
|
||||
Logger.error { "Socket to $ipaddress is null" }
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
Logger.error { "Failed to SendCommand to $ipaddress, Message : ${e.message}" }
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user