|
| 1 | +/* |
| 2 | + * Copyright 2018 The Hyve |
| 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 | + |
| 18 | +package org.radarbase.connect.rest.fitbit.converter; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import io.confluent.connect.avro.AvroData; |
| 22 | +import org.radarbase.connect.rest.RestSourceConnectorConfig; |
| 23 | +import org.radarbase.connect.rest.fitbit.FitbitRestSourceConnectorConfig; |
| 24 | +import org.radarbase.connect.rest.fitbit.request.FitbitRestRequest; |
| 25 | +import org.radarcns.connector.fitbit.FitbitIntradayHeartRateVariability; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import java.time.Instant; |
| 30 | +import java.time.LocalDateTime; |
| 31 | +import java.time.ZonedDateTime; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static org.radarbase.connect.rest.util.ThrowingFunction.tryOrNull; |
| 35 | + |
| 36 | +public class FitbitIntradayHeartRateVariabilityAvroConverter extends FitbitAvroConverter { |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(FitbitIntradayHeartRateVariabilityAvroConverter.class); |
| 38 | + private String heartRateVariabilityTopic; |
| 39 | + |
| 40 | + public FitbitIntradayHeartRateVariabilityAvroConverter(AvroData avroData) { |
| 41 | + super(avroData); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void initialize(RestSourceConnectorConfig config) { |
| 46 | + heartRateVariabilityTopic = ((FitbitRestSourceConnectorConfig) config).getFitbitIntradayHeartRateVariabilityTopic(); |
| 47 | + logger.info("Using intraday heart rate variability topic {}", heartRateVariabilityTopic); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Stream<TopicData> processRecords(FitbitRestRequest request, JsonNode root, double timeReceived) { |
| 52 | + JsonNode hrv = root.get("hrv"); |
| 53 | + if (hrv == null || !hrv.isArray()) { |
| 54 | + logger.warn("No HRV is provided for {}: {}", request, root); |
| 55 | + return Stream.empty(); |
| 56 | + } |
| 57 | + ZonedDateTime startDate = request.getDateRange().end(); |
| 58 | + |
| 59 | + return iterableToStream(hrv) |
| 60 | + .filter(m -> m != null && m.isObject()) |
| 61 | + .map(m -> m.get("minutes")) |
| 62 | + .filter(minutes -> minutes != null && minutes.isArray()) |
| 63 | + .flatMap(FitbitAvroConverter::iterableToStream) |
| 64 | + .map(tryOrNull(minuteData -> parseHrv(minuteData, startDate, timeReceived), |
| 65 | + (a, ex) -> logger.warn("Failed to convert heart rate variability from request {}, {}", request, a, ex))); |
| 66 | + } |
| 67 | + |
| 68 | + private TopicData parseHrv(JsonNode minuteData, ZonedDateTime startDate, double timeReceived) { |
| 69 | + Instant time = startDate.with(LocalDateTime.parse(minuteData.get("minute").asText())).toInstant(); |
| 70 | + JsonNode value = minuteData.get("value"); |
| 71 | + if (value == null || !value.isObject()) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + FitbitIntradayHeartRateVariability fitbitHrv = new FitbitIntradayHeartRateVariability(time.toEpochMilli() / 1000d, |
| 75 | + timeReceived, |
| 76 | + (float) value.get("dailyRmssd").asDouble(), |
| 77 | + (float) value.get("coverage").asDouble(), |
| 78 | + (float) value.get("hf").asDouble(), |
| 79 | + (float) value.get("lf").asDouble()); |
| 80 | + return new TopicData(time, heartRateVariabilityTopic, fitbitHrv); |
| 81 | + } |
| 82 | +} |
0 commit comments