diff --git a/modules/Simulation/OVMSimulator/OVMSimulator.hpp b/modules/Simulation/OVMSimulator/OVMSimulator.hpp index 5582672f14..7d4a71ae70 100644 --- a/modules/Simulation/OVMSimulator/OVMSimulator.hpp +++ b/modules/Simulation/OVMSimulator/OVMSimulator.hpp @@ -24,9 +24,11 @@ struct Conf {}; class OVMSimulator : public Everest::ModuleBase { public: OVMSimulator() = delete; - OVMSimulator(const ModuleInfo& info, std::unique_ptr p_main, Conf& config) : - ModuleBase(info), p_main(std::move(p_main)), config(config){}; + OVMSimulator(const ModuleInfo& info, Everest::MqttProvider& mqtt_provider, + std::unique_ptr p_main, Conf& config) : + ModuleBase(info), mqtt(mqtt_provider), p_main(std::move(p_main)), config(config){}; + Everest::MqttProvider& mqtt; const std::unique_ptr p_main; const Conf& config; diff --git a/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.cpp b/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.cpp index 0cc6ee03cf..e65ab10fe2 100644 --- a/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.cpp +++ b/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.cpp @@ -7,6 +7,22 @@ namespace module { namespace main { void over_voltage_monitorImpl::init() { + over_voltage_monitoring_active = false; + over_voltage_monitorImpl_thread_handle = + std::thread(&over_voltage_monitorImpl::over_voltage_monitorImpl_worker, this); + const std::string OVER_VOLTAGE_TOPIC = "everest_api/" + this->mod->info.id + "/cmd/set_over_voltage_measurement_V"; + EVLOG_info << "Can control the over voltage measurement values over the topic: " << OVER_VOLTAGE_TOPIC + << " via MQTT"; + this->mod->mqtt.subscribe(OVER_VOLTAGE_TOPIC, [this](const std::string& over_voltage_measurement_V) { + try { + EVLOG_info << "Setting simulated over voltage measurement to " << over_voltage_measurement_V + << " V via MQTT"; + voltage_measurement_V = std::stof(over_voltage_measurement_V); + } catch (const std::invalid_argument& e) { + EVLOG_error << "Failed to set over voltage measurement via MQTT, invalid value: " + << over_voltage_measurement_V; + } + }); } void over_voltage_monitorImpl::ready() { @@ -21,6 +37,7 @@ void over_voltage_monitorImpl::handle_set_limits(double& emergency_over_voltage_ void over_voltage_monitorImpl::handle_start() { EVLOG_info << "Over voltage monitoring: starting with " << emergency_threshold << " (emergency) and " << error_threshold << "(error)"; + over_voltage_monitoring_active = true; if (config.simulate_error_shutdown) { std::thread([this]() { std::this_thread::sleep_for(std::chrono::seconds(config.simulate_error_delay)); @@ -42,8 +59,23 @@ void over_voltage_monitorImpl::handle_start() { } } +void over_voltage_monitorImpl::over_voltage_monitorImpl_worker() { + while (true) { + if (this->over_voltage_monitorImpl_thread_handle.shouldExit()) { + break; + } + + if (this->over_voltage_monitoring_active == true) { + this->mod->p_main->publish_voltage_measurement_V(this->voltage_measurement_V); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(this->LOOP_SLEEP_MS)); + } +} + void over_voltage_monitorImpl::handle_stop() { EVLOG_info << "Over voltage monitoring: stopped."; + over_voltage_monitoring_active = false; } void over_voltage_monitorImpl::handle_reset_over_voltage_error() { diff --git a/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp b/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp index 89298f818a..ff636f3d6e 100644 --- a/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp +++ b/modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp @@ -14,6 +14,8 @@ // ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1 // insert your custom include headers here +#include +#include // ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1 namespace module { @@ -57,6 +59,12 @@ class over_voltage_monitorImpl : public over_voltage_monitorImplBase { // insert your private definitions here double error_threshold{0.}; double emergency_threshold{0.}; + + static constexpr int LOOP_SLEEP_MS{20}; + std::atomic over_voltage_monitoring_active; + float voltage_measurement_V{0.0f}; + Everest::Thread over_voltage_monitorImpl_thread_handle; + void over_voltage_monitorImpl_worker(void); // ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1 }; diff --git a/modules/Simulation/OVMSimulator/manifest.yaml b/modules/Simulation/OVMSimulator/manifest.yaml index ed3e8e6c45..a78db56aa5 100644 --- a/modules/Simulation/OVMSimulator/manifest.yaml +++ b/modules/Simulation/OVMSimulator/manifest.yaml @@ -16,6 +16,7 @@ provides: description: Simulate over voltage error N seconds after start of charging type: integer default: 5 +enable_external_mqtt: true metadata: license: https://opensource.org/licenses/Apache-2.0 authors: