-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start contentmanager cleanup - Make destination constant - Remove unneeded asignments - Improve loops - Move printHeap function into util * Refactor contentmanager::drawNew * Optimize contentManager::replaceVariables * Fix missing const in prepareCancelPending * Refactor drawDate * Refactor drawWeather, drawForecast & getLocation - Generalize http get json function * Add util function for printing largest free block * Reuse weather icons for both drawWeather & drawForecast * Make httpGetJson timeout const * Reafctor more functions * Add few more const * Fix spelling mistake * Add util for debugging streams - Add util for checking if strings are empty or contain null - Fix file and string checks * Remove leftover debug print
- Loading branch information
Showing
12 changed files
with
483 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <ArduinoJson.h> | ||
#include <HTTPClient.h> | ||
|
||
#include "web.h" | ||
|
||
namespace util { | ||
|
||
/// @brief Can be used to wrap a stream and see what's going on | ||
class DebugStream : public Stream { | ||
public: | ||
DebugStream(Stream &stream) : _stream(stream) {} | ||
|
||
int available() override { | ||
return _stream.available(); | ||
} | ||
|
||
int read() override { | ||
int data = _stream.read(); | ||
Serial.write(data); | ||
return data; | ||
} | ||
|
||
int peek() override { | ||
int data = _stream.peek(); | ||
Serial.print("Peek: "); | ||
Serial.println(data); | ||
return data; | ||
} | ||
|
||
void flush() override { | ||
_stream.flush(); | ||
Serial.println("Flush"); | ||
} | ||
|
||
size_t write(uint8_t data) override { | ||
Serial.write(data); | ||
return _stream.write(data); | ||
} | ||
|
||
size_t write(const uint8_t *buffer, size_t size) override { | ||
for (size_t i = 0; i < size; i++) { | ||
Serial.print("Write: "); | ||
Serial.println(buffer[i]); | ||
} | ||
return _stream.write(buffer, size); | ||
} | ||
|
||
private: | ||
Stream &_stream; | ||
}; | ||
|
||
/// @brief Prints free heap, allocatbale heap and free stack | ||
static void printHeap() { | ||
const uint32_t freeStack = uxTaskGetStackHighWaterMark(NULL); | ||
Serial.printf("Free heap: %d allocatable: %d stack: %d\n", ESP.getFreeHeap(), ESP.getMaxAllocHeap(), freeStack); | ||
} | ||
|
||
/// @brief Prints the maximum continuous heap space | ||
static void printLargestFreeBlock() { | ||
Serial.println("Maximum Continuous Heap Space: " + String(heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT))); | ||
} | ||
|
||
/// @brief Do a GET request to the given url and fill the given json with the response | ||
/// @param url Request URL | ||
/// @param json Json document to fill | ||
/// @param timeout Request timeout | ||
/// @param redirects Redirects handling | ||
/// @return True on success, false on error (httpCode != 200 || deserialization error) | ||
static bool httpGetJson(String &url, JsonDocument &json, const uint16_t timeout) //, const followRedirects_t redirects = followRedirects_t::HTTPC_DISABLE_FOLLOW_REDIRECTS) | ||
{ | ||
HTTPClient http; | ||
http.begin(url); | ||
http.setTimeout(timeout); | ||
// http.setFollowRedirects(redirects); | ||
const int httpCode = http.GET(); | ||
if (httpCode != 200) { | ||
http.end(); | ||
wsErr("http " + httpCode); | ||
return false; | ||
} | ||
|
||
DeserializationError error = deserializeJson(json, http.getString()); | ||
http.end(); | ||
if (error) { | ||
Serial.println(error.c_str()); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// @brief Check if the given string is empty or contains "null" | ||
/// | ||
/// @param str String to check | ||
/// @return True if empty or null, false if not | ||
static inline bool isEmptyOrNull(const String &str) { | ||
return str.isEmpty() || str == "null"; | ||
} | ||
|
||
} // namespace util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.