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 /// TemperatureSensor Class. Used for registering callbacks for temperature sensor and getting temperature data
24 public sealed class TemperatureSensor : Sensor
26 private static string TemperatureSensorKey = "http://tizen.org/feature/sensor.temperature";
29 /// Gets the value of the temperature sensor.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Temperature (Celsius) </value>
33 public float Temperature { get; private set; } = float.MinValue;
36 /// Returns true or false based on whether temperature 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 TemperatureSensor is supported");
45 return CheckIfSupported(SensorType.TemperatureSensor, TemperatureSensorKey);
50 /// Returns the number of temperature sensors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of temperature sensors </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of temperature sensors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.TemperatureSensor"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <feature>http://tizen.org/feature/sensor.temperature</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 temperature sensor in case of multiple sensors
74 public TemperatureSensor(uint index = 0) : base(index)
76 Log.Info(Globals.LogTag, "Creating TemperatureSensor object");
79 internal override SensorType GetSensorType()
81 return SensorType.TemperatureSensor;
85 /// Event Handler for storing the callback functions for event corresponding to change in temperature sensor data.
87 /// <since_tizen> 3 </since_tizen>
89 public event EventHandler<TemperatureSensorDataUpdatedEventArgs> DataUpdated;
92 private static int GetCount()
96 int error = Interop.SensorManager.GetSensorList(SensorType.TemperatureSensor, out list, out count);
97 if (error != (int)SensorError.None)
99 Log.Error(Globals.LogTag, "Error getting sensor list for temperature");
103 Interop.Libc.Free(list);
107 private static Interop.SensorListener.SensorEventCallback _callback;
109 internal override void EventListenStart()
111 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
112 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
114 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
115 Temperature = sensorData.values[0];
117 DataUpdated?.Invoke(this, new TemperatureSensorDataUpdatedEventArgs(sensorData.values[0]));
120 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
121 if (error != (int)SensorError.None)
123 Log.Error(Globals.LogTag, "Error setting event callback for temperature sensor");
124 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for temperature");
128 internal override void EventListenStop()
130 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
131 if (error != (int)SensorError.None)
133 Log.Error(Globals.LogTag, "Error unsetting event callback for temperature sensor");
134 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for temperature");