-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCBGeoDataStream.java
More file actions
176 lines (146 loc) · 5.21 KB
/
Copy pathCBGeoDataStream.java
File metadata and controls
176 lines (146 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright (c) 2013 Cloudbase.io Limited
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
*/
package com.cloudbase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
/**
* Opens a persistent connection to a particular geo-coded connection on a
* cloudbase.io Cloud Database and retrieves geo located data for the application.
*
* Data is handed back to the application using the protocol.
*
* This is meant to be used for augment reality applications.
*/
public class CBGeoDataStream implements CBHelperResponder {
protected static final long queryInterval = 2000; // 2 seconds
protected static final long refreshRadiusRatio = 4;
protected CBHelper helper;
protected CBLocation previousPosition;
protected double previousSpeed;
protected double queryRadius;
protected Map<String, CBGeoLocatedObject> foundObjects;
protected String streamName;
private Timer queryTimer;
/**
* The object implementing the CBGeoDataStreamDelegate interface
*/
private CBGeoDataStreamResponder responder;
/**
* The radius for the next search in meters from the point returned by the
* getLatestPosition method
*/
private double searchRadius;
/**
* The collection on which to run the search
*/
private String collection;
/**
* Initializes a new CBGeoDataStream object and uses the given CBHelper
* object to retrieve data from the cloudbase.io APIS.
*
* @param name A unique identifier for this stream object. It's always passed to the responder
* @param helper An initialized CBHelper object
* @param collection The name of the collection to search
*/
public CBGeoDataStream(String name, CBHelper helper, String collection) {
this.helper = helper;
this.streamName = name;
//helper.
this.setCollection(collection);
this.previousSpeed = 0.0;
this.queryRadius = 50; // by default we use 50 meters
this.foundObjects = new HashMap<String, CBGeoLocatedObject>();
}
/**
* Begins querying the cloudbase.io APIs and returning data periodically.
*
* @param resp The responder object to receive data
*/
public void startStream(CBGeoDataStreamResponder resp) {
this.responder = resp;
this.queryTimer = new Timer();
CBGeoDataStreamTask task = new CBGeoDataStreamTask(this);
this.queryTimer.scheduleAtFixedRate(task, CBGeoDataStream.queryInterval, CBGeoDataStream.queryInterval);
}
/**
* Stops the data stream
*/
public void stopStream() {
this.queryTimer.cancel();
this.queryTimer.purge();
}
public CBGeoDataStreamResponder getResponder() {
return responder;
}
public void setResponder(CBGeoDataStreamResponder responder) {
this.responder = responder;
}
public double getSearchRadius() {
return searchRadius;
}
public void setSearchRadius(double searchRadius) {
this.searchRadius = searchRadius;
this.queryRadius = searchRadius;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
@Override
@SuppressWarnings("unchecked")
public void handleResponse(CBQueuedRequest req, CBHelperResponse res) {
if (res.isSuccess()) {
List<Map<String, Object>> output = (List<Map<String, Object>>) res.getData();
if (output.size() > 0) {
for (Map<String, Object> curItem : output) {
CBGeoLocatedObject obj = new CBGeoLocatedObject();
CBLocation loc = new CBLocation();
Map<String, Float> locationData = (Map<String, Float>) curItem.get("cb_location");
loc.setLat(Float.valueOf(locationData.get("lat")));
loc.setLng(Float.valueOf(locationData.get("lng")));
if (curItem.containsKey("cb_location_altitude")) {
loc.setAlt(Float.valueOf((Float) curItem.get("cb_location_altitude")));
obj.setAltitude(Double.valueOf((Double) curItem.get("cb_location_altitude")));
}
obj.setCoordinate(loc);
curItem.remove("cb_location");
curItem.remove("cb_location_altitude");
obj.setObjectData(curItem);
this.foundObjects.put(String.valueOf(obj.hash()), obj);
this.responder.receivedPoint(this.streamName, obj);
}
}
} else {
if (this.helper.isDebugMode()) {
//Log.d(CBHelper.logTag, "Error while calling the cloudbase.io APIs");
}
}
List<String> itemsToRemove = new ArrayList<String>();
Iterator<String> it = this.foundObjects.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
CBGeoLocatedObject curObj = this.foundObjects.get(key);
if (curObj.getCoordinate().distanceTo(this.previousPosition) > this.searchRadius) {
this.responder.removingPoint(this.streamName, curObj);
itemsToRemove.add(key);
}
}
for (String curKey : itemsToRemove) {
this.foundObjects.remove(curKey);
}
}
}