Skip to content

Commit

Permalink
misc: Use Config element returned by add()
Browse files Browse the repository at this point in the history
Instead of discarding it and then retrieving the last added element.
  • Loading branch information
MattiasTF committed Oct 17, 2024
1 parent 635a4b3 commit 1527a86
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions software/src/modules/charge_manager/charge_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ void ChargeManager::setup()
auto default_current = config.get("default_available_current")->asUint();
available_current.get("current")->updateUint(default_current);
for (size_t i = 0; i < config.get("chargers")->count(); ++i) {
state.get("chargers")->add();
low_level_state.get("chargers")->add();
state.get("chargers")->get(i)->get("n")->updateString(config.get("chargers")->get(i)->get("name")->asString());
auto state_last_charger = state.get("chargers")->add();
state_last_charger->get("n")->updateString(config.get("chargers")->get(i)->get("name")->asString());
}

size_t hosts_buf_size = 0;
Expand Down
10 changes: 5 additions & 5 deletions software/src/modules/charge_tracker/charge_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ void ChargeTracker::readNRecords(File *f, size_t records_to_read)
memcpy(&cs, buf, sizeof(cs));
memcpy(&ce, buf + sizeof(cs), sizeof(ce));

last_charges.add();
last_charges.get(last_charges.count() - 1)->get("timestamp_minutes")->updateUint(cs.timestamp_minutes);
last_charges.get(last_charges.count() - 1)->get("charge_duration")->updateUint(ce.charge_duration);
last_charges.get(last_charges.count() - 1)->get("user_id")->updateUint(cs.user_id);
last_charges.get(last_charges.count() - 1)->get("energy_charged")->updateFloat(charged_invalid(cs, ce) ? NAN : ce.meter_end - cs.meter_start);
auto last_charge = last_charges.add();
last_charge->get("timestamp_minutes")->updateUint(cs.timestamp_minutes);
last_charge->get("charge_duration")->updateUint(ce.charge_duration);
last_charge->get("user_id")->updateUint(cs.user_id);
last_charge->get("energy_charged")->updateFloat(charged_invalid(cs, ce) ? NAN : ce.meter_end - cs.meter_start);
}
}

Expand Down
10 changes: 5 additions & 5 deletions software/src/modules/proxy/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ void Proxy::setup()
uint16_t device_id;

while (tf_hal_get_device_info(&hal, i, uid, &port_name, &device_id) == TF_E_OK) {
devices.add();
auto last_device = devices.add();

devices.get(devices.count() - 1)->get("uid")->updateString(String(uid));
devices.get(devices.count() - 1)->get("port")->updateString(String(port_name));
devices.get(devices.count() - 1)->get("name")->updateString(String(tf_get_device_display_name(device_id)));
devices.get(devices.count() - 1)->get("device_id")->updateUint(device_id);
last_device->get("uid")->updateString(uid);
last_device->get("port")->updateString(String(port_name));
last_device->get("name")->updateString(tf_get_device_display_name(device_id));
last_device->get("device_id")->updateUint(device_id);
++i;
}

Expand Down
14 changes: 6 additions & 8 deletions software/src/modules/uptime_tracker/uptime_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,16 @@ void UptimeTracker::setup()
{
if (uptimes.count() >= MAX_UPTIMES)
uptimes.remove(0);
uptimes.add();

size_t idx = uptimes.count() - 1;
auto last_uptime = uptimes.add();

//timestamp_min initialized with 0. 0 means not synced
if (rtc.clock_synced(&timestamp))
uptimes.get(idx)->get("timestamp_min")->updateUint((timestamp.tv_sec - millis() / 1000) / 60);
last_uptime->get("timestamp_min")->updateUint((timestamp.tv_sec - millis() / 1000) / 60);

uptimes.get(idx)->get("reset_reason")->updateUint(esp_reset_reason());
uptimes.get(idx)->get("uptime")->updateUint(old_uptime.uptime);
uptimes.get(idx)->get("uptime_overflows")->updateUint(old_uptime.overflow_count);
uptimes.get(idx)->get("boot_count")->updateUint(data.boot_count);
last_uptime->get("reset_reason")->updateUint(esp_reset_reason());
last_uptime->get("uptime")->updateUint(old_uptime.uptime);
last_uptime->get("uptime_overflows")->updateUint(old_uptime.overflow_count);
last_uptime->get("boot_count")->updateUint(data.boot_count);

api.writeConfig("info/last_boots", &uptimes);

Expand Down
3 changes: 1 addition & 2 deletions software/src/modules/users/users.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ void Users::register_urls()

api.addState("users/config", &config, {"digest_hash"});
api.addCommand("users/add", &add, {"digest_hash"}, [this](String &/*errmsg*/) {
config.get("users")->add();
Config *user = (Config *)config.get("users")->get(config.get("users")->count() - 1);
auto user = config.get("users")->add();

user->get("id")->updateUint(add.get("id")->asUint());
user->get("roles")->updateUint(add.get("roles")->asUint());
Expand Down

0 comments on commit 1527a86

Please sign in to comment.