clean up hooks and otr.publish()

This commit is contained in:
Jan-Piet Mens
2016-09-07 09:19:17 +02:00
parent 1d5c55b569
commit 678b20265c
2 changed files with 11 additions and 16 deletions
+11 -8
View File
@@ -84,6 +84,8 @@ struct luadata *hooks_init(struct udata *ud, char *script)
ld->L = luaL_newstate();
luaL_openlibs(ld->L);
MQTTconn = ud->mosq;
/*
* Set up a global with some values.
*/
@@ -107,8 +109,10 @@ struct luadata *hooks_init(struct udata *ud, char *script)
lua_pushcfunction(ld->L, otr_getdb);
lua_setfield(ld->L, -2, "getdb");
#ifdef WITH_MQTT
lua_pushcfunction(ld->L, otr_publish);
lua_setfield(ld->L, -2, "publish");
#endif
lua_setglobal(ld->L, "otr");
@@ -138,12 +142,6 @@ struct luadata *hooks_init(struct udata *ud, char *script)
return (ld);
}
#ifdef WITH_MQTT
void hooks_setmosq(struct mosquitto *mosq)
{
MQTTconn = mosq;
}
#endif
void hooks_exit(struct luadata *ld, char *reason)
{
@@ -485,9 +483,14 @@ int otr_publish(lua_State *lua)
qos = lua_tonumber(lua, 3);
retain = lua_tonumber(lua, 4);
rc = mosquitto_publish(MQTTconn, NULL, topic,
if (MQTTconn == NULL) {
olog(LOG_WARNING, "otr_publish(%s, %s, %d, %d): NULL MQTT connection\n",
topic, payload, qos, retain);
} else {
rc = mosquitto_publish(MQTTconn, NULL, topic,
strlen(payload), payload, qos, retain);
olog(LOG_DEBUG, "LUA_PUBLISH (%s, %s, %d, %d) == %d\n", topic, payload, qos, retain, rc);
olog(LOG_DEBUG, "otr_publish(%s, %s, %d, %d) == %d\n", topic, payload, qos, retain, rc);
}
}
return (rc);
}
-8
View File
@@ -3,9 +3,6 @@
#ifdef WITH_LUA
# include <lua.h>
# ifdef WITH_MQTT
# include <mosquitto.h>
# endif
struct luadata {
@@ -19,11 +16,6 @@ void hooks_hook(struct udata *ud, char *topic, JsonNode *obj);
int hooks_norec(struct udata *ud, char *user, char *device, char *payload);
JsonNode *hooks_http(struct udata *ud, char *user, char *device, char *payload);
#ifdef WITH_MQTT
# include <mosquitto.h>
void hooks_setmosq(struct mosquitto *);
#endif
#endif /* WITH_LUA */
#endif