Package com.gmv.its.deepsy.hal.geofencing

Classes

interface  Geofence
 Interface that represents a geofence. More...
 
interface  GeofenceBufferedLine
 Interface that represents a buffered line geofence. More...
 
interface  GeofenceCircle
 Interface that represents a circle geofence. More...
 
interface  GeofenceEvent
 Geofence Event. More...
 
interface  GeofenceObserver
 Geofence list observer. More...
 
interface  GeofencePolygon
 Interface that represents a polygon geofence. More...
 
class  GeofencingLowLevelException
 Geofencing low level exception. More...
 
interface  GeofencingManager
 Geofencing Manager. More...
 
class  GeofencingNotAvailableException
 Geofencing not available exception. More...
 
interface  Point
 Class that represents a point. More...
 
interface  SetOfGeofences
 Interface that represents a set of geofences. More...
 
interface  SetOfGeofencesEvent
 Set of geofences Event Event. More...
 
interface  SetOfGeofencesListEvent
 Set of geofences event. More...
 
interface  SetOfGeofencesListObserver
 Set of geofences list observer. More...
 
interface  SetOfGeofencesObserver
 Set of geofencesObserver list observer. More...
 

Detailed Description

To work with Deepsy geofencing service it is necessary to use DeepsyGeofencingManager located in com.gmv.its.deepsy.geofencing for getting the initial DeepsyGeofencingManager object.

GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance();


GeofencingManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:

GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance("192.168.1.10");


GeofencingManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a list of the set of geofences objects:

try (GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance()){
List<SetOfGeofences> sogList = geofencingManager.getSetOfGeofences();
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
}


If this object is not inside a try with resources block, it is recommended to call to geofencingManager.close() in order to finalize properly;

geofencingManager.close();

Service availability

To detect service availability the DeepsyGeofencingManager implements Manager interface which provides methods for getting availability and subscribing to availability events.

class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() + " in "+ event.getManager().getIp() + " "+ event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance()){
geofencingManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}


Examples

Getting available sets of geofences and every geofence of the set

package com.gmv.its.dpyjavaexamples.geofencing;
import java.util.List;
import com.gmv.its.deepsy.geofencing.DeepsyGeofencingManager;
public class GetGeofencesApp {
public static void main(String[] args) {
GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance("192.168.0.84");
try {
List<SetOfGeofences> sogList = geofencingManager.getSetOfGeofences();
System.out.println("There are " + sogList.size() + " set of geofences");
for (SetOfGeofences sog : sogList) {
System.out.println("- " + sog.getId());
List<Geofence> geofencesList = sog.getGeofences();
for (Geofence geo : geofencesList) {
System.out.println(" |- " + geo.getName());
System.out.print(" | ");
if (geo instanceof GeofenceBufferedLine) {
for(Point point : ((GeofenceBufferedLine)geo).getPoints()) {
System.out.print("["+ point.getLatitude() + "," + point.getLongitude()+"] ");
}
System.out.println();
System.out.println(" | Radius "+((GeofenceBufferedLine)geo).getRadius());
}else if(geo instanceof GeofenceCircle) {
Point point = ((GeofenceCircle)geo).getCenter();
System.out.print("["+ point.getLatitude() + "," + point.getLongitude()+"] ");
System.out.println();
System.out.println(" | Radius "+((GeofenceCircle)geo).getRadius());
}else if (geo instanceof GeofencePolygon) {
for(Point point : ((GeofencePolygon)geo).getPoints()) {
System.out.print("["+ point.getLatitude() + "," + point.getLongitude()+"] ");
}
System.out.println();
}
}
}
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
}
// If manager object is not inside a try with resources block, it is necessary to call to .close() in order to finalize properly;
geofencingManager.close();
}
}


Adding and enabling geofences

