Simplify MQTT structure
This commit is contained in:
@ -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));
|
||||
// Traverse Array
|
||||
for (idx = 0; json_scanf_array_elem(w->user_data, strlen(w->user_data), "", idx, &val) > 0; idx++) {
|
||||
char *t=NULL, *m=NULL;
|
||||
uint16_t t_len=0, m_len=0;
|
||||
char *topic;
|
||||
char *topic, *message;
|
||||
|
||||
LOG(LL_DEBUG, ("Index %d, token [%.*s]", idx, val.len, val.ptr));
|
||||
t=(char*)val.ptr;
|
||||
m=strstr(val.ptr, " ");
|
||||
if (m-val.ptr <= val.len) {
|
||||
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 (val.len==0) {
|
||||
LOG(LL_ERROR, ("mqtt element %d is empty token", idx));
|
||||
continue;
|
||||
}
|
||||
if ((topic=malloc(t_len+1))) {
|
||||
memcpy(topic, t, t_len);
|
||||
topic[t_len]=0;
|
||||
LOG(LL_INFO, ("Sending topic='%s', message='%.*s'", topic, m_len, m));
|
||||
free(topic);
|
||||
if (json_scanf(val.ptr, val.len, "{topic:%Q, message:%Q}", &topic, &message) != 2) {
|
||||
LOG(LL_ERROR, ("mqtt element %d ('%.*s'), required fields 'topic' and 'message'", idx, val.len, val.ptr));
|
||||
continue;
|
||||
}
|
||||
LOG(LL_INFO, ("topic='%s', message='%s'", topic, message));
|
||||
if (topic) free(topic);
|
||||
if (message) free(message);
|
||||
}
|
||||
(void) ev_data;
|
||||
}
|
||||
|
Reference in New Issue
Block a user