move response for unresolved ip into poll

This commit is contained in:
Barbudor 2021-05-15 17:32:18 +02:00
parent dd624c3ce6
commit 2e1ac3581f
1 changed files with 38 additions and 21 deletions

View File

@ -246,14 +246,13 @@ extern "C" {
int32_t t_ping_start(const char *hostname, uint32_t count) {
IPAddress ipfull;
if (!WiFi.hostByName(hostname, ipfull)) {
return -2;
ipfull = 0xFFFFFFFF;
}
uint32_t ip = ipfull;
if (0xFFFFFFFF == ip) { return -2; } // invalid address
// check if pings are already ongoing for this IP
if (t_ping_find(ip)) {
if (0xFFFFFFFF != ip && t_ping_find(ip)) {
return -1;
}
@ -271,6 +270,12 @@ extern "C" {
ping->next = ping_head;
ping_head = ping; // insert at head
if (0xFFFFFFFF == ip) { // If invalid address, set as completed
ping->done = true;
return -2;
}
// send
t_ping_register_pcb();
t_ping_send(t_ping_pcb, ping);
@ -293,24 +298,34 @@ void PingResponsePoll(void) {
uint32_t success = ping->success_count;
uint32_t ip = ping->ip;
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":%s"
",\"IP\":\"%d.%d.%d.%d\""
",\"Success\":%d"
",\"Timeout\":%d"
",\"MinTime\":%d"
",\"MaxTime\":%d"
",\"AvgTime\":%d"
"}}}"),
ping->hostname.c_str(),
success ? PSTR("true") : PSTR("false"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24,
success,
ping->timeout_count,
success ? ping->min_time : 0,
ping->max_time,
success ? ping->sum_time / success : 0
);
if (0xFFFFFFFF == ip) {
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":false"
",\"IP\":\"\""
",\"Success\":false"
"}}}"),
ping->hostname.c_str()
);
} else {
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":%s"
",\"IP\":\"%d.%d.%d.%d\""
",\"Success\":%d"
",\"Timeout\":%d"
",\"MinTime\":%d"
",\"MaxTime\":%d"
",\"AvgTime\":%d"
"}}}"),
ping->hostname.c_str(),
success ? PSTR("true") : PSTR("false"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24,
success,
ping->timeout_count,
success ? ping->min_time : 0,
ping->max_time,
success ? ping->sum_time / success : 0
);
}
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING));
// remove from linked list
@ -342,6 +357,7 @@ void CmndPing(void) {
} else if (-1 == res) {
ResponseCmndChar_P(PSTR("Ping already ongoing for this IP"));
} else {
/*
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":false"
",\"IP\":\"\""
@ -350,6 +366,7 @@ void CmndPing(void) {
XdrvMailbox.data
);
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING));
*/
ResponseCmndChar_P(PSTR("Unable to resolve IP address"));
}
}