From 7ac3d305e54de3ba384a16e79459376c49c93c30 Mon Sep 17 00:00:00 2001 From: MarzellT Date: Thu, 12 Sep 2024 15:06:25 +0200 Subject: [PATCH] some cleanup Signed-off-by: MarzellT --- modules/YetiSimulator/YetiSimulator.cpp | 2 + modules/YetiSimulator/util/mqtt_handler.cpp | 1 - modules/YetiSimulator/util/simulator.hpp | 11 +++-- modules/YetiSimulator/util/state.cpp | 50 +++++++++++--------- modules/YetiSimulator/util/state.hpp | 52 +++++++++++---------- modules/YetiSimulator/util/util.cpp | 11 +++-- modules/YetiSimulator/util/util.hpp | 11 +++-- 7 files changed, 76 insertions(+), 62 deletions(-) diff --git a/modules/YetiSimulator/YetiSimulator.cpp b/modules/YetiSimulator/YetiSimulator.cpp index 02ec70147..e54b784dd 100644 --- a/modules/YetiSimulator/YetiSimulator.cpp +++ b/modules/YetiSimulator/YetiSimulator.cpp @@ -12,7 +12,9 @@ void YetiSimulator::init() { invoke_init(*p_ev_board_support); invoke_init(*p_rcd); invoke_init(*p_connector_lock); + clear_data(); + mqtt_handler = std::make_unique(p_board_support.get(), p_rcd.get(), p_connector_lock.get()); mqtt.subscribe("everest_external/nodered/" + std::to_string(config.connector_id) + "/carsim/error", [this](const std::string& payload) { mqtt_handler->handle_mqtt_payload(payload); }); diff --git a/modules/YetiSimulator/util/mqtt_handler.cpp b/modules/YetiSimulator/util/mqtt_handler.cpp index 5d48f16a0..4537d7844 100644 --- a/modules/YetiSimulator/util/mqtt_handler.cpp +++ b/modules/YetiSimulator/util/mqtt_handler.cpp @@ -4,7 +4,6 @@ #include "mqtt_handler.hpp" #include "everest/logging.hpp" #include "nlohmann/json.hpp" -#include namespace module { diff --git a/modules/YetiSimulator/util/simulator.hpp b/modules/YetiSimulator/util/simulator.hpp index d8ce92233..7dc6a285f 100644 --- a/modules/YetiSimulator/util/simulator.hpp +++ b/modules/YetiSimulator/util/simulator.hpp @@ -21,7 +21,7 @@ template class Simulator { std::thread(&Simulator::run_simulation, this, sleep_time_ms, yeti_simulator).detach(); } - void run_simulation(int sleep_time_ms, YetiSimulatorT* yeti_simulator) { + [[noreturn]] void run_simulation(int sleep_time_ms, YetiSimulatorT* yeti_simulator) { auto& module_state = yeti_simulator->get_module_state(); while (true) { if (module_state.simulation_enabled) { @@ -83,9 +83,9 @@ template class Simulator { auto& module_state = yeti_simulator->get_module_state(); - auto amps1 = 0.0; - auto amps2 = 0.0; - auto amps3 = 0.0; + auto amps1 = double{}; + auto amps2 = double{}; + auto amps3 = double{}; auto hlc_active = false; if (module_state.pwm_duty_cycle >= 0.03 && module_state.pwm_duty_cycle <= 0.07) @@ -515,10 +515,12 @@ template class Simulator { } } + // NOLINTNEXTLINE void publish_event(evse_board_supportImplBase& board_support, state::State event) { board_support.publish_event(event_to_enum(event)); } + // NOLINTNEXTLINE static types::board_support_common::BspEvent event_to_enum(state::State event) { using state::State; using types::board_support_common::Event; @@ -548,6 +550,7 @@ template class Simulator { } } + // NOLINTNEXTLINE static std::string event_to_string(state::State state) { using state::State; diff --git a/modules/YetiSimulator/util/state.cpp b/modules/YetiSimulator/util/state.cpp index 0c9bc2117..c79c3fb3c 100644 --- a/modules/YetiSimulator/util/state.cpp +++ b/modules/YetiSimulator/util/state.cpp @@ -3,10 +3,13 @@ #include "state.hpp" #include +#include #include namespace module::state { -using namespace std::chrono; +using std::chrono::milliseconds; +using std::chrono::system_clock; +using std::chrono::time_point_cast; PowermeterData::PowermeterData() : time_stamp{time_point_cast(system_clock::now()).time_since_epoch().count() / 1000} { @@ -39,31 +42,32 @@ std::string state_to_string(state::ModuleState& module_state) { return ""; } } -void to_json(nlohmann::json& j, const PowermeterData& powermeter_data) { - j = nlohmann::json{{"time_stamp", powermeter_data.time_stamp}, - {"totalWattHr", powermeter_data.totalWattHr}, - {"wattL1", powermeter_data.wattL1}, - {"vrmsL1", powermeter_data.vrmsL1}, - {"irmsL1", powermeter_data.irmsL1}, - {"wattHrL1", powermeter_data.wattHrL1}, - {"tempL1", powermeter_data.tempL1}, - {"freqL1", powermeter_data.freqL1}, - {"wattL2", powermeter_data.wattL2}, - {"vrmsL2", powermeter_data.vrmsL2}, - {"irmsL2", powermeter_data.irmsL2}, - {"wattHrL2", powermeter_data.wattHrL2}, - {"tempL2", powermeter_data.tempL2}, - {"freqL2", powermeter_data.freqL2}, +void to_json(nlohmann::json& json, const PowermeterData& powermeter_data) { + json = nlohmann::json{{"time_stamp", powermeter_data.time_stamp}, + {"totalWattHr", powermeter_data.totalWattHr}, + {"wattL1", powermeter_data.wattL1}, + {"vrmsL1", powermeter_data.vrmsL1}, + {"irmsL1", powermeter_data.irmsL1}, + {"wattHrL1", powermeter_data.wattHrL1}, + {"tempL1", powermeter_data.tempL1}, + {"freqL1", powermeter_data.freqL1}, - {"wattL3", powermeter_data.wattL3}, - {"vrmsL3", powermeter_data.vrmsL3}, - {"irmsL3", powermeter_data.irmsL3}, - {"wattHrL3", powermeter_data.wattHrL3}, - {"tempL3", powermeter_data.tempL3}, - {"freqL3", powermeter_data.freqL3}, + {"wattL2", powermeter_data.wattL2}, + {"vrmsL2", powermeter_data.vrmsL2}, + {"irmsL2", powermeter_data.irmsL2}, + {"wattHrL2", powermeter_data.wattHrL2}, + {"tempL2", powermeter_data.tempL2}, + {"freqL2", powermeter_data.freqL2}, - {"irmsN", powermeter_data.irmsN}}; + {"wattL3", powermeter_data.wattL3}, + {"vrmsL3", powermeter_data.vrmsL3}, + {"irmsL3", powermeter_data.irmsL3}, + {"wattHrL3", powermeter_data.wattHrL3}, + {"tempL3", powermeter_data.tempL3}, + {"freqL3", powermeter_data.freqL3}, + + {"irmsN", powermeter_data.irmsN}}; } } // namespace module::state \ No newline at end of file diff --git a/modules/YetiSimulator/util/state.hpp b/modules/YetiSimulator/util/state.hpp index ccd3d09c2..9f81ae63b 100644 --- a/modules/YetiSimulator/util/state.hpp +++ b/modules/YetiSimulator/util/state.hpp @@ -1,16 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright Pionix GmbH and Contributors to EVerest -#pragma once +#ifndef EVEREST_CORE_MODULES_YETISIMULATOR_UTIL_STATE_HPP +#define EVEREST_CORE_MODULES_YETISIMULATOR_UTIL_STATE_HPP +// NOLINTBEGIN: ignore things like public access or magic values #include "nlohmann/json.hpp" #include -namespace module { -namespace state { + +namespace module::state { struct PowermeterData { PowermeterData(); - long time_stamp; + int64_t time_stamp; double totalWattHr = 0.0; @@ -114,21 +116,21 @@ struct WattHr { struct TelemetryData { struct PowerPathControllerVersion { - std::string timestamp = ""; + std::string timestamp; std::string type = "power_path_controller_version"; int hardware_version = 3; std::string software_version = "1.01"; std::string date_manufactured = "20220304"; - long operating_time_h = 2330; - long operating_time_h_warning = 5000; - long operating_time_h_error = 6000; + int64_t operating_time_h = 2330; + int64_t operating_time_h_warning = 5000; + int64_t operating_time_h_error = 6000; bool error = false; }; PowerPathControllerVersion power_path_controller_version; struct PowerPathController { - std::string timestamp = ""; + std::string timestamp; std::string type = "power_path_controller"; double cp_voltage_high = 0.0; double cp_voltage_low = 0.0; @@ -139,21 +141,21 @@ struct TelemetryData { double supply_voltage_minus_12V = -11.9; double temperature_controller = 33; double temperature_car_connector = 65; - long watchdog_reset_count = 1; + int64_t watchdog_reset_count = 1; bool error = false; }; PowerPathController power_path_controller; struct PowerSwitch { - std::string timestamp = ""; + std::string timestamp; std::string type = "power_switch"; - long switching_count = 0; - long switching_count_warning = 30000; - long switching_count_error = 50000; + int64_t switching_count = 0; + int64_t switching_count_warning = 30000; + int64_t switching_count_error = 50000; bool is_on = false; - long time_to_switch_on_ms = 110; - long time_to_switch_off_ms = 100; + int64_t time_to_switch_on_ms = 110; + int64_t time_to_switch_off_ms = 100; double temperature_C = 20; bool error = false; bool error_over_current = false; @@ -162,7 +164,7 @@ struct TelemetryData { PowerSwitch power_switch; struct Rcd { - std::string timestamp = ""; + std::string timestamp; std::string type = "rcd"; bool enabled = true; double current_mA = 2.5; @@ -193,14 +195,14 @@ struct ModuleState { SimdataSetting simdata_setting; TelemetryData telemetry_data; - long pubCnt = 0; + int64_t pubCnt = 0; bool power_on_allowed = false; bool relais_on = false; State current_state = State::STATE_DISABLED; State last_state = State::STATE_DISABLED; - long time_stamp; + int64_t time_stamp; bool use_three_phases = true; bool simplified_mode = false; @@ -219,11 +221,11 @@ struct ModuleState { double pwm_voltage_lo = 12.1; std::string country_code = "DE"; - long last_pwm_update = 0; + int64_t last_pwm_update = 0; WattHr watt_hr; - long powermeter_sim_last_time_stamp = 0L; + int64_t powermeter_sim_last_time_stamp = 0L; double ev_max_current = 0.0; int ev_three_phases = 3; @@ -231,7 +233,9 @@ struct ModuleState { std::string state_to_string(state::ModuleState& module_state); -void to_json(nlohmann::json& j, const PowermeterData& powermeter_data); +void to_json(nlohmann::json& json, const PowermeterData& powermeter_data); + +} // namespace module::state -} // namespace state -} // namespace module +#endif +// NOLINTEND diff --git a/modules/YetiSimulator/util/util.cpp b/modules/YetiSimulator/util/util.cpp index 638bbbf94..cc95d15b7 100644 --- a/modules/YetiSimulator/util/util.cpp +++ b/modules/YetiSimulator/util/util.cpp @@ -3,20 +3,21 @@ #include "util.hpp" #include +#include #include #include +#include -namespace module { -namespace util { +namespace module::util { std::string get_current_iso_time_string() { - using namespace std::chrono; + using std::chrono::system_clock; const auto date = system_clock::to_time_t(system_clock::now()); auto string_stream = std::stringstream{}; + // NOLINTNEXTLINE(concurrency-mt-unsafe) string_stream << std::put_time(gmtime(&date), "%F:%T%Z"); const auto iso_time_string = string_stream.str(); return iso_time_string; } -} // namespace util -} // namespace module \ No newline at end of file +} // namespace module::util \ No newline at end of file diff --git a/modules/YetiSimulator/util/util.hpp b/modules/YetiSimulator/util/util.hpp index db950b166..4afba2be8 100644 --- a/modules/YetiSimulator/util/util.hpp +++ b/modules/YetiSimulator/util/util.hpp @@ -1,13 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright Pionix GmbH and Contributors to EVerest -#pragma once +#ifndef EVEREST_CORE_MODULES_YETISIMULATOR_UTIL_UTIL_HPP +#define EVEREST_CORE_MODULES_YETISIMULATOR_UTIL_UTIL_HPP #include -namespace module { -namespace util { +namespace module::util { std::string get_current_iso_time_string(); -} // namespace util -} // namespace module +} // namespace module::util + +#endif