Skip to content

Commit 3b2375c

Browse files
committed
WIF - Reporters - Introduce unirec reporter
TG-35
1 parent a317759 commit 3b2375c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @file
3+
* @author Richard Plny <[email protected]>
4+
* @brief Unirec reporter interface
5+
*
6+
* SPDX-License-Identifier: BSD-3-Clause
7+
*/
8+
9+
#pragma once
10+
11+
#include "wif/reporters/reporter.hpp"
12+
13+
#include <unirec++/unirec.hpp>
14+
15+
namespace WIF {
16+
17+
/**
18+
* @brief Unirec Reporter class
19+
*
20+
* Reporter specialization which sends data in UniRec format to Trap interface
21+
*/
22+
class UnirecReporter : public Reporter {
23+
public:
24+
/**
25+
* @brief Construct a new Unirec Reporter object
26+
*
27+
* @param outputInterface output interface where data will be sent
28+
*/
29+
UnirecReporter(Nemea::UnirecOutputInterface& outputInterface);
30+
31+
/**
32+
* @brief Update UniRec field IDs
33+
* Should be called after UniRec template was changed
34+
*
35+
* @param unirecFieldIDs
36+
*/
37+
void updateUnirecFieldIDs(const std::vector<ur_field_id_t>& unirecFieldIDs);
38+
39+
/*! @copydoc Reporter::onRecordStart()
40+
*/
41+
virtual void onRecordStart() override;
42+
43+
/*! @copydoc Reporter::report(const DataVariant& data)
44+
* Data is stored to the UniRec record to the next ID
45+
*/
46+
void report(const DataVariant& data) override;
47+
48+
/*! @copydoc Reporter::onRecordEnd()
49+
* Current Unirec Record is sent to Unirec Output Interface
50+
*/
51+
virtual void onRecordEnd() override;
52+
53+
/*! @copydoc Reporter::flush()
54+
* Internally calls Nemea::UnirecOutputInterface::sendFlush()
55+
*/
56+
virtual void flush() override;
57+
58+
private:
59+
unsigned m_currentUnirecID = 0;
60+
std::vector<ur_field_id_t> m_unirecFieldIDs;
61+
Nemea::UnirecOutputInterface& m_outputInterface;
62+
};
63+
64+
} // namespace WIF

src/wif/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(LIBWIF_SOURCES
1414

1515
if(BUILD_WITH_UNIREC)
1616
list(APPEND LIBWIF_SOURCES
17+
reporters/unirecReporter.cpp
1718
)
1819
endif()
1920

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file
3+
* @author Richard Plny <[email protected]>
4+
* @brief Unirec reporter implementation
5+
*
6+
* SPDX-License-Identifier: BSD-3-Clause
7+
*/
8+
9+
#include "wif/reporters/unirecReporter.hpp"
10+
11+
namespace WIF {
12+
13+
UnirecReporter::UnirecReporter(Nemea::UnirecOutputInterface& outputInterface)
14+
: m_outputInterface(outputInterface)
15+
{
16+
}
17+
18+
void UnirecReporter::updateUnirecFieldIDs(const std::vector<ur_field_id_t>& unirecFieldIDs)
19+
{
20+
m_unirecFieldIDs = unirecFieldIDs;
21+
}
22+
23+
void UnirecReporter::onRecordStart()
24+
{
25+
m_currentUnirecID = 0;
26+
}
27+
28+
void UnirecReporter::report(const DataVariant& data)
29+
{
30+
ur_field_id_t fieldID = m_unirecFieldIDs[m_currentUnirecID++];
31+
auto& record = m_outputInterface.getUnirecRecord();
32+
33+
if (std::holds_alternative<uint8_t>(data)) {
34+
auto value = std::get<uint8_t>(data);
35+
record.setFieldFromType<uint8_t>(value, fieldID);
36+
} else if (std::holds_alternative<uint16_t>(data)) {
37+
auto value = std::get<uint16_t>(data);
38+
record.setFieldFromType<uint16_t>(value, fieldID);
39+
} else if (std::holds_alternative<uint32_t>(data)) {
40+
auto value = std::get<uint32_t>(data);
41+
record.setFieldFromType<uint32_t>(value, fieldID);
42+
} else if (std::holds_alternative<uint64_t>(data)) {
43+
auto value = std::get<uint64_t>(data);
44+
record.setFieldFromType<uint64_t>(value, fieldID);
45+
} else if (std::holds_alternative<double>(data)) {
46+
auto value = std::get<double>(data);
47+
record.setFieldFromType<double>(value, fieldID);
48+
} else if (std::holds_alternative<std::string>(data)) {
49+
auto value = std::get<std::string>(data);
50+
record.setFieldFromType<std::string>(value, fieldID);
51+
} else if (std::holds_alternative<IpAddress>(data)) {
52+
auto value = std::get<IpAddress>(data);
53+
Nemea::IpAddress nemeaIp(value.toString());
54+
record.setFieldFromType<Nemea::IpAddress>(nemeaIp, fieldID);
55+
} else if (std::holds_alternative<std::vector<double>>(data)) {
56+
auto value = std::get<std::vector<double>>(data);
57+
record.setFieldFromVector<double>(value, fieldID);
58+
}
59+
}
60+
61+
void UnirecReporter::onRecordEnd()
62+
{
63+
m_outputInterface.send(m_outputInterface.getUnirecRecord());
64+
}
65+
66+
void UnirecReporter::flush()
67+
{
68+
m_outputInterface.sendFlush();
69+
}
70+
71+
} // namespace WIF

0 commit comments

Comments
 (0)