|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2020 - 2025 Pionix GmbH and Contributors to EVerest |
| 3 | +#include "BUDCExternalDerate.hpp" |
| 4 | +#include "ftxui/component/component.hpp" |
| 5 | +#include "ftxui/component/component_base.hpp" |
| 6 | +#include "ftxui/component/component_options.hpp" |
| 7 | +#include "ftxui/component/event.hpp" |
| 8 | +#include "ftxui/component/screen_interactive.hpp" |
| 9 | +#include "ftxui/dom/elements.hpp" |
| 10 | +#include "ftxui/screen/color.hpp" |
| 11 | +#include <regex> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +namespace module { |
| 15 | + |
| 16 | +using namespace ftxui; |
| 17 | + |
| 18 | +void BUDCExternalDerate::init() { |
| 19 | + invoke_init(*p_main); |
| 20 | +} |
| 21 | + |
| 22 | +void BUDCExternalDerate::ready() { |
| 23 | + invoke_ready(*p_main); |
| 24 | + |
| 25 | + auto screen = ScreenInteractive::Fullscreen(); |
| 26 | + |
| 27 | + auto msg_component = Container::Vertical({Renderer([] { return text(""); })}); |
| 28 | + |
| 29 | + std::map<std::string, std::map<std::string, Component>> messages; |
| 30 | + |
| 31 | + r_derate->subscribe_all_errors( |
| 32 | + [&](Everest::error::Error const& error) { |
| 33 | + std::scoped_lock lock(data_mutex); |
| 34 | + auto error_elem = Renderer([=] { return text(" - " + error.type); }); |
| 35 | + auto sub_elem = Renderer([=] { return text(" " + error.sub_type); }); |
| 36 | + auto msg_elem = Renderer([=] { return text(" " + error.message); }); |
| 37 | + |
| 38 | + auto component = Container::Vertical({ |
| 39 | + error_elem, |
| 40 | + sub_elem, |
| 41 | + msg_elem, |
| 42 | + }); |
| 43 | + |
| 44 | + messages[error.type][error.sub_type] = component; |
| 45 | + msg_component->Add(component); |
| 46 | + screen.Post(Event::Custom); |
| 47 | + }, |
| 48 | + [&](Everest::error::Error const& error) { |
| 49 | + std::scoped_lock lock(data_mutex); |
| 50 | + if (messages.count(error.type)) { |
| 51 | + auto& sub = messages.at(error.type); |
| 52 | + if (sub.count(error.sub_type)) { |
| 53 | + auto& elem = sub.at(error.sub_type); |
| 54 | + elem->Detach(); |
| 55 | + sub.erase(error.sub_type); |
| 56 | + } |
| 57 | + if (not sub.size()) { |
| 58 | + messages.erase(error.type); |
| 59 | + } |
| 60 | + } |
| 61 | + screen.Post(Event::Custom); |
| 62 | + }); |
| 63 | + |
| 64 | + auto msg_component_holder = Container::Horizontal({msg_component}); |
| 65 | + |
| 66 | + auto msg_component_renderer = Renderer(msg_component_holder, [&] { |
| 67 | + auto win = window(text("Active Errors"), msg_component_holder->Render()); |
| 68 | + return vbox({ |
| 69 | + hbox({ |
| 70 | + win, |
| 71 | + }), |
| 72 | + }) | |
| 73 | + flex_grow; |
| 74 | + }); |
| 75 | + |
| 76 | + auto var_component = |
| 77 | + Container::Vertical({Renderer([&] { return text("plug_temperature_C: " + std::to_string(plug_temp_C)); })}); |
| 78 | + auto var_component_holder = Container::Horizontal({var_component}); |
| 79 | + auto var_component_renderer = Renderer(var_component_holder, [&] { |
| 80 | + auto win = window(text("Vars"), var_component_holder->Render()); |
| 81 | + return vbox({ |
| 82 | + hbox({ |
| 83 | + win, |
| 84 | + }), |
| 85 | + }) | |
| 86 | + flex_grow; |
| 87 | + }); |
| 88 | + |
| 89 | + r_derate->subscribe_plug_temperature_C([&](double temp) { |
| 90 | + std::scoped_lock lock(data_mutex); |
| 91 | + plug_temp_C = temp; |
| 92 | + screen.Post(Event::Custom); |
| 93 | + }); |
| 94 | + |
| 95 | + InputOption o; |
| 96 | + o.multiline = false; |
| 97 | + o.cursor_position = 0; |
| 98 | + |
| 99 | + auto max_export_current_A_input = Input(&max_export_current_A, "100.0", o); |
| 100 | + auto max_import_current_A_input = Input(&max_import_current_A, "200.0", o); |
| 101 | + auto max_export_power_W_input = Input(&max_export_power_W, "300.0", o); |
| 102 | + auto max_import_power_W_input = Input(&max_import_power_W, "400.0", o); |
| 103 | + |
| 104 | + Component ovm_start = Button( |
| 105 | + "Set", |
| 106 | + [&] { |
| 107 | + types::dc_external_derate::ExternalDerating val; |
| 108 | + val.max_export_current_A = std::stof(max_export_current_A); |
| 109 | + val.max_import_current_A = std::stof(max_import_current_A); |
| 110 | + val.max_export_power_W = std::stof(max_export_power_W); |
| 111 | + val.max_import_power_W = std::stof(max_import_power_W); |
| 112 | + r_derate->call_set_external_derating(val); |
| 113 | + }, |
| 114 | + ButtonOption::Animated(Color::Blue, Color::White, Color::BlueLight, Color::White)) | |
| 115 | + flex_grow; |
| 116 | + |
| 117 | + auto action_component = Container::Horizontal({ |
| 118 | + Container::Vertical({ |
| 119 | + max_export_current_A_input, |
| 120 | + max_import_current_A_input, |
| 121 | + max_export_power_W_input, |
| 122 | + max_import_power_W_input, |
| 123 | + ovm_start, |
| 124 | + }), |
| 125 | + }); |
| 126 | + |
| 127 | + auto action_component_renderer = Renderer(action_component, [&] { |
| 128 | + return vbox({hbox(ovm_start->Render()), |
| 129 | + |
| 130 | + vbox(hbox(text(" Max Export Current (A): "), max_export_current_A_input->Render()), |
| 131 | + hbox(text(" Max Import Current (A): "), max_import_current_A_input->Render()), |
| 132 | + hbox(text(" Max Export Power (W): "), max_export_power_W_input->Render()), |
| 133 | + hbox(text(" Max Import Power (W): "), max_import_power_W_input->Render()))}); |
| 134 | + }); |
| 135 | + |
| 136 | + auto action_renderer = Renderer(action_component, [&] { |
| 137 | + return vbox({ |
| 138 | + hbox({ |
| 139 | + window(text("DC External Derate Commands"), action_component_renderer->Render()), |
| 140 | + }), |
| 141 | + }) | |
| 142 | + flex_grow; |
| 143 | + }); |
| 144 | + |
| 145 | + auto main_container = Container::Horizontal({action_renderer, var_component_renderer, msg_component_renderer}); |
| 146 | + |
| 147 | + auto main_renderer = Renderer(main_container, [&] { |
| 148 | + std::scoped_lock lock(data_mutex); |
| 149 | + return vbox({ |
| 150 | + text("DC External Derate Bringup") | bold | hcenter, |
| 151 | + hbox({main_container->Render()}), |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + screen.Loop(main_renderer); |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace module |
0 commit comments