Tizen.Location.Geofence Setup
[platform/core/csapi/geofence.git] / Tizen.Location.Geofence / Tizen.Location.Geofence / Fence.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace Tizen.Location.Geofence
20 {
21     /// <summary>
22     /// Geo-fence defines a virtual perimeter for a real-world geographic area.
23     /// If you create a geofence, you can trigger some activities when a device enters(or exits) the geofences defined by you.
24     /// You can create a geofence with the information of Geopoint, Wi-Fi, or BT.
25     /// <list >
26     /// <item>Geopoint: Geofence is specified by coordinates (Latitude and Longitude) and Radius</item>
27     /// <item>WIFI: Geofence is specified by BSSID of Wi-Fi access point</item>
28     /// <item>BT: Geofence is specified by Bluetooth address</item>
29     /// </list>
30     /// Basic service set identification(BSSID) The BSSID is the MAC address of the wireless access point(WAP) generated by combining the 24 bit Organization Unique Identifier(the manufacturer's identity)
31     /// and the manufacturer's assigned 24-bit identifier for the radio chipset in the WAP.
32     /// </summary>
33     public class Fence : IDisposable
34     {
35         internal IntPtr Handle
36         {
37             get;
38             set;
39         }
40
41
42         internal Fence(IntPtr handle)
43         {
44             Handle = handle;
45         }
46
47         ~Fence()
48         {
49             Dispose(false);
50         }
51         /// <summary>
52         /// Gets the type of geofence
53         /// </summary>
54         public FenceType Type
55         {
56             get
57             {
58                 FenceType val;
59                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceType(Handle, out val);
60                 if (ret != GeofenceError.None)
61                 {
62                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceType");
63                 }
64
65                 return val;
66             }
67         }
68
69         /// <summary>
70         /// Gets the id of place.
71         /// </summary>
72         public int PlaceId
73         {
74             get
75             {
76                 int result = -1;
77                 GeofenceError ret = (GeofenceError)Interop.Geofence.FencePlaceID(Handle, out result);
78                 if (ret != GeofenceError.None)
79                 {
80                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get PlaceId");
81                 }
82
83                 return result;
84             }
85         }
86
87         /// <summary>
88         ///Gets the longitude of geofence.
89         /// </summary>
90         public double Longitude
91         {
92             get
93             {
94                 double result = -1;
95                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLongitude(Handle, out result);
96                 if (ret != GeofenceError.None)
97                 {
98                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Longitude");
99                 }
100
101                 return result;
102
103             }
104         }
105
106         /// <summary>
107         ///Gets the latitude of geofence.
108         /// </summary>
109         public double Latitude
110         {
111             get
112             {
113                 double result = -1;
114                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLatitude(Handle, out result);
115                 if (ret != GeofenceError.None)
116                 {
117                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Latitude");
118                 }
119
120                 return result;
121             }
122         }
123
124         /// <summary>
125         /// Gets the radius of geofence.
126         /// </summary>
127         public int Radius
128         {
129             get
130             {
131                 int result = -1;
132                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceRadius(Handle, out result);
133                 if (ret != GeofenceError.None)
134                 {
135                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Radius");
136                 }
137
138                 return result;
139             }
140         }
141
142         /// <summary>
143         /// Gets the state of geofence.
144         /// </summary>
145         public GeofenceState FenceState
146         {
147             get
148             {
149                 GeofenceState state;
150                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceState(Handle, out state);
151                 if (ret != GeofenceError.None)
152                 {
153                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceState");
154                 }
155
156                 return state;
157             }
158         }
159
160         /// <summary>
161         /// Gets the amount of seconds geofence is in the current state.
162         /// </summary>
163         public int Duration
164         {
165             get
166             {
167                 int result = -1;
168                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceDuration(Handle, out result);
169                 if (ret != GeofenceError.None)
170                 {
171                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Duration");
172                 }
173
174                 return result;
175             }
176         }
177
178         /// <summary>
179         /// Gets the address of geofence.
180         /// </summary>
181         public string Address
182         {
183             get
184             {
185                 string result = "";
186                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceAddress(Handle, out result);
187                 if (ret != GeofenceError.None)
188                 {
189                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Adress");
190                 }
191
192                 return result;
193             }
194         }
195
196         /// <summary>
197         /// Gets the bssid of geofence.
198         /// </summary>
199         public string Bssid
200         {
201             get
202             {
203                 string result = "";
204                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceBSSID(Handle, out result);
205                 if (ret != GeofenceError.None)
206                 {
207                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Bssid");
208                 }
209
210                 return result;
211             }
212         }
213
214         /// <summary>
215         ///Gets the ssid of geofence.
216         /// </summary>
217         public string Ssid
218         {
219             get
220             {
221                 string result = "";
222                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceSSID(Handle, out result);
223                 if (ret != GeofenceError.None)
224                 {
225                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Ssid");
226                 }
227
228                 return result;
229             }
230         }
231
232         /// <summary>
233         /// Creates a geopoint type of new geofence.
234         /// </summary>
235         /// <param name="placeId">The current place id</param>
236         /// <param name="latitude">Specifies the value of latitude of geofence [-90.0 ~ 90.0] (degrees) </param>
237         /// <param name="longitude">Specifies the value of longitude of geofence [-180.0 ~ 180.0] (degrees) </param>
238         /// <param name="radius">Specifies the value of radius of geofence [100 ~ 500](meter) </param>
239         /// <param name="adsress">Specifies the value of address</param>
240         /// <returns>Newly created geofence instance </returns>
241         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
242         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
243         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
244         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
245         public static Fence CreateGPSFence(int placeId, int latitude, int longitude, int radius, string address)
246         {
247             IntPtr handle = IntPtr.Zero;
248             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId,latitude, longitude, radius,address,out handle);
249             if (ret != GeofenceError.None)
250             {
251                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId);
252             }
253
254             return new Fence(handle);
255         }
256
257         /// <summary>
258         /// Creates a Wi-Fi type of new geofence.
259         /// </summary>
260         /// <param name="placeId">The current place id </param>
261         /// <param name="bssid">Specifies the value of BSSID of Wi-Fi MAC address</param>
262         /// <param name="ssid"> Specifies the value of SSID of Wi-Fi Device </param>
263         /// <returns>Newly created geofence instance </returns>
264         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
265         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
266         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
267         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
268         public static Fence CreateWifiFence(int placeId, string bssid, string ssid)
269         {
270             IntPtr handle = IntPtr.Zero;
271             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateWiFiFence(placeId, bssid, ssid, out handle);
272             if (ret != GeofenceError.None)
273             {
274                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Wifi Data for " + placeId);
275             }
276
277             return new Fence(handle);
278         }
279
280         /// <summary>
281         /// Creates a bluetooth type of new geofence.
282         /// </summary>
283         /// <param name="placeId">The current place id </param>
284         /// <param name="bssid">Specifies the value of BSSID of BT MAC address</param>
285         /// <param name="ssid"> Specifies the value of SSID of BT Device </param>
286         /// <returns>Newly created geofence instance </returns>
287         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
288         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
289         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
290         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
291         public static Fence CreateBTFence(int placeId, string bssid, string ssid)
292         {
293             IntPtr handle = IntPtr.Zero;
294             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateBTFence(placeId, bssid, ssid, out handle);
295             if (ret != GeofenceError.None)
296             {
297                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Bluetooth Data for " + placeId);
298             }
299
300             return new Fence(handle);
301         }
302
303         /// <summary>
304         /// Overloaded Dispose API for destroying the fence Handle.
305         /// </summary>
306         public void Dispose()
307         {
308             Dispose(true);
309             GC.SuppressFinalize(this);
310         }
311
312         private void Dispose(bool disposing)
313         {
314             if (!disposing)
315             {
316                 if (Handle != IntPtr.Zero)
317                 {
318                     Interop.Geofence.Destroy(Handle);
319                     Handle = IntPtr.Zero;
320                 }
321             }
322         }
323     }
324 }