eis/TestProject/play_gruond/HoldTime.cc

53 lines
1.4 KiB
C++

#pragma once
#include "HoldTime.h"
HoldTime::HoldTime(double time) : hold_time(time), value(false), last_value(0) {
last_time = std::chrono::system_clock::now();
}
void 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;
}
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) {
timeM = std::stod(exp_str.substr(res + 4, res2 - res));
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";
}
} else {
timeM = 1;
tagi = "no _on_tag";
}
} else {
timeM = 0;
tagi = "no hold";
}
return std::make_tuple(flag, timeM, tagi, var_name);
}