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.
17 #ifndef _CONTEXT_PLACE_RECOGNITION_VISIT_DETECTOR_H_
18 #define _CONTEXT_PLACE_RECOGNITION_VISIT_DETECTOR_H_
23 #include <unordered_map>
24 #include <unordered_set>
25 #include "../facade/UserPlacesTypes.h"
27 #include <DatabaseManager.h>
28 #include "VisitListenerIface.h"
29 #include "LocationLogger.h"
30 #include "LocationListenerIface.h"
31 #include "WifiListenerIface.h"
32 #include "WifiLogger.h"
36 class VisitDetector : public IWifiListener, ILocationListener {
40 std::map<std::string, std::string> __wifiAPsMap;
41 std::shared_ptr<Visits> __detectedVisits; // only used in test mode
42 LocationLogger *__locationLogger;
43 WifiLogger *__wifiLogger;
44 std::vector<IVisitListener*> __listeners;
45 std::shared_ptr<MacEvents> __currentMacEvents;
46 Interval __currentInterval;
47 std::vector<LocationEvent> __locationEvents;
48 std::vector<std::shared_ptr<Frame>> __historyFrames; // python: history_scans + history_times
49 std::vector<std::shared_ptr<Frame>> __bufferedFrames; // python: buffered_scans + buffered_times
52 bool __entranceToPlace;
54 DatabaseManager *__dbManager;
56 // fields that are used only in case of entrance detection
57 std::shared_ptr<MacSet> __representativesMacs; // macs that represent the current place
58 std::shared_ptr<MacSet> __stayMacs; // macs that can appear in the current place
59 time_t __entranceTime;
60 time_t __departureTime;
62 bool __isValid(const Mac &mac);
63 void __shiftCurrentInterval();
64 void __detectEntranceOrDeparture(std::shared_ptr<Frame> frame);
65 void __detectEntrance(std::shared_ptr<Frame> frame);
66 void __detectDeparture(std::shared_ptr<Frame> frame);
67 void __processBuffer(std::shared_ptr<Frame> frame); // python: buffer_analysing
68 std::shared_ptr<Frame> __makeFrame(std::shared_ptr<MacEvents> macEvents, Interval interval); // python: scans2fingerprint
69 void __resetHistory();
70 void __resetHistory(std::shared_ptr<Frame> frame);
71 void __visitStartDetected();
72 void __visitEndDetected();
73 void __putLocationToVisit(Visit &visit);
74 std::shared_ptr<MacSet> __selectRepresentatives(const std::vector<std::shared_ptr<Frame>> &frames);
75 std::shared_ptr<MacSet> __macSetOfGreaterOrEqualShare(const Macs2Shares &macs2Shares, share_t threshold);
76 std::shared_ptr<Macs2Shares> __macSharesFromCounts(Macs2Counts const &macs2Counts, count_t denominator); // python: response_rate
77 share_t __calcMaxShare(const Macs2Shares &macs2Shares);
78 bool __isDisjoint(const Macs2Counts &macs2Counts, const MacSet &macSet);
79 bool __protrudesFrom(const Macs2Counts &macs2Counts, const MacSet &macSet);
80 void __setPeriod(PlaceRecogMode mode);
81 void __processCurrentLogger();
82 std::shared_ptr<Visits> __getVisits();
85 void __dbCreateTables();
86 int __dbInsertVisit(Visit visit);
87 int __dbInsertWifiAPsMap(Visit visit);
88 void __putVisitCategToJson(const char* key, const Categs &categs, int categType, Json &data);
89 void __putVisitCategsToJson(const Categs &categs, Json &data);
92 void onWifiScan(MacEvent event);
93 void onNewLocation(LocationEvent location);
95 void __categorize(Visit &visit);
98 VisitDetector(time_t startScan, PlaceRecogMode energyMode = PLACE_RECOG_HIGH_ACCURACY_MODE, bool testMode = false);
101 void setMode(PlaceRecogMode energyMode);
103 }; /* class VisitDetector */
105 } /* namespace ctx */
107 #endif /* End of _CONTEXT_PLACE_RECOGNITION_VISIT_DETECTOR_H_ */