first commit
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
#include "boomcast_uploader.h"
|
||||
#include "boomcast_config.h"
|
||||
#include "boomcast_network.h"
|
||||
#include "boomcast_state.h"
|
||||
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
BoomUploader boomUploader;
|
||||
|
||||
static String eventIdFromFilename(String name) {
|
||||
if (name.endsWith(".wav")) name.remove(name.length() - 4);
|
||||
return name;
|
||||
}
|
||||
|
||||
static String isoUtc(uint64_t epochMs) {
|
||||
if (!epochMs) return "";
|
||||
time_t seconds = epochMs / 1000;
|
||||
struct tm utc;
|
||||
if (!gmtime_r(&seconds, &utc)) return "";
|
||||
char text[30];
|
||||
snprintf(text, sizeof(text), "%04d-%02d-%02dT%02d:%02d:%02d.%03uZ",
|
||||
utc.tm_year + 1900, utc.tm_mon + 1, utc.tm_mday,
|
||||
utc.tm_hour, utc.tm_min, utc.tm_sec, unsigned(epochMs % 1000));
|
||||
return String(text);
|
||||
}
|
||||
|
||||
static bool writeAll(WiFiClientSecure& client, const uint8_t* bytes, size_t size) {
|
||||
size_t done = 0;
|
||||
while (done < size && client.connected()) {
|
||||
size_t n = client.write(bytes + done, size - done);
|
||||
if (!n) return false;
|
||||
done += n;
|
||||
}
|
||||
return done == size;
|
||||
}
|
||||
static bool writeAll(WiFiClientSecure& client, const String& value) {
|
||||
return writeAll(client, reinterpret_cast<const uint8_t*>(value.c_str()), value.length());
|
||||
}
|
||||
static void addPart(String& body, const String& boundary, const char* name, const String& value) {
|
||||
body += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + value + "\r\n";
|
||||
}
|
||||
|
||||
bool BoomUploader::submitOne(const String& filename) {
|
||||
if (!queue_ || !queue_->ready() || !boomNetwork.connected() || !queue_->exists(filename)) return false;
|
||||
boomState.setSyncing(true, "Uploading " + filename);
|
||||
bool ok = doPost(filename, queue_->absolutePath(filename));
|
||||
if (ok && !queue_->remove(filename)) {
|
||||
boomState.setSyncing(false, "Receipt received; local cleanup failed");
|
||||
return false;
|
||||
}
|
||||
boomState.setSyncing(false, ok ? "Submitted " + filename : "Upload failed; queued for retry");
|
||||
return ok;
|
||||
}
|
||||
|
||||
int BoomUploader::submitAll() {
|
||||
if (!queue_ || !boomNetwork.connected()) return 0;
|
||||
int sent = 0;
|
||||
while (sent < 500) {
|
||||
String first[1];
|
||||
if (!queue_->listNames(first, 1) || !submitOne(first[0])) break;
|
||||
++sent;
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
void BoomUploader::tick() {
|
||||
if (!queue_ || !queue_->ready() || !boomNetwork.connected() || !queue_->count()) return;
|
||||
if ((int32_t)(millis() - nextAttemptMs_) < 0) return;
|
||||
String first[1];
|
||||
if (!queue_->listNames(first, 1)) return;
|
||||
if (submitOne(first[0])) {
|
||||
failures_ = 0;
|
||||
permanentFailure_ = false;
|
||||
nextAttemptMs_ = millis() + 1000;
|
||||
} else {
|
||||
failures_ = min(uint8_t(failures_ + 1), uint8_t(8));
|
||||
uint32_t base = permanentFailure_ ? 3600000UL : min(900000UL, 5000UL << failures_);
|
||||
nextAttemptMs_ = millis() + base + (esp_random() % 5000);
|
||||
}
|
||||
}
|
||||
|
||||
bool BoomUploader::doPost(const String& filename, const String& path) {
|
||||
permanentFailure_ = false;
|
||||
BoomEventMeta meta;
|
||||
if (!queue_->readMeta(filename, meta)) return false;
|
||||
String token = boomState.prefs().getString("token", SENSOR_TOKEN);
|
||||
if (!token.length() || !SENSOR_ID || !strlen(SERVER_CA_CERT)) {
|
||||
Serial.println("[Upload] sensor ID, token or CA certificate missing");
|
||||
permanentFailure_ = true;
|
||||
return false;
|
||||
}
|
||||
String origin(SERVER_BASE_URL);
|
||||
if (!origin.startsWith("https://")) {
|
||||
Serial.println("[Upload] HTTPS required");
|
||||
permanentFailure_ = true;
|
||||
return false;
|
||||
}
|
||||
String host = origin.substring(8);
|
||||
int slash = host.indexOf('/'); if (slash >= 0) host = host.substring(0, slash);
|
||||
uint16_t port = 443;
|
||||
int colon = host.lastIndexOf(':');
|
||||
if (colon >= 0) { port = host.substring(colon + 1).toInt(); host = host.substring(0, colon); }
|
||||
if (!host.length() || !port) return false;
|
||||
|
||||
FILE* file = fopen(path.c_str(), "rb");
|
||||
if (!file) return false;
|
||||
const size_t fileBytes = queue_->fileSize(filename);
|
||||
const String boundary = "BoomCast" + String(esp_random(), HEX);
|
||||
const String eventId = eventIdFromFilename(filename);
|
||||
String preamble; preamble.reserve(700);
|
||||
addPart(preamble, boundary, "sensor_id", String(SENSOR_ID));
|
||||
addPart(preamble, boundary, "event_id", eventId);
|
||||
if (meta.captureStartedMs) {
|
||||
addPart(preamble, boundary, "captured_at", isoUtc(meta.captureStartedMs));
|
||||
addPart(preamble, boundary, "capture_started_at", isoUtc(meta.captureStartedMs));
|
||||
} else addPart(preamble, boundary, "time_state", "unknown");
|
||||
if (meta.triggeredMs) addPart(preamble, boundary, "triggered_at", isoUtc(meta.triggeredMs));
|
||||
if (meta.queuedMs) addPart(preamble, boundary, "queued_at", isoUtc(meta.queuedMs));
|
||||
addPart(preamble, boundary, "duration_seconds", String(meta.sampleCount / float(AUDIO_SAMPLE_RATE), 4));
|
||||
addPart(preamble, boundary, "firmware_version", BOOMCAST_FIRMWARE_VERSION);
|
||||
addPart(preamble, boundary, "trigger_type", filename.indexOf("_manual_") >= 0 ? "manual" : "audio_spike");
|
||||
preamble += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\nContent-Type: audio/wav\r\n\r\n";
|
||||
const String trailer = "\r\n--" + boundary + "--\r\n";
|
||||
const size_t length = preamble.length() + fileBytes + trailer.length();
|
||||
|
||||
WiFiClientSecure client;
|
||||
client.setCACert(SERVER_CA_CERT);
|
||||
client.setTimeout(60);
|
||||
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; }
|
||||
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";
|
||||
header += "Content-Length: " + String(length) + "\r\nAccept: application/json\r\nConnection: close\r\n\r\n";
|
||||
bool sent = writeAll(client, header) && writeAll(client, preamble);
|
||||
uint8_t chunk[1024];
|
||||
size_t fileSent = 0;
|
||||
while (sent && fileSent < fileBytes) {
|
||||
size_t n = fread(chunk, 1, min(sizeof(chunk), fileBytes - fileSent), file);
|
||||
if (n == 0) { sent = false; break; }
|
||||
sent = writeAll(client, chunk, n);
|
||||
fileSent += n;
|
||||
}
|
||||
if (ferror(file) || fileSent != fileBytes) sent = false;
|
||||
fclose(file);
|
||||
if (sent) sent = writeAll(client, trailer);
|
||||
if (!sent) { client.stop(); return false; }
|
||||
|
||||
String statusLine = client.readStringUntil('\n');
|
||||
int status = 0;
|
||||
if (statusLine.startsWith("HTTP/1.")) {
|
||||
int firstSpace = statusLine.indexOf(' ');
|
||||
if (firstSpace > 0) status = statusLine.substring(firstSpace + 1).toInt();
|
||||
}
|
||||
// Django returns an ordinary Content-Length response; do not accept redirects.
|
||||
int contentLength = -1;
|
||||
while (client.connected()) {
|
||||
String line = client.readStringUntil('\n'); 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 (client.available() && body.length() < size_t(contentLength)) body += char(client.read());
|
||||
delay(1);
|
||||
}
|
||||
client.stop();
|
||||
Serial.printf("[Upload] HTTP %d, response bytes=%u\n", status, unsigned(body.length()));
|
||||
if (body.length() != size_t(contentLength)) return false;
|
||||
if (status != 200 && status != 201) {
|
||||
permanentFailure_ = status >= 400 && status < 500 && status != 408 && status != 429;
|
||||
return false;
|
||||
}
|
||||
JsonDocument receipt;
|
||||
if (deserializeJson(receipt, body)) return false;
|
||||
const char* returnedEvent = receipt["event_id"] | "";
|
||||
const char* submission = receipt["submission_id"] | "";
|
||||
const char* state = receipt["status"] | "";
|
||||
if (eventId != returnedEvent || strlen(submission) != 36 ||
|
||||
(strcmp(state, "pending") && strcmp(state, "reviewed")) ||
|
||||
(status == 200 && !receipt["duplicate"].as<bool>())) return false;
|
||||
Serial.printf("[Upload] accepted event %s; free heap=%u PSRAM=%u largest PSRAM=%u\n",
|
||||
eventId.c_str(), unsigned(ESP.getFreeHeap()), unsigned(ESP.getFreePsram()),
|
||||
unsigned(heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)));
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user