Refactor lua libraries | implement silence mode to /sonos endpoint

This commit is contained in:
2020-07-07 22:07:24 +02:00
parent b2f3f74237
commit 39a15e6b28
9 changed files with 27 additions and 80 deletions

View File

@@ -1,25 +0,0 @@
package com.bartlomiejpluta.ttsserver.core.lua.lib
import android.content.Context
import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.TwoArgFunction
import org.luaj.vm2.lib.jse.CoerceJavaToLua
import java.io.File
class CacheLibrary(private val context: Context) : TwoArgFunction() {
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
val cache = LuaValue.tableOf().apply {
set("file", FileFunction(context))
}
env.set("cache", cache)
return LuaValue.NIL
}
class FileFunction(private val context: Context) : OneArgFunction() {
override fun call(name: LuaValue) = File(context.cacheDir, name.checkjstring())
.let { CoerceJavaToLua.coerce(it) }
}
}

View File

@@ -1,31 +0,0 @@
package com.bartlomiejpluta.ttsserver.core.lua.lib
import android.content.Context
import android.content.Intent
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.bartlomiejpluta.R
import com.bartlomiejpluta.ttsserver.ui.main.MainActivity
import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.TwoArgFunction
class DebugLibrary(private val context: Context) : TwoArgFunction() {
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
env.set("debug", DebugFunction(context))
return LuaValue.NIL
}
class DebugFunction(private val context: Context) : OneArgFunction() {
override fun call(arg: LuaValue): LuaValue {
LocalBroadcastManager
.getInstance(context)
.sendBroadcast(Intent(MainActivity.POPUP).apply {
putExtra(MainActivity.TITLE, context.resources.getString(R.string.debug))
putExtra(MainActivity.MESSAGE, arg.toString())
})
return LuaValue.NIL
}
}
}

View File

@@ -1,7 +1,11 @@
package com.bartlomiejpluta.ttsserver.core.lua.lib
import android.content.Context
import android.content.Intent
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.bartlomiejpluta.R
import com.bartlomiejpluta.ttsserver.core.util.NetworkUtil
import com.bartlomiejpluta.ttsserver.ui.main.MainActivity
import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.TwoArgFunction
@@ -16,6 +20,7 @@ class ServerLibrary(private val context: Context, private val networkUtil: Netwo
set("address", networkUtil.address)
set("url", networkUtil.url)
set("getCachedFile", CacheFileFunction(context))
set("debug", DebugFunction(context))
}
env.set("server", server)
@@ -30,6 +35,18 @@ class ServerLibrary(private val context: Context, private val networkUtil: Netwo
.takeIf { it.exists() }
?.let { CoerceJavaToLua.coerce(it) }
?: LuaValue.NIL
}
class DebugFunction(private val context: Context) : OneArgFunction() {
override fun call(arg: LuaValue): LuaValue {
LocalBroadcastManager
.getInstance(context)
.sendBroadcast(Intent(MainActivity.POPUP).apply {
putExtra(MainActivity.TITLE, context.resources.getString(R.string.debug))
putExtra(MainActivity.MESSAGE, arg.toString())
})
return LuaValue.NIL
}
}
}

View File

@@ -13,7 +13,7 @@ class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
val tts = LuaValue.tableOf().apply {
set("say", SayMethod(ttsEngine))
set("sayToFile", FileMethod(ttsEngine))
set("sayToCache", CacheMethod(ttsEngine))
}
env.set("tts", tts)
@@ -34,7 +34,7 @@ class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
}
}
class FileMethod(private val ttsEngine: TTSEngine) : ThreeArgFunction() {
class CacheMethod(private val ttsEngine: TTSEngine) : ThreeArgFunction() {
override fun call(text: LuaValue, language: LuaValue, format: LuaValue): LuaValue {
val lang = Locale.forLanguageTag(language.checkjstring())
val audioFormat = format

View File

@@ -19,13 +19,11 @@ import org.luaj.vm2.lib.jse.JseOsLib
class SandboxFactory(
private val context: Context,
private val configLoader: ConfigLoader,
private val debugLibrary: DebugLibrary,
private val threadLibrary: ThreadLibrary,
private val serverLibrary: ServerLibrary,
private val httpLibrary: HTTPLibrary,
private val ttsLibrary: TTSLibrary,
private val sonosLibrary: SonosLibrary,
private val cacheLibrary: CacheLibrary
private val sonosLibrary: SonosLibrary
) {
fun createSandbox() = runBlocking {
withContext(Dispatchers.Default) {
@@ -50,13 +48,11 @@ class SandboxFactory(
}
private fun loadApplicationLibraries(sandbox: Globals) {
sandbox.load(debugLibrary)
sandbox.load(threadLibrary)
sandbox.load(serverLibrary)
sandbox.load(threadLibrary)
sandbox.load(httpLibrary)
sandbox.load(ttsLibrary)
sandbox.load(sonosLibrary)
sandbox.load(cacheLibrary)
}
private fun install(sandbox: Globals) {

View File

@@ -30,29 +30,21 @@ class LuaModule {
fun sandboxFactory(
context: Context,
configLoader: ConfigLoader,
debugLibrary: DebugLibrary,
threadLibrary: ThreadLibrary,
serverLibrary: ServerLibrary,
httpLibrary: HTTPLibrary,
ttsLibrary: TTSLibrary,
sonosLibrary: SonosLibrary,
cacheLibrary: CacheLibrary
sonosLibrary: SonosLibrary
) = SandboxFactory(
context,
configLoader,
debugLibrary,
threadLibrary,
serverLibrary,
httpLibrary,
ttsLibrary,
sonosLibrary,
cacheLibrary
sonosLibrary
)
@Provides
@Singleton
fun debugLibrary(context: Context) = DebugLibrary(context)
@Provides
@Singleton
fun threadLibrary() = ThreadLibrary()
@@ -74,9 +66,6 @@ class LuaModule {
@Singleton
fun sonosLibrary() = SonosLibrary()
@Provides
@Singleton
fun cacheLibrary(context: Context) = CacheLibrary(context)
@Provides
@Singleton

View File

@@ -3,7 +3,7 @@ return {
method = Method.GET,
consumer = function(request)
local filename = string.format("%s.%s", request.path.filename, request.path.ext)
local file = cache.file(filename)
local file = server.getCachedFile(filename)
local mime = Mime[request.path.ext:upper()]
return {

View File

@@ -6,7 +6,7 @@ return {
local audioFormat = AudioFormat[format]
local mime = Mime[format]
local file = tts.sayToFile(request.query.phrase, request.query.lang or "en", audioFormat)
local file = tts.sayToCache(request.query.phrase, request.query.lang or "en", audioFormat)
return {
mime = mime,

View File

@@ -1,7 +1,7 @@
local snapshot
function prepareTTSFile(phrase, language)
local file = tts.sayToFile(phrase, language, AudioFormat.MP3)
local file = tts.sayToCache(phrase, language, AudioFormat.MP3)
return string.format("%s/cache/%s", server.url, file:getName())
end
@@ -35,6 +35,7 @@ return {
accepts = Mime.JSON,
queued = true,
consumer = function(request, queueLength)
if(config.silenceMode()) then return end
local body = json.decode(request.body)
local zone = config.sonosDevices[body.zone]