3df18ecca1bc58eb9a7f4a92168448f06dc3f035
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / UltravioletSensor.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// UltravioletSensor Class. Used for registering callbacks for ultraviolet sensor and getting ultraviolet data
15     /// /// </summary>
16     public class UltravioletSensor : Sensor
17     {
18         private static string UltravioletSensorKey = "http://tizen.org/feature/sensor.ultraviolet";
19
20         /// <summary>
21         /// Gets the value of the ultraviolet sensor.
22         /// </summary>
23         public float UltravioletIndex { get; private set; }
24
25         /// <summary>
26         /// Returns true or false based on whether ultraviolet sensor is supported by device.
27         /// </summary>
28         public static bool IsSupported
29         {
30             get
31             {
32                 Log.Info(Globals.LogTag, "Checking if the UltravioletSensor is supported");
33                 return CheckIfSupported(SensorType.UltravioletSensor, UltravioletSensorKey);
34             }
35         }
36
37         /// <summary>
38         /// Returns the number of ultraviolet sensors available on the device.
39         /// </summary>
40         public static int Count
41         {
42             get
43             {
44                 Log.Info(Globals.LogTag, "Getting the count of ultraviolet sensors");
45                 return GetCount();
46             }
47         }
48
49         /// <summary>
50         /// Initializes a new instance of the <see cref="Tizen.Sensor.UltravioletSensor"/> class.
51         /// </summary>
52         /// <param name='index'>
53         /// Index. Default value for this is 0. Index refers to a particular ultraviolet sensor in case of multiple sensors
54         /// </param>
55         public UltravioletSensor(int index = 0) : base(index)
56         {
57             Log.Info(Globals.LogTag, "Creating UltravioletSensor object");
58         }
59
60         internal override SensorType GetSensorType()
61         {
62             return SensorType.UltravioletSensor;
63         }
64
65         /// <summary>
66         /// Event Handler for storing the callback functions for event corresponding to change in ultraviolet sensor data.
67         /// </summary>
68
69         public event EventHandler<UltravioletSensorDataUpdatedEventArgs> DataUpdated;
70
71
72         private static int GetCount()
73         {
74             IntPtr list;
75             int count;
76             int error = Interop.SensorManager.GetSensorList(SensorType.UltravioletSensor, out list, out count);
77             if (error != (int)SensorError.None)
78             {
79                 Log.Error(Globals.LogTag, "Error getting sensor list for ultraviolet");
80                 count = 0;
81             }
82             else
83                 Interop.Libc.Free(list);
84             return count;
85         }
86
87         protected override void EventListenStart()
88         {
89             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
90             if (error != (int)SensorError.None)
91             {
92                 Log.Error(Globals.LogTag, "Error setting event callback for ultraviolet sensor");
93                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for ultraviolet");
94             }
95         }
96
97         protected override void EventListenStop()
98         {
99             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
100             if (error != (int)SensorError.None)
101             {
102                 Log.Error(Globals.LogTag, "Error unsetting event callback for ultraviolet sensor");
103                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for ultraviolet");
104             }
105         }
106
107         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
108         {
109             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
110             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
111             UltravioletIndex = sensorData.values[0];
112
113             DataUpdated?.Invoke(this, new UltravioletSensorDataUpdatedEventArgs(sensorData.values[0]));
114         }
115     }
116 }