[place-recognition] HIGH_ACCURACY <-> LOW_POWER mode runtime switch. 00/53000/1
authorMarcin Masternak <m.masternak@samsung.com>
Mon, 30 Nov 2015 18:03:25 +0000 (19:03 +0100)
committerMarcin Masternak <m.masternak@samsung.com>
Mon, 30 Nov 2015 18:03:25 +0000 (19:03 +0100)
Change-Id: Ie3e86d6ed2f695c3de7eb8982421707f94a7c254
Signed-off-by: Marcin Masternak <m.masternak@samsung.com>
19 files changed:
src/place_recognition/place_recognition.cpp
src/place_recognition/place_recognition.h
src/place_recognition/place_recognition_types.h
src/place_recognition/user_places/debug_utils.h
src/place_recognition/user_places/location_listener_iface.h
src/place_recognition/user_places/location_logger.cpp
src/place_recognition/user_places/location_logger.h
src/place_recognition/user_places/mahal.h
src/place_recognition/user_places/piecewise_lin.h
src/place_recognition/user_places/user_places.cpp
src/place_recognition/user_places/user_places.h
src/place_recognition/user_places/user_places_params.h
src/place_recognition/user_places/visit_categer.h
src/place_recognition/user_places/visit_detector.cpp
src/place_recognition/user_places/visit_detector.h
src/place_recognition/user_places/visit_listener_iface.h
src/place_recognition/user_places/wifi_listener_iface.h
src/place_recognition/user_places/wifi_logger.cpp
src/place_recognition/user_places/wifi_logger.h

index 97517a4..625d022 100644 (file)
 #include <types_internal.h>
 #include <context_mgr.h>
 #include "place_recognition.h"
+#include "user_places/user_places.h"
 
 ctx::place_recognition_provider *ctx::place_recognition_provider::__instance = NULL;
 
