⚡ Adds event bus
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include <consumers/proto_event_storage.h>
|
||||
#include <platform_shared/api.pb.h>
|
||||
#include <settings/wifi_settings.h>
|
||||
#include <settings/ap_settings.h>
|
||||
#include <settings/mdns_settings.h>
|
||||
#include <settings/peripherals_settings.h>
|
||||
#include <settings/camera_settings.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
class EventStorageManager {
|
||||
public:
|
||||
void initialize() {
|
||||
ESP_LOGI(TAG, "Loading settings from storage");
|
||||
|
||||
_wifiStorage.begin();
|
||||
_apStorage.begin();
|
||||
_mdnsStorage.begin();
|
||||
_peripheralStorage.begin();
|
||||
#if FT_ENABLED(USE_CAMERA)
|
||||
_cameraStorage.begin();
|
||||
#endif
|
||||
|
||||
ESP_LOGI(TAG, "Settings loaded and published");
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr const char* TAG = "StorageManager";
|
||||
|
||||
ProtoEventStorage<api_WifiSettings, WiFiSettings_defaults> _wifiStorage =
|
||||
ProtoEventStorage<api_WifiSettings, WiFiSettings_defaults>("/config/wifiSettings.pb", api_WifiSettings_fields,
|
||||
api_WifiSettings_size, 1000);
|
||||
|
||||
ProtoEventStorage<api_APSettings, APSettings_defaults> _apStorage =
|
||||
ProtoEventStorage<api_APSettings, APSettings_defaults>("/config/apSettings.pb", api_APSettings_fields,
|
||||
api_APSettings_size, 1000);
|
||||
|
||||
ProtoEventStorage<api_MDNSSettings, MDNSSettings_defaults> _mdnsStorage =
|
||||
ProtoEventStorage<api_MDNSSettings, MDNSSettings_defaults>("/config/mdnsSettings.pb", api_MDNSSettings_fields,
|
||||
api_MDNSSettings_size, 1000);
|
||||
|
||||
ProtoEventStorage<api_PeripheralSettings, PeripheralsConfiguration_defaults> _peripheralStorage =
|
||||
ProtoEventStorage<api_PeripheralSettings, PeripheralsConfiguration_defaults>(
|
||||
"/config/peripheralSettings.pb", api_PeripheralSettings_fields, api_PeripheralSettings_size, 500);
|
||||
|
||||
#if FT_ENABLED(USE_CAMERA)
|
||||
ProtoEventStorage<api_CameraSettings, Camera::CameraSettings_defaults> _cameraStorage =
|
||||
ProtoEventStorage<api_CameraSettings, Camera::CameraSettings_defaults>(
|
||||
"/config/cameraSettings.pb", api_CameraSettings_fields, api_CameraSettings_size, 1000);
|
||||
#endif
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
#include <event_bus/event_bus.h>
|
||||
#include <pb_encode.h>
|
||||
#include <pb_decode.h>
|
||||
#include <esp_log.h>
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
|
||||
template <typename ProtoMsg, ProtoMsg (*DefaultsFn)()>
|
||||
class ProtoEventStorage {
|
||||
public:
|
||||
ProtoEventStorage(const char* filename, const pb_msgdesc_t* descriptor, size_t maxSize, uint32_t debounceMs = 1000)
|
||||
: _filename(filename), _descriptor(descriptor), _maxSize(maxSize), _debounceMs(debounceMs) {}
|
||||
|
||||
void begin() {
|
||||
auto loaded = std::unique_ptr<ProtoMsg>(new ProtoMsg {});
|
||||
loadOrDefault(*loaded);
|
||||
EventBus::publish(*loaded, "EventStorage");
|
||||
ESP_LOGI(TAG, "Loaded %s", _filename);
|
||||
|
||||
_handle = EventBus::subscribe<ProtoMsg>(_debounceMs, [this](const ProtoMsg& msg) { save(msg); });
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr const char* TAG = "ProtoStorage";
|
||||
const char* _filename;
|
||||
const pb_msgdesc_t* _descriptor;
|
||||
size_t _maxSize;
|
||||
uint32_t _debounceMs;
|
||||
typename EventBus::Handle<ProtoMsg> _handle;
|
||||
|
||||
void loadOrDefault(ProtoMsg& outMsg) {
|
||||
FILE* file = fopen(_filename, "rb");
|
||||
if (!file) {
|
||||
outMsg = DefaultsFn();
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
if (size == 0 || size > _maxSize) {
|
||||
fclose(file);
|
||||
outMsg = DefaultsFn();
|
||||
return;
|
||||
}
|
||||
|
||||
auto buffer = std::make_unique<uint8_t[]>(size);
|
||||
fread(buffer.get(), 1, size, file);
|
||||
fclose(file);
|
||||
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer.get(), size);
|
||||
if (!pb_decode(&stream, _descriptor, &outMsg)) {
|
||||
outMsg = DefaultsFn();
|
||||
}
|
||||
}
|
||||
|
||||
void save(const ProtoMsg& msg) {
|
||||
auto buffer = std::make_unique<uint8_t[]>(_maxSize);
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer.get(), _maxSize);
|
||||
|
||||
if (!pb_encode(&stream, _descriptor, &msg)) return;
|
||||
|
||||
FILE* file = fopen(_filename, "wb");
|
||||
if (!file) return;
|
||||
|
||||
fwrite(buffer.get(), 1, stream.bytes_written, file);
|
||||
fclose(file);
|
||||
ESP_LOGD(TAG, "Saved %s", _filename);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user