Added scheduling
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "boomcast_config.h"
|
||||
#include "boomcast_network.h"
|
||||
#include "boomcast_state.h"
|
||||
#include "boomcast_monitor.h"
|
||||
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <ArduinoJson.h>
|
||||
@@ -31,7 +32,10 @@ static String isoUtc(uint64_t epochMs) {
|
||||
|
||||
static bool writeAll(WiFiClientSecure& client, const uint8_t* bytes, size_t size) {
|
||||
size_t done = 0;
|
||||
const uint32_t started = millis();
|
||||
while (done < size && client.connected()) {
|
||||
boomMonitor.tick();
|
||||
if (millis() - started > 15000) return false;
|
||||
size_t n = client.write(bytes + done, size - done);
|
||||
if (!n) return false;
|
||||
done += n;
|
||||
@@ -52,6 +56,7 @@ static bool waitForNtpTime() {
|
||||
const uint32_t started = millis();
|
||||
while (!synchronized && boomNetwork.connected() && millis() - started < 20000) {
|
||||
if (sntp_get_sync_status() == SNTP_SYNC_STATUS_COMPLETED) synchronized = true;
|
||||
boomMonitor.tick();
|
||||
if (!synchronized) delay(100);
|
||||
}
|
||||
time_t now = time(nullptr);
|
||||
@@ -73,7 +78,9 @@ bool BoomUploader::submitOne(const String& filename) {
|
||||
int BoomUploader::submitAll() {
|
||||
if (!queue_ || !boomNetwork.connected()) return 0;
|
||||
int sent = 0;
|
||||
while (sent < 500) {
|
||||
const uint32_t started = millis();
|
||||
while (sent < 500 && millis() - started < 30000) {
|
||||
boomMonitor.tick();
|
||||
String first[1];
|
||||
if (!queue_->listNames(first, 1) || !submitOne(first[0])) break;
|
||||
++sent;
|
||||
@@ -148,11 +155,12 @@ bool BoomUploader::doPost(const String& filename, const String& path) {
|
||||
|
||||
WiFiClientSecure client;
|
||||
client.useBuiltinCACertBundle();
|
||||
client.setTimeout(60);
|
||||
client.setTimeout(5);
|
||||
client.setHandshakeTimeout(15);
|
||||
Serial.printf("[Upload] free heap=%u PSRAM=%u largest PSRAM=%u\n",
|
||||
unsigned(ESP.getFreeHeap()), unsigned(ESP.getFreePsram()),
|
||||
unsigned(heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)));
|
||||
if (!client.connect(host.c_str(), port)) { fclose(file); return false; }
|
||||
if (!client.connect(host.c_str(), port, 5000)) { fclose(file); return false; }
|
||||
String header = "POST " + String(SUBMIT_PATH) + " HTTP/1.1\r\nHost: " + host + "\r\n";
|
||||
header += "Authorization: Bearer " + token + "\r\n";
|
||||
header += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n";
|
||||
@@ -160,7 +168,8 @@ bool BoomUploader::doPost(const String& filename, const String& path) {
|
||||
bool sent = writeAll(client, header) && writeAll(client, preamble);
|
||||
uint8_t chunk[1024];
|
||||
size_t fileSent = 0;
|
||||
while (sent && fileSent < fileBytes) {
|
||||
const uint32_t uploadStarted = millis();
|
||||
while (sent && fileSent < fileBytes && millis() - uploadStarted < 30000) {
|
||||
size_t n = fread(chunk, 1, min(sizeof(chunk), fileBytes - fileSent), file);
|
||||
if (n == 0) { sent = false; break; }
|
||||
sent = writeAll(client, chunk, n);
|
||||
@@ -171,7 +180,21 @@ bool BoomUploader::doPost(const String& filename, const String& path) {
|
||||
if (sent) sent = writeAll(client, trailer);
|
||||
if (!sent) { client.stop(); return false; }
|
||||
|
||||
String statusLine = client.readStringUntil('\n');
|
||||
const uint32_t responseStarted = millis();
|
||||
auto readLine = [&client, responseStarted](String& line) {
|
||||
line = "";
|
||||
while (millis() - responseStarted < 60000 && line.length() < 1024) {
|
||||
boomMonitor.tick();
|
||||
if (client.available()) {
|
||||
char c = char(client.read());
|
||||
if (c == '\n') return true;
|
||||
line += c;
|
||||
} else { if (!client.connected()) return false; delay(5); }
|
||||
}
|
||||
return false;
|
||||
};
|
||||
String statusLine;
|
||||
if (!readLine(statusLine)) { client.stop(); return false; }
|
||||
int status = 0;
|
||||
if (statusLine.startsWith("HTTP/1.")) {
|
||||
int firstSpace = statusLine.indexOf(' ');
|
||||
@@ -179,16 +202,20 @@ bool BoomUploader::doPost(const String& filename, const String& path) {
|
||||
}
|
||||
// Django returns an ordinary Content-Length response; do not accept redirects.
|
||||
int contentLength = -1;
|
||||
while (client.connected()) {
|
||||
String line = client.readStringUntil('\n'); line.trim();
|
||||
size_t headerBytes = 0;
|
||||
while (true) {
|
||||
String line;
|
||||
if (!readLine(line) || (headerBytes += line.length()) > 8192) { client.stop(); return false; }
|
||||
line.trim();
|
||||
if (!line.length()) break;
|
||||
if (line.startsWith("Content-Length:") || line.startsWith("content-length:"))
|
||||
contentLength = line.substring(line.indexOf(':') + 1).toInt();
|
||||
}
|
||||
if (contentLength < 0 || contentLength > 8192) { client.stop(); return false; }
|
||||
String body; body.reserve(contentLength);
|
||||
uint32_t started = millis();
|
||||
while (body.length() < size_t(contentLength) && millis() - started < 60000) {
|
||||
while (body.length() < size_t(contentLength) && millis() - responseStarted < 60000) {
|
||||
boomMonitor.tick();
|
||||
if (!client.connected() && !client.available()) break;
|
||||
while (client.available() && body.length() < size_t(contentLength)) body += char(client.read());
|
||||
delay(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user