-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
extutils.cpp
219 lines (198 loc) · 6.33 KB
/
extutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2020-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#include "extutils.hpp"
#include <string.h>
#include <stdio.h>
#include <sys/types.h> // for ssize_t, size_t etc.
#include <sys/stat.h> // for mkdir
#include <math.h> // for fabs
using namespace p44;
#ifndef ESP_PLATFORM
ErrorPtr p44::string_fromfile(const string aFilePath, string &aData)
{
ErrorPtr err;
FILE* f = fopen(aFilePath.c_str(), "r");
if (f==NULL) {
err = SysError::errNo();
}
else {
if (!string_fgetfile(f, aData)) {
err = SysError::errNo();
}
fclose(f);
}
return err;
}
ErrorPtr p44::string_tofile(const string aFilePath, const string &aData)
{
ErrorPtr err;
FILE* f = fopen(aFilePath.c_str(), "w");
if (f==NULL) {
err = SysError::errNo();
}
else {
if (fwrite(aData.c_str(), aData.size(), 1, f)<1) {
err = SysError::errNo();
}
fclose(f);
}
return err;
}
#endif // !ESP_PLATFORM
/// make sure directory exists, otherwise make it (like mkdir -p)
/// @param aDirPath path for directory to create
ErrorPtr p44::ensureDirExists(const string aDirPath, int aMaxDepth, mode_t aCreationMode)
{
int ret = access(aDirPath.c_str(), F_OK);
if (ret==0) return ErrorPtr(); // exists -> fine
if (aMaxDepth==0) {
// cannot create more directories -> not found
return SysError::err(ENOENT);
}
// does not exist
size_t n = aDirPath.find_last_of('/');
if (n!=string::npos && n!=0) { // slash at beginning does not count as dir
// - there is a parent
string parent = aDirPath.substr(0,n);
if (aDirPath.substr(n)=="." || aDirPath.substr(n)=="..") return SysError::err(ENOENT); // do not mess with "." and ".."!
ErrorPtr err = ensureDirExists(parent, aMaxDepth<0 ? aMaxDepth : aMaxDepth-1);
if (Error::notOK(err)) return err; // abort
}
// does not yet exist, create now
return SysError::err(mkdir(aDirPath.c_str(), aCreationMode));
}
// MARK: - WindowEvaluator
WindowEvaluator::WindowEvaluator(MLMicroSeconds aWindowTime, MLMicroSeconds aDataPointCollTime, WinEvalMode aEvalMode) :
mWindowTime(aWindowTime),
mDataPointCollTime(aDataPointCollTime),
mWinEvalMode(aEvalMode)
{
}
void WindowEvaluator::addValue(double aValue, MLMicroSeconds aTimeStamp)
{
if (aTimeStamp==Never) aTimeStamp = MainLoop::now();
// process options
if (mWinEvalMode & eval_option_abs) {
aValue = fabs(aValue);
}
// clean away outdated datapoints
while (!mDataPoints.empty()) {
if (mDataPoints.front().timestamp<aTimeStamp-mWindowTime) {
// this one is outdated (lies more than windowTime in the past), remove it
mDataPoints.pop_front();
}
else {
break;
}
}
// add new value
if (!mDataPoints.empty()) {
// check if we should collect into last existing datapoint
DataPoint &last = mDataPoints.back();
if (mCollStart+mDataPointCollTime>aTimeStamp) {
// still in collection time window (from start of datapoint collection
switch (mWinEvalMode & eval_type_mask) {
case eval_max: {
if (aValue>last.value) last.value = aValue;
break;
}
case eval_min: {
if (aValue<last.value) last.value = aValue;
break;
}
case eval_timeweighted_average: {
MLMicroSeconds timeWeight = aTimeStamp-last.timestamp; // between last subdatapoint collected into this datapoint and new timestamp
if (mCollDivisor<=0 || timeWeight<=0) { // 0 or negative timeweight should not happen, safety only!
// first section
last.value = (last.value + aValue)/2;
mCollDivisor = timeWeight;
}
else {
double v = (last.value*mCollDivisor + aValue*timeWeight);
mCollDivisor += timeWeight;
last.value = v/mCollDivisor;
}
break;
}
case eval_average:
default: {
if (mCollDivisor<=0) mCollDivisor = 1;
double v = (last.value*mCollDivisor+aValue);
mCollDivisor++;
last.value = v/mCollDivisor;
break;
}
}
last.timestamp = aTimeStamp; // timestamp represents most recent sample in datapoint
return; // done
}
}
// accumulation of value in previous datapoint complete (or none available at all)
// -> start new datapoint
DataPoint dp;
dp.value = aValue;
dp.timestamp = aTimeStamp;
mDataPoints.push_back(dp);
mCollStart = aTimeStamp;
mCollDivisor = 0;
}
double WindowEvaluator::evaluate()
{
double result = 0;
double divisor = 0;
int count = 0;
MLMicroSeconds lastTs = Never;
for (DataPointsList::iterator pos = mDataPoints.begin(); pos != mDataPoints.end(); ++pos) {
switch (mWinEvalMode & eval_type_mask) {
case eval_max: {
if (count==0 || pos->value>result) result = pos->value;
divisor = 1;
break;
}
case eval_min: {
if (count==0 || pos->value<result) result = pos->value;
divisor = 1;
break;
}
case eval_timeweighted_average: {
if (count==0) {
// the first datapoint's time weight reaches back to beginning of window
lastTs = mDataPoints.back().timestamp-mWindowTime;
}
MLMicroSeconds timeWeight = pos->timestamp-lastTs;
result += pos->value*timeWeight;
divisor += timeWeight;
// next datapoint's time weight will reach back to this datapoint's time
lastTs = pos->timestamp;
break;
}
case eval_average:
default: {
result += pos->value;
divisor++;
break;
}
}
count++;
}
return divisor!=0 ? result/divisor : 0;
}