Added support for scheduled services

This commit is contained in:
2020-11-28 09:35:29 +01:00
parent cc70ee5447
commit 707349469d
4 changed files with 18 additions and 19 deletions

View File

@@ -9,6 +9,7 @@
#include "croncpp/croncpp.h" #include "croncpp/croncpp.h"
#include <ctime> #include <ctime>
#include <queue> #include <queue>
#include "spdlog/spdlog.h"
struct ScheduledService { struct ScheduledService {
cron::cronexpr cron; cron::cronexpr cron;
@@ -73,4 +74,16 @@ inline bool check_arguments<bool>(lua_State* lua, int stack_pos) {
return lua_isnumber(lua, stack_pos); return lua_isnumber(lua, stack_pos);
} }
template<typename F>
void lua_func_call(LuaService& service, const std::string& lua_func, F push_params) {
lua_getglobal(service.lua, lua_func.c_str());
int num_arguments = push_params();
if (lua_pcall(service.lua, num_arguments, 0, 0) != 0) {
spdlog::error("[{}] Unable to call '{}'. [Lua: '{}']", lua_func, service.name);
return;
}
}
#endif // LUA_ENGINE_H #endif // LUA_ENGINE_H

View File

@@ -2,7 +2,7 @@
#define VERSION_H #define VERSION_H
#define THRAWN_MAJOR_VERSION 1 #define THRAWN_MAJOR_VERSION 1
#define THRAWN_MINOR_VERSION 0 #define THRAWN_MINOR_VERSION 1
#define THRAWN_PATCH_VERSION 5 #define THRAWN_PATCH_VERSION 0
#endif // VERSION_H #endif // VERSION_H

View File

@@ -254,13 +254,13 @@ int thrawn_publish(lua_State* lua) {
} }
void call_thrawn_on_start(LuaService& service) { void call_thrawn_on_start(LuaService& service) {
thrawn_call(service, "thrawn_on_start", [&]() { lua_func_call(service, "thrawn_on_start", [&]() {
return 0; return 0;
}); });
} }
void call_thrawn_on_update(LuaService& service, TopicState state) { void call_thrawn_on_update(LuaService& service, TopicState state) {
thrawn_call(service, "thrawn_on_update", [&]() { lua_func_call(service, "thrawn_on_update", [&]() {
push_topic_state_table(service.lua, state); push_topic_state_table(service.lua, state);
return 1; return 1;
@@ -268,7 +268,7 @@ void call_thrawn_on_update(LuaService& service, TopicState state) {
} }
void call_thrawn_on_schedule(LuaService& service, std::time_t now, const std::string& callback) { void call_thrawn_on_schedule(LuaService& service, std::time_t now, const std::string& callback) {
thrawn_call(service, callback.c_str(), [&]() { lua_func_call(service, callback.c_str(), [&]() {
lua_pushinteger(service.lua, now); lua_pushinteger(service.lua, now);
return 1; return 1;

View File

@@ -1,6 +1,5 @@
#if !defined(THRAWN_API_H) #if !defined(THRAWN_API_H)
#define THRAWN_API_H #define THRAWN_API_H
#include <spdlog/spdlog.h>
extern "C" extern "C"
{ {
@@ -47,17 +46,4 @@ int thrawn_log_light(lua_State* lua);
int thrawn_log_light_dimmable(lua_State* lua); int thrawn_log_light_dimmable(lua_State* lua);
int thrawn_log_light_tw(lua_State* lua); int thrawn_log_light_tw(lua_State* lua);
template<typename F>
void thrawn_call(LuaService& service, const std::string& lua_func, F push_params) {
lua_getglobal(service.lua, lua_func.c_str());
int num_arguments = push_params();
if (lua_pcall(service.lua, num_arguments, 0, 0) != 0) {
spdlog::error("[{}] Unable to call '{}'. [Lua: '{}']", lua_func, service.name);
return;
}
}
#endif // THRAWN_API_H #endif // THRAWN_API_H