#include <ctime>
#include <memory>
+#include <sstream>
#include <Types.h>
#include "UserPlaces.h"
#include "../place/PlacesDetector.h"
#include <MyPlaceTypes.h>
+#define __GET_PLACES_QUERY "SELECT "\
+ PLACE_COLUMN_CATEG_ID ", "\
+ PLACE_COLUMN_CATEG_CONFIDENCE ", "\
+ PLACE_COLUMN_NAME ", "\
+ PLACE_COLUMN_LOCATION_VALID ", "\
+ PLACE_COLUMN_LOCATION_LATITUDE ", "\
+ PLACE_COLUMN_LOCATION_LONGITUDE ", "\
+ PLACE_COLUMN_LOCATION_ACCURACY ", "\
+ PLACE_COLUMN_WIFI_APS ", "\
+ PLACE_COLUMN_CREATE_DATE \
+ " FROM " PLACE_TABLE
+
+#define __GET_WIFI_APS_MAP_QUERY "SELECT "\
+ WIFI_APS_MAP_COLUMN_MAC ", "\
+ WIFI_APS_MAP_COLUMN_NETWORK_NAME \
+ " FROM " WIFI_APS_MAP_TABLE
+
ctx::UserPlaces::UserPlaces(PlaceRecogMode energyMode):
__visitDetector(nullptr),
__placesDetector(nullptr),
delete __placesDetector;
};
-std::vector<std::shared_ptr<ctx::Place>> ctx::UserPlaces::__getPlaces()
-{
- if (__placesDetector) {
- return __placesDetector->getPlaces();
- } else {
- return std::vector<std::shared_ptr<ctx::Place>>();
- }
-}
-
ctx::Json ctx::UserPlaces::getPlaces()
{
- std::vector<std::shared_ptr<ctx::Place>> places = __getPlaces();
- Json dataRead = UserPlaces::__composeJson(places);
- return dataRead;
+ std::vector<Json> records = __dbGetPlaces();
+ std::map<std::string, std::string> wifiAPsMap = __dbGetWifiAPsMap();
+ std::vector<std::shared_ptr<Place>> places = __placesFromJsons(records, wifiAPsMap);
+ return UserPlaces::__composeJson(places);
}
-/*
- * Example JSON output:
- * ------------------------------------------------
- * {
- * "PlacesList": [
- * {
- * "TypeId": 2,
- * "Name": "Work",
- * "GeoLatitude": 10.94433,
- * "GeoLongitude": 50.85504,
- * "WifiAPs": "00:1f:f3:5b:2b:1f,15:34:56:78:9a:ba,13:34:56:78:9a:ba",
- * "CreateDate": 12132567
- * },
- * {
- * "TypeId": 1,
- * "Name": "Home",
- * "GeoLatitude": 10.96233,
- * "GeoLongitude": 50.84304,
- * "WifiAPs": "aa:bb:cc:dd:ee:ff,10:34:56:78:9a:bc",
- * "CreateDate": 12132889
- * },
- * {
- * "TypeId": 3,
- * "Name": "Other",
- * "GeoLatitude": 10.96553,
- * "GeoLongitude": 50.80404,
- * "WifiAPs": "12:34:56:78:9a:ba",
- * "CreateDate": 12132346
- * }
- * ]
- * }
- */
ctx::Json ctx::UserPlaces::__composeJson(std::vector<std::shared_ptr<Place>> places)
{
ctx::Json data;
if (__visitDetector)
__visitDetector->setMode(energyMode);
}
+
+std::vector<ctx::Json> ctx::UserPlaces::__dbGetPlaces()
+{
+ std::vector<Json> records;
+ bool ret = __dbManager->executeSync(__GET_PLACES_QUERY, &records);
+ _D("load places execute query result: %s", ret ? "SUCCESS" : "FAIL");
+ return records;
+}
+
+std::map<std::string, std::string> ctx::UserPlaces::__dbGetWifiAPsMap()
+{
+ std::vector<Json> records;
+ std::map<std::string, std::string> wifiAPsMap;
+ bool ret = __dbManager->executeSync(__GET_WIFI_APS_MAP_QUERY, &records);
+ // TODO: add return statements if db fail
+ _D("load Wifi APs map (size = %d), result: %s", records.size(), ret ? "SUCCESS" : "FAIL");
+ std::string mac = "";
+ std::string networkName = "";
+ for (Json record : records) {
+ record.get(NULL, WIFI_APS_MAP_COLUMN_MAC, &mac);
+ record.get(NULL, WIFI_APS_MAP_COLUMN_NETWORK_NAME, &networkName);
+ wifiAPsMap.insert(std::pair<std::string, std::string>(mac, networkName));
+ }
+ return wifiAPsMap;
+}
+
+std::shared_ptr<ctx::Place> ctx::UserPlaces::__placeFromJson(Json &row, std::map<std::string, std::string> &wifiAPsMap)
+{
+ std::shared_ptr<Place> place = std::make_shared<Place>();
+ __placeCategoryFromJson(row, *place);
+ row.get(NULL, PLACE_COLUMN_NAME, &(place->name));
+ __placeLocationFromJson(row, *place);
+ __placeWifiAPsFromJson(row, wifiAPsMap, *place);
+ __placeCreateDateFromJson(row, *place);
+ _D("db_result: categId: %d; place: name: %s; locationValid: %d; latitude: %lf, longitude: %lf, createDate: %d", place->categId, place->name.c_str(), place->locationValid, place->location.latitude, place->location.longitude, place->createDate);
+ return place;
+}
+
+void ctx::UserPlaces::__placeCategoryFromJson(Json &row, ctx::Place &place)
+{
+ int categId;
+ row.get(NULL, PLACE_COLUMN_CATEG_ID, &categId);
+ // This is due to the fact the JSON module API interface doesn't handle enum
+ place.categId = static_cast<PlaceCategId>(categId);
+}
+
+void ctx::UserPlaces::__placeLocationFromJson(Json &row, ctx::Place &place)
+{
+ int locationValidInt;
+ row.get(NULL, PLACE_COLUMN_LOCATION_VALID, &locationValidInt);
+ place.locationValid = (bool) locationValidInt;
+ row.get(NULL, PLACE_COLUMN_LOCATION_LATITUDE, &(place.location.latitude));
+ row.get(NULL, PLACE_COLUMN_LOCATION_LONGITUDE, &(place.location.longitude));
+ row.get(NULL, PLACE_COLUMN_LOCATION_ACCURACY, &(place.location.accuracy));
+}
+
+void ctx::UserPlaces::__placeWifiAPsFromJson(Json &row, std::map<std::string, std::string> &wifiAPsMap, ctx::Place &place)
+{
+ std::string wifiAps;
+ row.get(NULL, PLACE_COLUMN_WIFI_APS, &wifiAps);
+ std::stringstream ss;
+ ss << wifiAps;
+ std::shared_ptr<MacSet> macSet = std::make_shared<MacSet>();
+ ss >> *macSet;
+ for (ctx::Mac mac : *macSet) {
+ place.wifiAps.insert(std::pair<std::string, std::string>(mac, wifiAPsMap[mac]));
+ }
+}
+
+void ctx::UserPlaces::__placeCreateDateFromJson(Json &row, ctx::Place &place)
+{
+ int createDate;
+ row.get(NULL, PLACE_COLUMN_CREATE_DATE, &(createDate));
+ // This is due to the fact the JSON module API interface doesn't handle time_t
+ place.createDate = static_cast<time_t>(createDate);
+}
+
+std::vector<std::shared_ptr<ctx::Place>> ctx::UserPlaces::__placesFromJsons(std::vector<Json>& records, std::map<std::string, std::string> &wifiAPsMap)
+{
+ std::vector<std::shared_ptr<Place>> places;
+ _D("db_result: number of all places: %d", records.size());
+
+ for (Json &row : records) {
+ std::shared_ptr<Place> place = __placeFromJson(row, wifiAPsMap);
+ places.push_back(place);
+ }
+ _D("number of all places in vector: %d", places.size());
+ return places;
+}
#define _CONTEXT_PLACE_RECOGNITION_USER_PLACES_H_
#include <vector>
+#include <map>
+#include <string>
#include <Json.h>
#include <TimerManager.h>
+#include <DatabaseManager.h>
#include "../visit-detector/VisitDetector.h"
#include "../place/PlacesDetector.h"
#include "UserPlacesTypes.h"
private:
VisitDetector *__visitDetector;
PlacesDetector *__placesDetector;
+ DatabaseManager *__dbManager;
int __placesDetectorTimerId;
TimerManager __timerManager;
+ std::vector<Json> __dbGetPlaces();
+ std::map<std::string, std::string> __dbGetWifiAPsMap();
+ std::shared_ptr<ctx::Place> __placeFromJson(Json &row, std::map<std::string,
+ std::string> &wifiAPsMap);
+ void __placeCategoryFromJson(Json &row, ctx::Place &place);
+ void __placeLocationFromJson(Json &row, ctx::Place &place);
+ void __placeWifiAPsFromJson(Json &row, std::map<std::string,
+ std::string> &wifiAPsMap, ctx::Place &place);
+ void __placeCreateDateFromJson(Json &row, ctx::Place &place);
+ std::vector<std::shared_ptr<Place>> __placesFromJsons(
+ std::vector<Json>& records,
+ std::map<std::string, std::string> &wifiAPsMap);
std::vector<std::shared_ptr<Place>> __getPlaces();
static Json __composeJson(std::vector<std::shared_ptr<Place>> places);
VISIT_COLUMN_CATEG_OTHER \
" FROM " VISIT_TABLE
-#define __GET_PLACES_QUERY "SELECT "\
- PLACE_COLUMN_CATEG_ID ", "\
- PLACE_COLUMN_CATEG_CONFIDENCE ", "\
- PLACE_COLUMN_NAME ", "\
- PLACE_COLUMN_LOCATION_VALID ", "\
- PLACE_COLUMN_LOCATION_LATITUDE ", "\
- PLACE_COLUMN_LOCATION_LONGITUDE ", "\
- PLACE_COLUMN_LOCATION_ACCURACY ", "\
- PLACE_COLUMN_WIFI_APS ", "\
- PLACE_COLUMN_CREATE_DATE \
- " FROM " PLACE_TABLE
-
-#define __GET_WIFI_APS_MAP_QUERY "SELECT "\
- WIFI_APS_MAP_COLUMN_MAC ", "\
- WIFI_APS_MAP_COLUMN_NETWORK_NAME \
- " FROM " WIFI_APS_MAP_TABLE
-
#define __PLACE_TABLE_COLUMNS \
PLACE_COLUMN_CATEG_ID " INTEGER, "\
PLACE_COLUMN_CATEG_CONFIDENCE " REAL, "\
PLACE_COLUMN_WIFI_APS " STRING, "\
PLACE_COLUMN_CREATE_DATE " timestamp"
-bool ctx::PlacesDetector::onTimerExpired(int timerId)
+bool ctx::PlacesDetector::onTimerExpired(int timerId) {
+ _D("");
+ detectPlaces();
+ return true;
+}
+
+void ctx::PlacesDetector::detectPlaces()
{
_D("");
__dbDeletePlaces();
__dbDeleteOldEntries();
std::vector<Json> records = __dbGetVisits();
Visits visits = __visitsFromJsons(records);
- __dbGetWifiAPsMap();
- __processVisits(visits);
- __wifiAPsMap.clear();
- return true;
}
std::vector<ctx::Json> ctx::PlacesDetector::__dbGetVisits()
return records;
}
-std::vector<ctx::Json> ctx::PlacesDetector::__dbGetPlaces()
-{
- std::vector<Json> records;
- bool ret = __dbManager->executeSync(__GET_PLACES_QUERY, &records);
- _D("load places execute query result: %s", ret ? "SUCCESS" : "FAIL");
- return records;
-}
-
-void ctx::PlacesDetector::__dbGetWifiAPsMap()
-{
- std::vector<Json> records;
- bool ret = __dbManager->executeSync(__GET_WIFI_APS_MAP_QUERY, &records);
- _D("load Wifi APs map (size = %d), result: %s", records.size(), ret ? "SUCCESS" : "FAIL");
- std::string mac = "";
- std::string networkName = "";
- for (Json record : records) {
- record.get(NULL, WIFI_APS_MAP_COLUMN_MAC, &mac);
- record.get(NULL, WIFI_APS_MAP_COLUMN_NETWORK_NAME, &networkName);
- __wifiAPsMap.insert(std::pair<std::string, std::string>(mac, networkName));
- }
-}
-
double ctx::PlacesDetector::__doubleValueFromJson(Json &row, const char* key)
{
double value;
return visits;
}
-std::shared_ptr<ctx::Place> ctx::PlacesDetector::__placeFromJson(Json &row)
-{
- std::shared_ptr<Place> place = std::make_shared<Place>();
- __placeCategoryFromJson(row, *place);
- row.get(NULL, PLACE_COLUMN_NAME, &(place->name));
- __placeLocationFromJson(row, *place);
- __placeWifiAPsFromJson(row, *place);
- __placeCreateDateFromJson(row, *place);
- _D("db_result: categId: %d; place: name: %s; locationValid: %d; latitude: %lf, longitude: %lf, createDate: %d", place->categId, place->name.c_str(), place->locationValid, place->location.latitude, place->location.longitude, place->createDate);
- return place;
-}
-
-void ctx::PlacesDetector::__placeCategoryFromJson(Json &row, ctx::Place &place)
-{
- int categId;
- row.get(NULL, PLACE_COLUMN_CATEG_ID, &categId);
- // This is due to the fact the JSON module API interface doesn't handle enum
- place.categId = static_cast<PlaceCategId>(categId);
-}
-
-void ctx::PlacesDetector::__placeLocationFromJson(Json &row, ctx::Place &place)
-{
- int locationValidInt;
- row.get(NULL, PLACE_COLUMN_LOCATION_VALID, &locationValidInt);
- place.locationValid = (bool) locationValidInt;
- row.get(NULL, PLACE_COLUMN_LOCATION_LATITUDE, &(place.location.latitude));
- row.get(NULL, PLACE_COLUMN_LOCATION_LONGITUDE, &(place.location.longitude));
- row.get(NULL, PLACE_COLUMN_LOCATION_ACCURACY, &(place.location.accuracy));
-}
-
-void ctx::PlacesDetector::__placeWifiAPsFromJson(Json &row, ctx::Place &place)
-{
- std::string wifiAps;
- row.get(NULL, PLACE_COLUMN_WIFI_APS, &wifiAps);
- std::stringstream ss;
- ss << wifiAps;
- std::shared_ptr<MacSet> macSet = std::make_shared<MacSet>();
- ss >> *macSet;
- for (ctx::Mac mac : *macSet) {
- place.wifiAps.insert(std::pair<std::string, std::string>(mac, __wifiAPsMap[mac]));
- }
-}
-
-void ctx::PlacesDetector::__placeCreateDateFromJson(Json &row, ctx::Place &place)
-{
- int createDate;
- row.get(NULL, PLACE_COLUMN_CREATE_DATE, &(createDate));
- // This is due to the fact the JSON module API interface doesn't handle time_t
- place.createDate = static_cast<time_t>(createDate);
-}
-
-std::vector<std::shared_ptr<ctx::Place>> ctx::PlacesDetector::__placesFromJsons(std::vector<Json>& records)
-{
- std::vector<std::shared_ptr<Place>> places;
- _D("db_result: number of all places: %d", records.size());
-
- for (Json &row : records) {
- std::shared_ptr<Place> place = __placeFromJson(row);
- places.push_back(place);
- }
- _D("number of all places in vector: %d", places.size());
- return places;
-}
void ctx::PlacesDetector::__reduceOutliers(ctx::Visits &visits)
{
_D("Visits number from %d to %d (to short and to long reduction)", size, newSize);
}
-void ctx::PlacesDetector::__processVisits(ctx::Visits &visits)
+std::vector<std::shared_ptr<ctx::Place>> ctx::PlacesDetector::__processVisits(ctx::Visits &visits)
{
- __reduceOutliers(visits);
-
- _D("__testMode = %d", __testMode);
- auto components = __mergeVisits(visits);
- std::vector<std::shared_ptr<Place>> newDetectedPlaces;
#ifdef TIZEN_ENGINEER_MODE
std::vector<Visits> placesVisits; // TODO: remove from final solution.
#endif /* TIZEN_ENGINEER_MODE */
+
+ std::vector<std::shared_ptr<Place>> newDetectedPlaces;
+
+ __reduceOutliers(visits);
+ auto components = __mergeVisits(visits);
+
for (std::shared_ptr<graph::Component> component : *components) {
// Small places outliers reduction
if (!__testMode && component->size() < PLACES_DETECTOR_MIN_VISITS_PER_BIG_PLACE)
#endif /* TIZEN_ENGINEER_MODE */
}
- __detectedPlacesUpdate(newDetectedPlaces);
-
#ifdef TIZEN_ENGINEER_MODE
{ // Print to file TODO: Only for debug -> remove in final solution
std::ofstream out(__USER_PLACES_FILE);
Gmap::writeMap(newDetectedPlaces);
}
#endif /* TIZEN_ENGINEER_MODE */
-}
-/*
- * Replace old places by new ones.
- */
-void ctx::PlacesDetector::__detectedPlacesUpdate(std::vector<std::shared_ptr<Place>> &newPlaces)
-{
- _D("");
- // XXX: In case of thread safety issues use std::mutex to protect places list.
- __detectedPlaces = newPlaces;
+return newDetectedPlaces;
}
void ctx::PlacesDetector::__mergeLocation(const Visits &visits, Place &place)
macSets.push_back(visit.macSet);
}
std::shared_ptr<MacSet> allMacs = macSetsUnion(macSets);
- for (ctx::Mac mac : *allMacs) {
- place->wifiAps.insert(std::pair<std::string, std::string>(mac, __wifiAPsMap[mac]));
- }
__mergeLocation(mergedVisits, *place);
PlaceCateger::categorize(mergedVisits, *place);
if (testMode)
return;
__dbCreateTable();
- std::vector<Json> records = __dbGetPlaces();
- __dbGetWifiAPsMap();
- std::vector<std::shared_ptr<Place>> dbPlaces = __placesFromJsons(records);
- __wifiAPsMap.clear();
- __detectedPlacesUpdate(dbPlaces);
}
void ctx::PlacesDetector::__dbCreateTable()
_D("insert place execute query result: %s", ret ? "SUCCESS" : "FAIL");
}
-std::vector<std::shared_ptr<ctx::Place>> ctx::PlacesDetector::getPlaces()
-{
- // XXX: In case of thread safety issues use std::mutex to protect places list.
- return __detectedPlaces;
-}
#include <cstdint>
#include <ITimerListener.h>
#include <DatabaseManager.h>
-//#include "visit_detector.h"
#include "../facade/UserPlacesTypes.h"
#include <MyPlaceTypes.h>
#include "Graph.h"
void __visitLocationFromJson(Json &row, ctx::Visit &visit);
Visit __visitFromJson(Json &row);
Visits __visitsFromJsons(std::vector<Json>& records);
- std::shared_ptr<ctx::Place> __placeFromJson(Json &row);
- void __placeCategoryFromJson(Json &row, ctx::Place &place);
- void __placeLocationFromJson(Json &row, ctx::Place &place);
- void __placeWifiAPsFromJson(Json &row, ctx::Place &place);
- void __placeCreateDateFromJson(Json &row, ctx::Place &place);
- std::vector<std::shared_ptr<Place>> __placesFromJsons(std::vector<Json>& records);
std::shared_ptr<graph::Graph> __graphFromVisits(const std::vector<Visit> &visits);
void __dbDeleteOlderVisitsThan(time_t threshold);
void __dbDeleteOlderWifiAPsThan(time_t threshold);
std::vector<Json> __dbGetVisits();
- std::vector<Json> __dbGetPlaces();
- void __dbGetWifiAPsMap();
void __dbInsertPlace(const Place &place);
std::shared_ptr<Place> __placeFromMergedVisits(Visits &mergedVisits);
- std::map<std::string, std::string> __wifiAPsMap;
- std::vector<std::shared_ptr<Place>> __detectedPlaces;
- void __detectedPlacesUpdate(std::vector<std::shared_ptr<Place>> &newPlaces);
- void __processVisits(Visits &visits);
+ std::vector<std::shared_ptr<ctx::Place>> __processVisits(Visits &visits);
static void __mergeLocation(const Visits &mergedVisits, Place &place);
std::shared_ptr<graph::Components> __mergeVisits(const std::vector<Visit> &visits);
static void __reduceOutliers(Visits &visits);
public:
PlacesDetector(bool testMode = false);
- std::vector<std::shared_ptr<Place>> getPlaces();
+ void detectPlaces();
}; /* class PlacesDetector */
void ctx::VisitDetector::__categorize(ctx::Visit &visit)
{
- _D("mmastern try to categorize from visit detector");
+ _D("");
GModule *soHandle = g_module_open(SO_PATH, G_MODULE_BIND_LAZY);
IF_FAIL_VOID_TAG(soHandle, _E, "%s", g_module_error());
gpointer symbol;
if (!g_module_symbol(soHandle, "categorize", &symbol) || symbol == NULL) {
- _E("mmastern %s", g_module_error());
+ _E("%s", g_module_error());
g_module_close(soHandle);
return;
}