Files
OpenEPaperLink/ESP32_AP-Flasher/include/util.h
Nic Limper 2edbf27033 various fixes / sleep at night / resend image at tag boot
- apconfig: configure night time, where the system is not updating tags, and the tags are sleeping
- fixed bug calculating expected checkin
- throttle down apinfo update to once per minute
- fixed too many concurrent requests getting tagtypes
- resend current image when a tag reboots and the content type is 'static image' (all other content types were already regenerating the content)
- fixed timing of main loop
2023-08-20 20:16:34 +02:00

128 lines
3.5 KiB
C++

#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, JsonDocument *filter = nullptr) //, 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;
if (filter) {
error = deserializeJson(json, http.getString(), DeserializationOption::Filter(*filter));
} else {
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";
}
/// @brief checks if the current time is between sleeptime1 and sleeptime2
///
/// @param sleeptime1 Start of time block
/// @param sleeptime2 End of time block
/// @return True if within time block, false is outside time block
static bool isSleeping(int sleeptime1, int sleeptime2) {
if (sleeptime1 == sleeptime2) return false;
struct tm timeinfo;
getLocalTime(&timeinfo);
int currentHour = timeinfo.tm_hour;
if (sleeptime1 < sleeptime2) {
return currentHour >= sleeptime1 && currentHour < sleeptime2;
} else {
return currentHour >= sleeptime1 || currentHour < sleeptime2;
}
}
} // namespace util