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.
23 #include "UserPlacesTypes.h"
24 #include "UserPlacesParams.h"
25 #include "../utils/DebugUtils.h"
27 #define __MAC_STRING_COMPONENTS_SEPARATOR ':'
28 #define __MAC_SET_STRING_DELIMITER ','
30 ctx::Mac::Mac(const std::string& str)
32 std::stringstream ss(str);
35 } catch (std::runtime_error &e) {
40 ctx::Mac::Mac(const char *str)
42 std::stringstream ss(str);
45 } catch (std::runtime_error &e) {
50 std::istream& ctx::operator>>(std::istream &input, ctx::Mac &mac)
54 for (size_t i = 0; i < ctx::Mac::MAC_SIZE; i++) {
58 if (i + 1 >= ctx::Mac::MAC_SIZE)
61 if (colon != __MAC_STRING_COMPONENTS_SEPARATOR)
62 throw std::runtime_error("Invalid MAC format");
68 std::ostream& ctx::operator<<(std::ostream &output, const ctx::Mac &mac)
72 output << std::hex << std::setfill('0') << std::setw(2);
73 output << static_cast<int>(mac.c[i]);
75 if (i >= Mac::MAC_SIZE)
77 output << __MAC_STRING_COMPONENTS_SEPARATOR;
83 ctx::Mac::operator std::string() const
90 bool ctx::operator==(const Mac &m1, const Mac &m2)
92 for (size_t i = 0; i < Mac::MAC_SIZE; i++) {
93 if (m1.c[i] != m2.c[i])
99 bool ctx::operator!=(const Mac &m1, const Mac &m2)
104 bool ctx::operator<(const Mac &m1, const Mac &m2)
106 unsigned char c1, c2;
107 for (size_t i = 0; i < Mac::MAC_SIZE; i++) {
115 return false; // they are equal
118 std::istream& ctx::operator>>(std::istream &input, ctx::MacSet &macSet)
122 while (!input.eof()) {
125 } catch (std::runtime_error &e) {
126 _E("Cannot read macSet. Exception: %s", e.what());
132 delimeter = input.get();
133 if (delimeter != __MAC_SET_STRING_DELIMITER) {
141 std::ostream& ctx::operator<<(std::ostream &output, const ctx::MacSet &macSet)
143 std::vector<Mac> macVec(macSet.size());
144 std::copy(macSet.begin(), macSet.end(), macVec.begin());
145 std::sort(macVec.begin(), macVec.end());
148 for (auto &mac: macVec) {
152 output << __MAC_SET_STRING_DELIMITER;
159 void ctx::LocationEvent::log()
161 std::string time_str = DebugUtils::humanReadableDateTime(timestamp, "%T", 9);
162 #ifdef TIZEN_ENGINEER_MODE
163 _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s, method=%d",
164 coordinates.latitude,
165 coordinates.longitude,
166 coordinates.accuracy,
169 #else /* TIZEN_ENGINEER_MODE */
170 _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s",
171 coordinates.latitude,
172 coordinates.longitude,
173 coordinates.accuracy,
175 #endif /* TIZEN_ENGINEER_MODE */
178 void ctx::Visit::setLocation(Location location_)
180 locationValid = true;
181 location = location_;
184 void ctx::Visit::printShort2Stream(std::ostream &out) const
186 // print only valid visits
187 if (interval.end != 0) {
188 float duration = ((float) (interval.end - interval.start)) / 3600; // [h]
189 out << "__VISIT " << duration << "h: ";
190 out << DebugUtils::humanReadableDateTime(interval.start, "%m/%d %H:%M", 15) << " รท ";
191 out << DebugUtils::humanReadableDateTime(interval.end, "%m/%d %H:%M", 15) << std::endl;
195 bool ctx::operator==(const ctx::Visit &v1, const ctx::Visit &v2)
197 return v1.interval.start == v2.interval.start
198 && v1.interval.end == v2.interval.end
199 && v1.categs == v2.categs
200 && v1.location.latitude == v2.location.latitude
201 && v1.location.longitude == v2.location.longitude
202 && v1.location.accuracy == v2.location.accuracy
203 && v1.locationValid == v2.locationValid
204 && v1.macSet == v2.macSet;
207 ctx::MacSet ctx::macSetFromString(const std::string &str)
210 std::stringstream ss;
216 bool ctx::operator>(const Mac &m1, const Mac &m2)
221 std::shared_ptr<ctx::MacSet> ctx::macSetFromMacs2Counts(const Macs2Counts &macs2Counts)
223 std::shared_ptr<MacSet> macSet(std::make_shared<MacSet>());
224 for (auto &macCount: macs2Counts) {
225 macSet->insert(macCount.first);
230 std::shared_ptr<ctx::MacSet> ctx::macSetsUnion(const std::vector<std::shared_ptr<MacSet>> &macSets)
232 std::shared_ptr<MacSet> unionSet = std::make_shared<MacSet>();
233 for (std::shared_ptr<MacSet> macSet : macSets) {
234 unionSet->insert(macSet->begin(), macSet->end());
239 ctx::Interval::Interval(time_t start_, time_t end_) : start(start_), end(end_) {
241 _E("Negative interval, start=%d, end=%d", start_, end_);