Skip to content

Commit

Permalink
Add push semaphore and response checking
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 3, 2020
1 parent eb4c44b commit 4ca5f30
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void loop() {
// Target timer
Ticker urlTarget;
// config->targetfreq * 60
urlTarget.attach(5, [](){doURLTarget = true;});
urlTarget.attach(5, setDoURLTarget);

// Brewer's friend timer
// Ticker bfTimer;
Expand All @@ -93,7 +93,7 @@ void loop() {
if (config->updateTargetFreq) {
Log.notice(F("Resetting URL Target frequency timer to %l minutes." CR), config->targetfreq);
urlTarget.detach();
urlTarget.attach(config->targetfreq * 60, [](){doURLTarget = true;});
urlTarget.attach(config->targetfreq * 60, setDoURLTarget);
config->updateTargetFreq = false;
}
// if (config->updateBFFreq) {
Expand Down
78 changes: 47 additions & 31 deletions src/pushhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,21 @@ bool pushToTarget(PushTarget *target, IPAddress targetIP, int port) {
String response;
while (client.connected() || client.available()) {
if (client.available()) {
response = client.readStringUntil('\n');
response = client.readStringUntil('\r');
}
}
client.stop();
Log.verbose(F("Status: %s" CR), response.c_str());
if (response.indexOf("200") >= 0) {
Log.verbose(F("JSON posted." CR));
if (target->checkBody.enabled == true) {
if (response.indexOf(target->checkBody.name) >= 0) {
Log.verbose(F("JSON posted." CR));
return true;
} else {
Log.error(F("Unexpected status: %s" CR), response.c_str());
return false;
}
} else if (true) {
return true;
} else {
Log.error(F("Unexpected status: %s" CR), response.c_str());
return false;
}
} else {
Log.warning(F("Connection failed, Host: %s, Port: %l (Err: %d)" CR),
Expand All @@ -129,37 +133,49 @@ bool pushToTarget(PushTarget *target, IPAddress targetIP, int port) {
}
}

void setDoURLTarget() {
doURLTarget = true; // Semaphore required for Ticker + radio event
}

void setDoBFTarget() {
doBFTarget = true; // Semaphore required for Ticker + radio event
}

void setDoBRFTarget() {
doBRFTarget = true; // Semaphore required for Ticker + radio event
}

void tickerLoop() {
Bubbles *bubble = Bubbles::getInstance();
URLTarget *urlTarget = URLTarget::getInstance();

// Handle JSON posts
//
// Do URL Target post
if (doURLTarget) {
doURLTarget = false;
urlTarget->push();
}
//
// Do Brewer's Friend Post
if (doBFTarget) { // Do BF post
doBFTarget = false;
// urlTarget->push(); // TODO - Attach BF Target
}
// Handle JSON posts
//
// Do URL Target post
if (doURLTarget) {
doURLTarget = false;
urlTarget->push();
}
//
// Do Brewer's Friend Post
if (doBFTarget) { // Do BF post
doBFTarget = false;
// urlTarget->push(); // TODO - Attach BF Target
}

// Handle the board LED status
// Smarter to do it in the loop than in the ISR
if (digitalRead(COUNTPIN) == HIGH) { // Non-interrupt driven LED logic
digitalWrite(LED, LOW); // Turn LED on when not obstructed
} else {
digitalWrite(LED, HIGH); // Make sure LED turns off after a bubble4
}
// Handle the board LED status
// Smarter to do it in the loop than in the ISR
if (digitalRead(COUNTPIN) == HIGH) { // Non-interrupt driven LED logic
digitalWrite(LED, LOW); // Turn LED on when not obstructed
} else {
digitalWrite(LED, HIGH); // Make sure LED turns off after a bubble4
}

// Some just for fun serial logging
if (bubble->doBub) { // Serial log for bubble detect
// Some just for fun serial logging
if (bubble->doBub) { // Serial log for bubble detect
#ifdef LOG_LEVEL
Log.verbose(F("॰ₒ๐°৹" CR)); // Looks like a bubble, right?
Log.verbose(F("॰ₒ๐°৹" CR)); // Looks like a bubble, right?
#endif
bubble->doBub = false;
}
bubble->doBub = false;
}
}
1 change: 1 addition & 0 deletions src/pushhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE. */
IPAddress resolveHost(const char hostname[129]);
bool pushToTarget(PushTarget*, IPAddress, int);
void tickerLoop();
void setDoURLTarget();

static bool __attribute__((unused)) doURLTarget = false; // Semaphore for Target timer
static bool __attribute__((unused)) doBFTarget = false; // Semaphore for BF timer
Expand Down

0 comments on commit 4ca5f30

Please sign in to comment.