Skip to content

Commit

Permalink
Move configuration to declarations in *.h
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Feb 25, 2020
1 parent b990727 commit 5195103
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 38 deletions.
75 changes: 40 additions & 35 deletions src/urltarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,36 @@ URLTarget* URLTarget::getInstance() {
single->target = new PushTarget;
single->target->ip = INADDR_NONE;

// /////////////////////////////////////////////////////////////////////
// // Configure Target
// /////////////////////////////////////////////////////////////////////

String _tempVal;
// Enable target and target name
single->target->target.enabled = (String(TARGETURL).length() > 3);
strlcpy(single->target->target.name, TARGET_NAME, sizeof(TARGET_NAME));
//
// Check return body for success
single->target->checkBody.enabled = false;
strlcpy(single->target->checkBody.name, "", sizeof(""));
single->target->checkBody.enabled = CHECKBODY_ENABLED;
strlcpy(single->target->checkBody.name, CHECKBODY_NAME, sizeof(CHECKBODY_NAME));
//
// Grab correct URL for target type
strlcpy(single->target->url, single->config->targeturl, sizeof(single->config->targeturl));
// Change JSON point enabled and name for target type
single->target->apiName.enabled = APINAME_ENABLED;
strlcpy(single->target->apiName.name, APINAME_NAME, sizeof(APINAME_NAME));
//
// Enable target and target name
single->target->target.enabled = true;
strlcpy(single->target->target.name, URLTARGET, sizeof(URLTARGET));
single->target->bubName.enabled = BUBNAME_ENABLED;
strlcpy(single->target->bubName.name, BUBNAME_NAME, sizeof(BUBNAME_NAME));
//
// Change JSON point enabled and name for target type
single->target->apiName.enabled = (String(single->target->url).length() > 3);
strlcpy(single->target->apiName.name, "api_key", sizeof("api_key"));
single->target->bpm.enabled = BPM_ENABLED;
strlcpy(single->target->bpm.name, BPM_NAME, sizeof(BPM_NAME));
//
single->target->bubName.enabled = true;
strlcpy(single->target->bubName.name, "name", sizeof("name"));
single->target->ambientTemp.enabled = AMBIENTTEMP_ENABLED;
strlcpy(single->target->ambientTemp.name, AMBIENTTEMP_NAME, sizeof(AMBIENTTEMP_NAME));
//
single->target->bpm.enabled = true;
strlcpy(single->target->bpm.name, "bpm", sizeof("bpm"));
single->target->vesselTemp.enabled = VESSELTEMP_ENABLED;
strlcpy(single->target->vesselTemp.name, VESSELTEMP_NAME, sizeof(VESSELTEMP_NAME));
//
single->target->ambientTemp.enabled = true;
strlcpy(single->target->ambientTemp.name, "ambient", sizeof("ambient"));
single->target->tempFormat.enabled = TEMPFORMAT_ENABLED;
strlcpy(single->target->tempFormat.name, TEMPFORMAT_NAME, sizeof(TEMPFORMAT_NAME));
//
single->target->vesselTemp.enabled = true;
strlcpy(single->target->vesselTemp.name, "temp", sizeof("temp"));
// Grab correct URL for target type
strlcpy(single->target->url, TARGETURL, sizeof(TARGETURL));
//
single->target->tempFormat.enabled = true;
strlcpy(single->target->tempFormat.name, "temp_unit", sizeof("temp_unit"));
}
return single;
}
Expand All @@ -74,26 +70,35 @@ bool URLTarget::push() {
if (single->target->apiName.enabled) {
LCBUrl lcburl;
if (lcburl.setUrl(String(single->target->url))) {
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());
if (pushTarget(single->target)) {
Log.notice(F("%s post ok." CR), single->target->target.name);
return true;
} else {
Log.error(F("%s post failed." CR), single->target->target.name);
IPAddress resolvedIP = resolveHost(single->target);
if (resolvedIP == INADDR_NONE) {
if (single->target->ip == INADDR_NONE) {
Log.error(F("Unable to resolve host %s to IP address." CR), lcburl.getHost().c_str());
Serial.println(); // DEBUG
return false;
} else {
Log.verbose(F("Using cached information for host %s at IP %s." CR), lcburl.getHost().c_str(), single->target->ip.toString().c_str());
}
} else {
Log.error(F("Unable to resolve host %s to IP address." CR), lcburl.getHost().c_str());
return false;
Log.verbose(F("Resolved host %s to IP %s." CR), lcburl.getHost().c_str(), resolvedIP.toString().c_str());
single->target->ip = resolvedIP;
}
} else {
Log.error(F("Invalid URL in %s configuration: %s" CR), single->target->target.name, single->target->url);
return false;
}
} else {
Log.verbose(F("%s not enabled, skipping." CR), single->target->target.name);
return true;
}

if (pushTarget(single->target)) {
Log.notice(F("%s post ok." CR), single->target->target.name);
Serial.println(); // DEBUG
return true;
} else {
Log.error(F("%s post failed." CR), single->target->target.name);
Serial.println(); // DEBUG
return false;
}
}
42 changes: 39 additions & 3 deletions src/urltarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,48 @@ SOFTWARE. */

#include "pushtarget.h"
#include "jsonconfig.h"
// #include "bubbles.h"
#include <LCBUrl.h>
// #include <WiFiClient.h>
// #include <ArduinoLog.h>
#include <ArduinoLog.h>
#include <Arduino.h>

/////////////////////////////////////////////////////////////////////
// Configure Target - Below are configuration items per target type
/////////////////////////////////////////////////////////////////////

// Enable target and target name
#define TARGET_ENABLED true
#define TARGET_NAME URLTARGET
//
// Check return body for success
#define CHECKBODY_ENABLED true
#define CHECKBODY_NAME "200 (Ok)"
//
// Turn JSON points on/off and provide JSON field name per target type
//
#define APINAME_ENABLED true
#define APINAME_NAME "api_key"
//
#define BUBNAME_ENABLED true
#define BUBNAME_NAME "name"
//
#define BPM_ENABLED true
#define BPM_NAME "bpm"
//
#define AMBIENTTEMP_ENABLED true
#define AMBIENTTEMP_NAME "ambient"
//
#define VESSELTEMP_ENABLED true
#define VESSELTEMP_NAME "temp"
//
#define TEMPFORMAT_ENABLED true
#define TEMPFORMAT_NAME "temp_unit"
//
#define TARGETURL single->config->targeturl

/////////////////////////////////////////////////////////////////////
// Configure Target - Above are configuration items per target type
/////////////////////////////////////////////////////////////////////

class URLTarget {
private:
// Singleton Declarations
Expand Down

0 comments on commit 5195103

Please sign in to comment.