Simplify MQTT structure

This commit is contained in:
Pim van Pelt
2017-12-22 21:38:19 +01:00
parent da963f46f5
commit 67bc9b9d23
2 changed files with 14 additions and 20 deletions

View File

@ -6,5 +6,8 @@
"h": 56, "h": 56,
"label": "On", "label": "On",
"type": 1, "type": 1,
"mqtt": [ "/topic1 Hello", "/t2 Wereld", "/topic3", "", "/top4" ] "mqtt": [
{ "topic": "/topic1", "message": {"string": 100, "args": {"int":100, "str":"string1"}}},
{ "topic": "/topic2", "message": {"string": 200, "args": { "int":200, "str":"string2"}}}
]
} }

View File

@ -83,29 +83,20 @@ static void widget_default_mqtt_send(struct widget_t *w, void *ev_data) {
LOG(LL_INFO, ("MQTT string: '%s'", (char *)w->user_data)); LOG(LL_INFO, ("MQTT string: '%s'", (char *)w->user_data));
// Traverse Array // Traverse Array
for (idx = 0; json_scanf_array_elem(w->user_data, strlen(w->user_data), "", idx, &val) > 0; idx++) { for (idx = 0; json_scanf_array_elem(w->user_data, strlen(w->user_data), "", idx, &val) > 0; idx++) {
char *t=NULL, *m=NULL; char *topic, *message;
uint16_t t_len=0, m_len=0;
char *topic;
LOG(LL_DEBUG, ("Index %d, token [%.*s]", idx, val.len, val.ptr)); LOG(LL_DEBUG, ("Index %d, token [%.*s]", idx, val.len, val.ptr));
t=(char*)val.ptr; if (val.len==0) {
m=strstr(val.ptr, " "); LOG(LL_ERROR, ("mqtt element %d is empty token", idx));
if (m-val.ptr <= val.len) { continue;
LOG(LL_INFO, ("Space found"));
t_len=m-t;
m++;
m_len=val.len-t_len-1;
} else {
t_len=val.len;
m_len=0;
m=NULL;
} }
if ((topic=malloc(t_len+1))) { if (json_scanf(val.ptr, val.len, "{topic:%Q, message:%Q}", &topic, &message) != 2) {
memcpy(topic, t, t_len); LOG(LL_ERROR, ("mqtt element %d ('%.*s'), required fields 'topic' and 'message'", idx, val.len, val.ptr));
topic[t_len]=0; continue;
LOG(LL_INFO, ("Sending topic='%s', message='%.*s'", topic, m_len, m));
free(topic);
} }
LOG(LL_INFO, ("topic='%s', message='%s'", topic, message));
if (topic) free(topic);
if (message) free(message);
} }
(void) ev_data; (void) ev_data;
} }