From e8b7ffa70194d1cc3ef4cc52bb0ace9f133514cf Mon Sep 17 00:00:00 2001 From: fulup Date: Tue, 15 Aug 2017 12:48:59 +0200 Subject: docall working with simple table --- Controler-afb/ctl-lua.c | 67 ++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'Controler-afb/ctl-lua.c') diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c index 275b9d3..a7cd52f 100644 --- a/Controler-afb/ctl-lua.c +++ b/Controler-afb/ctl-lua.c @@ -238,6 +238,9 @@ STATIC void LuaFormatMessage(lua_State* luaState, LuaAfbMessageT action) { message="-- Empty Message ???"; goto PrintMessage; } + + //AFB_NOTICE("**** responseJ=%s", json_object_get_string(responseJ)); + // if we have only on argument just return the value. if (json_object_get_type(responseJ)!=json_type_array || json_object_array_length(responseJ) <2) { @@ -256,22 +259,31 @@ STATIC void LuaFormatMessage(lua_State* luaState, LuaAfbMessageT action) { if (format[idx]=='%' && format[idx] !='\0') { json_object *slotJ= json_object_array_get_idx(responseJ, arrayIdx); + //if (slotJ) AFB_NOTICE("**** idx=%d slotJ=%s", arrayIdx, json_object_get_string(slotJ)); + switch (format[++idx]) { case 'd': if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%d", json_object_get_int(slotJ)); else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil"); + arrayIdx++; break; case 'f': if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%f", json_object_get_double(slotJ)); else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil"); + arrayIdx++; + break; + + case'%': + targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%"); break; case 's': default: if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%s", json_object_get_string(slotJ)); else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil"); - } + arrayIdx++; + } } else { message[targetIdx++] = format[idx]; @@ -497,7 +509,7 @@ STATIC int LuaAfbPushEvent(lua_State* luaState) { // Generated some fake event based on watchdog/counter PUBLIC int LuaCallFunc (DispatchActionT *action, json_object *queryJ) { - int err, count=0; + int err, count; json_object* argsJ = action->argsJ; const char* func = action->call; @@ -506,29 +518,22 @@ PUBLIC int LuaCallFunc (DispatchActionT *action, json_object *queryJ) { lua_getglobal(luaState, func); // push argsJ on the stack + count=0; if (!argsJ) { lua_pushnil(luaState); count++; - } else if (json_object_get_type(argsJ) != json_type_array) { + } else { count+= LuaPushArgument (argsJ); - } else { - for (int idx=0; idx