// Copyright 2016 by Samsung Electronics, Inc., // // This software is the confidential and proprietary information // of Samsung Electronics, Inc. ("Confidential Information"). You // shall not disclose such Confidential Information and shall use // it only in accordance with the terms of the license agreement // you entered into with Samsung. using System; namespace Tizen.Sensor { /// /// HumiditySensor Class. Used for registering callbacks for humidity sensor and getting humidity data /// /// public class HumiditySensor : Sensor { private const string HumiditySensorKey = "http://tizen.org/feature/sensor.humidity"; /// /// Gets the value of the humidity sensor. /// public float Humidity { get; private set; } /// /// Returns true or false based on whether humidity sensor is supported by device. /// public static bool IsSupported { get { Log.Info(Globals.LogTag, "Checking if the HumiditySensor is supported"); return CheckIfSupported(SensorType.HumiditySensor, HumiditySensorKey); } } /// /// Returns the number of humidity sensors available on the device. /// public static int Count { get { Log.Info(Globals.LogTag, "Getting the count of humidity sensors"); return GetCount(); } } /// /// Initializes a new instance of the class. /// /// Thrown when an invalid argument is used /// Thrown when the sensor is not supported /// Thrown when the operation is invalid for the current state /// /// Index. Default value for this is 0. Index refers to a particular humidity sensor in case of multiple sensors /// public HumiditySensor(int index = 0) : base(index) { Log.Info(Globals.LogTag, "Creating HumiditySensor object"); } internal override SensorType GetSensorType() { return SensorType.HumiditySensor; } /// /// Event Handler for storing the callback functions for event corresponding to change in humidity sensor data. /// public event EventHandler DataUpdated; private static int GetCount() { IntPtr list; int count; int error = Interop.SensorManager.GetSensorList(SensorType.HumiditySensor, out list, out count); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error getting sensor list for humidity"); count = 0; } else Interop.Libc.Free(list); return count; } protected override void EventListenStart() { int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error setting event callback for humidity sensor"); throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for humidity"); } } protected override void EventListenStop() { int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error unsetting event callback for humidity sensor"); throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for humidity"); } } private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data) { Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr); TimeSpan = new TimeSpan((Int64)sensorData.timestamp); Humidity = sensorData.values[0]; DataUpdated?.Invoke(this, new HumiditySensorDataUpdatedEventArgs(sensorData.values[0])); } } }