830242f7e11305679add5ec62cec4d6e1d443950
[platform/core/context/context-provider.git] / src / my-place / facade / user_places.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <ctime>
18 #include <memory>
19 #include <Types.h>
20 #include "user_places.h"
21 #include "../place/places_detector.h"
22 #include <MyPlaceTypes.h>
23
24 ctx::UserPlaces::UserPlaces(PlaceRecogMode energyMode):
25         __visitDetector(nullptr),
26         __placesDetector(nullptr),
27         __placesDetectorTimerId(-1)
28 {
29         time_t now = std::time(nullptr);
30         __visitDetector = new(std::nothrow) VisitDetector(now, energyMode);
31         if (__visitDetector == nullptr) {
32                 _E("Cannot initialize __visitDetector");
33                 return;
34         }
35
36         __placesDetector = new(std::nothrow) PlacesDetector();
37         if (__placesDetector == nullptr) {
38                 _E("Cannot initialize __placesDetector");
39                 return;
40         }
41
42         __placesDetectorTimerId = __timerManager.setAt( // execute once every night
43                         PLACES_DETECTOR_TASK_START_HOUR,
44                         PLACES_DETECTOR_TASK_START_MINUTE,
45                         DayOfWeek::EVERYDAY,
46                         __placesDetector);
47         if (__placesDetectorTimerId < 0) {
48                 _E("PlacesDetector timer set FAIL");
49                 return;
50         } else {
51                 _D("PlacesDetector timer set SUCCESS");
52         }
53 }
54
55 ctx::UserPlaces::~UserPlaces()
56 {
57         if (__placesDetectorTimerId >= 0) {
58                 __timerManager.remove(__placesDetectorTimerId);
59                 _D("PlacesDetector timer removed");
60         }
61         if (__visitDetector)
62                 delete __visitDetector;
63         if (__placesDetector)
64                 delete __placesDetector;
65 };
66
67 std::vector<std::shared_ptr<ctx::Place>> ctx::UserPlaces::__getPlaces()
68 {
69         if (__placesDetector) {
70                 return __placesDetector->getPlaces();
71         } else {
72                 return std::vector<std::shared_ptr<ctx::Place>>();
73         }
74 }
75
76 ctx::Json ctx::UserPlaces::getPlaces()
77 {
78         std::vector<std::shared_ptr<ctx::Place>> places = __getPlaces();
79         Json dataRead = UserPlaces::__composeJson(places);
80         return dataRead;
81 }
82
83 /*
84  * Example JSON output:
85  * ------------------------------------------------
86  * {
87  *        "PlacesList": [
88  *              {
89  *                "TypeId": 2,
90  *                "Name": "Work",
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
95  *              },
96  *              {
97  *                "TypeId": 1,
98  *                "Name": "Home",
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
103  *              },
104  *              {
105  *                "TypeId": 3,
106  *                "Name": "Other",
107  *                "GeoLatitude": 10.96553,
108  *                "GeoLongitude": 50.80404,
109  *                "WifiAPs": "12:34:56:78:9a:ba",
110  *                "CreateDate": 12132346
111  *              }
112  *        ]
113  * }
114  */
115 ctx::Json ctx::UserPlaces::__composeJson(std::vector<std::shared_ptr<Place>> places)
116 {
117         ctx::Json data;
118         for (std::shared_ptr<ctx::Place> place : places) {
119                 ctx::Json placeJson;
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);
129                 }
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);
137                         }
138                         placeJson.set(NULL, PLACE_WIFI_APS, wifiApsListJson);
139                 }
140                 placeJson.set(NULL, PLACE_CREATE_DATE, static_cast<int64_t>(place->createDate));
141                 data.append(NULL, PLACE_DATA_READ, placeJson);
142         }
143         return data;
144 }
145
146 void ctx::UserPlaces::setMode(PlaceRecogMode energyMode)
147 {
148         if (__visitDetector)
149                 __visitDetector->setMode(energyMode);
150 }