Files
data-collection-terminal/opcua-exporter/fault-simulation-algorithm/simulation-manager/algorithm_wrap.hpp
2024-09-04 10:25:47 +08:00

69 lines
1.6 KiB
C++

//
// Created by fly on 2022/4/21.
//
#ifndef FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP
#define FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP
#include <algorithm.hpp>
#include <utility>
class algorithm_wrap : public algorithm {
public:
explicit algorithm_wrap(std::shared_ptr<algorithm> algo) {
algorithm_ = std::move(algo);
}
bool enable() const {
return enable_;
}
void set_enable(bool flag) {
enable_ = flag;
}
int duration_period() const {
return duration_period_;
}
void set_duration_period(int durationPeriod) {
duration_period_ = durationPeriod;
}
void set_config(nlohmann::json const &config) override {
if (algorithm_) {
algorithm_->set_config(config);
if (config.contains("enable") && config["enable"].is_boolean()) {
set_enable(config["enable"].get<bool>());
}
}
}
nlohmann::json config() override {
nlohmann::json config;
config["enable"] = enable();
if (algorithm_) {
return algorithm_->config();
}
return config;
}
double eval(double value) override {
if (algorithm_ && enable_ && duration_period_) {
if (duration_period_ > 0) {
--duration_period_;
}
return algorithm_->eval(value);
}
return value;
}
private:
std::shared_ptr<algorithm> algorithm_;
bool enable_ = false;
int duration_period_ = -1;
};
#endif //FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP