Files
data-collection-terminal/opcua-expoter/fault-simulation-algorithm/simulation-manager/simulation_manager.hpp
2024-08-28 09:26:25 +08:00

107 lines
3.4 KiB
C++

//
// Created by fly on 2022/4/21.
//
#ifndef FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP
#define FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP
#include "algorithm_wrap.hpp"
#include <periodic_interference.h>
#include <step.h>
#include <white_noise.h>
#include <temperature_drift.h>
#include <spdlog/spdlog.h>
class simulation_manager {
public:
void add_algorithm(std::string const &name, std::shared_ptr<algorithm> algo) {
if (!algorithms_.contains(name)) {
algorithms_.emplace(name, std::make_shared<algorithm_wrap>(algo));
}
}
void remove_algorithm(std::string const &name) {
algorithms_.erase(name);
}
void set_algorithm_enable(std::string const &name, bool flag) {
if (algorithms_.contains(name)) {
algorithms_[name]->set_enable(flag);
}
}
bool algorithm_enable(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->enable();
}
return false;
}
void set_algorithm_duration_period(std::string const &name, int duration_period) {
if (algorithms_.contains(name)) {
algorithms_[name]->set_duration_period(duration_period);
}
}
int algorithm_duration_period(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->duration_period();
}
return -1;
}
void set_algorithm_config(std::string const &name, nlohmann::json const &config) {
if (!algorithms_.contains(name)) {
spdlog::debug("Create {} Algorithm Object", name);
if ("periodic_interference" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<periodic_interference>());
} else if ("step" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<step>());
} else if ("white_noise" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<white_noise>());
} else if ("temperature_drift" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<temperature_drift>());
} else if ("pulse" == name) {
algorithms_[name] = std::make_shared<algorithm_wrap>(std::make_shared<step>());
}
}
if (algorithms_.contains(name)) {
algorithms_[name]->set_config(config);
}
}
nlohmann::json algorithm_config(std::string const &name) {
if (algorithms_.contains(name)) {
return algorithms_[name]->config();
}
return {};
}
nlohmann::json config() {
nlohmann::json j;
for (auto iter = algorithms_.begin(); iter != algorithms_.end(); ++iter) {
j[iter->first] = iter->second->config();
}
return j;
}
void set_config(nlohmann::json const &c) {
for (auto iter = c.begin(); iter != c.end(); ++iter) {
set_algorithm_config(iter.key(), iter.value());
}
}
double eval(double value) {
for (auto &iter: algorithms_) {
value = iter.second->eval(value);
}
return value;
}
private:
std::map<std::string, std::shared_ptr<algorithm_wrap>> algorithms_;
};
#endif //FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP