From e222180b973f69147242ce9dbba3f0a8a28d8d58 Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Fri, 10 Jun 2016 15:15:59 +0200 Subject: [PATCH] [my-place] Move getting places from db feature to UserPlaces facade. Change-Id: I78f09213ac6e1cce4a56828bd0debb04d4c98941 Signed-off-by: Marcin Masternak --- src/my-place/facade/UserPlaces.cpp | 155 +++++++++++++----- src/my-place/facade/UserPlaces.h | 16 ++ src/my-place/place/PlacesDetector.cpp | 152 ++--------------- src/my-place/place/PlacesDetector.h | 16 +- src/my-place/visit-detector/VisitDetector.cpp | 4 +- 5 files changed, 146 insertions(+), 197 deletions(-) diff --git a/src/my-place/facade/UserPlaces.cpp b/src/my-place/facade/UserPlaces.cpp index 196f97d..d0ae2fb 100755 --- a/src/my-place/facade/UserPlaces.cpp +++ b/src/my-place/facade/UserPlaces.cpp @@ -16,11 +16,29 @@ #include #include +#include #include #include "UserPlaces.h" #include "../place/PlacesDetector.h" #include +#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), @@ -64,54 +82,14 @@ ctx::UserPlaces::~UserPlaces() delete __placesDetector; }; -std::vector> ctx::UserPlaces::__getPlaces() -{ - if (__placesDetector) { - return __placesDetector->getPlaces(); - } else { - return std::vector>(); - } -} - ctx::Json ctx::UserPlaces::getPlaces() { - std::vector> places = __getPlaces(); - Json dataRead = UserPlaces::__composeJson(places); - return dataRead; + std::vector records = __dbGetPlaces(); + std::map wifiAPsMap = __dbGetWifiAPsMap(); + std::vector> 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> places) { ctx::Json data; @@ -148,3 +126,92 @@ void ctx::UserPlaces::setMode(PlaceRecogMode energyMode) if (__visitDetector) __visitDetector->setMode(energyMode); } + +std::vector ctx::UserPlaces::__dbGetPlaces() +{ + std::vector records; + bool ret = __dbManager->executeSync(__GET_PLACES_QUERY, &records); + _D("load places execute query result: %s", ret ? "SUCCESS" : "FAIL"); + return records; +} + +std::map ctx::UserPlaces::__dbGetWifiAPsMap() +{ + std::vector records; + std::map 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(mac, networkName)); + } + return wifiAPsMap; +} + +std::shared_ptr ctx::UserPlaces::__placeFromJson(Json &row, std::map &wifiAPsMap) +{ + std::shared_ptr place = std::make_shared(); + __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(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 &wifiAPsMap, ctx::Place &place) +{ + std::string wifiAps; + row.get(NULL, PLACE_COLUMN_WIFI_APS, &wifiAps); + std::stringstream ss; + ss << wifiAps; + std::shared_ptr macSet = std::make_shared(); + ss >> *macSet; + for (ctx::Mac mac : *macSet) { + place.wifiAps.insert(std::pair(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(createDate); +} + +std::vector> ctx::UserPlaces::__placesFromJsons(std::vector& records, std::map &wifiAPsMap) +{ + std::vector> places; + _D("db_result: number of all places: %d", records.size()); + + for (Json &row : records) { + std::shared_ptr place = __placeFromJson(row, wifiAPsMap); + places.push_back(place); + } + _D("number of all places in vector: %d", places.size()); + return places; +} diff --git a/src/my-place/facade/UserPlaces.h b/src/my-place/facade/UserPlaces.h index f645fc1..2bcf56d 100644 --- a/src/my-place/facade/UserPlaces.h +++ b/src/my-place/facade/UserPlaces.h @@ -18,8 +18,11 @@ #define _CONTEXT_PLACE_RECOGNITION_USER_PLACES_H_ #include +#include +#include #include #include +#include #include "../visit-detector/VisitDetector.h" #include "../place/PlacesDetector.h" #include "UserPlacesTypes.h" @@ -31,8 +34,21 @@ namespace ctx { private: VisitDetector *__visitDetector; PlacesDetector *__placesDetector; + DatabaseManager *__dbManager; int __placesDetectorTimerId; TimerManager __timerManager; + std::vector __dbGetPlaces(); + std::map __dbGetWifiAPsMap(); + std::shared_ptr __placeFromJson(Json &row, std::map &wifiAPsMap); + void __placeCategoryFromJson(Json &row, ctx::Place &place); + void __placeLocationFromJson(Json &row, ctx::Place &place); + void __placeWifiAPsFromJson(Json &row, std::map &wifiAPsMap, ctx::Place &place); + void __placeCreateDateFromJson(Json &row, ctx::Place &place); + std::vector> __placesFromJsons( + std::vector& records, + std::map &wifiAPsMap); std::vector> __getPlaces(); static Json __composeJson(std::vector> places); diff --git a/src/my-place/place/PlacesDetector.cpp b/src/my-place/place/PlacesDetector.cpp index 0162f73..e452a13 100644 --- a/src/my-place/place/PlacesDetector.cpp +++ b/src/my-place/place/PlacesDetector.cpp @@ -49,23 +49,6 @@ 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, "\ @@ -77,17 +60,19 @@ 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 records = __dbGetVisits(); Visits visits = __visitsFromJsons(records); - __dbGetWifiAPsMap(); - __processVisits(visits); - __wifiAPsMap.clear(); - return true; } std::vector ctx::PlacesDetector::__dbGetVisits() @@ -98,28 +83,6 @@ std::vector ctx::PlacesDetector::__dbGetVisits() return records; } -std::vector ctx::PlacesDetector::__dbGetPlaces() -{ - std::vector 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 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(mac, networkName)); - } -} - double ctx::PlacesDetector::__doubleValueFromJson(Json &row, const char* key) { double value; @@ -184,69 +147,6 @@ ctx::Visits ctx::PlacesDetector::__visitsFromJsons(std::vector& records) return visits; } -std::shared_ptr ctx::PlacesDetector::__placeFromJson(Json &row) -{ - std::shared_ptr place = std::make_shared(); - __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(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 = std::make_shared(); - ss >> *macSet; - for (ctx::Mac mac : *macSet) { - place.wifiAps.insert(std::pair(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(createDate); -} - -std::vector> ctx::PlacesDetector::__placesFromJsons(std::vector& records) -{ - std::vector> places; - _D("db_result: number of all places: %d", records.size()); - - for (Json &row : records) { - std::shared_ptr 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) { @@ -265,16 +165,17 @@ 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> ctx::PlacesDetector::__processVisits(ctx::Visits &visits) { - __reduceOutliers(visits); - - _D("__testMode = %d", __testMode); - auto components = __mergeVisits(visits); - std::vector> newDetectedPlaces; #ifdef TIZEN_ENGINEER_MODE std::vector placesVisits; // TODO: remove from final solution. #endif /* TIZEN_ENGINEER_MODE */ + + std::vector> newDetectedPlaces; + + __reduceOutliers(visits); + auto components = __mergeVisits(visits); + for (std::shared_ptr component : *components) { // Small places outliers reduction if (!__testMode && component->size() < PLACES_DETECTOR_MIN_VISITS_PER_BIG_PLACE) @@ -301,8 +202,6 @@ void ctx::PlacesDetector::__processVisits(ctx::Visits &visits) #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); @@ -317,16 +216,8 @@ void ctx::PlacesDetector::__processVisits(ctx::Visits &visits) Gmap::writeMap(newDetectedPlaces); } #endif /* TIZEN_ENGINEER_MODE */ -} -/* - * Replace old places by new ones. - */ -void ctx::PlacesDetector::__detectedPlacesUpdate(std::vector> &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) @@ -363,9 +254,6 @@ std::shared_ptr ctx::PlacesDetector::__placeFromMergedVisits(Visits macSets.push_back(visit.macSet); } std::shared_ptr allMacs = macSetsUnion(macSets); - for (ctx::Mac mac : *allMacs) { - place->wifiAps.insert(std::pair(mac, __wifiAPsMap[mac])); - } __mergeLocation(mergedVisits, *place); PlaceCateger::categorize(mergedVisits, *place); @@ -455,11 +343,6 @@ ctx::PlacesDetector::PlacesDetector(bool testMode): if (testMode) return; __dbCreateTable(); - std::vector records = __dbGetPlaces(); - __dbGetWifiAPsMap(); - std::vector> dbPlaces = __placesFromJsons(records); - __wifiAPsMap.clear(); - __detectedPlacesUpdate(dbPlaces); } void ctx::PlacesDetector::__dbCreateTable() @@ -493,8 +376,3 @@ void ctx::PlacesDetector::__dbInsertPlace(const Place &place) _D("insert place execute query result: %s", ret ? "SUCCESS" : "FAIL"); } -std::vector> ctx::PlacesDetector::getPlaces() -{ - // XXX: In case of thread safety issues use std::mutex to protect places list. - return __detectedPlaces; -} diff --git a/src/my-place/place/PlacesDetector.h b/src/my-place/place/PlacesDetector.h index 1c2aa47..86b3860 100644 --- a/src/my-place/place/PlacesDetector.h +++ b/src/my-place/place/PlacesDetector.h @@ -21,7 +21,6 @@ #include #include #include -//#include "visit_detector.h" #include "../facade/UserPlacesTypes.h" #include #include "Graph.h" @@ -39,12 +38,6 @@ namespace ctx { void __visitLocationFromJson(Json &row, ctx::Visit &visit); Visit __visitFromJson(Json &row); Visits __visitsFromJsons(std::vector& records); - std::shared_ptr __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> __placesFromJsons(std::vector& records); std::shared_ptr __graphFromVisits(const std::vector &visits); @@ -54,15 +47,10 @@ namespace ctx { void __dbDeleteOlderVisitsThan(time_t threshold); void __dbDeleteOlderWifiAPsThan(time_t threshold); std::vector __dbGetVisits(); - std::vector __dbGetPlaces(); - void __dbGetWifiAPsMap(); void __dbInsertPlace(const Place &place); std::shared_ptr __placeFromMergedVisits(Visits &mergedVisits); - std::map __wifiAPsMap; - std::vector> __detectedPlaces; - void __detectedPlacesUpdate(std::vector> &newPlaces); - void __processVisits(Visits &visits); + std::vector> __processVisits(Visits &visits); static void __mergeLocation(const Visits &mergedVisits, Place &place); std::shared_ptr __mergeVisits(const std::vector &visits); static void __reduceOutliers(Visits &visits); @@ -72,7 +60,7 @@ namespace ctx { public: PlacesDetector(bool testMode = false); - std::vector> getPlaces(); + void detectPlaces(); }; /* class PlacesDetector */ diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index 1e4823e..26353b8 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -492,13 +492,13 @@ void ctx::VisitDetector::setMode(PlaceRecogMode energyMode) 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; } -- 2.34.1