Merge pull request #8955 from s-hadinger/ping_hostname

Change ``Ping`` now reports the hostname instead of IP address (#8948)
This commit is contained in:
Theo Arends 2020-07-21 22:19:50 +02:00 committed by GitHub
commit a8abfc9fc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -8,6 +8,7 @@
- Add command ``SetOption100 0/1`` to remove Zigbee ``ZbReceived`` value from ``{"ZbReceived":{xxx:yyy}}`` JSON message - Add command ``SetOption100 0/1`` to remove Zigbee ``ZbReceived`` value from ``{"ZbReceived":{xxx:yyy}}`` JSON message
- Add command ``SetOption101 0/1`` to add the Zigbee source endpoint as suffix to attributes, ex `Power3` instead of `Power` if sent from endpoint 3 - Add command ``SetOption101 0/1`` to add the Zigbee source endpoint as suffix to attributes, ex `Power3` instead of `Power` if sent from endpoint 3
- Add command (``S``)``SerialSend6`` \<comma seperated values\> (#8937) - Add command (``S``)``SerialSend6`` \<comma seperated values\> (#8937)
- Change ``Ping`` now reports the hostname instead of IP address (#8948)
### 8.3.1.6 20200617 ### 8.3.1.6 20200617

View File

@ -58,6 +58,7 @@ extern "C" {
uint32_t sum_time; // cumulated time in ms for all successful responses (used to compute the average) uint32_t sum_time; // cumulated time in ms for all successful responses (used to compute the average)
bool done; // indicates the ping campaign is finished bool done; // indicates the ping campaign is finished
bool fast; // fast mode, i.e. stop pings when first successful response bool fast; // fast mode, i.e. stop pings when first successful response
String hostname; // original hostname before convertion to IP address
} Ping_t; } Ping_t;
// globals // globals
@ -229,10 +230,22 @@ extern "C" {
// ================================================================================ // ================================================================================
// Start pings // Start pings
// ================================================================================ // ================================================================================
bool t_ping_start(uint32_t ip, uint32_t count) { // returns:
// 0: OK
// -1: ping already ongoing for this address
// -2: unable to resolve address
int32_t t_ping_start(const char *hostname, uint32_t count) {
IPAddress ipfull;
if (!WiFi.hostByName(hostname, ipfull)) {
return -2;
}
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 (t_ping_find(ip)) {
return false; return -1;
} }
Ping_t *ping = new Ping_t(); Ping_t *ping = new Ping_t();
@ -243,6 +256,7 @@ extern "C" {
ping->min_time = UINT32_MAX; ping->min_time = UINT32_MAX;
ping->ip = ip; ping->ip = ip;
ping->to_send_count = count - 1; ping->to_send_count = count - 1;
ping->hostname = hostname;
// add to Linked List from head // add to Linked List from head
ping->next = ping_head; ping->next = ping_head;
@ -254,6 +268,8 @@ extern "C" {
// set timers for time-out and cadence // set timers for time-out and cadence
sys_timeout(Ping_timeout_ms, t_ping_timeout, ping); sys_timeout(Ping_timeout_ms, t_ping_timeout, ping);
sys_timeout(Ping_coarse, t_ping_coarse_tmr, ping); sys_timeout(Ping_coarse, t_ping_coarse_tmr, ping);
return 0;
} }
} }
@ -268,18 +284,22 @@ 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 "\":{\"%d.%d.%d.%d\":{" Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":%s" "\"Reachable\":%s"
",\"IP\":\"%d.%d.%d.%d\""
",\"Success\":%d" ",\"Success\":%d"
",\"Timeout\":%d" ",\"Timeout\":%d"
",\"MinTime\":%d" ",\"MinTime\":%d"
",\"MaxTime\":%d" ",\"MaxTime\":%d"
",\"AvgTime\":%d" ",\"AvgTime\":%d"
"}}}"), "}}}"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24, ping->hostname.c_str(),
success ? "true" : "false", success ? "true" : "false",
success, ping->timeout_count, ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24,
success ? ping->min_time : 0, ping->max_time, success,
ping->timeout_count,
success ? ping->min_time : 0,
ping->max_time,
success ? ping->sum_time / success : 0 success ? ping->sum_time / success : 0
); );
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING)); MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING));
@ -303,18 +323,15 @@ void PingResponsePoll(void) {
void CmndPing(void) { void CmndPing(void) {
uint32_t count = XdrvMailbox.index; uint32_t count = XdrvMailbox.index;
IPAddress ip;
RemoveSpace(XdrvMailbox.data); RemoveSpace(XdrvMailbox.data);
if (count > 10) { count = 8; } // max 8 seconds if (count > 10) { count = 8; } // max 8 seconds
if (WiFi.hostByName(XdrvMailbox.data, ip)) { int32_t res = t_ping_start(XdrvMailbox.data, count);
bool ok = t_ping_start(ip, count); if (0 == res) {
if (ok) { ResponseCmndDone();
ResponseCmndDone(); } else if (-1 == res) {
} else { ResponseCmndChar_P(PSTR("Ping already ongoing for this IP"));
ResponseCmndChar_P(PSTR("Ping already ongoing for this IP"));
}
} else { } else {
ResponseCmndChar_P(PSTR("Unable to resolve IP address")); ResponseCmndChar_P(PSTR("Unable to resolve IP address"));
} }