[Sensor] Update sensor description regarding batch sensor. (#1730)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / HeartRateMonitorLEDGreenBatch.cs
1 /*
2  * Copyright (c) 2020 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 using System.Collections.Generic;
19 using System.ComponentModel;
20
21 namespace Tizen.Sensor
22 {
23     /// <summary>
24     /// The HeartRateMonitorLEDGreenBatch class registers callbacks for the HRMLEDGreen batch and provides batch data of HRMLEDGreen batch.
25     /// </summary>
26     /// <since_tizen> 8 </since_tizen>
27     public sealed class HeartRateMonitorLEDGreenBatch : BatchSensor<HeartRateMonitorLEDGreenBatchData>
28     {
29         private static string HRMLEDGreenBatchKey = "http://tizen.org/feature/sensor.heart_rate_monitor.led_green.batch";
30
31         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
32
33         protected override IReadOnlyList<HeartRateMonitorLEDGreenBatchData> ConvertBatchData()
34         {
35             List<HeartRateMonitorLEDGreenBatchData> list = new List<HeartRateMonitorLEDGreenBatchData>();
36             Interop.SensorEventStruct sensorData;
37             for (int i = 0; i < BatchedEvents.Count; i++)
38             {
39                 sensorData = BatchedEvents[i];
40                 list.Add(new HeartRateMonitorLEDGreenBatchData(sensorData.timestamp, sensorData.accuracy, (uint)sensorData.values[0], (int)sensorData.values[1], (int)sensorData.values[2], (int)sensorData.values[3], (uint)sensorData.values[4]));
41             }
42             return list.AsReadOnly(); ;
43         }
44
45         /// <summary>
46         /// Gets the accuracy of the auto HeartRateMonitorLEDGreenBatch data.
47         /// </summary>
48         /// <since_tizen> 8 </since_tizen>
49         /// <value> Accuracy </value>
50         public SensorDataAccuracy Accuracy { get; private set; } = SensorDataAccuracy.Undefined;
51
52         /// <summary>
53         /// Returns true or false based on whether the HeartRateMonitorLEDGreenBatch sensor is supported by the device.
54         /// </summary>
55         /// <since_tizen> 8 </since_tizen>
56         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
57         public static bool IsSupported
58         {
59             get
60             {
61                 Log.Info(Globals.LogTag, "Checking if the HeartRateMonitorLEDGreenBatch is supported");
62                 return CheckIfSupported(SensorType.HRMLEDGreenBatch, HRMLEDGreenBatchKey);
63             }
64         }
65
66         /// <summary>
67         /// Returns the number of HeartRateMonitorLEDGreenBatch sensors available on the device.
68         /// </summary>
69         /// <since_tizen> 8 </since_tizen>
70         /// <value> The count of HeartRateMonitorLEDGreenBatch sensors. </value>
71         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
72         public static int Count
73         {
74             get
75             {
76                 Log.Info(Globals.LogTag, "Getting the count of accelerometer sensors");
77                 return GetCount();
78             }
79         }
80
81         /// <summary>
82         /// Initializes a new instance of the <see cref="Tizen.Sensor.HeartRateMonitorLEDGreenBatch"/> class.
83         /// </summary>
84         /// <since_tizen> 8 </since_tizen>
85         /// <privilege>http://tizen.org/privilege/healthinfo</privilege>
86         /// <privlevel>public</privlevel>
87         /// <feature>http://tizen.org/feature/sensor.heart_rate_monitor.led_green.batch</feature>
88         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
89         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
90         /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the sensor.</exception>
91         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
92         /// <param name='index'>
93         /// Index. Default value for this is 0. Index refers to a particular HeartRateMonitorLEDGreenBatch sensor in case of multiple sensors.
94         /// </param>
95         public HeartRateMonitorLEDGreenBatch(uint index = 0) : base(index)
96         {
97             Log.Info(Globals.LogTag, "Creating HeartRateMonitorLEDGreenBatch object");
98         }
99
100         internal override SensorType GetSensorType()
101         {
102             return SensorType.HRMLEDGreenBatch;
103         }
104
105         /// <summary>
106         /// An event handler for storing the callback functions for the event corresponding to the change in the HeartRateMonitorLEDGreenBatch sensor data.
107         /// </summary>
108         /// <since_tizen> 8 </since_tizen>
109
110         public event EventHandler<HeartRateMonitorLEDGreenBatchDataUpdatedEventArgs> DataUpdated;
111
112         /// <summary>
113         /// An event handler for accuracy changed events.
114         /// </summary>
115         /// <since_tizen> 8 </since_tizen>
116         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
117         {
118             add
119             {
120                 if (_accuracyChanged == null)
121                 {
122                     AccuracyListenStart();
123                 }
124                 _accuracyChanged += value;
125             }
126             remove
127             {
128                 _accuracyChanged -= value;
129                 if (_accuracyChanged == null)
130                 {
131                     AccuracyListenStop();
132                 }
133             }
134         }
135
136         private static int GetCount()
137         {
138             IntPtr list;
139             int count;
140             int error = Interop.SensorManager.GetSensorList(SensorType.HRMLEDGreenBatch, out list, out count);
141             if (error != (int)SensorError.None)
142             {
143                 Log.Error(Globals.LogTag, "Error getting sensor list for HeartRateMonitorLEDGreenBatch");
144                 count = 0;
145             }
146             else
147                 Interop.Libc.Free(list);
148             return count;
149         }
150
151         /// <summary>
152         /// Reads HeartRateMonitorLEDGreenBatch data synchronously.
153         /// </summary>
154         internal override void ReadData()
155         {
156             int error = Interop.SensorListener.ReadDataList(ListenerHandle, out IntPtr eventsPtr, out uint events_count);
157             if (error != (int)SensorError.None)
158             {
159                 Log.Error(Globals.LogTag, "Error reading HeartRateMonitorLEDGreenBatch data");
160                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading HeartRateMonitorBatch data failed");
161             }
162             UpdateBatchData(eventsPtr, events_count);
163             Interop.SensorEventStruct sensorData = latestEvent();
164             Timestamp = sensorData.timestamp;
165             Accuracy = sensorData.accuracy;
166             Interop.Libc.Free(eventsPtr);
167         }
168
169         private static Interop.SensorListener.SensorEventsCallback _callback;
170
171         internal override void EventListenStart()
172         {
173             _callback = (IntPtr sensorHandle, IntPtr eventsPtr, uint events_count, IntPtr data) =>
174             {
175                 UpdateBatchData(eventsPtr, events_count);
176                 Interop.SensorEventStruct sensorData = latestEvent();
177                 Timestamp = (ulong)DateTimeOffset.Now.ToUnixTimeMilliseconds();
178                 Accuracy = sensorData.accuracy;
179                 DataUpdated?.Invoke(this, new HeartRateMonitorLEDGreenBatchDataUpdatedEventArgs((IReadOnlyList<HeartRateMonitorLEDGreenBatchData>)Data));
180
181             };
182
183             int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
184             if (error != (int)SensorError.None)
185             {
186                 Log.Error(Globals.LogTag, "Error setting event callback for HeartRateMonitorLEDGreenBatch sensor");
187                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for HeartRateMonitorLEDGreenBatch");
188             }
189         }
190
191         internal override void EventListenStop()
192         {
193             int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
194             if (error != (int)SensorError.None)
195             {
196                 Log.Error(Globals.LogTag, "Error unsetting event callback for HeartRateMonitorLEDGreenBatch sensor");
197                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for HeartRateMonitorLEDGreenBatch");
198             }
199         }
200
201         private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
202
203         private void AccuracyListenStart()
204         {
205             _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) =>
206             {
207                 Timestamp = timestamp;
208                 Accuracy = accuracy;
209                 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(timestamp, accuracy));
210             };
211
212             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
213             if (error != (int)SensorError.None)
214             {
215                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for HeartRateMonitorLEDGreenBatch sensor");
216                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for HeartRateMonitorLEDGreenBatch");
217             }
218         }
219
220         private void AccuracyListenStop()
221         {
222             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
223             if (error != (int)SensorError.None)
224             {
225                 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for HeartRateMonitorLEDGreenBatch sensor");
226                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for HeartRateMonitorLEDGreenBatch");
227             }
228         }
229     }
230 }