Skip to content

Commit

Permalink
Add async thatVersion lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 22, 2020
1 parent c95ba4a commit abf30f2
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ lib_deps =
CircularBuffer
LCBUrl
https://github.com/lbussy/AsyncWiFiManager.git
ESPAsyncTCP
; ESP Async WebServer
https://github.com/me-no-dev/ESPAsyncTCP.git
https://github.com/me-no-dev/ESPAsyncWebServer.git
https://github.com/boblemaire/asyncHTTPrequest.git
build_type = release

[env:d1_mini]
Expand Down
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ void setup() {
doWiFi();
}

setClock(); // Set NTP Time
initWebServer(); // Turn on web server
mdnssetup(); // Set up mDNS responder
execspiffs(); // Check for pending SPIFFS update
setClock(); // Set NTP Time
loadBpm() ; // Get last BPM reading if it was a controlled reboot
mdnssetup(); // Set up mDNS responder
initWebServer(); // Turn on web server

Log.notice(F("Started %s version %s (%s) [%s]." CR), API_KEY, version(), branch(), build());
}

void loop() {
// Poll for server version
Ticker getThatVersion;
doPoll();
getThatVersion.attach(60, doPoll);
Bubbles *bubble = Bubbles::getInstance();

// Bubble loop to create 60 second readings
Expand Down
2 changes: 2 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SOFTWARE. */
#include "bubbles.h"
#include "mdns.h"
#include "ntp.h"
#include "thatVersion.h"
#include <DoubleResetDetect.h>
#include <ArduinoLog.h>
#include <Arduino.h>
Expand All @@ -52,5 +53,6 @@ SOFTWARE. */
extern struct Config config;
extern bool loadConfig();
extern const char *filename;
extern struct ThatVersion thatVersion;

#endif // _MAIN_H
99 changes: 99 additions & 0 deletions src/thatVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* 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 "thatVersion.h"

asyncHTTPrequest request;
ThatVersion __attribute__((unused)) thatVersion;

void sendRequest() {
if (request.readyState() == 0 || request.readyState() == 4) {
request.open("GET", VERSIONJSONLOC);
request.send();
}
}

void requestHandler(void* optParm, asyncHTTPrequest* request, int readyState) {
String body = request->responseText();
const char * src = body.c_str();
TV_NOT(src);
if (!deserializeVersion(src, thatVersion)) {
TV_ERR(F("failed to deserialize version"));
} else {
TV_NOT(F("version deserialized"));
}
}

bool serializeVersion(const ThatVersion &thatVersion, Print &dst) {
// Serialize version
const size_t capacity = JSON_OBJECT_SIZE(1);
DynamicJsonDocument doc(capacity);

// Create an object at the root
JsonObject root = doc.to<JsonObject>();

// Fill the object
thatVersion.save(root);

// Serialize JSON to file
return serializeJsonPretty(doc, dst) > 0;
}

bool deserializeVersion(const char * &src, ThatVersion &thatVersion) {
// Deserialize version
const size_t capacity = JSON_OBJECT_SIZE(1) + 50;
DynamicJsonDocument doc(capacity);

// Parse the JSON object in the file
DeserializationError err = deserializeJson(doc, src);
if (err) {
TV_NOT(F("no existing version"));
thatVersion.load(doc.as<JsonObject>());
TV_NOT(F("loaded default version"));
return true;
} else {
thatVersion.load(doc.as<JsonObject>());
TV_NOT(F("loaded existing version"));
return true;
}
// TODO: Can/should I return false here somehow?
}

void doPoll() {
#ifdef TV_DEBUG
request.setDebug(true);
#endif
request.onData(requestHandler);
sendRequest();
}

void ThatVersion::save(JsonObject obj) const {
obj["version"] = version;
}

void ThatVersion::load(JsonObjectConst obj) {
const char* v = obj["version"];
if (v)
strlcpy(version, v, sizeof(version));
else
strlcpy(version, "0.0.0", sizeof(version)); // Default
}
57 changes: 57 additions & 0 deletions src/thatVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* 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. */

#ifndef _THATVERSION_H
#define _THATVERSION_H

// #define TV_DEBUG // Enable Debug for module

#include "config.h"
#include <ArduinoLog.h>
#include <ESPAsyncTCP.h>
#include <asyncHTTPrequest.h>
#include <ArduinoJson.h>
#include <Arduino.h>

struct ThatVersion {
char version[32] = {'0','.','0','.','0'};

void load(JsonObjectConst);
void save(JsonObject) const;
};

void doPoll();
void sendRequest();
void requestHandler(void*, asyncHTTPrequest*, int);
bool serializeVersion(const ThatVersion &, Print &);
bool deserializeVersion(const char * &, ThatVersion &);

#ifdef TV_DEBUG
#include <ArduinoLog.h>
#define TV_NOT(...) Log.notice(F("[TV Debug] in %s(): %s." CR), __func__, __VA_ARGS__);
#define TV_ERR(...) Log.error(F("[TV Debug] in %s(): %s." CR), __func__, __VA_ARGS__);
#else
#define TV_NOT(...)
#define TV_ERR(...)
#endif // End control debug printing

#endif // _THATVERSION_H

0 comments on commit abf30f2

Please sign in to comment.