Skip to content

Commit

Permalink
fix duplicate entries
Browse files Browse the repository at this point in the history
  • Loading branch information
kevdliu committed Apr 22, 2024
1 parent 364c33c commit 33aaef9
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/power_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,42 @@ PowerPanel::~PowerPanel() {

void PowerPanel::create_device(json &j) {
std::string name = j["device"].template get<std::string>();
std::string status = j["status"].template get<std::string>();

spdlog::debug("Fetched initial status for power device {}: {}", name, status);
lv_obj_t *power_device_toggle;
auto entry = devices.find(name);
if (entry == devices.end()) {
lv_obj_t *power_device_toggle_cont = lv_obj_create(cont);
lv_obj_set_size(power_device_toggle_cont, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(power_device_toggle_cont, 0, 0);

lv_obj_t *power_device_toggle_cont = lv_obj_create(cont);
lv_obj_set_size(power_device_toggle_cont, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(power_device_toggle_cont, 0, 0);
lv_obj_t *l = lv_label_create(power_device_toggle_cont);
lv_label_set_text(l, name.c_str());
lv_obj_align(l, LV_ALIGN_LEFT_MID, 0, 0);

lv_obj_t *l = lv_label_create(power_device_toggle_cont);
lv_label_set_text(l, name.c_str());
lv_obj_align(l, LV_ALIGN_LEFT_MID, 0, 0);
power_device_toggle = lv_switch_create(power_device_toggle_cont);
lv_obj_align(power_device_toggle, LV_ALIGN_RIGHT_MID, 0, 0);

lv_obj_t *power_device_toggle = lv_switch_create(power_device_toggle_cont);
lv_obj_align(power_device_toggle, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_add_event_cb(power_device_toggle, &PowerPanel::_handle_callback,
LV_EVENT_VALUE_CHANGED, this);

devices.insert({name, power_device_toggle});
} else {
power_device_toggle = entry->second;
}

std::string status = j["status"].template get<std::string>();
spdlog::debug("Fetched initial status for power device {}: {}", name, status);

if (status == "on") {
lv_obj_add_state(power_device_toggle, LV_STATE_CHECKED);
} else {
lv_obj_clear_state(power_device_toggle, LV_STATE_CHECKED);
}

lv_obj_add_event_cb(power_device_toggle, &PowerPanel::_handle_callback,
LV_EVENT_VALUE_CHANGED, this);

devices.insert({name, power_device_toggle});
}

void PowerPanel::create_devices(json &j) {
std::lock_guard<std::mutex> lock(lv_lock);

for (auto &device : devices) {
lv_obj_clean(device.second);
}
devices.clear();

if (j.contains("result")) {
json result = j["result"];
if (result.contains("devices")) {
Expand Down

0 comments on commit 33aaef9

Please sign in to comment.