Files
thrawn/src/thrawn_api.cpp

191 lines
3.8 KiB
C++

#include "thrawn_api.h"
#include <iostream>
#include "Cache.h"
#include "LuaEngine.h"
#include "spdlog/spdlog.h"
/*
HELPER
*/
void push_topic_state_table(lua_State* lua, TopicState state) {
lua_newtable(lua);
lua_pushliteral(lua, "topic");
lua_pushstring(lua, state.topic.c_str());
lua_settable(lua, -3);
lua_pushliteral(lua, "value");
lua_pushstring(lua, state.value.c_str());
lua_settable(lua, -3);
}
void push_list_topic_state(lua_State* lua, std::vector<TopicState> states) {
lua_newtable(lua);
int i = 1;
for (auto& state : states) {
lua_pushinteger(lua, i);
push_topic_state_table(lua, state);
lua_settable(lua, -3);
i++;
}
}
//Ab hier ist ok
int thrawn_log(lua_State* lua)
{
int paramCount = lua_gettop(lua);
if(paramCount != 1)
{
return -1;
}
if (!lua_isstring (lua, -1)) {
return -1;
}
spdlog::info("[{}] {}", LuaEngine::get_instance().service_by_lua_state(lua)->name, lua_tostring(lua, -1));
return 0;
}
int thrawn_register_service(lua_State* lua) {
int paramCount = lua_gettop(lua);
if(paramCount != 1)
{
return -1;
}
LuaEngine::get_instance().service_by_lua_state(lua)->name = lua_tostring(lua, -1);
spdlog::info("Service '{}' registered", lua_tostring(lua, -1));
return 0;
}
int thrawn_create_topic_state(lua_State* lua) {
int paramCount = lua_gettop(lua);
if(paramCount < 0 || paramCount > 3)
{
return -1;
}
TopicState state;
if (paramCount == 1) {
if (!lua_isstring (lua, -1)) {
return -1;
}
state.topic = lua_tostring(lua, -1);
}else if (paramCount == 2) {
if (!lua_isstring (lua, -2) && !lua_isstring (lua, -1)) {
return -1;
}
state.topic = lua_tostring(lua, -2);
state.value = lua_tostring(lua, -1);
}
push_topic_state_table(lua, state);
return 1;
}
int thrawn_topic_state_str(lua_State* lua) {
int paramCount = lua_gettop(lua);
if(paramCount != 1)
{
return -1;
}
std::optional<TopicState> state = TopicState::create_from_lua(lua, -1);
if (! state.has_value()) {
return -1;
}
lua_pushstring(lua, state.value().to_string().c_str());
return 1;
}
int thrawn_subscribe(lua_State* lua) {
int paramCount = lua_gettop(lua);
if(paramCount != 1){
return -1;
}
LuaService* service = LuaEngine::get_instance().service_by_lua_state(lua);
if (service->name == "") {
spdlog::error("Not registered service tried to subscribe to '{}'", lua_tostring(lua, -1));
return 0;
}
service->subscriptions.push_back(lua_tostring(lua, -1));
spdlog::info("Service '{}' subscribed to '{}'", service->name, lua_tostring(lua, -1));
return 0;
}
int thrawn_topic_cache(lua_State* lua) {
int paramCount = lua_gettop(lua);
if(paramCount != 0){
return -1;
}
LuaEngine& lua_engine = LuaEngine::get_instance();
LuaService* service = lua_engine.service_by_lua_state(lua);
push_list_topic_state(lua, lua_engine.cache.topic_states(service->subscriptions));
return 1;
}
// thrawn_publish(topic, value)
int thrawn_publish(lua_State* lua) {
int paramCount = lua_gettop(lua);
if (paramCount != 1) {
return -1;
}
std::optional<TopicState> state = TopicState::create_from_lua(lua, -1);
if (! state.has_value()) {
return -1;
}
LuaEngine::get_instance().publish(state.value());
return 0;
}
void call_thrawn_on_start(LuaService& service) {
lua_getglobal(service.lua, "thrawn_on_start");
if (lua_pcall(service.lua,0, 0, 0) != 0) {
spdlog::error("[{}] Unable to call 'thrawn_on_start'. [Lua: '{}']", service.name, lua_tostring(service.lua,-1));
return;
}
}
void call_thrawn_on_update(LuaService& service, TopicState state) {
lua_getglobal(service.lua, "thrawn_on_update");
push_topic_state_table(service.lua, state);
if (lua_pcall(service.lua, 1, 0, 0) != 0) {
spdlog::error("[{}] Unable to call 'thrawn_on_update'. [Lua: '{}']", service.name, lua_tostring(service.lua,-1));
return;
}
}