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