Enable queuing Sonos TTS requests
This commit is contained in:
@@ -1,17 +0,0 @@
|
|||||||
package io.bartek.ttsserver.sonos
|
|
||||||
|
|
||||||
import com.vmichalak.sonoscontroller.SonosDiscovery
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class SonosController(private val host: String, private val port: Int) {
|
|
||||||
fun clip(wave: File, zone: String, volume: Int) {
|
|
||||||
SonosDiscovery.discover().firstOrNull { it.zoneGroupState.name == zone }?.let {
|
|
||||||
val filename = wave.name
|
|
||||||
val url = "http://$host:$port/sonos/$filename"
|
|
||||||
val currentVolume = it.volume
|
|
||||||
it.volume = volume
|
|
||||||
it.clip(url, "")
|
|
||||||
it.volume = currentVolume
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
44
app/src/main/java/io/bartek/ttsserver/sonos/SonosQueue.kt
Normal file
44
app/src/main/java/io/bartek/ttsserver/sonos/SonosQueue.kt
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package io.bartek.ttsserver.sonos
|
||||||
|
|
||||||
|
import com.vmichalak.sonoscontroller.SonosDiscovery
|
||||||
|
import io.bartek.ttsserver.tts.TTS
|
||||||
|
import io.bartek.ttsserver.web.SonosTTSRequestData
|
||||||
|
import java.util.concurrent.BlockingQueue
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue
|
||||||
|
|
||||||
|
private class Consumer(
|
||||||
|
private val tts: TTS,
|
||||||
|
private val host: String,
|
||||||
|
private val port: Int,
|
||||||
|
private val queue: BlockingQueue<SonosTTSRequestData>
|
||||||
|
) : Runnable {
|
||||||
|
override fun run() = try {
|
||||||
|
while (true) {
|
||||||
|
consume(queue.take())
|
||||||
|
}
|
||||||
|
} catch (e: InterruptedException) {
|
||||||
|
Thread.currentThread().interrupt()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun consume(data: SonosTTSRequestData) {
|
||||||
|
SonosDiscovery.discover().firstOrNull { it.zoneGroupState.name == data.zone }?.let {
|
||||||
|
val file = tts.createTTSFile(data.text, data.language)
|
||||||
|
val filename = file.name
|
||||||
|
val url = "http://$host:$port/sonos/$filename"
|
||||||
|
val currentVolume = it.volume
|
||||||
|
it.volume = data.volume
|
||||||
|
it.clip(url, "")
|
||||||
|
it.volume = currentVolume
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SonosQueue(tts: TTS, host: String, port: Int) {
|
||||||
|
private val queue: BlockingQueue<SonosTTSRequestData> = LinkedBlockingQueue()
|
||||||
|
private val consumer = Thread(Consumer(tts, host, port, queue))
|
||||||
|
|
||||||
|
init { consumer.start() }
|
||||||
|
|
||||||
|
fun push(data: SonosTTSRequestData) = queue.add(data)
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
package io.bartek.ttsserver.sonos
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ import io.bartek.ttsserver.network.NetworkUtil
|
|||||||
import io.bartek.ttsserver.preference.PreferenceKey
|
import io.bartek.ttsserver.preference.PreferenceKey
|
||||||
import io.bartek.ttsserver.service.ForegroundService
|
import io.bartek.ttsserver.service.ForegroundService
|
||||||
import io.bartek.ttsserver.service.ServiceState
|
import io.bartek.ttsserver.service.ServiceState
|
||||||
import io.bartek.ttsserver.sonos.SonosController
|
import io.bartek.ttsserver.sonos.SonosQueue
|
||||||
import io.bartek.ttsserver.tts.TTS
|
import io.bartek.ttsserver.tts.TTS
|
||||||
import java.io.BufferedInputStream
|
import java.io.BufferedInputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -24,7 +24,7 @@ class WebServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
|||||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
private val tts = TTS(context, this)
|
private val tts = TTS(context, this)
|
||||||
private val endpoints = Endpoints()
|
private val endpoints = Endpoints()
|
||||||
private val sonos = SonosController(NetworkUtil.getIpAddress(context), port)
|
private val sonos = SonosQueue(tts, NetworkUtil.getIpAddress(context), port)
|
||||||
|
|
||||||
override fun serve(session: IHTTPSession?): Response {
|
override fun serve(session: IHTTPSession?): Response {
|
||||||
try {
|
try {
|
||||||
@@ -100,11 +100,11 @@ class WebServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
|||||||
throw ResponseException(BAD_REQUEST, "")
|
throw ResponseException(BAD_REQUEST, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
val (text, language, zone, volume) = extractBody(session) { SonosTTSRequestData.fromJSON(it) }
|
val data = extractBody(session) { SonosTTSRequestData.fromJSON(it) }
|
||||||
|
|
||||||
val file = tts.createTTSFile(text, language)
|
sonos.push(data)
|
||||||
sonos.clip(file, zone, volume)
|
|
||||||
return newFixedLengthResponse("")
|
return newFixedLengthResponse(ACCEPTED, MIME_PLAINTEXT, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sonosCache(session: IHTTPSession): Response {
|
private fun sonosCache(session: IHTTPSession): Response {
|
||||||
|
|||||||
Reference in New Issue
Block a user