Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / HumiditySensor.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.Sensor
20 {
21     /// <summary>
22     /// HumiditySensor Class. Used for registering callbacks for humidity sensor and getting humidity data
23     /// </summary>
24     public sealed class HumiditySensor : Sensor
25     {
26         private const string HumiditySensorKey = "http://tizen.org/feature/sensor.humidity";
27
28         /// <summary>
29         /// Gets the value of the humidity sensor.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <value> Humidity </value>
33         public float Humidity { get; private set; } = float.MinValue;
34
35         /// <summary>
36         /// Returns true or false based on whether humidity sensor is supported by device.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
40         public static bool IsSupported
41         {
42             get
43             {
44                 Log.Info(Globals.LogTag, "Checking if the HumiditySensor is supported");
45                 return CheckIfSupported(SensorType.HumiditySensor, HumiditySensorKey);
46             }
47         }
48
49         /// <summary>
50         /// Returns the number of humidity sensors available on the device.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <value> The count of humidity sensors </value>
54         public static int Count
55         {
56             get
57             {
58                 Log.Info(Globals.LogTag, "Getting the count of humidity sensors");
59                 return GetCount();
60             }
61         }
62
63         /// <summary>
64         /// Initializes a new instance of the <see cref="Tizen.Sensor.HumiditySensor"/> class.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <feature>http://tizen.org/feature/sensor.humidity</feature>
68         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
69         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
70         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
71         /// <param name='index'>
72         /// Index. Default value for this is 0. Index refers to a particular humidity sensor in case of multiple sensors
73         /// </param>
74         public HumiditySensor(uint index = 0) : base(index)
75         {
76             Log.Info(Globals.LogTag, "Creating HumiditySensor object");
77         }
78
79         internal override SensorType GetSensorType()
80         {
81             return SensorType.HumiditySensor;
82         }
83
84         /// <summary>
85         /// Event Handler for storing the callback functions for event corresponding to change in humidity sensor data.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88
89         public event EventHandler<HumiditySensorDataUpdatedEventArgs> DataUpdated;
90
91         private static int GetCount()
92         {
93             IntPtr list;
94             int count;
95             int error = Interop.SensorManager.GetSensorList(SensorType.HumiditySensor, out list, out count);
96             if (error != (int)SensorError.None)
97             {
98                 Log.Error(Globals.LogTag, "Error getting sensor list for humidity");
99                 count = 0;
100             }
101             else
102                 Interop.Libc.Free(list);
103             return count;
104         }
105
106         private static Interop.SensorListener.SensorEventCallback _callback;
107
108         internal override void EventListenStart()
109         {
110             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
111                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
112
113                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
114                 Humidity = sensorData.values[0];
115
116                 DataUpdated?.Invoke(this, new HumiditySensorDataUpdatedEventArgs(sensorData.values[0]));
117             };
118
119             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
120             if (error != (int)SensorError.None)
121             {
122                 Log.Error(Globals.LogTag, "Error setting event callback for humidity sensor");
123                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for humidity");
124             }
125         }
126
127         internal override void EventListenStop()
128         {
129             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
130             if (error != (int)SensorError.None)
131             {
132                 Log.Error(Globals.LogTag, "Error unsetting event callback for humidity sensor");
133                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for humidity");
134             }
135         }
136     }
137 }