Refactor lua libraries | implement silence mode to /sonos endpoint
This commit is contained in:
@@ -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) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||||
|
|
||||||
import android.content.Context
|
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.core.util.NetworkUtil
|
||||||
|
import com.bartlomiejpluta.ttsserver.ui.main.MainActivity
|
||||||
import org.luaj.vm2.LuaValue
|
import org.luaj.vm2.LuaValue
|
||||||
import org.luaj.vm2.lib.OneArgFunction
|
import org.luaj.vm2.lib.OneArgFunction
|
||||||
import org.luaj.vm2.lib.TwoArgFunction
|
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("address", networkUtil.address)
|
||||||
set("url", networkUtil.url)
|
set("url", networkUtil.url)
|
||||||
set("getCachedFile", CacheFileFunction(context))
|
set("getCachedFile", CacheFileFunction(context))
|
||||||
|
set("debug", DebugFunction(context))
|
||||||
}
|
}
|
||||||
|
|
||||||
env.set("server", server)
|
env.set("server", server)
|
||||||
@@ -30,6 +35,18 @@ class ServerLibrary(private val context: Context, private val networkUtil: Netwo
|
|||||||
.takeIf { it.exists() }
|
.takeIf { it.exists() }
|
||||||
?.let { CoerceJavaToLua.coerce(it) }
|
?.let { CoerceJavaToLua.coerce(it) }
|
||||||
?: LuaValue.NIL
|
?: 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
|
|||||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
val tts = LuaValue.tableOf().apply {
|
val tts = LuaValue.tableOf().apply {
|
||||||
set("say", SayMethod(ttsEngine))
|
set("say", SayMethod(ttsEngine))
|
||||||
set("sayToFile", FileMethod(ttsEngine))
|
set("sayToCache", CacheMethod(ttsEngine))
|
||||||
}
|
}
|
||||||
|
|
||||||
env.set("tts", tts)
|
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 {
|
override fun call(text: LuaValue, language: LuaValue, format: LuaValue): LuaValue {
|
||||||
val lang = Locale.forLanguageTag(language.checkjstring())
|
val lang = Locale.forLanguageTag(language.checkjstring())
|
||||||
val audioFormat = format
|
val audioFormat = format
|
||||||
|
|||||||
@@ -19,13 +19,11 @@ import org.luaj.vm2.lib.jse.JseOsLib
|
|||||||
class SandboxFactory(
|
class SandboxFactory(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val configLoader: ConfigLoader,
|
private val configLoader: ConfigLoader,
|
||||||
private val debugLibrary: DebugLibrary,
|
|
||||||
private val threadLibrary: ThreadLibrary,
|
private val threadLibrary: ThreadLibrary,
|
||||||
private val serverLibrary: ServerLibrary,
|
private val serverLibrary: ServerLibrary,
|
||||||
private val httpLibrary: HTTPLibrary,
|
private val httpLibrary: HTTPLibrary,
|
||||||
private val ttsLibrary: TTSLibrary,
|
private val ttsLibrary: TTSLibrary,
|
||||||
private val sonosLibrary: SonosLibrary,
|
private val sonosLibrary: SonosLibrary
|
||||||
private val cacheLibrary: CacheLibrary
|
|
||||||
) {
|
) {
|
||||||
fun createSandbox() = runBlocking {
|
fun createSandbox() = runBlocking {
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
@@ -50,13 +48,11 @@ class SandboxFactory(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun loadApplicationLibraries(sandbox: Globals) {
|
private fun loadApplicationLibraries(sandbox: Globals) {
|
||||||
sandbox.load(debugLibrary)
|
|
||||||
sandbox.load(threadLibrary)
|
|
||||||
sandbox.load(serverLibrary)
|
sandbox.load(serverLibrary)
|
||||||
|
sandbox.load(threadLibrary)
|
||||||
sandbox.load(httpLibrary)
|
sandbox.load(httpLibrary)
|
||||||
sandbox.load(ttsLibrary)
|
sandbox.load(ttsLibrary)
|
||||||
sandbox.load(sonosLibrary)
|
sandbox.load(sonosLibrary)
|
||||||
sandbox.load(cacheLibrary)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun install(sandbox: Globals) {
|
private fun install(sandbox: Globals) {
|
||||||
|
|||||||
@@ -30,29 +30,21 @@ class LuaModule {
|
|||||||
fun sandboxFactory(
|
fun sandboxFactory(
|
||||||
context: Context,
|
context: Context,
|
||||||
configLoader: ConfigLoader,
|
configLoader: ConfigLoader,
|
||||||
debugLibrary: DebugLibrary,
|
|
||||||
threadLibrary: ThreadLibrary,
|
threadLibrary: ThreadLibrary,
|
||||||
serverLibrary: ServerLibrary,
|
serverLibrary: ServerLibrary,
|
||||||
httpLibrary: HTTPLibrary,
|
httpLibrary: HTTPLibrary,
|
||||||
ttsLibrary: TTSLibrary,
|
ttsLibrary: TTSLibrary,
|
||||||
sonosLibrary: SonosLibrary,
|
sonosLibrary: SonosLibrary
|
||||||
cacheLibrary: CacheLibrary
|
|
||||||
) = SandboxFactory(
|
) = SandboxFactory(
|
||||||
context,
|
context,
|
||||||
configLoader,
|
configLoader,
|
||||||
debugLibrary,
|
|
||||||
threadLibrary,
|
threadLibrary,
|
||||||
serverLibrary,
|
serverLibrary,
|
||||||
httpLibrary,
|
httpLibrary,
|
||||||
ttsLibrary,
|
ttsLibrary,
|
||||||
sonosLibrary,
|
sonosLibrary
|
||||||
cacheLibrary
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun debugLibrary(context: Context) = DebugLibrary(context)
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun threadLibrary() = ThreadLibrary()
|
fun threadLibrary() = ThreadLibrary()
|
||||||
@@ -74,9 +66,6 @@ class LuaModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
fun sonosLibrary() = SonosLibrary()
|
fun sonosLibrary() = SonosLibrary()
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun cacheLibrary(context: Context) = CacheLibrary(context)
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ return {
|
|||||||
method = Method.GET,
|
method = Method.GET,
|
||||||
consumer = function(request)
|
consumer = function(request)
|
||||||
local filename = string.format("%s.%s", request.path.filename, request.path.ext)
|
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()]
|
local mime = Mime[request.path.ext:upper()]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ return {
|
|||||||
local audioFormat = AudioFormat[format]
|
local audioFormat = AudioFormat[format]
|
||||||
local mime = Mime[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 {
|
return {
|
||||||
mime = mime,
|
mime = mime,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local snapshot
|
local snapshot
|
||||||
|
|
||||||
function prepareTTSFile(phrase, language)
|
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())
|
return string.format("%s/cache/%s", server.url, file:getName())
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -35,6 +35,7 @@ return {
|
|||||||
accepts = Mime.JSON,
|
accepts = Mime.JSON,
|
||||||
queued = true,
|
queued = true,
|
||||||
consumer = function(request, queueLength)
|
consumer = function(request, queueLength)
|
||||||
|
if(config.silenceMode()) then return end
|
||||||
local body = json.decode(request.body)
|
local body = json.decode(request.body)
|
||||||
local zone = config.sonosDevices[body.zone]
|
local zone = config.sonosDevices[body.zone]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user