0.4.1
Loading...
Searching...
No Matches
WiFiObsFile.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "WiFiObsFile.hpp"
10
11#include "util/Logger.hpp"
12
14
16namespace nm = NAV::NodeManager;
18
20
22 : Node(typeStatic())
23{
24 LOG_TRACE("{}: called", name);
25
26 _hasConfig = true;
27 _guiConfigDefaultWindowSize = { 488, 248 };
28
30 // nm::CreateOutputPin(this, "Header Columns", Pin::Type::Object, { "std::vector<std::string>" }, &_headerColumns);
31}
32
34{
35 LOG_TRACE("{}: called", nameId());
36}
37
39{
40 return "WiFiObsFile";
41}
42
43std::string NAV::WiFiObsFile::type() const
44{
45 return typeStatic();
46}
47
49{
50 return "Data Provider";
51}
52
54{
55 if (auto res = FileReader::guiConfig(".csv,.*", { ".csv" }, size_t(id), nameId()))
56 {
57 LOG_DEBUG("{}: Path changed to {}", nameId(), _path);
59 if (res == FileReader::PATH_CHANGED)
60 {
62 }
63 else
64 {
66 }
67 }
68}
69
70[[nodiscard]] json NAV::WiFiObsFile::save() const
71{
72 LOG_TRACE("{}: called", nameId());
73
74 json j;
75
76 j["FileReader"] = FileReader::save();
77
78 return j;
79}
80
82{
83 LOG_TRACE("{}: called", nameId());
84
85 if (j.contains("FileReader"))
86 {
87 FileReader::restore(j.at("FileReader"));
88 }
89}
90
92{
93 LOG_TRACE("{}: called", nameId());
94
96}
97
99{
100 LOG_TRACE("{}: called", nameId());
101
103}
104
106{
108
109 return true;
110}
111
112std::shared_ptr<const NAV::NodeData> NAV::WiFiObsFile::pollData()
113{
114 auto obs = std::make_shared<WiFiObs>();
115
116 // Read line
117 std::string line;
118 getline(line);
119 // Remove any starting non text characters
120 line.erase(line.begin(), std::ranges::find_if(line, [](int ch) { return std::isgraph(ch); }));
121
122 if (line.empty())
123 {
124 return nullptr;
125 }
126
127 // Convert line into stream
128 std::stringstream lineStream(line);
129 std::string cell;
130
131 uint16_t gpsCycle = 0;
132 uint16_t gpsWeek = 0;
133 long double gpsToW = 0.0;
134 InsTime time;
135
136 bool gpsCycleSet = false;
137 bool gpsWeekSet = false;
138 bool gpsToWSet = false;
139 bool macAddressSet = false;
140 bool distanceSet = false;
141 bool distanceStdSet = false;
142
143 // Split line at comma
144 for (const auto& column : _headerColumns)
145 {
146 if (std::getline(lineStream, cell, ','))
147 {
148 // Remove any trailing non text characters
149 cell.erase(std::ranges::find_if(cell, [](int ch) { return std::iscntrl(ch); }), cell.end());
150 if (cell.empty())
151 {
152 continue;
153 }
154
155 if (column == "GpsCycle")
156 {
157 gpsCycle = static_cast<uint16_t>(std::stoul(cell));
158 gpsCycleSet = true;
159 }
160 else if (column == "GpsWeek")
161 {
162 gpsWeek = static_cast<uint16_t>(std::stoul(cell));
163 gpsWeekSet = true;
164 }
165 else if (column == "GpsToW [s]")
166 {
167 gpsToW = std::stold(cell);
168 gpsToWSet = true;
169 }
170 else if (column == "MacAddress")
171 {
172 obs->macAddress = cell;
173 macAddressSet = true;
174 }
175 else if (column == "Distance [m]")
176 {
177 obs->distance = std::stod(cell);
178 distanceSet = true;
179 }
180 else if (column == "DistanceStd [m]")
181 {
182 obs->distanceStd = std::stod(cell);
183 distanceStdSet = true;
184 }
185 }
186 }
187
188 if (!gpsCycleSet || !gpsWeekSet || !gpsToWSet || !macAddressSet || !distanceSet || !distanceStdSet)
189 {
190 LOG_ERROR("{}: Not all columns are set", nameId());
191 return nullptr;
192 }
193 time = InsTime(gpsCycle, gpsWeek, gpsToW);
194 obs->insTime = time;
196 return obs;
197}
Save/Load the Nodes.
nlohmann::json json
json namespace
Utility class for logging to console and file.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_ERROR
Error occurred, which stops part of the program to work, but not everything.
Definition Logger.hpp:73
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Manages all Nodes.
Keeps track of the current real/simulation time.
WiFiObs File Reader.
Espressif Observation Class.
bool initialize()
Initialize the file reader.
void restore(const json &j)
Restores the node from a json object.
std::string _path
Path to the file.
@ PATH_CHANGED
The path changed and exists.
std::vector< std::string > _headerColumns
Header Columns of a CSV file.
GuiResult guiConfig(const char *vFilters, const std::vector< std::string > &extensions, size_t id, const std::string &nameId)
ImGui config.
void resetReader()
Moves the read cursor to the start.
auto & getline(std::string &str)
Reads a line from the filestream.
json save() const
Saves the node into a json object.
void deinitialize()
Deinitialize the file reader.
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
bool doDeinitialize(bool wait=false)
Asks the node worker to deinitialize the node.
Definition Node.cpp:395
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
Node(std::string name)
Constructor.
Definition Node.cpp:30
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
bool doReinitialize(bool wait=false)
Asks the node worker to reinitialize the node.
Definition Node.cpp:350
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:180
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
static constexpr size_t OUTPUT_PORT_INDEX_WiFiObs_OBS
Flow (WiFiObs)
std::shared_ptr< const NodeData > pollData()
Polls data from the file.
json save() const override
Saves the node into a json object.
void deinitialize() override
Deinitialize the node.
WiFiObsFile()
Default constructor.
bool initialize() override
Initialize the node.
void restore(const json &j) override
Restores the node from a json object.
std::string type() const override
String representation of the Class Type.
void guiConfig() override
ImGui config window which is shown on double click.
static std::string typeStatic()
String representation of the Class Type.
bool resetNode() override
Resets the node. Moves the read cursor to the start.
static std::string category()
String representation of the Class Category.
~WiFiObsFile() override
Destructor.
static std::string type()
Returns the type of the data class.
Definition WiFiObs.hpp:27
OutputPin * CreateOutputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
void ApplyChanges()
Signals that there have been changes to the flow.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52