Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SoftSensorPlugin / IndoorTrajectorySensor / src / Trajectory.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20 #include "Trajectory.h"
21
22 #include <stdlib.h>
23 #include <iostream>
24
25 // ServiceInfo Class member Function define.
26 ServiceInfo::ServiceInfo( void ) : timestamp(""), things_ea(0) {    validThings.clear(); }
27
28 ServiceInfo::~ServiceInfo()
29 {
30     if ( things_ea )
31     {
32         validThings.clear();
33         things_ea = 0;
34     }
35 }
36
37 MAP_TYPE::iterator ServiceInfo::ThingBegin( void )
38 {
39     return validThings.begin();
40 }
41
42 MAP_TYPE::iterator ServiceInfo::ThingEnd( void )
43 {
44     return validThings.end();
45 }
46
47 void ServiceInfo::insert( ThingInfo &thing )
48 {
49     if ( find(thing.ID) == NULL )
50     {
51         validThings[thing.ID] = thing;
52         things_ea++;
53     }
54 }
55
56 ThingInfo *ServiceInfo::find( std::string thingID )
57 {
58     MAP_TYPE::iterator itr = validThings.find( thingID );
59
60     if ( itr == validThings.end() )
61         return NULL;
62
63     return &itr->second;
64 }
65
66 int ServiceInfo::getThingsEA( void )
67 {
68     return things_ea;
69 }
70
71 //
72 // CurrentService Class member Function define.
73 CurrentService::CurrentService( void )
74 {
75     trackee.ID = "";
76 }
77
78 CurrentService::CurrentService( std::string trackeeID, std::string time)
79 {
80     trackee.ID = trackeeID;
81     service.timestamp = time;
82 }
83
84 CurrentService::~CurrentService()
85 {
86     trackee.ID = "";
87 }
88
89 //
90 // Trajectory Class member Function define.
91 Trajectory::Trajectory( void )
92 {
93     trackee.ID = "";
94     ServiceList.clear();
95     latestServiceNum = ServiceList.size();
96     resultList = NULL;
97 }
98
99 Trajectory::Trajectory( std::string trackeeID )
100 {
101     trackee.ID = trackeeID;
102     ServiceList.clear();
103     latestServiceNum = ServiceList.size();
104     resultList = NULL;
105 }
106
107 Trajectory::~Trajectory( void )
108 {
109     ServiceList.clear();
110     latestServiceNum = ServiceList.size();
111     if ( resultList )
112     {
113         delete resultList;
114         resultList = NULL;
115     }
116 }
117
118 std::string Trajectory::getTrackeeID( void )
119 {
120     return trackee.ID;
121 }
122
123 int Trajectory::getTrajectoryDeepSize( void )
124 {
125     return latestServiceNum;
126 }
127
128 int Trajectory::TrajectoryUpdate( CurrentService &CurrentSvc )
129 {
130     if ( latestServiceNum <= 0 )
131         goto INSERT_ELEMENT;
132
133     if ( trackee.ID.compare( CurrentSvc.trackee.ID ) == 0 )
134     {
135         // if trackee.ID of CurrentSvc == trackee.ID ,
136         ServiceInfo *recentSvc = &ServiceList[latestServiceNum - 1];
137         ServiceInfo *crtSvc = &CurrentSvc.service;
138
139         if ( recentSvc->getThingsEA() != crtSvc->getThingsEA() )
140             goto INSERT_ELEMENT ;
141
142         MAP_TYPE::iterator itr = crtSvc->ThingBegin();
143         for ( ; itr != crtSvc->ThingEnd() ; itr++ )
144         {
145             if ( recentSvc->find( itr->second.ID ) == NULL )
146                 goto INSERT_ELEMENT;
147         }
148
149         return latestServiceNum;
150     }
151
152 INSERT_ELEMENT :
153     ServiceList.push_back( CurrentSvc.service );
154     latestServiceNum = ServiceList.size();
155
156     return latestServiceNum;
157 }
158
159 std::vector<ServiceInfo> *Trajectory::getTrajectoryList( int latestNum, int ListSize )
160 {
161     if ( latestServiceNum <= 0 || latestServiceNum <= latestNum )
162         return NULL;
163
164     if ( resultList )
165     {
166         delete resultList;
167         resultList = NULL;
168     }
169     resultList = new std::vector<ServiceInfo>;
170
171     int endIndex = 0;
172     if ( latestServiceNum >= (latestNum + ListSize) && ListSize > 0 )
173     {
174         endIndex = latestServiceNum - latestNum - ListSize;
175     }
176
177     for ( int i = (latestServiceNum - 1 - latestNum) ; i >= endIndex ; i-- )
178         resultList->push_back( ServiceList[i] );
179
180     return resultList;
181 }
182
183
184