Browse Source

Save room topic to a new "/room_prefs" file

pull/3070/head
Ben Batt 1 week ago
parent
commit
7adff025fe
  1. 46
      examples/simple_room_server/MyMesh.cpp
  2. 8
      examples/simple_room_server/MyMesh.h

46
examples/simple_room_server/MyMesh.cpp

@ -714,6 +714,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
mesh::Mesh::begin(); mesh::Mesh::begin();
_fs = fs; _fs = fs;
// load persisted prefs // load persisted prefs
loadRoomPrefs();
_cli.loadPrefs(_fs); _cli.loadPrefs(_fs);
acl.load(_fs, self_id); acl.load(_fs, self_id);
@ -747,6 +748,10 @@ void MyMesh::begin(FILESYSTEM *fs) {
updateAdvertTimer(); updateAdvertTimer();
updateFloodAdvertTimer(); updateFloodAdvertTimer();
if (strlen(topic) > 0) {
topic_timestamp = getRTCClock()->getCurrentTimeUnique();
}
board.setAdcMultiplier(_prefs.adc_multiplier); board.setAdcMultiplier(_prefs.adc_multiplier);
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
@ -754,6 +759,45 @@ void MyMesh::begin(FILESYSTEM *fs) {
#endif #endif
} }
#define PREFS_FILE_PATH "/room_prefs"
void MyMesh::loadRoomPrefs() {
#if defined(RP2040_PLATFORM)
File file = _fs->open(PREFS_FILE_PATH, "r");
#else
File file = _fs->open(PREFS_FILE_PATH);
#endif
if (file) {
file.read((uint8_t *)&topic[0], sizeof(topic)); // 0
topic[MAX_TOPIC_TEXT_LEN] = '\0';
// next: 144
file.close();
}
}
void MyMesh::saveRoomPrefs() {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
_fs->remove(PREFS_FILE_PATH);
File file = _fs->open(PREFS_FILE_PATH, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
File file = _fs->open(PREFS_FILE_PATH, "w");
#else
File file = _fs->open(PREFS_FILE_PATH, "w", true);
#endif
if (file) {
file.write((uint8_t *)&topic[0], sizeof(topic)); // 0
// next: 144
file.close();
}
}
void MyMesh::savePrefs() {
saveRoomPrefs();
_cli.savePrefs(_fs);
}
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) { void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
if (scope.isNull()) { if (scope.isNull()) {
sendFlood(pkt, delay_millis, path_hash_size); sendFlood(pkt, delay_millis, path_hash_size);
@ -964,6 +1008,8 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
strcpy(topic, new_topic); strcpy(topic, new_topic);
topic_timestamp = getRTCClock()->getCurrentTimeUnique(); topic_timestamp = getRTCClock()->getCurrentTimeUnique();
saveRoomPrefs();
sprintf(reply, "New topic set with length %d", len); sprintf(reply, "New topic set with length %d", len);
return; return;

8
examples/simple_room_server/MyMesh.h

@ -134,6 +134,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
File openAppend(const char* fname); File openAppend(const char* fname);
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len); int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
void loadRoomPrefs();
void saveRoomPrefs();
protected: protected:
float getAirtimeBudgetFactor() const override { float getAirtimeBudgetFactor() const override {
return _prefs.airtime_factor; return _prefs.airtime_factor;
@ -194,13 +197,10 @@ public:
return &_prefs; return &_prefs;
} }
void savePrefs() override {
_cli.savePrefs(_fs);
}
void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size); void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
// CommonCLICallbacks // CommonCLICallbacks
void savePrefs() override;
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
bool formatFileSystem() override; bool formatFileSystem() override;
void sendSelfAdvertisement(int delay_millis, bool flood) override; void sendSelfAdvertisement(int delay_millis, bool flood) override;

Loading…
Cancel
Save