This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/api/DummyAPI.h
2016-09-07 11:51:56 +02:00

101 lines
1.8 KiB
C++

#ifndef DUMMYAPI_H
#define DUMMYAPI_H
#include "api.h"
class DummyAPI : public API {
private:
/** all attached listeners */
std::vector<APIListener*> listeners;
int dstID = -1;
bool enabled;
std::thread thread;
std::string dataFolder;
float speed = 1.0f;
std::vector<Point3> points;
public:
/** ctor with the folder where the data-files reside */
DummyAPI(const std::string& dataFolder) : dataFolder(dataFolder) {
;
}
void setSpeed(const float speed) {
this->speed = speed;
}
void setTarget(const Point3 p) override {
(void) p;
throw Exception("not yet implemented!");
}
void setTarget(const int id) override {
const std::string fileName = dataFolder + "/" + std::to_string(id) + ".dat";
std::ifstream in(fileName);
if (id == -1) {throw Exception("call setTarget(id) first!");}
if (!in.good()) {throw Exception("failed to load file: '" + fileName + "'");}
// load
while (in.good() && !in.eof()) {
float x, y, z;
in >> x;
in >> y;
in >> z;
points.push_back(Point3(x,y,z));
}
}
void startNavigation() override {
enabled = true;
dummySender();
}
void stopNavigation() override {
enabled = false;
thread.join();
}
void addListener(APIListener* l) override {
listeners.push_back(l);
}
Point3 getDst() {
return points[points.size() - 2];
}
private:
/** send dummy data using the given file */
void dummySender() {
// run-loop
auto run = [&] () {
std::vector<Point3> pts = points; // local copy
while(!pts.empty() && enabled) {
Point3 cur = pts.front();
pts.erase(pts.begin());
for (APIListener* l : listeners) {l->onPositionUpdate(cur);}
std::this_thread::sleep_for(std::chrono::milliseconds( (int)(500/speed) ));
}
};
// run
thread = std::thread(run);
}
};
#endif // DUMMYAPI_H