-ctx::place_recognition_provider::place_recognition_provider()
-{
-       engine.start();
-}
-
-ctx::place_recognition_provider::~place_recognition_provider()
-{
-       engine.stop();
-}
-
 ctx::context_provider_iface *ctx::place_recognition_provider::create(void *data)
 {
        IF_FAIL_RETURN(!__instance, __instance);
index e26b0a8..1f870af 100644 (file)
@@ -24,6 +24,7 @@
 namespace ctx {
 
        class place_recognition_provider : public context_provider_iface {
+
        public:
                static context_provider_iface *create(void *data);
                static void destroy(void *data);
@@ -38,8 +39,8 @@ namespace ctx {
                static place_recognition_provider *__instance;
                UserPlaces engine;
 
-               place_recognition_provider();
-               ~place_recognition_provider();
+               place_recognition_provider() : engine(PLACE_RECOG_HIGH_ACCURACY_MODE) {}
+               ~place_recognition_provider() {}
 
        };      /* class place_recognition_provider */
 
index 6cd87ac..50b445c 100644 (file)
@@ -18,9 +18,9 @@
 #define __CONTEXT_PLACE_RECOGNITION_TYPES__
 
 // Context Items
-#define PLACE_SUBJ_RECOGNITION                              "place/pattern/personal_poi"
+#define PLACE_SUBJ_RECOGNITION                 "place/pattern/personal_poi"
 
-#define PLACE_PRIV_RECOGNITION                              "location"
+#define PLACE_PRIV_RECOGNITION                 "location"
 
 // Database
 #define VISIT_TABLE                            "place_status_user_place_visit"
@@ -57,4 +57,9 @@ typedef enum {
        PLACE_CATEG_ID_OTHER = 3
 } place_categ_id_e;
 
+typedef enum {
+       PLACE_RECOG_HIGH_ACCURACY_MODE = 0,
+       PLACE_RECOG_LOW_POWER_MODE = 1
+} place_recog_mode_e;
+
 #endif
index bcb0252..f399f4a 100644 (file)
 namespace ctx {
 
        class DebugUtils {
+
        public:
                static std::string human_readable_date_time(time_t timestamp, std::string format, size_t size, bool utc = false);
+
        };      /* class DebugUtils */
 
 }      /* namespace ctx */
index e396403..1085030 100644 (file)
@@ -22,6 +22,7 @@
 namespace ctx {
 
        class ILocationListener {
+
        public:
                virtual ~ILocationListener() {};
                virtual void on_new_location(location_event_s location) = 0;
index 7755a0c..94057a0 100644 (file)
@@ -39,7 +39,6 @@
        } \
 }
 
-
 void ctx::LocationLogger::location_service_state_changed_cb(location_service_state_e state, void *user_data)
 {
        ctx::LocationLogger* location_logger_p = (ctx::LocationLogger *)user_data;
index bf7bc15..21bcec6 100644 (file)
@@ -64,6 +64,7 @@ namespace ctx {
        } timer_purpose_e;
 
        class LocationLogger : public timer_listener_iface, public IVisitListener {
+
        public:
                LocationLogger(ILocationListener *listener_ = nullptr,
                                bool test_mode_ = false);
index 61a75b2..b70ac70 100644 (file)
@@ -26,6 +26,7 @@ namespace ctx {
         * Class for Mahalanobis distance computation.
         */
        class MahalModel {
+
        private:
                std::vector<num_t> mean;
                std::vector<num_t> sigma; // represents square matrix row-wise
index deac882..275449c 100644 (file)
@@ -25,6 +25,7 @@ namespace ctx {
         * Piecewise linear function. Used for approximation.
         */
        class PiecewiseLin {
+
        private:
                std::vector<num_t> xs; // nodes
                std::vector<num_t> vs; // values in nodes
index 0e55a27..56fb189 100644 (file)
 #include "../place_recognition_types.h"
 #include "db_mgr.h"
 
-int ctx::UserPlaces::start()
+ctx::UserPlaces::UserPlaces(place_recog_mode_e energy_mode)
+       : visit_detector(nullptr)
 {
-       running = true;
        time_t now = std::time(nullptr);
-       visit_detector = new(std::nothrow) VisitDetector(now);
+       visit_detector = new(std::nothrow) VisitDetector(now, energy_mode);
        if (visit_detector == nullptr) {
                _E("Cannot initialize visit_detector");
-               return ERR_OUT_OF_MEMORY;
+               return;
        }
 
-       return ERR_NONE;
 }
 
-int ctx::UserPlaces::stop()
+ctx::UserPlaces::~UserPlaces()
 {
-       running = false;
+       if (places_detector_timer_id >= 0) {
+               timer_manager::remove(places_detector_timer_id);
+               _D("PlacesDetector timer removed");
+       }
 
-       if (visit_detector == nullptr) {
-               _E("No visit_detector to destroy");
-       } else {
+       if (visit_detector) {
                delete visit_detector;
        }
 
-       return ERR_NONE;
 }
 
 
-bool ctx::UserPlaces::is_running()
+void ctx::UserPlaces::set_mode(place_recog_mode_e energy_mode)
 {
-       return running;
+       if (visit_detector) {
+               visit_detector->set_mode(energy_mode);
+       }
 }
index 43fcd5a..6c11930 100644 (file)
 namespace ctx {
 
        class UserPlaces {
+
        private:
-               bool running;
                VisitDetector *visit_detector;
                int places_detector_timer_id;
        public:
-               UserPlaces() : running(false) {}
-               ~UserPlaces() {};
+               UserPlaces(place_recog_mode_e energy_mode = PLACE_RECOG_HIGH_ACCURACY_MODE);
+               ~UserPlaces();
 
-               int start();
-               int stop();
-               bool is_running();
+               void set_mode(place_recog_mode_e energy_mode);
 
        };      /* class UserPlaces */
 
index 5e5a7af..f392799 100644 (file)
 #define __CONTEXT_PLACE_STATUS_USER_PLACES_PARAMS_H__
 
 /*
- * WiFi scanning frequency (in minutes).
+ * WiFi scanning frequency (in minutes) in PLACE_RECOG_HIGH_ACCURACY_MODE.
  */
-#define WIFI_LOGGER_INTERVAL_MINUTES 3
+#define WIFI_LOGGER_INTERVAL_MINUTES_HIGH_ACCURACY 3
 
 /*
- * Time window taken into consideration (in seconds).
+ * WiFi scanning frequency (in minutes) in PLACE_RECOG_LOW_POWER_MODE.
  */
-#define VISIT_DETECTOR_PERIOD_SECONDS 360
+#define WIFI_LOGGER_INTERVAL_MINUTES_LOW_POWER 60
+
+/*
+ * Time window taken into consideration (in seconds) in PLACE_RECOG_HIGH_ACCURACY_MODE.
+ */
+#define VISIT_DETECTOR_PERIOD_SECONDS_HIGH_ACCURACY 360
+
+/*
+ * Time window taken into consideration (in seconds) in PLACE_RECOG_LOW_POWER_MODE.
+ */
+#define VISIT_DETECTOR_PERIOD_SECONDS_LOW_POWER 3600
 
 /*
  * Overlap threshold between two sets of mac addresses (overlap
index 5e95f91..6ab762e 100644 (file)
@@ -35,6 +35,7 @@ namespace ctx {
         * visit categorizer class
         */
        class VisitCateger {
+
        private:
                const static int MINUTES_IN_WEEK = 60 * 24 * 7;
                const static std::map<int, MahalModel> models;
index cace17e..c16df29 100644 (file)
        VISIT_COLUMN_CATEG_WORK " REAL, "\
        VISIT_COLUMN_CATEG_OTHER " REAL"
 
-ctx::VisitDetector::VisitDetector(time_t t_start_scan, bool test_mode_)
+ctx::VisitDetector::VisitDetector(time_t t_start_scan, place_recog_mode_e energy_mode, bool test_mode_)
        : test_mode(test_mode_)
        , location_logger(this, test_mode_)
-       , wifi_logger(this, test_mode_)
-       , current_interval(t_start_scan, t_start_scan + VISIT_DETECTOR_PERIOD_SECONDS)
+       , wifi_logger(this, energy_mode, test_mode_)
+       , current_interval(t_start_scan, t_start_scan + VISIT_DETECTOR_PERIOD_SECONDS_HIGH_ACCURACY)
+       , stable_counter(0)
+       , tolerance(VISIT_DETECTOR_TOLERANCE_DEPTH)
+       , entrance_to_place(false)
+       , period_seconds(VISIT_DETECTOR_PERIOD_SECONDS_HIGH_ACCURACY)
+       , entrance_time(0)
+       , departure_time(0)
 {
+       set_period(energy_mode);
+       current_interval = interval_s(t_start_scan, t_start_scan + period_seconds);
        current_logger = std::make_shared<mac_events>();
        stay_macs = std::make_shared<mac_set_t>();
-       history_frames.push_back(std::make_shared<frame_s>(interval_s(0, VISIT_DETECTOR_PERIOD_SECONDS))); // prehistoric period
-       entrance_to_place = false;
-       stable_counter = 0;
-       tolerance = VISIT_DETECTOR_TOLERANCE_DEPTH;
-       departure_time = 0; // prehistoric time
 
        if (test_mode) {
                detected_visits = std::make_shared<ctx::visits_t>();
@@ -119,8 +122,8 @@ std::shared_ptr<ctx::frame_s> ctx::VisitDetector::make_frame(std::shared_ptr<ctx
 
 void ctx::VisitDetector::shift_current_interval()
 {
-       current_interval.end   += VISIT_DETECTOR_PERIOD_SECONDS;
-       current_interval.start += VISIT_DETECTOR_PERIOD_SECONDS;
+       current_interval.end += period_seconds;
+       current_interval.start += period_seconds;
 }
 
 void ctx::VisitDetector::detect_entrance_or_departure(std::shared_ptr<ctx::frame_s> frame)
@@ -253,13 +256,13 @@ void ctx::VisitDetector::buffer_processing(std::shared_ptr<ctx::frame_s> frame)
 
 void ctx::VisitDetector::detect_entrance(std::shared_ptr<ctx::frame_s> current_frame)
 {
-       if (current_frame->mac_counts.empty()) { // empty frame
+       if (current_frame->mac_counts.empty() || history_frames.empty()) {
                history_reset(current_frame);
                return;
        }
 
        if (stable_counter == 0) {
-               std::shared_ptr<frame_s>  oldest_history_frame = history_frames[0];
+               std::shared_ptr<frame_s> oldest_history_frame = history_frames[0];
                stay_macs = mac_set_from_mac_counts(oldest_history_frame->mac_counts);
        }
 
@@ -414,3 +417,24 @@ void ctx::VisitDetector::on_new_location(location_event_s location_event)
        location_event.log();
        locations.push_back(location_event);
 };
+
+void ctx::VisitDetector::set_period(place_recog_mode_e energy_mode)
+{
+       switch (energy_mode) {
+       case PLACE_RECOG_LOW_POWER_MODE:
+               period_seconds = VISIT_DETECTOR_PERIOD_SECONDS_LOW_POWER;
+               break;
+       case PLACE_RECOG_HIGH_ACCURACY_MODE:
+               period_seconds = VISIT_DETECTOR_PERIOD_SECONDS_HIGH_ACCURACY;
+               break;
+       default:
+               _E("Incorrect energy mode");
+       }
+}
+
+void ctx::VisitDetector::set_mode(place_recog_mode_e energy_mode)
+{
+       _D("");
+       set_period(energy_mode);
+       wifi_logger.set_mode(energy_mode);
+}
index 9b1e914..ccde0b2 100644 (file)
@@ -33,6 +33,7 @@
 namespace ctx {
 
        class VisitDetector : public IWifiListener, ILocationListener {
+
        private:
                bool test_mode;
                std::shared_ptr<visits_t> detected_visits;   // only used in test mode
@@ -49,6 +50,7 @@ namespace ctx {
                int stable_counter;
                int tolerance;
                bool entrance_to_place;
+               int period_seconds;
 
                // fields that  are used only in case of entrance detection
                std::shared_ptr<mac_set_t> rep_macs; // mac that represent the current place
@@ -81,15 +83,17 @@ namespace ctx {
                void json_put_visit_categ(json &data, const char* key, const categs_t &categs, int categ_type);
                void json_put_visit_categs(json &data, const categs_t &categs);
                int db_insert_visit(visit_s visit);
+               void set_period(place_recog_mode_e mode);
 
        public:
-               VisitDetector(time_t _t_start_scan, bool test_mode_ = false);
+               VisitDetector(time_t t_start_scan, place_recog_mode_e energy_mode = PLACE_RECOG_HIGH_ACCURACY_MODE, bool test_mode_ = false);
                ~VisitDetector();
                interval_s get_current_interval();
                void on_wifi_scan(mac_event_s event);
                void process_current_logger();
                std::shared_ptr<visits_t> get_visits(); // only used in test mode
                void on_new_location(location_event_s location);
+               void set_mode(place_recog_mode_e energy_mode);
 
        };      /* class VisitDetector */
 
index 0aa15cc..9b87b70 100644 (file)
@@ -20,6 +20,7 @@
 namespace ctx {
 
        class IVisitListener {
+
        public:
                virtual ~IVisitListener() {};
                virtual void on_visit_start() = 0;
index 241b5f9..e9a546c 100644 (file)
@@ -22,6 +22,7 @@
 namespace ctx {
 
        class IWifiListener {
+
        public:
                virtual ~IWifiListener() {};
                virtual void on_wifi_scan(ctx::mac_event_s e) = 0;
index a68f42c..936e3a3 100644 (file)
@@ -33,7 +33,6 @@
        } \
 }
 
-
 int ctx::WifiLogger::create_table()
 {
        bool ret = db_manager::create_table(0, WIFI_TABLE_NAME, WIFI_CREATE_TABLE_COLUMNS, NULL, NULL);
@@ -65,25 +64,22 @@ int ctx::WifiLogger::db_insert_logs()
        return 0;
 }
 
-ctx::WifiLogger::WifiLogger(IWifiListener * listener_, bool test_mode_, int interval_minutes_)
+ctx::WifiLogger::WifiLogger(IWifiListener * listener_, place_recog_mode_e energy_mode, bool test_mode_)
        : test_mode(test_mode_)
        , listener(listener_)
        , last_scan_time(time_t(0))
        , last_timer_callback_time(time_t(0))
        , timer_on(false)
-       , interval_minutes(interval_minutes_)
+       , interval_minutes(WIFI_LOGGER_INTERVAL_MINUTES_HIGH_ACCURACY)
        , during_visit(false)
        , started(false)
        , running(false)
 {
        _D("CONSTRUCTOR");
-
-       if (interval_minutes <= 0) {
-               _E("interval_minutes should be greater than zero");
-       }
-
        if (test_mode) return;
 
+       set_interval(energy_mode);
+
        if (WIFI_LOGGER_DATABASE) {
                create_table();
        }
@@ -442,3 +438,32 @@ void ctx::WifiLogger::on_visit_end()
        during_visit = false;
        last_scans_pool.clear();
 }
+
+void ctx::WifiLogger::set_interval(place_recog_mode_e energy_mode)
+{
+       switch (energy_mode) {
+       case PLACE_RECOG_LOW_POWER_MODE:
+               interval_minutes = WIFI_LOGGER_INTERVAL_MINUTES_LOW_POWER;
+               break;
+       case PLACE_RECOG_HIGH_ACCURACY_MODE:
+               interval_minutes = WIFI_LOGGER_INTERVAL_MINUTES_HIGH_ACCURACY;
+               break;
+       default:
+               _E("Incorrect energy mode");
+       }
+}
+
+void ctx::WifiLogger::timer_restart()
+{
+       timer_manager::remove(timer_id);
+       timer_start(interval_minutes);
+}
+
+void ctx::WifiLogger::set_mode(place_recog_mode_e energy_mode)
+{
+       _D("");
+       set_interval(energy_mode);
+       if (WIFI_LOGGER_ACTIVE_SCANNING && timer_on) {
+               timer_restart();
+       }
+}
index d9e3b06..4db601c 100644 (file)
 namespace ctx {
 
        class WifiLogger : public timer_listener_iface, public IVisitListener {
+
        public:
                WifiLogger(IWifiListener * listener_ = nullptr,
-                               bool test_mode_ = false,
-                               int interval_minutes = WIFI_LOGGER_INTERVAL_MINUTES);
+                               place_recog_mode_e mode = PLACE_RECOG_HIGH_ACCURACY_MODE,
+                               bool test_mode_ = false);
                ~WifiLogger();
 
                void start_logging();
                void stop_logging();
+               void set_mode(place_recog_mode_e energy_mode);
 
                /* INPUT */
                void on_visit_start();
@@ -79,6 +81,7 @@ namespace ctx {
 
                void _start_logging();
                void _stop_logging();
+               void set_interval(place_recog_mode_e energy_mode);
 
                bool check_timer_id(int id);
                bool check_timer_time(time_t now);
@@ -102,6 +105,7 @@ namespace ctx {
                static const char* wifi_error_str(int error);
 
                void timer_start(time_t minutes);
+               void timer_restart();
 
        };      /* class WifiLogger */