2 * Copyright (c) 2020 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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
21 namespace Tizen.Sensor
24 /// The HeartRateMonitorBatch class is used for registering callbacks for the heart rate monitor batch and getting the heart rate batch data.
26 /// <since_tizen> 8 </since_tizen>
27 public sealed class HeartRateMonitorBatch : BatchSensor<HeartRateMonitorBatchData>
29 private static string HRMBatchKey = "http://tizen.org/feature/sensor.heart_rate_monitor.batch";
31 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
33 protected override IReadOnlyList<HeartRateMonitorBatchData> ConvertBatchData()
35 List<HeartRateMonitorBatchData> list = new List<HeartRateMonitorBatchData>();
36 Interop.SensorEventStruct sensorData;
37 for (int i = 0; i < BatchedEvents.Count; i++)
39 sensorData = BatchedEvents[i];
40 list.Add(new HeartRateMonitorBatchData(sensorData.timestamp, sensorData.accuracy, (HeartRateMonitorBatchState)sensorData.values[0], (int)sensorData.values[1], (int)sensorData.values[2]));
42 return list.AsReadOnly();
46 /// Gets the accuracy of the HeartRateMonitorBatch data.
48 /// <since_tizen> 8 </since_tizen>
49 /// <value> Accuracy </value>
50 public SensorDataAccuracy Accuracy { get; private set; } = SensorDataAccuracy.Undefined;
53 /// Returns true or false based on whether the HeartRateMonitorBatch sensor is supported by the device.
55 /// <since_tizen> 8 </since_tizen>
56 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
57 public static bool IsSupported
61 Log.Info(Globals.LogTag, "Checking if the HeartRateMonitorBatch is supported");
62 return CheckIfSupported(SensorType.HRMBatch, HRMBatchKey);
67 /// Returns the number of HeartRateMonitorBatch sensors available on the device.
69 /// <since_tizen> 8 </since_tizen>
70 /// <value> The count of HeartRateMonitorBatch sensors. </value>
71 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
72 public static int Count
76 Log.Info(Globals.LogTag, "Getting the count of accelerometer sensors");
82 /// Initializes a new instance of the <see cref="Tizen.Sensor.HeartRateMonitorBatch"/> class.
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.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 HeartRateMonitorBatch in case of multiple sensors.
95 public HeartRateMonitorBatch(uint index = 0) : base(index)
97 Log.Info(Globals.LogTag, "Creating HeartRateMonitorBatch object");
100 internal override SensorType GetSensorType()
102 return SensorType.HRMBatch;
106 /// An event handler for storing the callback functions for the event corresponding to the change in the HeartRateMonitorBatch data.
108 /// <since_tizen> 8 </since_tizen>
110 public event EventHandler<HeartRateMonitorBatchDataUpdatedEventArgs> DataUpdated;
113 /// An event handler for accuracy changed events.
115 /// <since_tizen> 8 </since_tizen>
116 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
120 if (_accuracyChanged == null)
122 AccuracyListenStart();
124 _accuracyChanged += value;
128 _accuracyChanged -= value;
129 if (_accuracyChanged == null)
131 AccuracyListenStop();
136 private static int GetCount()
140 int error = Interop.SensorManager.GetSensorList(SensorType.HRMBatch, out list, out count);
141 if (error != (int)SensorError.None)
143 Log.Error(Globals.LogTag, "Error getting sensor list for HeartRateMonitorBatch");
147 Interop.Libc.Free(list);
152 /// Read HeartRateMonitorBatch data synchronously.
154 internal override void ReadData()
156 int error = Interop.SensorListener.ReadDataList(ListenerHandle, out IntPtr eventsPtr, out uint events_count);
157 if (error != (int)SensorError.None)
159 Log.Error(Globals.LogTag, "Error reading HeartRateMonitorBatch data");
160 throw SensorErrorFactory.CheckAndThrowException(error, "Reading HeartRateMonitorBatch data failed");
162 UpdateBatchData(eventsPtr, events_count);
163 Interop.SensorEventStruct sensorData = latestEvent();
164 Timestamp = (ulong)DateTimeOffset.Now.ToUnixTimeMilliseconds();
165 Accuracy = sensorData.accuracy;
166 Interop.Libc.Free(eventsPtr);
169 private static Interop.SensorListener.SensorEventsCallback _callback;
171 internal override void EventListenStart()
173 _callback = (IntPtr sensorHandle, IntPtr eventsPtr, uint events_count, IntPtr data) =>
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 HeartRateMonitorBatchDataUpdatedEventArgs((IReadOnlyList<HeartRateMonitorBatchData>)Data));
181 int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
182 if (error != (int)SensorError.None)
184 Log.Error(Globals.LogTag, "Error setting event callback for HeartRateMonitorBatch sensor");
185 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for HeartRateMonitorBatch");
189 internal override void EventListenStop()
191 int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
192 if (error != (int)SensorError.None)
194 Log.Error(Globals.LogTag, "Error unsetting event callback for HeartRateMonitorBatch sensor");
195 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for HeartRateMonitorBatch");
199 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
201 private void AccuracyListenStart()
203 _accuracyCallback = (IntPtr sensorHandle, ulong timestamp, SensorDataAccuracy accuracy, IntPtr data) =>
205 Timestamp = timestamp;
207 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(timestamp, accuracy));
210 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
211 if (error != (int)SensorError.None)
213 Log.Error(Globals.LogTag, "Error setting accuracy event callback for HeartRateMonitorBatch sensor");
214 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for HeartRateMonitorBatch");
218 private void AccuracyListenStop()
220 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
221 if (error != (int)SensorError.None)
223 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for HeartRateMonitorBatch sensor");
224 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for HeartRateMonitorBatch");