package com.gmv.its.dpyjavaexamples.geofencing;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import com.gmv.its.deepsy.geofencing.DeepsyGeofencingManager;
class AppGeofenceObserver implements GeofenceObserver {
public void notify(GeofenceEvent event) {
System.out.println("[ExampleAPP] [AppGeofenceObserver] EVENT Geofence " + event.getGeofence().getId() + " " + event.getAction().name());
}
}
class AppSogObserver implements SetOfGeofencesObserver {
AppGeofenceObserver myGeofenceObserver;
AppSogObserver(AppGeofenceObserver geofenceObserver) {
myGeofenceObserver = geofenceObserver;
}
public void notify(SetOfGeofencesEvent event) {
System.out.println("[ExampleAPP] [SetOfGeofencesEvent] EVENT Geofence " + event.getGeofence().getId() + " " + event.getAction().name() + " in sog: " + event.getSetOfGeofences().getId());
if (event.getAction() == SetOfGeofencesAction.ADDED) {
try {
event.getGeofence().subscribe(myGeofenceObserver);
} catch (CannotSubscribeException e) {
e.printStackTrace();
}
}
}
}
class AppSogListObserver implements SetOfGeofencesListObserver {
AppSogObserver mySogObserver;
AppGeofenceObserver myGeofenceObserver;
AppSogListObserver(AppSogObserver sogObserver, AppGeofenceObserver geofenceObserver) {
mySogObserver = sogObserver;
myGeofenceObserver = geofenceObserver;
}
public void notify(SetOfGeofencesListEvent event) {
System.out.println("[ExampleAPP] [SetOfGeofencesListEvent] EVENT Set of geofences " + event.getSetOfGeofences().getId() + " " + event.getAction().name());
if (event.getAction() == SetOfGeofencesListAction.ADDED) {
try {
event.getSetOfGeofences().subscribe(mySogObserver);
for (Geofence g : event.getSetOfGeofences().getGeofences()) {
g.subscribe(myGeofenceObserver);
}
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
}
} else {
try {
event.getSetOfGeofences().unSubscribe(mySogObserver);
} catch (ObserverNotSubscribedException e) {
e.printStackTrace();
}
}
}
}
public class AddAndEnableGeofencesApp {
public static void main(String[] args) {
AppGeofenceObserver myGeofenceObserver = new AppGeofenceObserver();
AppSogObserver mySogObserver = new AppSogObserver(myGeofenceObserver);
AppSogListObserver mySogListObserver = new AppSogListObserver(mySogObserver, myGeofenceObserver);
try (GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance("192.168.0.1")){
System.out.println("[ExampleAPP] ----------- Starting ------------ " );
geofencingManager.subscribe(mySogListObserver);
/*System.out.println("[ExampleAPP] Checking for geofences to remove , soglist size is " + sogList0.size());
for (SetOfGeofences sog : geofencingManager.getSetOfGeofences()) {
for (Geofence geo : sog.getGeofences()) {
System.out.println("[ExampleAPP] Sog " + sog.getId() + ": disabling and removing geofence " + geo.getId());
if (geo.getEnabled()) {
geo.disable();
}
sog.removeGeofence(geo);
}
}*/
Object sogArray[] = geofencingManager.getSetOfGeofences().toArray();
for (int i = 0; i < sogArray.length; i++) {
System.out.println("[ExampleAPP] Removing geofences from sog " + ((SetOfGeofences) sogArray[i]));
Object geoArray[] = ((SetOfGeofences) sogArray[i]).getGeofences().toArray();
System.out.println("[ExampleAPP] geofencesList2 size is " + geoArray.length);
for (int j = 0; j < geoArray.length; j++) {
System.out.println("[ExampleAPP] Disabling and removing geofence " + ((Geofence) geoArray[j]).getId());
if (((Geofence) geoArray[j]).getEnabled()) {
((Geofence) geoArray[j]).disable();
}
((SetOfGeofences) sogArray[i]).removeGeofence((Geofence) geoArray[j]);
}
}
System.out.println("[ExampleAPP] ----------- Database clean 1 ------------ " );
SetOfGeofences myset = geofencingManager.createSetOfGeofences("my_set");
System.out.println("[ExampleAPP] Sog my_set created");
System.out.println("[ExampleAPP] Sog geofences size is " + myset.getGeofences().size());
System.out.println("[ExampleAPP] mySogObserver subscribed" );
myset.addCircle(1, "poi", true, new Point(40, 4), 10);
List<Point> pointList1 = new ArrayList<Point>();
pointList1.add(new Point(41.651856, -4.728325));
pointList1.add(new Point(41.652217, -4.729280));
pointList1.add(new Point(41.652441, -4.727939));
myset.addPolygon(2, "polygon", true, pointList1);
List<Point> pointList2 = new ArrayList<Point>();
pointList2.add(new Point(42.201922, -4.396001));
pointList2.add(new Point(42.202578, -4.394323));
pointList2.add(new Point(42.203107, -4.393882));
myset.addBufferedLine(3, "line", true, pointList2, 12);
System.out.println("[ExampleAPP] Added Geofence x 3, OK " );
System.out.println("[ExampleAPP] ----------- Geofences added 1 ------------ " );
List<SetOfGeofences> sogList = geofencingManager.getSetOfGeofences();
for (SetOfGeofences sog : sogList) {
System.out.println("[ExampleAPP] Enabling geofences in sog " + sog.getId());
List<Geofence> geofencesList = sog.getGeofences();
System.out.println("[ExampleAPP] geofencesList size is " + geofencesList.size());
for (Geofence geo : geofencesList) {
System.out.println("[ExampleAPP] Enabling geofence " + geo.getId());
geo.enable();
}
}
System.out.println("[ExampleAPP] ----------- Geofences enabled 1 ------------ " );
System.out.println("[ExampleAPP] Sleeping for 5");
Thread.sleep(5000);
Object sogArray2[] = geofencingManager.getSetOfGeofences().toArray();
for (int i = 0; i < sogArray2.length; i++) {
System.out.println("[ExampleAPP] Removing geofences from sog " + ((SetOfGeofences) sogArray2[i]));
Object geoArray2[] = ((SetOfGeofences) sogArray2[i]).getGeofences().toArray();
System.out.println("[ExampleAPP] geofencesList2 size is " + geoArray2.length);
for (int j = 0; j < geoArray2.length; j++) {
System.out.println("[ExampleAPP] Disabling and removing geofence " + ((Geofence) geoArray2[j]).getId());
if (((Geofence) geoArray2[j]).getEnabled()) {
((Geofence) geoArray2[j]).disable();
}
((SetOfGeofences) sogArray2[i]).removeGeofence((Geofence) geoArray2[j]);
}
}
System.out.println("[ExampleAPP] ----------- Database clean 2 ------------ " );
System.out.println("[ExampleAPP] Sleeping for 5");
Thread.sleep(5000);
SetOfGeofences myset2 = geofencingManager.createSetOfGeofences("my_set");
myset2.addCircle(1, "poi", true, new Point(40, 4), 10);
myset2.addPolygon(2, "polygon", true, pointList1);
myset2.addBufferedLine(3, "line", true, pointList2, 12);
System.out.println("[ExampleAPP] ----------- Geofences added 2------------ " );
List<SetOfGeofences> sogList3 = geofencingManager.getSetOfGeofences();
for (SetOfGeofences sog3 : sogList3) {
System.out.println("[ExampleAPP] Enabling geofences in sog " + sog3.getId());
List<Geofence> geofencesList3 = sog3.getGeofences();
System.out.println("[ExampleAPP] geofencesList3 size is " + geofencesList3.size());
for (Geofence geo : geofencesList3) {
geo.enable();
}
}
System.out.println("[ExampleAPP] ----------- Geofences enabled 2------------ " );
System.out.println("Sleeping for 20" );
Thread.sleep(20000);
System.out.println("[ExampleAPP] ----------- Finishing ------------ " );
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


Subscribe to events
There are 4 types of events in the geofencing system:

  • SetOfGeofencesListEvent : This event happens when a set of geofences is added to or removed from the system. To receive this kind of events, it is necessary to subscribe to the GeofencingManager object with an object implementing interface SetOfGeofencesListObserver
  • SetOfGeofencesEvent : This event happens when a a geofence is added or removed from a set of geofences. To receive this kind of events, it is necessary to subscribe to the SetOfGeofences object with an object implementing interface SetOfGeofencesObserver
  • GeofenceEvent : This event happens when a geofence is enabled/disabled or when the system enters/exits from the geofence. To receive this kind of events, it is necessary to subscribe to the Geofence object with an object implementing interface GeofenceObserver

This example subscribes to every event.

package com.gmv.its.dpyjavaexamples.geofencing;
import java.util.List;
import com.gmv.its.deepsy.geofencing.DeepsyGeofencingManager;
class MyGeofenceObserver implements GeofenceObserver {
public void notify(GeofenceEvent event) {
System.out.println("Geofence " + event.getGeofence().getId() + " " + event.getAction().name() + " with angle: " + event.getAngle());
}
}
class MySogObserver implements SetOfGeofencesObserver {
MyGeofenceObserver myGeofenceObserver;
MySogObserver(MyGeofenceObserver geofenceObserver) {
myGeofenceObserver = geofenceObserver;
}
public void notify(SetOfGeofencesEvent event) {
System.out.println("Geofence " + event.getGeofence().getId() + " " + event.getAction().name() + " in sog: " + event.getSetOfGeofences().getId());
if (event.getAction() == SetOfGeofencesAction.ADDED) {
try {
event.getGeofence().subscribe(myGeofenceObserver);
} catch (CannotSubscribeException e) {
e.printStackTrace();
}
}
}
}
class MySogListObserver implements SetOfGeofencesListObserver {
MySogObserver mySogObserver;
MyGeofenceObserver myGeofenceObserver;
MySogListObserver(MySogObserver sogObserver, MyGeofenceObserver geofenceObserver) {
mySogObserver = sogObserver;
myGeofenceObserver = geofenceObserver;
}
public void notify(SetOfGeofencesListEvent event) {
System.out.println("Set of geofences " + event.getSetOfGeofences().getId() + " " + event.getAction().name());
if (event.getAction() == SetOfGeofencesListAction.ADDED) {
try {
event.getSetOfGeofences().subscribe(mySogObserver);
for (Geofence g : event.getSetOfGeofences().getGeofences()) {
g.subscribe(myGeofenceObserver);
}
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
}
} else {
try {
event.getSetOfGeofences().unSubscribe(mySogObserver);
} catch (ObserverNotSubscribedException e) {
e.printStackTrace();
}
}
}
}
public class GeofencesEventsApp {
public static void main(String[] args) {
MyGeofenceObserver myGeofenceObserver = new MyGeofenceObserver();
MySogObserver mySogObserver = new MySogObserver(myGeofenceObserver);
MySogListObserver mySogListObserver = new MySogListObserver(mySogObserver, myGeofenceObserver);
try (GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance("192.168.0.144")){
List<SetOfGeofences> sogList = geofencingManager.getSetOfGeofences();
geofencingManager.subscribe(mySogListObserver);
for (SetOfGeofences sog : sogList) {
sog.subscribe(mySogObserver);
for (Geofence geo : sog.getGeofences()) {
geo.subscribe(myGeofenceObserver);
}
}
Thread.sleep(10000);
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


Feed Custom Position to Geofencing service
It is possible to feed the geofencing service with custom positions. If you want to use GNSS positions again, you must disable this feature. This is an example of how to use custom position feeding:

package com.gmv.its.dpyjavaexamples.geofencing;
import java.util.List;
import com.gmv.its.deepsy.geofencing.DeepsyGeofencingManager;
public class FeedPositionApp {
public static void main(String[] args) {
try(GeofencingManager geofencingManager = DeepsyGeofencingManager.getInstance("192.168.0.140")) {
double lat = 41.516083;
double lon = -4.716939;
geofencingManager.feedPosition(new Point(lon, lat));
System.out.println("[ExampleAPP] ----------- Feed Custom Position ------------ " );
Thread.sleep(1500);
geofencingManager.disableFeedPosition();
System.out.println("[ExampleAPP] ----------- Disable Feed Custom Position ------------ " );
Thread.sleep(1500);
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}