Skip to content

Commit

Permalink
For #100 - implemented calculation for device specific geo-fences
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Apr 3, 2015
1 parent 7049356 commit 521c68d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import javax.servlet.ServletException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -89,7 +91,7 @@ public static class GeoFenceDetector extends ScheduledTask {
@Transactional
public void doWork() throws Exception {
Date currentDate = new Date();
List<GeoFence> geoFences = entityManager.get().createQuery("SELECT g FROM GeoFence g", GeoFence.class).getResultList();
Set<GeoFence> geoFences = new HashSet<GeoFence>(entityManager.get().createQuery("SELECT g FROM GeoFence g LEFT JOIN FETCH g.devices", GeoFence.class).getResultList());
if (geoFences.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.awt.*;
import java.awt.geom.Path2D;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,7 +53,7 @@ private static class GeoFenceData {

private final Map<GeoFence, GeoFenceData> geoFences;

public GeoFenceCalculator(List<GeoFence> geoFences) {
public GeoFenceCalculator(Collection<GeoFence> geoFences) {
this.geoFences = new HashMap<GeoFence, GeoFenceData>(geoFences.size());
for (GeoFence geoFence : geoFences) {
List<GeoFence.LonLat> points = geoFence.points();
Expand All @@ -76,6 +77,12 @@ public GeoFenceCalculator(List<GeoFence> geoFences) {
}

public boolean contains(GeoFence geoFence, Position position) {
// if geo-fence is device specific then check whether position's device matches
if (!geoFence.isAllDevices() &&
(position.getDevice() == null || !geoFence.getDevices().contains(position.getDevice()))) {
return false;
}

GeoFenceData data = geoFences.get(geoFence);
switch (geoFence.getType()) {
case POLYGON:
Expand Down

0 comments on commit 521c68d

Please sign in to comment.