[my-place] User consent starts/stops UserPlaces engine. 95/82995/3
authorMarcin Masternak <m.masternak@samsung.com>
Tue, 9 Aug 2016 11:31:50 +0000 (13:31 +0200)
committerMarcin Masternak <m.masternak@samsung.com>
Tue, 9 Aug 2016 11:31:50 +0000 (13:31 +0200)
Change-Id: I54e8525e127efa5713e415beebb99d8775805a8c
Signed-off-by: Marcin Masternak <m.masternak@samsung.com>
src/my-place/PlaceRecognitionProvider.cpp
src/my-place/facade/UserPlaces.cpp
src/my-place/facade/UserPlaces.h
src/my-place/visit-detector/VisitDetector.cpp

index 02b4071..1d17e84 100644 (file)
@@ -39,28 +39,32 @@ int ctx::PlaceRecognitionProvider::unsubscribe(ctx::Json option)
 
 int ctx::PlaceRecognitionProvider::read(ctx::Json option, ctx::Json* requestResult)
 {
-       _I(BLUE("Read"));
+       _D(BLUE("Read"));
        _J("Option", option);
 
        std::string request;
        Json dataRead;
        option.get(NULL, PLACE_DETECTION_REQUEST, &request);
+       bool consent = __engine.getConsent();
+       int ret = ERR_NONE;
        if (request == PLACE_DETECTION_REQUEST_CONSENT) {
-               dataRead.set(NULL, PLACE_DETECTION_REQUEST_CONSENT, UserPlaces::getConsent() ? MYPLACE_SETTING_VALUE_TRUE : MYPLACE_SETTING_VALUE_FALSE);
+               dataRead.set(NULL, PLACE_DETECTION_REQUEST_CONSENT, consent ? MYPLACE_SETTING_VALUE_TRUE : MYPLACE_SETTING_VALUE_FALSE);
        } else if (request == PLACE_DETECTION_REQUEST_LIST) {
-               dataRead = UserPlaces::getPlaces();
+               if (consent)
+                       dataRead = UserPlaces::getPlaces();
+               else
+                       ret = ERR_NOT_STARTED;
        } else {
-               replyToRead(option, ERR_INVALID_PARAMETER, dataRead);
-               return ERR_INVALID_PARAMETER;
+               ret = ERR_INVALID_PARAMETER;
        }
 
-       replyToRead(option, ERR_NONE, dataRead);
-       return ERR_NONE;
+       replyToRead(option, ret, dataRead);
+       return ret;
 }
 
 int ctx::PlaceRecognitionProvider::write(ctx::Json data, ctx::Json* requestResult)
 {
-       _I(BLUE("Write"));
+       _D(BLUE("Write"));
        _J("Data", data);
 
        std::string value;
@@ -70,9 +74,9 @@ int ctx::PlaceRecognitionProvider::write(ctx::Json data, ctx::Json* requestResul
                        _E,
                        "Invalid write request");
        if (value == MYPLACE_SETTING_VALUE_TRUE) {
-               UserPlaces::setConsent(true);
+               __engine.setConsent(true);
        } else if (value == MYPLACE_SETTING_VALUE_FALSE) {
-               UserPlaces::setConsent(false);
+               __engine.setConsent(false);
        } else {
                _E("Invalid write parameter %s = '%s'", PLACE_DETECTION_REQUEST_CONSENT, value.c_str());
                return ERR_INVALID_PARAMETER;
index 0e72436..0c593b4 100755 (executable)
@@ -57,37 +57,19 @@ typedef void (*places_detector_t)();
 
 ctx::UserPlaces::UserPlaces(PlaceRecogMode energyMode):
        __visitDetector(nullptr),
-       __timerId(-1)
+       __timerId(-1),
+       __started(false),
+       __energyMode(energyMode)
 {
+       _D("");
        __dbCreateMyPlaceSettingsTable();
-       bool userConsent = getConsent();
-       time_t now = std::time(nullptr);
-       __visitDetector = new(std::nothrow) VisitDetector(now, energyMode);
-       if (__visitDetector == nullptr) {
-               _E("Cannot initialize __visitDetector");
-               return;
-       }
-
-       __timerId = __timerManager.setAt( // execute once every night
-                       PLACES_DETECTOR_TASK_START_HOUR,
-                       PLACES_DETECTOR_TASK_START_MINUTE,
-                       DayOfWeek::EVERYDAY,
-                       this);
-       if (__timerId < 0) {
-               _E("timer set FAIL");
-               return;
-       } else {
-               _D("timer set SUCCESS");
-       }
+       if (getConsent())
+               __start();
 }
 
 ctx::UserPlaces::~UserPlaces()
 {
-       if (__timerId >= 0) {
-               __timerManager.remove(__timerId);
-       }
-       if (__visitDetector)
-               delete __visitDetector;
+       __stop();
 };
 
 ctx::Json ctx::UserPlaces::getPlaces()
@@ -131,6 +113,7 @@ ctx::Json ctx::UserPlaces::__composeJson(std::vector<std::shared_ptr<Place>> pla
 
 void ctx::UserPlaces::setMode(PlaceRecogMode energyMode)
 {
+       __energyMode = energyMode;
        if (__visitDetector)
                __visitDetector->setMode(energyMode);
 }
@@ -255,16 +238,22 @@ void ctx::UserPlaces::__dbCreateMyPlaceSettingsTable()
 
 void ctx::UserPlaces::setConsent(bool consent)
 {
+       _D("");
        DatabaseManager dbManager;
        std::vector<Json> records;
        std::stringstream query;
        query << "REPLACE INTO " MYPLACE_SETTINGS_TABLE \
                        " ( " MYPLACE_SETTINGS_COLUMN_KEY ", " MYPLACE_SETTINGS_COLUMN_VALUE " )" \
                        " VALUES ( '" MYPLACE_SETTING_KEY_USER_CONSENT "', '";
-       if (consent)
+       if (consent) {
                query << MYPLACE_SETTING_VALUE_TRUE;
-       else
+               if (!__started)
+                       __start();
+       } else {
                query << MYPLACE_SETTING_VALUE_FALSE;
+               if (__started)
+                       __stop();
+       }
        query << "' );";
 
        bool ret = dbManager.executeSync(query.str().c_str(), &records);
@@ -273,6 +262,7 @@ void ctx::UserPlaces::setConsent(bool consent)
 
 bool ctx::UserPlaces::getConsent()
 {
+       _D("");
        DatabaseManager dbManager;
        std::vector<Json> records;
        bool ret = dbManager.executeSync(__GET_USER_CONSENT_QUERY, &records);
@@ -281,8 +271,10 @@ bool ctx::UserPlaces::getConsent()
                std::string consentStr;
                records[0].get(NULL, MYPLACE_SETTINGS_COLUMN_VALUE, &consentStr);
                if (consentStr == MYPLACE_SETTING_VALUE_TRUE) {
+                       _D("User consent value from db: TRUE");
                        return true;
                } else if (consentStr == MYPLACE_SETTING_VALUE_FALSE) {
+                       _D("User consent value from db: FALSE");
                        return false;
                } else {
                        _E("Unknown user consent value \"%s\" from db, it should be \"%s\" or \"%s\"",
@@ -295,3 +287,40 @@ bool ctx::UserPlaces::getConsent()
        setConsent(MYPLACE_SETTING_USER_CONSENT_DEFAULT);
        return MYPLACE_SETTING_USER_CONSENT_DEFAULT;
 }
+
+void ctx::UserPlaces::__start()
+{
+       _D("");
+       time_t now = std::time(nullptr);
+       __visitDetector = new(std::nothrow) VisitDetector(now, __energyMode);
+       if (__visitDetector == nullptr) {
+               _E("Cannot initialize VisitDetector");
+               return;
+       }
+       __timerId = __timerManager.setAt( // execute once every night
+                       PLACES_DETECTOR_TASK_START_HOUR,
+                       PLACES_DETECTOR_TASK_START_MINUTE,
+                       DayOfWeek::EVERYDAY,
+                       this);
+       if (__timerId < 0) {
+               _E("timer set FAIL");
+               return;
+       } else {
+               _D("timer set SUCCESS");
+       }
+       __started = true;
+}
+
+void ctx::UserPlaces::__stop()
+{
+       _D("");
+       if (__timerId >= 0) {
+               __timerManager.remove(__timerId);
+               __timerId = -1;
+       }
+       if (__visitDetector) {
+               delete __visitDetector;
+               __visitDetector = nullptr;
+       }
+       __started = false;
+}
index 5cade8f..a760a5f 100644 (file)
@@ -35,6 +35,8 @@ namespace ctx {
                VisitDetector *__visitDetector;
                int __timerId;
                TimerManager __timerManager;
+               bool __started;
+               PlaceRecogMode __energyMode;
                static std::vector<Json> __dbGetPlaces();
                static std::map<std::string, std::string> __dbGetWifiAPsMap();
                static std::shared_ptr<ctx::Place> __placeFromJson(Json &row, std::map<std::string,
@@ -50,6 +52,8 @@ namespace ctx {
                static std::vector<std::shared_ptr<Place>> __getPlaces();
                static Json __composeJson(std::vector<std::shared_ptr<Place>> places);
                static void __dbCreateMyPlaceSettingsTable();
+               void __start();
+               void __stop();
 
                bool onTimerExpired(int timerId);
 
@@ -59,8 +63,8 @@ namespace ctx {
 
                void setMode(PlaceRecogMode energyMode);
                static ctx::Json getPlaces();
-               static void setConsent(bool consent);
-               static bool getConsent();
+               void setConsent(bool consent);
+               bool getConsent();
 
        };      /* class UserPlaces */
 
index cde448e..f8af125 100644 (file)
@@ -219,7 +219,7 @@ void ctx::VisitDetector::__visitStartDetected()
        }
        __representativesMacs = __selectRepresentatives(__historyFrames);
        __entranceTime = __historyFrames[0]->interval.start;
-       _I("Entrance detected, timestamp: %d", __entranceTime);
+       _D("Entrance detected, timestamp: %d", __entranceTime);
        __resetHistory();
 }
 
@@ -230,7 +230,7 @@ void ctx::VisitDetector::__visitEndDetected()
                        listener->onVisitEnd();
                }
        }
-       _I("Departure detected, timestamp: %d", __departureTime);
+       _D("Departure detected, timestamp: %d", __departureTime);
 
        Interval interval(__entranceTime, __departureTime);
        Visit visit(interval, __representativesMacs);