C# Geofencing API
Collaboration diagram for C# Geofencing API:

Modules

 Enumerations
 
 Events related with Geofencing Service
 

Classes

class  Geofence
 Implementation of IGeofence.
 
class  GeofencingManager
 Implementation of IGeofencingManager. Start point for interacting with Deepsy Geofencing service More...
 
interface  IGeofence
 Interface for geofence. More...
 
interface  IGeofencingManager
 Geofencing Manager allows to manage the geofences in devices running deepsy More...
 

Detailed Description

To work with Deepsy Geofencing Service it is necessary to use GeofencingManager located in GMV.ITS.HAL.DEEPSY.Geofencing.Facade.

GeofencingManager geofencingManager = new GeofencingManager("192.168.1.234");


To obtain a list of Geofence objects:

try
{
GeofencingManager geofencingManager = new GeofencingManager("192.168.1.234");
IEnumerable<IGeofence> geofences = geofencingManager.GetGeofences(<sogId>);
}
catch (NotAvailableException e)
{
Console.WriteLine(e.Message);
}
catch (LowLevelException e)
{
Console.WriteLine(e.Message);
}

Examples

  • Add and Delete Geofences
    Adding, getting and deleting geofences from the machine

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Geofencing
    {
    public static class AddAndDeleteGeofences
    {
    public static void Main(string[] args)
    {
    try
    {
    string deepsyServerIp = args.Length == 0 ? "192.168.0.140" : args[0];
    using (GeofencingManager geofencingManager = new GeofencingManager(deepsyServerIp))
    {
    string sog1 = "mysog1";
    string sog2 = "mysog2";
    Console.WriteLine($"Adding circle geofence to {sog1}");
    IGeofence CircleGeofence = geofencingManager.AddCircleGeofence(sog1, 1, "circle_geofence", false, new Position(41.516842, -4.716624), 10);
    Console.WriteLine($"Adding polygon geofence to {sog1}");
    List<IPosition> polygon_positions = new List<IPosition>();
    polygon_positions.Add(new Position(41.651856, -4.728325));
    polygon_positions.Add(new Position(41.652217, -4.729280));
    polygon_positions.Add(new Position(41.652441, -4.727939));
    IGeofence PolygonGeofence = geofencingManager.AddPolygonGeofence(sog1, 2, "polygon_geofence", false, polygon_positions);
    Console.WriteLine($"Adding line geofence to {sog2}");
    List<IPosition> line_positions = new List<IPosition>();
    line_positions.Add(new Position(42.201922, -4.396001));
    line_positions.Add(new Position(42.202578, -4.394323));
    line_positions.Add(new Position(42.203107, -4.393882));
    IGeofence LineGeofence = geofencingManager.AddBufferedLineGeofence(sog2, 3, "line_geofence", false, line_positions, 12);
    Thread.Sleep(1000);
    List<string> sogsList = geofencingManager.GetSOGs().ToList();
    Console.WriteLine($"There are {sogsList.Count()} SOGs");
    if (sogsList != null)
    {
    foreach (string s in sogsList)
    {
    List<IGeofence> geofences = geofencingManager.GetGeofences(s).ToList();
    Console.WriteLine($"There are {geofences.Count()} geofences in SOG {s}");
    foreach (IGeofence g in geofences)
    {
    Console.WriteLine($"Geofence {g.Id} of SOG {s}:");
    foreach (IPosition p in g.Positions)
    {
    Console.WriteLine($"Point ({p.Latitude}, {p.Longitude})");
    }
    Console.WriteLine($"Radius: {g.Radius}");
    Console.WriteLine($"Enabled: {g.Enabled}");
    Console.WriteLine($"Active: {g.Active}");
    Console.WriteLine($"\t-----------------");
    }
    }
    }
    Console.WriteLine($"Deleting geofences");
    CircleGeofence.Delete();
    PolygonGeofence.Delete();
    LineGeofence.Delete();
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error Not Handled exception {ex.Message}");
    }
    }
    }
    }


  • Enable and Disable Geofences
    Enabling and disabling a geofence

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Text;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Geofencing
    {
    public static class EnableAndDisableGeofence
    {
    public static void Main(string[] args)
    {
    try
    {
    string deepsyServerIp = args.Length == 0 ? "192.168.0.140" : args[0];
    using (GeofencingManager geofencingManager = new GeofencingManager(deepsyServerIp))
    {
    string sog1 = "mysog1";
    Console.WriteLine($"Adding polygon geofence to {sog1}");
    List<IPosition> polygon_positions = new List<IPosition>();
    polygon_positions.Add(new Position(41.651856, -4.728325));
    polygon_positions.Add(new Position(41.652217, -4.729280));
    polygon_positions.Add(new Position(41.652441, -4.727939));
    IGeofence PolygonGeofence = geofencingManager.AddPolygonGeofence(sog1, 2, "polygon_geofence", false, polygon_positions);
    Thread.Sleep(1000);
    Console.WriteLine($"Enabling geofence");
    PolygonGeofence.Enable();
    Thread.Sleep(1000);
    List<IGeofence> geofences = geofencingManager.GetGeofences(sog1).ToList();
    foreach (IGeofence geofence in geofences)
    {
    Console.WriteLine($"Geofence {geofence.Id} of sog {sog1}:");
    Console.WriteLine($"Enabled: {geofence.Enabled}");
    Console.WriteLine($"\t-----------------");
    }
    Console.WriteLine($"Disabling geofence");
    PolygonGeofence.Disable();
    Thread.Sleep(1000);
    geofences = geofencingManager.GetGeofences(sog1).ToList();
    foreach (IGeofence geofence in geofences)
    {
    Console.WriteLine($"Geofence {geofence.Id} of sog {sog1}:");
    Console.WriteLine($"Enabled: {geofence.Enabled}");
    Console.WriteLine($"\t-----------------");
    }
    Console.WriteLine($"Deleting geofence");
    PolygonGeofence.Delete();
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error Not Handled exception {ex.Message}");
    }
    }
    }
    }


  • Subscribe to geofencing events
    Subscribing to geofencing events

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Geofencing
    {
    class SubscribeGeofencingEvents
    {
    public static void Main(string[] args)
    {
    try
    {
    string deepsyServerIp = args.Length == 0 ? "192.168.0.140" : args[0];
    using (GeofencingManager geofencingManager = new GeofencingManager(deepsyServerIp))
    {
    geofencingManager.ServiceAvailabilityEvent += GeofencingManager_ServiceAvailabilityEvent;
    geofencingManager.SogListEvent += GeofencingManager_SogListEvent;
    geofencingManager.GeofenceListEvent += GeofencingManager_GeofenceListEvent;
    List<string> sogsList = geofencingManager.GetSOGs().ToList();
    if (sogsList != null)
    {
    foreach (string s in sogsList)
    {
    List<IGeofence> geofences = geofencingManager.GetGeofences(s).ToList();
    foreach (IGeofence geofence in geofences)
    {
    Console.WriteLine($"Subscribed to events for Geofence {geofence.Id} of sog {s}");
    geofence.GeofenceStatusEvent += Geofence_GeofenceStatusEvent;
    }
    }
    }
    Console.WriteLine("Press any key for exit");
    Console.ReadKey();
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error Not Handled exception {ex.Message}");
    }
    }
    private static void GeofencingManager_ServiceAvailabilityEvent(object sender, ServiceAvailabilityEvent e)
    {
    if (e.NewStatus == AvilableStatus.AVAILABLE)
    {
    Console.WriteLine("Geofencing Service AVAILABLE");
    }
    else if (e.NewStatus == AvilableStatus.NOT_AVAILABLE)
    {
    Console.WriteLine("Geofencing Service NOT AVAILABLE");
    }
    }
    private static void GeofencingManager_SogListEvent(object sender, ISogListEvent e)
    {
    if (e.Type == SogListType.Type.SOG_ADDED)
    {
    Console.WriteLine($"SOG {e.SogId} ADDED");
    }
    else if (e.Type == SogListType.Type.SOG_REMOVED)
    {
    Console.WriteLine($"SOG {e.SogId} REMOVED");
    }
    }
    private static void GeofencingManager_GeofenceListEvent(object sender, IGeofenceListEvent e)
    {
    if (e.Type == GeofenceListEventType.Type.GEOFENCE_ADDED)
    {
    Console.WriteLine($"Geofence {e.Geofence.Id} of sog {e.SogId} ADDED. Subscribing to events for this geofence.");
    e.Geofence.GeofenceStatusEvent += Geofence_GeofenceStatusEvent;
    }
    else if (e.Type == GeofenceListEventType.Type.GEOFENCE_REMOVED)
    {
    Console.WriteLine($"Geofence {e.Geofence.Id} of sog {e.SogId} REMOVED");
    e.Geofence.GeofenceStatusEvent -= Geofence_GeofenceStatusEvent;
    }
    }
    private static void Geofence_GeofenceStatusEvent(object sender, IGeofenceStatusEvent e)
    {
    if (e.Type == GeofenceStatusEventType.Type.ENTER)
    {
    Console.WriteLine($"ENTER Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    else if (e.Type == GeofenceStatusEventType.Type.EXIT)
    {
    Console.WriteLine($"EXIT Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    else if (e.Type == GeofenceStatusEventType.Type.ENABLED)
    {
    Console.WriteLine($"ENABLED Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    else if (e.Type == GeofenceStatusEventType.Type.DISABLED)
    {
    Console.WriteLine($"DISABLED Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    else if (e.Type == GeofenceStatusEventType.Type.ENTER_ENABLED)
    {
    Console.WriteLine($"ENTER_ENABLED Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    else if (e.Type == GeofenceStatusEventType.Type.EXIT_DISABLED)
    {
    Console.WriteLine($"EXIT_DISABLED Geofence {e.Geofence.Id} of sog {e.Geofence.SogId}");
    }
    }
    }
    }


  • Manage set of geofences
    Managing set of geofences

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Geofencing
    {
    public static class ManageSOG
    {
    public static void Main(string[] args)
    {
    try
    {
    string deepsyServerIp = args.Length == 0 ? "192.168.0.140" : args[0];
    using (GeofencingManager geofencingManager = new GeofencingManager(deepsyServerIp))
    {
    string sog1 = "mysog1";
    string sog2 = "mysog2";
    Console.WriteLine($"Adding circle geofence to {sog1}");
    IGeofence CircleGeofence = geofencingManager.AddCircleGeofence(sog1, 1, "circle_geofence", false, new Position(41.516842, -4.716624), 10);
    Console.WriteLine($"Adding polygon geofence to {sog1}");
    List<IPosition> polygon_positions = new List<IPosition>();
    polygon_positions.Add(new Position(41.651856, -4.728325));
    polygon_positions.Add(new Position(41.652217, -4.729280));
    polygon_positions.Add(new Position(41.652441, -4.727939));
    IGeofence PolygonGeofence = geofencingManager.AddPolygonGeofence(sog1, 2, "polygon_geofence", false, polygon_positions);
    Console.WriteLine($"Adding line geofence to {sog2}");
    List<IPosition> line_positions = new List<IPosition>();
    line_positions.Add(new Position(42.201922, -4.396001));
    line_positions.Add(new Position(42.202578, -4.394323));
    line_positions.Add(new Position(42.203107, -4.393882));
    IGeofence LineGeofence = geofencingManager.AddBufferedLineGeofence(sog2, 3, "line_geofence", false, line_positions, 12);
    Console.WriteLine($"Adding circle geofence to {sog2}");
    IGeofence CircleGeofence2 = geofencingManager.AddCircleGeofence(sog2, 4, "circle_geofence2", false, new Position(41.5163444, -4.7172305), 2);
    Thread.Sleep(500);
    List<string> sogsList = geofencingManager.GetSOGs().ToList();
    Console.WriteLine($"There are {sogsList.Count()} SOGs");
    List<IGeofence> geofencesSog1 = geofencingManager.GetGeofences(sog1).ToList();
    Console.WriteLine($"There are {geofencesSog1.Count()} geofences in SOG {sog1}");
    foreach (IGeofence g in geofencesSog1)
    {
    Console.WriteLine($"Geofence {g.Id} of SOG {sog1}. Enabled: {g.Enabled}");
    }
    Console.WriteLine($"Enabling all geofences of SOG {sog1}");
    geofencingManager.EnableSOG(sog1);
    Thread.Sleep(500);
    foreach (IGeofence g in geofencesSog1)
    {
    Console.WriteLine($"Geofence {g.Id} of SOG {sog1}. Enabled: {g.Enabled}");
    }
    Console.WriteLine($"Disabling all geofences of SOG {sog1}");
    geofencingManager.DisableSOG(sog1);
    Thread.Sleep(500);
    foreach (IGeofence g in geofencesSog1)
    {
    Console.WriteLine($"Geofence {g.Id} of SOG {sog1}. Enabled: {g.Enabled}");
    }
    Console.WriteLine($"Deleting all geofences of SOG {sog1}");
    geofencingManager.DeleteSOG(sog1);
    Console.WriteLine($"Deleting all geofences of SOG {sog2}");
    geofencingManager.DeleteSOG(sog2);
    sogsList = geofencingManager.GetSOGs().ToList();
    Console.WriteLine($"There are {sogsList.Count()} SOGs");
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error Not Handled exception {ex.Message}");
    }
    }
    }
    }