From ecf4fede9db47910b9be8e508bbf558d5e899e87 Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Tue, 31 May 2016 19:25:05 +0200 Subject: [PATCH] [my-place] Extend Place Wifi AP's list by network names. Change-Id: I556f4dfaf238e482ca05bf4694068e123219f50e Signed-off-by: Marcin Masternak --- src/my-place/user_places/debug_utils.cpp | 5 +- src/my-place/user_places/places_detector.cpp | 72 +++++++++++++++++--- src/my-place/user_places/places_detector.h | 6 +- src/my-place/user_places/user_places.cpp | 9 ++- 4 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/my-place/user_places/debug_utils.cpp b/src/my-place/user_places/debug_utils.cpp index bb03751..40253f6 100644 --- a/src/my-place/user_places/debug_utils.cpp +++ b/src/my-place/user_places/debug_utils.cpp @@ -52,6 +52,9 @@ void ctx::DebugUtils::printPlace2Stream(const Place &place, std::ostream &out) out << "__LOCATION: lat=" << std::setprecision(GEO_LOCATION_PRECISION + 2) << place.location.latitude; out << ", lon=" << place.location.longitude << std::setprecision(5) << std::endl; } - out << "__WIFI:" << place.wifiAps << std::endl; + out << "__WIFI:" << std::endl; + for (std::pair ap : place.wifiAps) { + out << "____ " << ap.first << " : " << ap.second << std::endl; + } out << "__CREATE_DATE: " << humanReadableDateTime(place.createDate, "%F %T", 80) << std::endl; } diff --git a/src/my-place/user_places/places_detector.cpp b/src/my-place/user_places/places_detector.cpp index 11ddbb2..c2fb897 100644 --- a/src/my-place/user_places/places_detector.cpp +++ b/src/my-place/user_places/places_detector.cpp @@ -60,6 +60,11 @@ 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, "\ @@ -74,10 +79,12 @@ bool ctx::PlacesDetector::onTimerExpired(int timerId) { _D(""); __dbDeletePlaces(); - __dbDeleteOldVisits(); + __dbDeleteOldEntries(); std::vector records = __dbGetVisits(); Visits visits = __visitsFromJsons(records); + __dbGetWifiAPsMap(); __processVisits(visits); + __wifiAPsMap.clear(); return true; } @@ -97,6 +104,20 @@ std::vector ctx::PlacesDetector::__dbGetPlaces() 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; @@ -162,8 +183,8 @@ 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)); - row.get(NULL, PLACE_COLUMN_WIFI_APS, &(place->wifiAps)); __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; @@ -186,6 +207,19 @@ void ctx::PlacesDetector::__placeLocationFromJson(Json &row, ctx::Place &place) row.get(NULL, PLACE_COLUMN_LOCATION_LONGITUDE, &(place.location.longitude)); } +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; @@ -315,10 +349,9 @@ std::shared_ptr ctx::PlacesDetector::__placeFromMergedVisits(Visits macSets.push_back(visit.macSet); } std::shared_ptr allMacs = macSetsUnion(macSets); - std::stringstream ss; - ss << *allMacs; - place->wifiAps = ss.str(); - + for (ctx::Mac mac : *allMacs) { + place->wifiAps.insert(std::pair(mac, __wifiAPsMap[mac])); + } __mergeLocation(mergedVisits, *place); PlaceCateger::categorize(mergedVisits, *place); @@ -371,24 +404,34 @@ void ctx::PlacesDetector::__dbDeletePlaces() _D("delete places execute query result: %s", ret ? "SUCCESS" : "FAIL"); } -void ctx::PlacesDetector::__dbDeleteOldVisits() +void ctx::PlacesDetector::__dbDeleteOldEntries() { time_t currentTime; time(¤tTime); time_t thresholdTime = currentTime - PLACES_DETECTOR_RETENTION_SECONDS; __dbDeleteOlderVisitsThan(thresholdTime); + __dbDeleteOlderWifiAPsThan(thresholdTime); } void ctx::PlacesDetector::__dbDeleteOlderVisitsThan(time_t threshold) { - _D("deleting vistits older than: %d", threshold); std::stringstream query; query << "DELETE FROM " << VISIT_TABLE; query << " WHERE " << VISIT_COLUMN_END_TIME << " < " << threshold; // query << " AND 0"; // XXX: Always false condition. Uncomment it for not deleting any visit during development. std::vector records; bool ret = __dbManager->executeSync(query.str().c_str(), &records); - _D("delete old visits execute query result: %s", ret ? "SUCCESS" : "FAIL"); + _D("deleting visits older than: %d, result: %s", threshold, ret ? "SUCCESS" : "FAIL"); +} + +void ctx::PlacesDetector::__dbDeleteOlderWifiAPsThan(time_t threshold) +{ + std::stringstream query; + query << "DELETE FROM " << WIFI_APS_MAP_TABLE; + query << " WHERE " << WIFI_APS_MAP_COLUMN_INSERT_TIME << " < " << threshold; + std::vector records; + bool ret = __dbManager->executeSync(query.str().c_str(), &records); + _D("deleting Wifi APs older than: %d, result: %s", threshold, ret ? "SUCCESS" : "FAIL"); } ctx::PlacesDetector::PlacesDetector(bool testMode): @@ -399,7 +442,9 @@ ctx::PlacesDetector::PlacesDetector(bool testMode): return; __dbCreateTable(); std::vector records = __dbGetPlaces(); + __dbGetWifiAPsMap(); std::vector> dbPlaces = __placesFromJsons(records); + __wifiAPsMap.clear(); __detectedPlacesUpdate(dbPlaces); } @@ -419,8 +464,13 @@ void ctx::PlacesDetector::__dbInsertPlace(const Place &place) data.set(NULL, PLACE_COLUMN_LOCATION_VALID, place.locationValid); data.set(NULL, PLACE_COLUMN_LOCATION_LATITUDE, place.location.latitude); data.set(NULL, PLACE_COLUMN_LOCATION_LONGITUDE, place.location.longitude); - - data.set(NULL, PLACE_COLUMN_WIFI_APS, place.wifiAps); + std::string wifiAps; + for (std::pair ap : place.wifiAps) { + wifiAps.append(ap.first); + wifiAps.append(","); + } + wifiAps = wifiAps.substr(0, wifiAps.size()-1); + data.set(NULL, PLACE_COLUMN_WIFI_APS, wifiAps); data.set(NULL, PLACE_COLUMN_CREATE_DATE, static_cast(place.createDate)); int64_t rowId; diff --git a/src/my-place/user_places/places_detector.h b/src/my-place/user_places/places_detector.h index 492fb23..91c4fd3 100644 --- a/src/my-place/user_places/places_detector.h +++ b/src/my-place/user_places/places_detector.h @@ -40,6 +40,7 @@ namespace ctx { 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); @@ -47,13 +48,16 @@ namespace ctx { void __dbCreateTable(); void __dbDeletePlaces(); - void __dbDeleteOldVisits(); + void __dbDeleteOldEntries(); 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); diff --git a/src/my-place/user_places/user_places.cpp b/src/my-place/user_places/user_places.cpp index e9b43d8..a2a129a 100755 --- a/src/my-place/user_places/user_places.cpp +++ b/src/my-place/user_places/user_places.cpp @@ -125,7 +125,14 @@ ctx::Json ctx::UserPlaces::__composeJson(std::vector> pla placeJson.set(NULL, PLACE_LOCATION_LATITUDE, static_cast(place->location.latitude)); placeJson.set(NULL, PLACE_LOCATION_LONGITUDE, static_cast(place->location.longitude)); } - placeJson.set(NULL, PLACE_WIFI_APS, place->wifiAps); + ctx::Json wifiApsListJson; + for (std::pair ap : place->wifiAps) { + ctx::Json wifiApJson; + wifiApJson.set(NULL, PLACE_WIFI_AP_MAC, ap.first); + wifiApJson.set(NULL, PLACE_WIFI_AP_NETWORK_NAME, ap.second); + wifiApsListJson.append(NULL, PLACE_WIFI_APS, wifiApJson); + } + placeJson.set(NULL, PLACE_WIFI_APS, wifiApsListJson); placeJson.set(NULL, PLACE_CREATE_DATE, static_cast(place->createDate)); data.append(NULL, PLACE_DATA_READ, placeJson); } -- 2.34.1