refactor loop handling

This commit is contained in:
2020-11-13 15:37:32 +01:00
parent f10ac76fdb
commit 2a93c96345

View File

@ -25,11 +25,6 @@ static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength)
logMsg("mcb: %s : %*s", topic, payloadLength, payload); logMsg("mcb: %s : %*s", topic, payloadLength, payload);
} }
void mqttLoopHandler(void *handle) {
mqttLoop(&mqttClient);
}
void mqttTestHandler(void *handle) { void mqttTestHandler(void *handle) {
static uint8_t state = 0; static uint8_t state = 0;
static uint8_t message[] = "Hello world\n\r"; static uint8_t message[] = "Hello world\n\r";
@ -59,33 +54,36 @@ void mqttTestHandler(void *handle) {
break; break;
case 2: case 2:
coloredMsg(LOG_YELLOW, "mth, start mqtt loop"); coloredMsg(LOG_YELLOW, "mth, publish something");
schAdd(mqttLoopHandler, NULL, 0, 100); res = publish(&mqttClient, "wiznet/hello", message, strlen(message), false);
coloredMsg(LOG_YELLOW, "mth, publish returned %d", res);
state = 3; state = 3;
break; break;
case 3: case 3:
coloredMsg(LOG_YELLOW, "mth, publish something"); coloredMsg(LOG_YELLOW, "mth, subscribe something");
res = publish(&mqttClient, "wiznet/hello", message, strlen(message), false); res = subscribe(&mqttClient, "wiznet/helloback", MQTTQOS0);
coloredMsg(LOG_YELLOW, "mth, publish returned %d", res); coloredMsg(LOG_YELLOW, "mth, subscribe returned %d", res);
state = 4; state = 4;
break; break;
case 4: case 4:
coloredMsg(LOG_YELLOW, "mth, subscribe something"); coloredMsg(LOG_YELLOW, "mth, now entering the loop");
res = subscribe(&mqttClient, "wiznet/helloback", MQTTQOS0);
coloredMsg(LOG_YELLOW, "mth, subscribe returned %d", res);
state = 5; state = 5;
break; break;
case 5: case 5:
// coloredMsg(LOG_YELLOW, "mth, waiting"); // coloredMsg(LOG_YELLOW, "mth, looping");
if (! mqttLoop(&mqttClient)) {
state = 0;
}
break; break;
case 255: case 255:
coloredMsg(LOG_YELLOW, "mth, error state, will stop here"); coloredMsg(LOG_YELLOW, "mth, error state, will stop here");
schDel(mqttTestHandler, NULL); schDel(mqttTestHandler, NULL);
schDel(mqttLoopHandler, NULL); coloredMsg(LOG_YELLOW, "mth, trying again in one minute");
schAdd(mqttTestHandler, NULL, 60000, 100);
break; break;
} }
} else { } else {