/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* 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.
*/
using System;
namespace Tizen.Location.Geofence
{
///
/// Geofence defines a virtual perimeter for a real-world geographic area.
/// If you create a geofence, you can trigger some activities when a device enters (or exits) the geofences defined by you.
/// You can create a geofence with the information of the Geopoint, Wi-Fi, or BT.
///
/// - Geopoint: Geofence is specified by the coordinates (Latitude and Longitude) and radius.
/// - WIFI: Geofence is specified by the BSSID of the Wi-Fi access point.
/// - BT: Geofence is specified by the Bluetooth address.
///
/// The Basic service set identifier (BSSID) is the MAC address of the wireless access point (WAP) generated by combining the 24-bit Organization Unique Identifier (the manufacturer's identity)
/// and the manufacturer's assigned 24-bit identifier for the radio chipset in the WAP.
///
/// 3
public class Fence : IDisposable
{
private bool _disposed = false;
internal IntPtr Handle
{
get;
set;
}
internal Fence(IntPtr handle)
{
Handle = handle;
}
~Fence()
{
Dispose(false);
}
///
/// Gets the type of geofence.
///
/// 3
public FenceType Type
{
get
{
FenceType val;
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceType(Handle, out val);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceType");
}
return val;
}
}
///
/// Gets the ID of the place.
///
/// 3
public int PlaceId
{
get
{
int result = -1;
GeofenceError ret = (GeofenceError)Interop.Geofence.FencePlaceID(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get PlaceId");
}
return result;
}
}
///
/// Gets the longitude of geofence.
///
/// 3
public double Longitude
{
get
{
double result = -1;
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLongitude(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Longitude");
}
return result;
}
}
///
/// Gets the latitude of geofence.
///
/// 3
public double Latitude
{
get
{
double result = -1;
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLatitude(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Latitude");
}
return result;
}
}
///
/// Gets the radius of geofence.
///
/// 3
public int Radius
{
get
{
int result = -1;
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceRadius(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Radius");
}
return result;
}
}
///
/// Gets the address of geofence.
///
/// 3
public string Address
{
get
{
string result = "";
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceAddress(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Adress");
}
return result;
}
}
///
/// Gets the BSSID of geofence.
///
/// 3
public string Bssid
{
get
{
string result = "";
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceBSSID(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Bssid");
}
return result;
}
}
///
/// Gets the SSID of geofence.
///
/// 3
public string Ssid
{
get
{
string result = "";
GeofenceError ret = (GeofenceError)Interop.Geofence.FenceSSID(Handle, out result);
if (ret != GeofenceError.None)
{
Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Ssid");
}
return result;
}
}
///
/// Creates a geopoint type of the new geofence.
///
/// 3
/// The current place ID.
/// Specifies the value of latitude of the geofence [-90.0 ~ 90.0] (degrees).
/// Specifies the value of longitude of the geofence [-180.0 ~ 180.0] (degrees).
/// Specifies the value of radius of the geofence [100 ~ 500](meter).
/// Specifies the value of the address.
/// The newly created geofence instance.
/// In case of an invalid parameter.
/// In case of any system error.
/// In case the geofence is not supported.
public static Fence CreateGPSFence(int placeId, int latitude, int longitude, int radius, string address)
{
IntPtr handle = IntPtr.Zero;
GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId, latitude, longitude, radius,address, out handle);
if (ret != GeofenceError.None)
{
throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId);
}
return new Fence(handle);
}
///
/// Creates a Wi-Fi type of the new geofence.
///
/// 3
/// The current place ID.
/// Specifies the value of BSSID of the Wi-Fi MAC address.
/// Specifies the value of SSID of the Wi-Fi device.
/// The newly created geofence instance.
/// In case of an invalid parameter.
/// In case of any system error.
/// In case the geofence is not supported.
public static Fence CreateWifiFence(int placeId, string bssid, string ssid)
{
IntPtr handle = IntPtr.Zero;
GeofenceError ret = (GeofenceError)Interop.Geofence.CreateWiFiFence(placeId, bssid, ssid, out handle);
if (ret != GeofenceError.None)
{
throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Wifi Data for " + placeId);
}
return new Fence(handle);
}
///
/// Creates a Bluetooth type of the new geofence.
///
/// 3
/// The current place ID.
/// Specifies the value of BSSID of BT MAC address.
/// Specifies the value of SSID of BT Device.
/// The newly created geofence instance.
/// In case of an invalid parameter.
/// In case of any system error.
/// In case the geofence is not supported.
public static Fence CreateBTFence(int placeId, string bssid, string ssid)
{
IntPtr handle = IntPtr.Zero;
GeofenceError ret = (GeofenceError)Interop.Geofence.CreateBTFence(placeId, bssid, ssid, out handle);
if (ret != GeofenceError.None)
{
throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Bluetooth Data for " + placeId);
}
return new Fence(handle);
}
///
/// The overloaded Dispose API for destroying the fence handle.
///
/// 3
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed)
return;
if (Handle != IntPtr.Zero)
{
Interop.Geofence.Destroy(Handle);
Handle = IntPtr.Zero;
}
_disposed = true;
}
}
}