AverageAnalyzer.cpp 1.33 KB
Newer Older
1 2 3 4 5 6
//
// Created by Netti, Alessio on 16.01.19.
//

#include "AverageAnalyzer.h"

Alessio Netti's avatar
Alessio Netti committed
7
AverageAnalyzer::AverageAnalyzer(const std::string& name) : AnalyzerTemplate(name) { _window = 0; }
8 9 10 11 12 13

AverageAnalyzer::~AverageAnalyzer() {
    if(_buffer)
        delete _buffer;
}

14 15 16 17 18
void AverageAnalyzer::printConfig(LOG_LEVEL ll) {
    LOG_VAR(ll) << "            Window:          " << _window;
    AnalyzerTemplate<SensorBase>::printConfig(ll);
}

19
void AverageAnalyzer::compute(int unitID) {
Alessio Netti's avatar
Alessio Netti committed
20
    long long max=0, sum=0, avg=0;
21 22

    for(const auto& in : _units[unitID]->getInputs()) {
Alessio Netti's avatar
Alessio Netti committed
23 24
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
25
        if(_buffer->empty()) {
Alessio Netti's avatar
Alessio Netti committed
26
            LOG(error) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
27 28 29
            return;
        }

Alessio Netti's avatar
Alessio Netti committed
30 31 32 33 34
        for(const auto& v : *_buffer) {
            sum += v.value;
            if (v.value > max)
                max = v.value;
        }
35 36 37 38 39 40 41 42 43 44 45
    }

    reading_t out;
    out.timestamp = getTimestamp();

    out.value = sum;
    _units[unitID]->getOutputs()[0]->storeReading(out);

    out.value = max;
    _units[unitID]->getOutputs()[1]->storeReading(out);

Alessio Netti's avatar
Alessio Netti committed
46 47
    out.value = sum > 0 ? sum / _units[unitID]->getInputs().size() : 0;
    _units[unitID]->getOutputs()[2]->storeReading(out);
48

Alessio Netti's avatar
Alessio Netti committed
49
}