Initial commit
This commit is contained in:
97
src/test_mode.cpp
Normal file
97
src/test_mode.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "test_mode.h"
|
||||
|
||||
#ifdef ENABLE_TEST_MODE
|
||||
#include "config.h"
|
||||
#include "compressor.h"
|
||||
#include "lora_transport.h"
|
||||
#include "json_codec.h"
|
||||
#include "time_manager.h"
|
||||
#include "display_ui.h"
|
||||
#include "mqtt_client.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
static uint32_t g_last_test_ms = 0;
|
||||
static uint32_t g_last_timesync_ms = 0;
|
||||
|
||||
void test_sender_loop(uint16_t short_id, const char *device_id) {
|
||||
LoraPacket rx = {};
|
||||
if (lora_receive(rx, 0) && rx.payload_type == PayloadType::TimeSync) {
|
||||
time_handle_timesync_payload(rx.payload, rx.payload_len);
|
||||
}
|
||||
|
||||
if (millis() - g_last_test_ms < 30000) {
|
||||
return;
|
||||
}
|
||||
g_last_test_ms = millis();
|
||||
|
||||
char code[5];
|
||||
uint16_t val = random(0, 9999);
|
||||
snprintf(code, sizeof(code), "%04u", val);
|
||||
display_set_test_code(code);
|
||||
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["id"] = device_id;
|
||||
doc["role"] = "sender";
|
||||
doc["test_code"] = code;
|
||||
doc["ts"] = time_get_utc();
|
||||
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
uint8_t compressed[LORA_MAX_PAYLOAD];
|
||||
size_t compressed_len = 0;
|
||||
if (!compressBuffer(reinterpret_cast<const uint8_t *>(json.c_str()), json.length(), compressed, sizeof(compressed), compressed_len)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LoraPacket pkt = {};
|
||||
pkt.protocol_version = PROTOCOL_VERSION;
|
||||
pkt.role = DeviceRole::Sender;
|
||||
pkt.device_id_short = short_id;
|
||||
pkt.payload_type = PayloadType::TestCode;
|
||||
pkt.payload_len = compressed_len;
|
||||
memcpy(pkt.payload, compressed, compressed_len);
|
||||
lora_send(pkt);
|
||||
}
|
||||
|
||||
void test_receiver_loop(SenderStatus *statuses, uint8_t count) {
|
||||
if (millis() - g_last_timesync_ms > TIME_SYNC_INTERVAL_SEC * 1000UL) {
|
||||
g_last_timesync_ms = millis();
|
||||
time_send_timesync(0);
|
||||
}
|
||||
|
||||
LoraPacket pkt = {};
|
||||
if (!lora_receive(pkt, 0)) {
|
||||
return;
|
||||
}
|
||||
if (pkt.payload_type != PayloadType::TestCode) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t decompressed[128];
|
||||
size_t decompressed_len = 0;
|
||||
if (!decompressBuffer(pkt.payload, pkt.payload_len, decompressed, sizeof(decompressed), decompressed_len)) {
|
||||
return;
|
||||
}
|
||||
decompressed[decompressed_len] = '\0';
|
||||
|
||||
StaticJsonDocument<128> doc;
|
||||
if (deserializeJson(doc, reinterpret_cast<const char *>(decompressed)) != DeserializationError::Ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *id = doc["id"] | "";
|
||||
const char *code = doc["test_code"] | "";
|
||||
|
||||
for (uint8_t i = 0; i < count; ++i) {
|
||||
if (strncmp(statuses[i].last_data.device_id, id, sizeof(statuses[i].last_data.device_id)) == 0) {
|
||||
display_set_test_code_for_sender(i, code);
|
||||
statuses[i].has_data = true;
|
||||
statuses[i].last_update_ts_utc = time_get_utc();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mqtt_publish_test(id, String(reinterpret_cast<const char *>(decompressed)));
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user