use a queue

This commit is contained in:
2023-10-20 13:40:35 -04:00
parent d5d52ad2bd
commit e96e73136a
5 changed files with 43 additions and 37 deletions

View File

@@ -7,9 +7,10 @@ import org.freedesktop.dbus.interfaces.DBusInterface
import org.freedesktop.dbus.interfaces.DBusSigHandler
import org.freedesktop.dbus.messages.DBusSignal
import org.slf4j.LoggerFactory
import java.util.Queue
import kotlin.concurrent.thread
class DBus(handler: Handler) {
class DBus(private val q: Queue<Op>) {
private val thread = thread(
start = true,
@@ -26,7 +27,7 @@ class DBus(handler: Handler) {
log.info("signal: ${signal.op}")
val op = Op.valueOf(signal.op)
log.info("op: ${op}")
handler.handle(op)
q.offer(op)
}
while (true) {
try {

View File

@@ -1,9 +1,13 @@
package net.eksb.obsdc
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
object Main {
@JvmStatic
fun main(args: Array<String>) {
val obs = ObsCommunity()
DBus(obs.handler) // forks non-daemon thread
val q:BlockingQueue<Op> = LinkedBlockingQueue()
DBus(q) // forks non-daemon thread
ObsCommunity(q).run() // blocks
}
}

View File

@@ -1,5 +0,0 @@
package net.eksb.obsdc
interface Obs {
val handler:Handler
}

View File

@@ -2,45 +2,49 @@ package net.eksb.obsdc
import io.obswebsocket.community.client.OBSRemoteController
import org.slf4j.LoggerFactory
import java.util.concurrent.BlockingQueue
class ObsCommunity: Obs, AutoCloseable {
class ObsCommunity(private val q:BlockingQueue<Op>): AutoCloseable {
// TODO: make a single controller, and read ops from a queue.
private val controller = OBSRemoteController.builder()
.host("localhost")
.port(4455)
.password("R3tRkVXhFofJ2wRF") // TODO put this in a file
.autoConnect(false)
.lifecycle().onReady(::ready).and()
.build()
override fun close() {}
fun run() {
controller.connect()
}
override val handler: Handler = Handler { op ->
var controller:OBSRemoteController? = null
override fun close() {
controller.disconnect()
}
fun ready() {
controller?.let { controller ->
log.info("ready to send ${op}")
private fun ready() {
while (true) {
val op: Op = q.take() // blocks
log.info("op: ${op}")
op(op)
}
}
when(op) {
Op.STUDIO_TRANSITION -> {
controller.triggerStudioModeTransition { response ->
log.info("Response successful: ${response.isSuccessful}")
controller.disconnect()
}
}
private fun op(op:Op) {
when(op) {
Op.STUDIO_TRANSITION -> {
controller.triggerStudioModeTransition { response ->
// This does not get called?
log.info("Response successful: ${response.isSuccessful}")
}
}
}
controller = OBSRemoteController.builder()
.host("localhost")
.port(4455)
.password("R3tRkVXhFofJ2wRF") // TODO put this in a file
.autoConnect(true)
.lifecycle()
.onReady {
ready()
}
.and()
.build()
}
companion object {
private val log = LoggerFactory.getLogger(ObsCommunity::class.java)
}
}
}
// Move window:
// https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#setsceneitemtransform

2
src/scripts/obs-signal Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
dbus-send --session --type=signal --dest=net.eksb.obsdc / net.eksb.Obsdc.Signal string:$1