|
| 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.FitbitIntradaySpo2; |
| 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 FitbitIntradaySpo2AvroConverter extends FitbitAvroConverter { |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(FitbitIntradaySpo2AvroConverter.class); |
| 38 | + private String spo2Topic; |
| 39 | + |
| 40 | + public FitbitIntradaySpo2AvroConverter(AvroData avroData) { |
| 41 | + super(avroData); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void initialize(RestSourceConnectorConfig config) { |
| 46 | + spo2Topic = ((FitbitRestSourceConnectorConfig) config).getFitbitIntradaySpo2Topic(); |
| 47 | + logger.info("Using intraday spo2 topic {}", spo2Topic); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Stream<TopicData> processRecords(FitbitRestRequest request, JsonNode root, double timeReceived) { |
| 52 | + JsonNode spo2 = root; |
| 53 | + if (spo2 == null || !spo2.isArray()) { |
| 54 | + logger.warn("No Spo2 is provided for {}: {}", request, root); |
| 55 | + return Stream.empty(); |
| 56 | + } |
| 57 | + ZonedDateTime startDate = request.getDateRange().end(); |
| 58 | + |
| 59 | + return iterableToStream(spo2) |
| 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 -> parseSpo2(minuteData, startDate, timeReceived), |
| 65 | + (a, ex) -> logger.warn("Failed to convert spo2 from request {}, {}", request, a, ex))); |
| 66 | + } |
| 67 | + |
| 68 | + private TopicData parseSpo2(JsonNode minuteData, ZonedDateTime startDate, double timeReceived) { |
| 69 | + Instant time = startDate.with(LocalDateTime.parse(minuteData.get("minute").asText())).toInstant(); |
| 70 | + Float value = (float) minuteData.get("value").asDouble(); |
| 71 | + if (value == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + FitbitIntradaySpo2 fitbitSpo2 = new FitbitIntradaySpo2(time.toEpochMilli() / 1000d, |
| 75 | + timeReceived, |
| 76 | + (float) value); |
| 77 | + return new TopicData(time, spo2Topic, fitbitSpo2); |
| 78 | + } |
| 79 | +} |
0 commit comments