csapi-sensor: update API spec about exceptions
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / HumiditySensor.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// HumiditySensor Class. Used for registering callbacks for humidity sensor and getting humidity data
15     /// /// </summary>
16     public class HumiditySensor : Sensor
17     {
18         private const string HumiditySensorKey = "http://tizen.org/feature/sensor.humidity";
19
20         /// <summary>
21         /// Gets the value of the humidity sensor.
22         /// </summary>
23         public float Humidity { get; private set; }
24
25         /// <summary>
26         /// Returns true or false based on whether humidity sensor is supported by device.
27         /// </summary>
28         public static bool IsSupported
29         {
30             get
31             {
32                 Log.Info(Globals.LogTag, "Checking if the HumiditySensor is supported");
33                 return CheckIfSupported(SensorType.HumiditySensor, HumiditySensorKey);
34             }
35         }
36
37         /// <summary>
38         /// Returns the number of humidity sensors available on the device.
39         /// </summary>
40         public static int Count
41         {
42             get
43             {
44                 Log.Info(Globals.LogTag, "Getting the count of humidity sensors");
45                 return GetCount();
46             }
47         }
48
49         /// <summary>
50         /// Initializes a new instance of the <see cref="Tizen.Sensor.HumiditySensor"/> class.
51         /// </summary>
52         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
53         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
54         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
55         /// <param name='index'>
56         /// Index. Default value for this is 0. Index refers to a particular humidity sensor in case of multiple sensors
57         /// </param>
58         public HumiditySensor(int index = 0) : base(index)
59         {
60             Log.Info(Globals.LogTag, "Creating HumiditySensor object");
61         }
62
63         internal override SensorType GetSensorType()
64         {
65             return SensorType.HumiditySensor;
66         }
67
68         /// <summary>
69         /// Event Handler for storing the callback functions for event corresponding to change in humidity sensor data.
70         /// </summary>
71
72         public event EventHandler<HumiditySensorDataUpdatedEventArgs> DataUpdated;
73
74         private static int GetCount()
75         {
76             IntPtr list;
77             int count;
78             int error = Interop.SensorManager.GetSensorList(SensorType.HumiditySensor, out list, out count);
79             if (error != (int)SensorError.None)
80             {
81                 Log.Error(Globals.LogTag, "Error getting sensor list for humidity");
82                 count = 0;
83             }
84             else
85                 Interop.Libc.Free(list);
86             return count;
87         }
88
89         protected override void EventListenStart()
90         {
91             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
92             if (error != (int)SensorError.None)
93             {
94                 Log.Error(Globals.LogTag, "Error setting event callback for humidity sensor");
95                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for humidity");
96             }
97         }
98
99         protected override void EventListenStop()
100         {
101             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
102             if (error != (int)SensorError.None)
103             {
104                 Log.Error(Globals.LogTag, "Error unsetting event callback for humidity sensor");
105                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for humidity");
106             }
107         }
108
109         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
110         {
111             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
112             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
113             Humidity = sensorData.values[0];
114
115             DataUpdated?.Invoke(this, new HumiditySensorDataUpdatedEventArgs(sensorData.values[0]));
116         }
117     }
118 }