Skip to content

Commit

Permalink
New target lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 3, 2020
1 parent a5845a2 commit c579902
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/bftarget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)

This file is part of Lee Bussy's Brew Bubbles (brew-bubbles).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#include "bftarget.h"

BFTarget* BFTarget::single = NULL;

BFTarget* BFTarget::getInstance() {
if (!single) {
single = new BFTarget();
single->config = JsonConfig::getInstance();
single->target = new PushTarget;
single->target->ip = INADDR_NONE;

// 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 = CHECKBODY_ENABLED;
strlcpy(single->target->checkBody.name, CHECKBODY_NAME, sizeof(CHECKBODY_NAME));
//
// 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));
//
single->target->bubName.enabled = BUBNAME_ENABLED;
strlcpy(single->target->bubName.name, BUBNAME_NAME, sizeof(BUBNAME_NAME));
//
single->target->bpm.enabled = BPM_ENABLED;
strlcpy(single->target->bpm.name, BPM_NAME, sizeof(BPM_NAME));
//
single->target->ambientTemp.enabled = AMBIENTTEMP_ENABLED;
strlcpy(single->target->ambientTemp.name, AMBIENTTEMP_NAME, sizeof(AMBIENTTEMP_NAME));
//
single->target->vesselTemp.enabled = VESSELTEMP_ENABLED;
strlcpy(single->target->vesselTemp.name, VESSELTEMP_NAME, sizeof(VESSELTEMP_NAME));
//
single->target->tempFormat.enabled = TEMPFORMAT_ENABLED;
strlcpy(single->target->tempFormat.name, TEMPFORMAT_NAME, sizeof(TEMPFORMAT_NAME));
//
// Grab correct URL for target type
strlcpy(single->target->url, TARGETURL, sizeof(TARGETURL));
//
}
return single;
}

bool BFTarget::push() {
Log.verbose(F("Triggered %s push." CR), single->target->target.name);
LCBUrl lcburl;
if (single->target->apiName.enabled) {
if (lcburl.setUrl(String(single->target->url))) {
IPAddress resolvedIP = resolveHost(lcburl.getHost().c_str());
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());
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.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 (pushToTarget(single->target, target->ip, lcburl.getPort())) {
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);
return false;
}
}
96 changes: 96 additions & 0 deletions src/bftarget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)

This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#ifndef _BFTARGET_H
#define _BFTARGET_H

#include "pushtarget.h"
#include "pushhelper.h"
#include "jsonconfig.h"
#include <LCBUrl.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 false
#define CHECKBODY_NAME ""
//
// 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 BFTarget {
private:
// Singleton Declarations
BFTarget() {}
static BFTarget *single;
// External Declarations
PushTarget *target;
JsonConfig *config;
// Private Methods

// Private Properties


public:
// Singleton Declarations
static BFTarget* getInstance();
~BFTarget() {single = NULL;}
// External Declarations

// Public Methods
bool push();
// Public Properties

};

#endif // _BFTARGET_H

0 comments on commit c579902

Please sign in to comment.