[my-place][visit_detector] Put Wifi network names into database. 39/72339/2
authorMarcin Masternak <m.masternak@samsung.com>
Tue, 31 May 2016 13:57:05 +0000 (15:57 +0200)
committerMarcin Masternak <m.masternak@samsung.com>
Tue, 31 May 2016 13:57:05 +0000 (15:57 +0200)
Change-Id: Ia2c91e7f08a83f49f4afd9f4fc185c99e9bb3591
Signed-off-by: Marcin Masternak <m.masternak@samsung.com>
src/my-place/place_recognition_types.h
src/my-place/user_places/visit_detector.cpp
src/my-place/user_places/visit_detector.h

index ef046be..0ecc8f1 100644 (file)
 #define VISIT_COLUMN_CATEG_WORK                "categ_work"
 #define VISIT_COLUMN_CATEG_OTHER               "categ_other"
 
+#define WIFI_APS_MAP_TABLE                     "place_status_user_place_wifi_aps_map"
+#define WIFI_APS_MAP_COLUMN_MAC                "mac"
+#define WIFI_APS_MAP_COLUMN_NETWORK_NAME       "network_name"
+#define WIFI_APS_MAP_COLUMN_INSERT_TIME        "insert_time"
+
 #define PLACE_TABLE                            "place_status_user_place"
 #define PLACE_COLUMN_CATEG_ID                  "type_id" // Name inconsistency: "cated_id" vs "type_id" TODO make it consistent
 #define PLACE_COLUMN_CATEG_CONFIDENCE          "type_confidence"
index 39c2f75..03af967 100644 (file)
        VISIT_COLUMN_CATEG_OTHER " REAL"
 #endif /* TIZEN_ENGINEER_MODE */
 
+#define __WIFI_APS_MAP_TABLE_COLUMNS \
+       WIFI_APS_MAP_COLUMN_MAC " TEXT NOT NULL UNIQUE, "\
+       WIFI_APS_MAP_COLUMN_NETWORK_NAME " TEXT NOT NULL, "\
+       WIFI_APS_MAP_COLUMN_INSERT_TIME " timestamp"
+
 ctx::VisitDetector::VisitDetector(time_t startScan, PlaceRecogMode energyMode, bool testMode) :
        __testMode(testMode),
        __locationLogger(testMode ? nullptr : new LocationLogger(this)),
@@ -80,7 +85,7 @@ ctx::VisitDetector::VisitDetector(time_t startScan, PlaceRecogMode energyMode, b
        __listeners.push_back(__locationLogger);
        __listeners.push_back(__wifiLogger);
 
-       __dbCreateTable();
+       __dbCreateTables();
        __wifiLogger->startLogging();
 }
 
@@ -142,6 +147,11 @@ void ctx::VisitDetector::__shiftCurrentInterval()
 void ctx::VisitDetector::__detectEntranceOrDeparture(std::shared_ptr<ctx::Frame> frame)
 {
        __entranceToPlace ? __detectDeparture(frame) : __detectEntrance(frame);
+       if (__entranceToPlace) {
+               for (MacEvent e : *__currentMacEvents) {
+                       __wifiAPsMap.insert(std::pair<std::string, std::string>(e.mac, e.networkName));
+               }
+       }
 }
 
 bool ctx::VisitDetector::__isDisjoint(const ctx::Macs2Counts &macs2Counts, const ctx::MacSet &macSet)
@@ -220,6 +230,7 @@ void ctx::VisitDetector::__visitEndDetected()
                __detectedVisits->push_back(visit);
        } else {
                __dbInsertVisit(visit);
+               __dbInsertWifiAPsMap(visit);
        }
 
        // cleaning
@@ -357,10 +368,13 @@ std::shared_ptr<ctx::Visits> ctx::VisitDetector::getVisits()
        return __detectedVisits;
 }
 
-void ctx::VisitDetector::__dbCreateTable()
+void ctx::VisitDetector::__dbCreateTables()
 {
        bool ret = __dbManager->createTable(0, VISIT_TABLE, __VISIT_TABLE_COLUMNS);
-       _D("db: visit Table Creation Result: %s", ret ? "SUCCESS" : "FAIL");
+       _D("db: Visit Table Creation Result: %s", ret ? "SUCCESS" : "FAIL");
+
+       ret = __dbManager->createTable(0, WIFI_APS_MAP_TABLE, __WIFI_APS_MAP_TABLE_COLUMNS);
+       _D("db: Wifi AP Map Table Creation Result: %s", ret ? "SUCCESS" : "FAIL");
 }
 
 void ctx::VisitDetector::__putVisitCategToJson(const char* key, const Categs &categs, int categType, Json &data)
@@ -414,6 +428,28 @@ int ctx::VisitDetector::__dbInsertVisit(Visit visit)
        return ret;
 }
 
+int ctx::VisitDetector::__dbInsertWifiAPsMap(Visit visit)
+{
+       std::stringstream query;
+       time_t now = time(nullptr);
+       const char* separator = " ";
+       query << "BEGIN TRANSACTION; \
+                       REPLACE INTO " WIFI_APS_MAP_TABLE " \
+                       ( " WIFI_APS_MAP_COLUMN_MAC ", " WIFI_APS_MAP_COLUMN_NETWORK_NAME ", " WIFI_APS_MAP_COLUMN_INSERT_TIME " ) \
+                       VALUES";
+       for (Mac mac : *visit.macSet) {
+               // TODO: Add protection from SQL injection in network name!!
+               query << separator << "( '" << mac << "', '" << __wifiAPsMap.find(mac)->second << "', '" << now << "' )";
+               separator = ", ";
+       }
+       __wifiAPsMap.clear();
+       query << "; \
+                       END TRANSACTION;";
+       bool ret = __dbManager->execute(0, query.str().c_str(), NULL);
+       _D("DB Wifi APs map insert request: %s", ret ? "SUCCESS" : "FAIL");
+       return ret;
+}
+
 void ctx::VisitDetector::onNewLocation(LocationEvent locationEvent)
 {
        _D("");
index 0fa6ad6..7b63e57 100644 (file)
@@ -37,6 +37,7 @@ namespace ctx {
 
        private:
                bool __testMode;
+               std::map<std::string, std::string> __wifiAPsMap;
                std::shared_ptr<Visits> __detectedVisits; // only used in test mode
                LocationLogger *__locationLogger;
                WifiLogger *__wifiLogger;
@@ -80,8 +81,9 @@ namespace ctx {
                void __processCurrentLogger();
 
                /* DATABASE */
-               void __dbCreateTable();
+               void __dbCreateTables();
                int __dbInsertVisit(Visit visit);
+               int __dbInsertWifiAPsMap(Visit visit);
                void __putVisitCategToJson(const char* key, const Categs &categs, int categType, Json &data);
                void __putVisitCategsToJson(const Categs &categs, Json &data);