2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Sensor
22 /// HumiditySensor Class. Used for registering callbacks for humidity sensor and getting humidity data
24 public sealed class HumiditySensor : Sensor
26 private const string HumiditySensorKey = "http://tizen.org/feature/sensor.humidity";
29 /// Gets the value of the humidity sensor.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Humidity </value>
33 public float Humidity { get; private set; } = float.MinValue;
36 /// Returns true or false based on whether humidity sensor is supported by device.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
40 public static bool IsSupported
44 Log.Info(Globals.LogTag, "Checking if the HumiditySensor is supported");
45 return CheckIfSupported(SensorType.HumiditySensor, HumiditySensorKey);
50 /// Returns the number of humidity sensors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of humidity sensors </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of humidity sensors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.HumiditySensor"/> class.
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
74 public HumiditySensor(uint index = 0) : base(index)
76 Log.Info(Globals.LogTag, "Creating HumiditySensor object");
79 internal override SensorType GetSensorType()
81 return SensorType.HumiditySensor;
85 /// Event Handler for storing the callback functions for event corresponding to change in humidity sensor data.
87 /// <since_tizen> 3 </since_tizen>
89 public event EventHandler<HumiditySensorDataUpdatedEventArgs> DataUpdated;
91 private static int GetCount()
95 int error = Interop.SensorManager.GetSensorList(SensorType.HumiditySensor, out list, out count);
96 if (error != (int)SensorError.None)
98 Log.Error(Globals.LogTag, "Error getting sensor list for humidity");
102 Interop.Libc.Free(list);
106 private static Interop.SensorListener.SensorEventCallback _callback;
108 internal override void EventListenStart()
110 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
111 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
113 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
114 Humidity = sensorData.values[0];
116 DataUpdated?.Invoke(this, new HumiditySensorDataUpdatedEventArgs(sensorData.values[0]));
119 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
120 if (error != (int)SensorError.None)
122 Log.Error(Globals.LogTag, "Error setting event callback for humidity sensor");
123 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for humidity");
127 internal override void EventListenStop()
129 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
130 if (error != (int)SensorError.None)
132 Log.Error(Globals.LogTag, "Error unsetting event callback for humidity sensor");
133 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for humidity");