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

View File

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