Refactor libraries
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.ttsserver.core.web.mime.MimeType
|
||||||
import fi.iki.elonen.NanoHTTPD
|
import fi.iki.elonen.NanoHTTPD
|
||||||
import org.luaj.vm2.LuaValue
|
import org.luaj.vm2.LuaValue
|
||||||
import org.luaj.vm2.lib.TwoArgFunction
|
import org.luaj.vm2.lib.TwoArgFunction
|
||||||
@@ -8,10 +9,13 @@ class HTTPLibrary : TwoArgFunction() {
|
|||||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
val methods = LuaValue.tableOf()
|
val methods = LuaValue.tableOf()
|
||||||
val responses = LuaValue.tableOf()
|
val responses = LuaValue.tableOf()
|
||||||
|
val mimeTypes = LuaValue.tableOf()
|
||||||
NanoHTTPD.Method.values().forEach { methods.set(it.name, it.name) }
|
NanoHTTPD.Method.values().forEach { methods.set(it.name, it.name) }
|
||||||
NanoHTTPD.Response.Status.values().forEach { responses.set(it.name, it.requestStatus) }
|
NanoHTTPD.Response.Status.values().forEach { responses.set(it.name, it.requestStatus) }
|
||||||
|
MimeType.values().forEach { mimeTypes.set(it.name, it.mimeType) }
|
||||||
env.set("Method", methods)
|
env.set("Method", methods)
|
||||||
env.set("Status", responses)
|
env.set("Status", responses)
|
||||||
|
env.set("Mime", mimeTypes)
|
||||||
return methods
|
return methods
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import org.luaj.vm2.lib.jse.CoerceJavaToLua
|
|||||||
class SonosLibrary : TwoArgFunction() {
|
class SonosLibrary : TwoArgFunction() {
|
||||||
|
|
||||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
val sonos = LuaValue.tableOf().also {
|
val sonos = LuaValue.tableOf().apply {
|
||||||
it.set("discover", DiscoverFunction())
|
set("discover", DiscoverFunction())
|
||||||
it.set("of", OfFunction())
|
set("of", OfFunction())
|
||||||
}
|
}
|
||||||
|
|
||||||
env.set("sonos", sonos)
|
env.set("sonos", sonos)
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ import java.util.*
|
|||||||
|
|
||||||
class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
|
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()
|
val tts = LuaValue.tableOf().apply {
|
||||||
tts.set("say", SayMethod(ttsEngine))
|
set("say", SayMethod(ttsEngine))
|
||||||
tts.set("sayToFile", FileMethod(ttsEngine))
|
set("sayToFile", FileMethod(ttsEngine))
|
||||||
|
}
|
||||||
|
|
||||||
env.set("tts", tts)
|
env.set("tts", tts)
|
||||||
|
|
||||||
val audioFormats = LuaValue.tableOf()
|
val audioFormats = LuaValue.tableOf()
|
||||||
|
|||||||
@@ -4,9 +4,13 @@ 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
|
||||||
|
|
||||||
class UtilLibrary : TwoArgFunction() {
|
class ThreadLibrary : TwoArgFunction() {
|
||||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
env.set("sleep", SleepFunction())
|
val thread = LuaValue.tableOf().apply {
|
||||||
|
set("sleep", SleepFunction())
|
||||||
|
}
|
||||||
|
|
||||||
|
env.set("thread", thread)
|
||||||
|
|
||||||
return LuaValue.NIL
|
return LuaValue.NIL
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ import org.luaj.vm2.lib.jse.JseOsLib
|
|||||||
|
|
||||||
class SandboxFactory(
|
class SandboxFactory(
|
||||||
private val configLoader: ConfigLoader,
|
private val configLoader: ConfigLoader,
|
||||||
private val utilLibrary: UtilLibrary,
|
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,
|
||||||
@@ -27,7 +27,7 @@ class SandboxFactory(
|
|||||||
it.load(StringLib())
|
it.load(StringLib())
|
||||||
it.load(JseMathLib())
|
it.load(JseMathLib())
|
||||||
it.load(JseOsLib())
|
it.load(JseOsLib())
|
||||||
it.load(utilLibrary)
|
it.load(threadLibrary)
|
||||||
it.load(serverLibrary)
|
it.load(serverLibrary)
|
||||||
it.load(httpLibrary)
|
it.load(httpLibrary)
|
||||||
it.load(ttsLibrary)
|
it.load(ttsLibrary)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ enum class MimeType(val mimeType: String) {
|
|||||||
WMA("audio/x-ms-wma"),
|
WMA("audio/x-ms-wma"),
|
||||||
WAV("audio/x-wav"),
|
WAV("audio/x-wav"),
|
||||||
FLAC("audio/x-wav"),
|
FLAC("audio/x-wav"),
|
||||||
|
TEXT("text/plain"),
|
||||||
JSON("application/json");
|
JSON("application/json");
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -27,14 +27,14 @@ class LuaModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
fun sandboxFactory(
|
fun sandboxFactory(
|
||||||
configLoader: ConfigLoader,
|
configLoader: ConfigLoader,
|
||||||
utilLibrary: UtilLibrary,
|
threadLibrary: ThreadLibrary,
|
||||||
serverLibrary: ServerLibrary,
|
serverLibrary: ServerLibrary,
|
||||||
httpLibrary: HTTPLibrary,
|
httpLibrary: HTTPLibrary,
|
||||||
ttsLibrary: TTSLibrary,
|
ttsLibrary: TTSLibrary,
|
||||||
sonosLibrary: SonosLibrary
|
sonosLibrary: SonosLibrary
|
||||||
) = SandboxFactory(
|
) = SandboxFactory(
|
||||||
configLoader,
|
configLoader,
|
||||||
utilLibrary,
|
threadLibrary,
|
||||||
serverLibrary,
|
serverLibrary,
|
||||||
httpLibrary,
|
httpLibrary,
|
||||||
ttsLibrary,
|
ttsLibrary,
|
||||||
@@ -43,7 +43,7 @@ class LuaModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun utilLibrary() = UtilLibrary()
|
fun utilLibrary() = ThreadLibrary()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
Reference in New Issue
Block a user