eis/eqpalg/utility/HoldTime.cc

105 lines
2.9 KiB
C++
Raw Permalink Normal View History

#include <eqpalg/utility/HoldTime.h>
HoldTime::HoldTime(double time, std::string tagi, std::string var_name)
: hold_time(time),
tagi(tagi),
var_name(var_name),
value(false),
last_value(0) {
last_time = std::chrono::system_clock::now();
}
HoldTime::~HoldTime() {}
bool HoldTime::update_value(double tag_value) {
if (fabs(tag_value - last_value) > std::numeric_limits<double>::epsilon()) {
value = false;
last_time = std::chrono::system_clock::now();
} else {
if (std::chrono::system_clock::now() - last_time >
std::chrono::seconds(int(hold_time * 60))) {
value = true;
} else {
value = false;
}
}
last_value = tag_value;
return value;
}
std::tuple<bool, double, std::string, std::string> HoldTime::find_hold(
std::string exp_str) {
bool flag = false;
double timeM = 0;
std::string tagi = "";
std::string var_name = "";
auto res = exp_str.find("hold");
if (res != std::string::npos) {
auto res2 = exp_str.find("_on_tag");
if (res2 != std::string::npos) {
std::string time_str = exp_str.substr(res + 4, res2 - res);
auto res21 = time_str.find("_");
auto res22 = time_str.find(".");
if (res21 != std::string::npos) {
time_str.replace(res21, res21 + 1, ".");
}
timeM = std::stod(time_str);
auto res3 = exp_str.find("_HE");
if (res3 != std::string::npos) {
tagi = exp_str.substr(res2 + 4, res3 - res2 - 4);
flag = true;
var_name = exp_str.substr(res, res3 + 3 - res);
} else {
timeM = 2;
tagi = "no _HE";
}
if (res22 != std::string::npos) {
timeM = 3;
tagi = "has dot";
flag = false;
}
} else {
timeM = 1;
tagi = "no _on_tag";
}
} else {
timeM = 0;
tagi = "no hold";
}
return std::make_tuple(flag, timeM, tagi, var_name);
}
std::vector<std::string> HoldTime::find_substr(std::string str,
std::string sub_str) {
std::vector<std::string> res;
std::string str1 = str;
auto res1 = find_str(str1, sub_str);
while (res1.first != " ") {
res.push_back(res1.first);
str1 = res1.second;
res1 = find_str(str1, sub_str);
}
return res;
}
std::pair<std::string, std::string> HoldTime::find_str(std::string str,
std::string sub_str) {
if (str == " ") {
return std::make_pair(" ", " ");
}
try {
auto index = str.find(sub_str);
if (index != std::string::npos) {
if (index >= str.size() - 1) {
return std::make_pair(str.substr(0, index + sub_str.size() + 1), " ");
}
return std::make_pair(
str.substr(0, index + sub_str.size()),
str.substr(index + sub_str.size(), str.size() - index));
} else {
return std::make_pair(" ", " ");
}
} catch (const std::exception& e) {
return std::make_pair(" ", " ");
}
}