Skip to content

Commit

Permalink
Testing new post classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Feb 24, 2020
1 parent 46098e8 commit d0e02d3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
21 changes: 11 additions & 10 deletions src/pushtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ SOFTWARE. */
IPAddress resolveHost(PushTarget *target) {
LCBUrl lcburl;
lcburl.setUrl(String(target->url));
// Resolve hostname. This is necessary due to flakey name resolution
// and because WiFiClient::connect() does not use mDNS
IPAddress resolvedIP = INADDR_NONE;
if (!WiFi.hostByName(lcburl.getHost().c_str(), resolvedIP)) {
// Failed so set to the previous IP
resolvedIP = target->ip;
Log.verbose(F("Host lookup: %s." CR), lcburl.getHost().c_str());
IPAddress returnIP;
if (WiFi.hostByName(lcburl.getHost().c_str(), returnIP, 10000) == 0) {
Log.error(F("Host lookup error, using cached IP (if any.)" CR));
returnIP = target->ip;
}
return resolvedIP;
return returnIP;
}

bool pushTarget(PushTarget *target) {
Log.notice(F("Posting to: %s" CR), target->url);
LCBUrl lcburl;
lcburl.setUrl(String(target->url));

Expand Down Expand Up @@ -64,7 +64,7 @@ bool pushTarget(PushTarget *target) {
// Post JSON to Server
//
// Use the IP address we resolved if we are connecting with mDNS
Log.verbose(F("Connecting to: %s, %l" CR), lcburl.getHost().c_str(), lcburl.getHost().c_str());
Log.verbose(F("Connecting to: %s, %s" CR), lcburl.getHost().c_str(), target->ip.toString().c_str());
WiFiClient client;
client.setTimeout(10000);
int retval = client.connect(target->ip, lcburl.getPort());
Expand All @@ -76,7 +76,7 @@ bool pushTarget(PushTarget *target) {
// -4 = INVALID_RESPONSE
if (!retval == 1) {
Log.warning(F("Connection failed, Host: %s, Port: %l (Err: %d)" CR),
lcburl.getHost().c_str(), lcburl.getHost().c_str(), retval
lcburl.getHost().c_str(), lcburl.getPort(), retval
);
return false;
} else {
Expand Down Expand Up @@ -114,14 +114,15 @@ bool pushTarget(PushTarget *target) {

// Post Data
Log.verbose(F("Posting JSON to target." CR));
Serial.println(json); // DEBUG
client.println(json);
// Check the HTTP status (should be "HTTP/1.1 200 OK")
char status[32] = {0};
// TODO: Need to check response body
client.readBytesUntil('\r', status, sizeof(status));
client.stop();
Log.verbose(F("Status: %s" CR), status);
if ((String(status).endsWith("200 OK"))) {
if ((String(status).endsWith("200 (OK)"))) {
Log.verbose(F("JSON posted." CR));
return true;
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/pushtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ SOFTWARE. */
#include "bubbles.h"
#include "jsonconfig.h"
#include <LCBUrl.h>
// #include <ArduinoLog.h>
#include <ArduinoLog.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
// #include <Arduino.h>
#include <Arduino.h>

struct pushPoint {
bool enabled; // Whether to send or not
Expand Down
1 change: 0 additions & 1 deletion src/urltarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ bool URLTarget::push() {
single->target->ip = resolveHost(single->target);
if (single->target->ip != INADDR_NONE) {
Log.verbose(F("Resolved host %s to IP %s." CR), lcburl.getHost().c_str(), single->target->ip.toString().c_str());
Log.notice(F("Posting to: %s" CR), single->target->url);
if (pushTarget(single->target)) {
Log.notice(F("%s post ok." CR), single->target->target.name);
return true;
Expand Down

0 comments on commit d0e02d3

Please sign in to comment.