2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "user_places.h"
21 #include "../place/places_detector.h"
22 #include <MyPlaceTypes.h>
24 ctx::UserPlaces::UserPlaces(PlaceRecogMode energyMode):
25 __visitDetector(nullptr),
26 __placesDetector(nullptr),
27 __placesDetectorTimerId(-1)
29 time_t now = std::time(nullptr);
30 __visitDetector = new(std::nothrow) VisitDetector(now, energyMode);
31 if (__visitDetector == nullptr) {
32 _E("Cannot initialize __visitDetector");
36 __placesDetector = new(std::nothrow) PlacesDetector();
37 if (__placesDetector == nullptr) {
38 _E("Cannot initialize __placesDetector");
42 __placesDetectorTimerId = __timerManager.setAt( // execute once every night
43 PLACES_DETECTOR_TASK_START_HOUR,
44 PLACES_DETECTOR_TASK_START_MINUTE,
47 if (__placesDetectorTimerId < 0) {
48 _E("PlacesDetector timer set FAIL");
51 _D("PlacesDetector timer set SUCCESS");
55 ctx::UserPlaces::~UserPlaces()
57 if (__placesDetectorTimerId >= 0) {
58 __timerManager.remove(__placesDetectorTimerId);
59 _D("PlacesDetector timer removed");
62 delete __visitDetector;
64 delete __placesDetector;
67 std::vector<std::shared_ptr<ctx::Place>> ctx::UserPlaces::__getPlaces()
69 if (__placesDetector) {
70 return __placesDetector->getPlaces();
72 return std::vector<std::shared_ptr<ctx::Place>>();
76 ctx::Json ctx::UserPlaces::getPlaces()
78 std::vector<std::shared_ptr<ctx::Place>> places = __getPlaces();
79 Json dataRead = UserPlaces::__composeJson(places);
84 * Example JSON output:
85 * ------------------------------------------------
91 * "GeoLatitude": 10.94433,
92 * "GeoLongitude": 50.85504,
93 * "WifiAPs": "00:1f:f3:5b:2b:1f,15:34:56:78:9a:ba,13:34:56:78:9a:ba",
94 * "CreateDate": 12132567
99 * "GeoLatitude": 10.96233,
100 * "GeoLongitude": 50.84304,
101 * "WifiAPs": "aa:bb:cc:dd:ee:ff,10:34:56:78:9a:bc",
102 * "CreateDate": 12132889
107 * "GeoLatitude": 10.96553,
108 * "GeoLongitude": 50.80404,
109 * "WifiAPs": "12:34:56:78:9a:ba",
110 * "CreateDate": 12132346
115 ctx::Json ctx::UserPlaces::__composeJson(std::vector<std::shared_ptr<Place>> places)
118 for (std::shared_ptr<ctx::Place> place : places) {
120 placeJson.set(NULL, PLACE_CATEG_ID, static_cast<int>(place->categId));
121 placeJson.set(NULL, PLACE_CATEG_CONFIDENCE, static_cast<double>(place->categConfidence));
122 placeJson.set(NULL, PLACE_NAME, place->name);
123 if (place->locationValid) {
124 ctx::Json locationJson;
125 locationJson.set(NULL, PLACE_LOCATION_LATITUDE, static_cast<double>(place->location.latitude));
126 locationJson.set(NULL, PLACE_LOCATION_LONGITUDE, static_cast<double>(place->location.longitude));
127 locationJson.set(NULL, PLACE_LOCATION_ACCURACY, static_cast<double>(place->location.accuracy));
128 placeJson.set(NULL, PLACE_LOCATION, locationJson);
130 if (place->wifiAps.size()) {
131 ctx::Json wifiApsListJson;
132 for (std::pair<std::string, std::string> ap : place->wifiAps) {
133 ctx::Json wifiApJson;
134 wifiApJson.set(NULL, PLACE_WIFI_AP_MAC, ap.first);
135 wifiApJson.set(NULL, PLACE_WIFI_AP_NETWORK_NAME, ap.second);
136 wifiApsListJson.append(NULL, PLACE_WIFI_APS, wifiApJson);
138 placeJson.set(NULL, PLACE_WIFI_APS, wifiApsListJson);
140 placeJson.set(NULL, PLACE_CREATE_DATE, static_cast<int64_t>(place->createDate));
141 data.append(NULL, PLACE_DATA_READ, placeJson);
146 void ctx::UserPlaces::setMode(PlaceRecogMode energyMode)
149 __visitDetector->setMode(energyMode);