42{
44
45
46
47
48 std::vector<char> buf;
49 buf.insert(buf.end(), condition, condition+strlen(condition)+1);
50 char*
str = buf.data();
51
52
53 char op[3];
54 op[0] = op[1] = op[2] = 0;
55 for (
i = strlen(
str) - 1;
i > 0;
i--)
56 if (strchr(
"<>=!&",
str[
i]) != NULL)
57 break;
59 for (
j = 1;
str[
i +
j] ==
' ';
j++)
60 ;
61
62 std::string value2_str =
str +
i +
j;
63 double value2 = atof(value2_str.c_str());
64
66
67 if (
i > 0 && strchr(
"<>=!&",
str[
i - 1])) {
68 op[1] = op[0];
71 }
72
74 while (
i > 0 &&
str[
i] ==
' ')
77
78
79 std::string function;
82 if (strchr(
str,
'(')) {
83 *strchr(
str,
'(') = 0;
89 }
90 }
91
92 std::vector<int> indices;
93 bool index_present = false;
94
95
96 if (strchr(
str,
'[') &&
str[strlen(
str)-1] ==
']') {
97 index_present = true;
98
99
100 std::string cleanInput = strchr(
str,
'[') + 1;
101 cleanInput.pop_back();
102
103
104 *strchr(
str,
'[') = 0;
105
106
108 cm_msg(
MERROR,
"al_evaluate_condition",
"Alarm \"%s\": Cannot find ODB key \"%s\" to evaluate alarm condition", alarm_name,
str);
109 if (pvalue)
110 *pvalue = "(cannot find in odb)";
112 }
114
115
116 std::stringstream ss(cleanInput);
117 std::string token;
118 while (std::getline(ss, token, ',')) {
119
120 token.erase(std::remove_if(token.begin(), token.end(), ::isspace), token.end());
121
122
123 if (token == "*") {
124 for (
int i = 0;
i <
k.get_num_values(); ++
i)
125 indices.push_back(
i);
126 } else {
127
128 auto dashPos = token.find('-');
129 if (dashPos != std::string::npos) {
130
131 int start = std::stoi(token.substr(0, dashPos));
132 int end = std::stoi(token.substr(dashPos + 1));
133
134
135 for (
int i = start;
i <=
end; ++
i) {
136 indices.push_back(
i);
137 }
138 } else {
139
140 indices.push_back(std::stoi(token));
141 }
142 }
143 }
144 }
145
149 if (!hkey) {
150 cm_msg(
MERROR,
"al_evaluate_condition",
"Alarm \"%s\": Cannot find ODB key \"%s\" to evaluate alarm condition", alarm_name,
str);
151 if (pvalue)
152 *pvalue = "(cannot find in odb)";
154 }
155
158
159
160
161
162 if (indices.empty())
163 indices.push_back(0);
164
165
166 for (int idx : indices) {
167 std::string value1_str;
168 double value1 = 0;
169
171
175 value1 = atof(value1_str.c_str());
176 }
else if (
equal_ustring(function.c_str(),
"access_running")) {
177
181 size =
sizeof(
state);
184 value1_str =
msprintf(
"%d", dtime).c_str();
185 value1 = atof(value1_str.c_str());
186 } else {
187 value1_str = "0";
188 value1 = 0;
189 }
190 } else {
191
194 cm_msg(
MERROR,
"al_evaluate_condition",
"Alarm \"%s\": Cannot get ODB key \"%s\" to evaluate alarm condition", alarm_name,
str);
195 if (pvalue)
196 *pvalue = "(odb error)";
198 } else {
203 cm_msg(
MERROR,
"al_evaluate_condition",
"Alarm \"%s\": Cannot get ODB value \"%s\" index %d to evaluate alarm condition", alarm_name,
str, idx);
204 if (pvalue)
205 *pvalue = "(odb error)";
207 }
208
210 value1 = atof(value1_str.c_str());
211 }
212 }
213
214
216 value1 = (value1_str[0] == 'Y' || value1_str[0] == 'y' || value1_str[0] == '1');
217 value2 = (value2_str[0] == 'Y' || value2_str[0] == 'y' || value2_str[0] == '1');
218 }
219
220
221 if (pvalue) {
222 if (index_present)
223 *pvalue =
msprintf(
"[%d] %s", idx, value1_str.c_str());
224 else
225 *pvalue = value1_str;
226 }
227
228
229 if (strcmp(op, "=") == 0)
230 if (value1 == value2)
232 if (strcmp(op, "==") == 0)
233 if (value1 == value2)
235 if (strcmp(op, "!=") == 0)
236 if (value1 != value2)
238 if (strcmp(op, "<") == 0)
239 if (value1 < value2)
241 if (strcmp(op, ">") == 0)
242 if (value1 > value2)
244 if (strcmp(op, "<=") == 0)
245 if (value1 <= value2)
247 if (strcmp(op, ">=") == 0)
248 if (value1 >= value2)
250 if (strcmp(op, "&") == 0)
251 if (((unsigned int) value1 & (unsigned int) value2) > 0)
253 }
254
256}
static bool exists(const std::string &name)
INT db_get_data_index(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, INT idx, DWORD type)
INT db_sprintf(char *string, const void *data, INT data_size, INT idx, DWORD type)
INT db_get_key_time(HNDLE hDB, HNDLE hKey, DWORD *delta)