initial version
This commit is contained in:
22
sensors/AccelerometerSensor.h
Normal file
22
sensors/AccelerometerSensor.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef ACCELEROMETERSENSOR_H
|
||||
#define ACCELEROMETERSENSOR_H
|
||||
|
||||
#include "Sensor.h"
|
||||
|
||||
struct AccelerometerData {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
AccelerometerData(const float x, const float y, const float z) : x(x), y(y), z(z) {;}
|
||||
std::string asString() const {
|
||||
std::stringstream ss;
|
||||
ss << "(" << x << "," << y << "," << z << ")";
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
class AccelerometerSensor : public Sensor<AccelerometerData> {
|
||||
|
||||
};
|
||||
|
||||
#endif // ACCELEROMETERSENSOR_H
|
||||
50
sensors/Sensor.h
Normal file
50
sensors/Sensor.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef SENSOR_H
|
||||
#define SENSOR_H
|
||||
|
||||
#include <vector>
|
||||
#include <QObject>
|
||||
|
||||
/** listen for sensor events */
|
||||
template <typename T> class SensorListener {
|
||||
|
||||
public:
|
||||
|
||||
/** incoming sensor data */
|
||||
virtual void onSensorData(const T& data) = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/** base-class for all sensors */
|
||||
template <typename T> class Sensor {
|
||||
|
||||
private:
|
||||
|
||||
std::vector<SensorListener<T>*> listeners;
|
||||
|
||||
public:
|
||||
|
||||
/** start this sensor */
|
||||
virtual void start() = 0;
|
||||
|
||||
/** stop this sensor */
|
||||
virtual void stop() = 0;
|
||||
|
||||
/** add the given listener to the sensor */
|
||||
void addListener(SensorListener<T>* l) {
|
||||
listeners.push_back(l);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/** inform all attached listeners */
|
||||
void informListeners(const T& sensorData) const {
|
||||
for (SensorListener<T>* l : listeners) {
|
||||
l->onSensorData(sensorData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // SENSOR_H
|
||||
47
sensors/SensorFactory.h
Normal file
47
sensors/SensorFactory.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef SENSORFACTORY_H
|
||||
#define SENSORFACTORY_H
|
||||
|
||||
#include "Sensor.h"
|
||||
|
||||
#include "WiFiSensor.h"
|
||||
#include "dummy/WiFiSensorDummy.h"
|
||||
#include "linux/WiFiSensorLinux.h"
|
||||
#include "android/WiFiSensorAndroid.h"
|
||||
|
||||
#include "AccelerometerSensor.h"
|
||||
#include "dummy/AccelerometerSensorDummy.h"
|
||||
#include "android/AccelerometerSensorAndroid.h"
|
||||
|
||||
#include "StepSensor.h"
|
||||
|
||||
class SensorFactory {
|
||||
|
||||
public:
|
||||
|
||||
/** get the WiFi sensor */
|
||||
static WiFiSensor& getWiFi() {
|
||||
#ifdef ANDROID
|
||||
return WiFiSensorAndroid::get();
|
||||
#else
|
||||
return WiFiSensorDummy::get();
|
||||
#endif
|
||||
}
|
||||
|
||||
/** get the Accelerometer sensor */
|
||||
static AccelerometerSensor& getAccelerometer() {
|
||||
#ifdef ANDROID
|
||||
return AccelerometerSensor::get();
|
||||
#else
|
||||
return AccelerometerSensorDummy::get();
|
||||
#endif
|
||||
}
|
||||
|
||||
/** get the Step sensor */
|
||||
static StepSensor& getSteps() {
|
||||
static StepSensor steps(getAccelerometer());
|
||||
return steps;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // SENSORFACTORY_H
|
||||
66
sensors/StepSensor.h
Normal file
66
sensors/StepSensor.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef STEPSENSOR_H
|
||||
#define STEPSENSOR_H
|
||||
|
||||
|
||||
#include "../misc/fixc11.h"
|
||||
#include "AccelerometerSensor.h"
|
||||
#include "Sensor.h"
|
||||
|
||||
struct StepData {
|
||||
;
|
||||
};
|
||||
|
||||
class StepSensor : public Sensor<StepData>, public SensorListener<AccelerometerData> {
|
||||
|
||||
private:
|
||||
|
||||
AccelerometerSensor& acc;
|
||||
|
||||
public:
|
||||
|
||||
/** hidden ctor. use singleton */
|
||||
StepSensor(AccelerometerSensor& acc) : acc(acc) {
|
||||
;
|
||||
}
|
||||
|
||||
void start() override {
|
||||
acc.addListener(this);
|
||||
acc.start();
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
throw "todo";
|
||||
}
|
||||
|
||||
virtual void onSensorData(const AccelerometerData& data) override {
|
||||
parse(data);
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
const float threshold = 11.0;
|
||||
const int blockTime = 25;
|
||||
int block = 0;
|
||||
|
||||
void parse(const AccelerometerData& data) {
|
||||
|
||||
const float x = data.x;
|
||||
const float y = data.y;
|
||||
const float z = data.z;
|
||||
|
||||
const float mag = std::sqrt( (x*x) + (y*y) + (z*z) );
|
||||
|
||||
if (block > 0) {
|
||||
--block;
|
||||
} else if (mag > threshold) {
|
||||
informListeners(StepData());
|
||||
block = blockTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // STEPSENSOR_H
|
||||
43
sensors/WiFiSensor.h
Normal file
43
sensors/WiFiSensor.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WIFISENSOR_H
|
||||
#define WIFISENSOR_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "Sensor.h"
|
||||
|
||||
|
||||
struct WiFiSensorDataEntry {
|
||||
std::string bssid;
|
||||
float rssi;
|
||||
WiFiSensorDataEntry(const std::string& bssid, const float rssi) : bssid(bssid), rssi(rssi) {;}
|
||||
std::string asString() const {
|
||||
std::stringstream ss;
|
||||
ss << bssid << '\t' << (int)rssi;
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct WiFiSensorData {
|
||||
std::vector<WiFiSensorDataEntry> entries;
|
||||
std::string asString() const {
|
||||
std::stringstream ss;
|
||||
for(const WiFiSensorDataEntry& e : entries) {ss << e.asString() << '\n';}
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** interface for all wifi sensors */
|
||||
class WiFiSensor : public Sensor<WiFiSensorData> {
|
||||
|
||||
protected:
|
||||
|
||||
/** hidden ctor. use SensorFactory */
|
||||
WiFiSensor() {
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // WIFISENSOR_H
|
||||
55
sensors/android/AccelerometerSensorAndroid.h
Normal file
55
sensors/android/AccelerometerSensorAndroid.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef ACCELEROMETERSENSOR_H
|
||||
#define ACCELEROMETERSENSOR_H
|
||||
|
||||
|
||||
#ifdef ANDROID
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "../AccelerometerSensor.h"
|
||||
|
||||
#include <QtSensors/QAccelerometer>
|
||||
|
||||
|
||||
|
||||
class AccelerometerSensorAndroid : public AccelerometerSensor {
|
||||
|
||||
private:
|
||||
|
||||
QAccelerometer acc;
|
||||
|
||||
/** hidden ctor. use singleton */
|
||||
AccelerometerSensorAndroid() {
|
||||
;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/** singleton access */
|
||||
static AccelerometerSensor& get() {
|
||||
static AccelerometerSensor acc;
|
||||
return acc;
|
||||
}
|
||||
|
||||
void start() override {
|
||||
|
||||
auto onSensorData = [&] () {
|
||||
AccelerometerData data(acc.reading()->x(), acc.reading()->y(), acc.reading()->z());
|
||||
informListeners(data);
|
||||
};
|
||||
|
||||
acc.connect(&acc, &QAccelerometer::readingChanged, onSensorData);
|
||||
acc.start();
|
||||
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
throw "todo";
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif ANDROID
|
||||
|
||||
#endif // ACCELEROMETERSENSOR_H
|
||||
76
sensors/android/WiFiSensorAndroid.h
Normal file
76
sensors/android/WiFiSensorAndroid.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef WIFISENSORANDROID_H
|
||||
#define WIFISENSORANDROID_H
|
||||
|
||||
|
||||
#ifdef ANDROID
|
||||
|
||||
#include <QAndroidJniObject>
|
||||
|
||||
#include "Debug.h"
|
||||
#include "WiFiSensor.h"
|
||||
|
||||
class WiFiSensorAndroid : public WiFiSensor {
|
||||
|
||||
private:
|
||||
|
||||
/** hidden ctor. use singleton! */
|
||||
WiFiSensorAndroid() {;}
|
||||
|
||||
public:
|
||||
|
||||
/** singleton access */
|
||||
static WiFiSensorAndroid& get() {
|
||||
static WiFiSensorAndroid wifi;
|
||||
return wifi;
|
||||
}
|
||||
|
||||
void start() override {
|
||||
|
||||
// start scanning
|
||||
int res = QAndroidJniObject::callStaticMethod<int>("java/indoor/WiFi", "start", "()I");
|
||||
(void) res;
|
||||
|
||||
}
|
||||
|
||||
/** called from java. handle the given incoming scan result */
|
||||
void handle(const std::string& data) {
|
||||
|
||||
// to-be-constructed sensor data
|
||||
WiFiSensorData sensorData;
|
||||
|
||||
// parse each mac->rssi entry
|
||||
for (int i = 0; i < (int)data.length(); i += 17+1+2) {
|
||||
const std::string bssid = data.substr(i, 17);
|
||||
const int8_t rssi = data[i+17];
|
||||
const int8_t pad1 = data[i+18];
|
||||
const int8_t pad2 = data[i+19];
|
||||
if (pad1 != 0 || pad2 != 0) {Debug::error("padding error within WiFi scan result");}
|
||||
sensorData.entries.push_back(WiFiSensorDataEntry(bssid, rssi));
|
||||
}
|
||||
|
||||
// call listeners
|
||||
informListeners(sensorData);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
/** called after each successful WiFi scan */
|
||||
JNIEXPORT void JNICALL Java_java_indoor_WiFi_onScanComplete(JNIEnv* env, jobject jobj, jbyteArray arrayID) {
|
||||
(void) env; (void) jobj;
|
||||
jsize length = env->GetArrayLength(arrayID);
|
||||
jboolean isCopy;
|
||||
jbyte* data = env->GetByteArrayElements(arrayID, &isCopy);
|
||||
std::string str((char*)data, length);
|
||||
env->ReleaseByteArrayElements(arrayID, data, JNI_ABORT);
|
||||
WiFiSensorAndroid::get().handle(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // WIFISENSORANDROID_H
|
||||
27
sensors/dummy/AccelerometerSensorDummy.h
Normal file
27
sensors/dummy/AccelerometerSensorDummy.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef ACCELEROMETERSENSORDUMMY_H
|
||||
#define ACCELEROMETERSENSORDUMMY_H
|
||||
|
||||
#include "../AccelerometerSensor.h"
|
||||
|
||||
class AccelerometerSensorDummy : public AccelerometerSensor {
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/** singleton access */
|
||||
static AccelerometerSensorDummy& get() {
|
||||
static AccelerometerSensorDummy acc;
|
||||
return acc;
|
||||
}
|
||||
|
||||
void start() override {
|
||||
//throw "todo";
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
throw "todo";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // ACCELEROMETERSENSORDUMMY_H
|
||||
92
sensors/dummy/WiFiSensorDummy.h
Normal file
92
sensors/dummy/WiFiSensorDummy.h
Normal file
@@ -0,0 +1,92 @@
|
||||
#ifndef WIFISENSORDUMMY_H
|
||||
#define WIFISENSORDUMMY_H
|
||||
|
||||
#include "../WiFiSensor.h"
|
||||
#include <thread>
|
||||
|
||||
#include <Indoor/geo/Point3.h>
|
||||
#include <Indoor/sensors/radio/model/LogDistanceModel.h>
|
||||
|
||||
#include <Indoor/grid/Grid.h>
|
||||
#include <Indoor/grid/factory/v2/GridFactory.h>
|
||||
|
||||
|
||||
class WiFiSensorDummy : public WiFiSensor {
|
||||
|
||||
private:
|
||||
|
||||
bool enabled;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/** singleton access */
|
||||
static WiFiSensorDummy& get() {
|
||||
static WiFiSensorDummy wifi;
|
||||
return wifi;
|
||||
}
|
||||
|
||||
|
||||
void start() override {
|
||||
|
||||
enabled = true;
|
||||
std::thread t(&WiFiSensorDummy::simulate, this);
|
||||
t.detach();
|
||||
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
struct DummyAP {
|
||||
std::string mac;
|
||||
Point2 pos;
|
||||
DummyAP(const std::string mac, const Point2 pos) : mac(mac), pos(pos) {;}
|
||||
};
|
||||
|
||||
void simulate() {
|
||||
|
||||
std::vector<DummyAP> aps;
|
||||
aps.push_back(DummyAP("00:00:00:00:00:01", Point2(0, 0)));
|
||||
aps.push_back(DummyAP("00:00:00:00:00:02", Point2(20, 0)));
|
||||
aps.push_back(DummyAP("00:00:00:00:00:03", Point2(10, 20)));
|
||||
|
||||
float deg = 0;
|
||||
|
||||
while(enabled) {
|
||||
|
||||
// wait
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
|
||||
// circle-run around center
|
||||
deg += M_PI * 0.4;
|
||||
|
||||
const float cx = 10;
|
||||
const float cy = 10;
|
||||
const float rad = 5;
|
||||
|
||||
const float x = cx + std::sin(deg) * rad;
|
||||
const float y = cy + std::cos(deg) * rad;
|
||||
|
||||
// construct scan data
|
||||
WiFiSensorData scan;
|
||||
for (DummyAP& ap : aps) {
|
||||
const float dist = ap.pos.getDistance(Point2(x, y));
|
||||
const float rssi = LogDistanceModel::distanceToRssi(-40, 1.5, dist);
|
||||
scan.entries.push_back(WiFiSensorDataEntry(ap.mac, rssi));
|
||||
}
|
||||
|
||||
// call
|
||||
informListeners(scan);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // WIFISENSORDUMMY_H
|
||||
33
sensors/linux/WiFiSensorLinux.h
Normal file
33
sensors/linux/WiFiSensorLinux.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef WIFISENSORLINUX_H
|
||||
#define WIFISENSORLINUX_H
|
||||
|
||||
#include "../WiFiSensor.h"
|
||||
|
||||
class WiFiSensorLinux : public WiFiSensor {
|
||||
|
||||
|
||||
private:
|
||||
|
||||
WiFiSensorLinux() {
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/** singleton access */
|
||||
static WiFiSensorLinux& get() {
|
||||
static WiFiSensorLinux wifi;
|
||||
return wifi;
|
||||
}
|
||||
|
||||
void start() override {
|
||||
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // WIFISENSORLINUX_H
|
||||
Reference in New Issue
Block a user