2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.handler;
15 import static org.openhab.binding.netatmo.internal.APIUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.MEASURABLE_CHANNELS;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.ChannelTypeUtils;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.types.State;
28 import io.swagger.client.model.NAMeasureBodyElem;
29 import io.swagger.client.model.NAMeasureResponse;
32 * {@link MeasurableChannels} is a helper class designed to handle
33 * manipulation of requests and responses provided by calls to
34 * someNetatmoApi.getMeasures(....)
36 * @author Gaƫl L'hopital - Initial contribution
40 public class MeasurableChannels {
41 protected @Nullable NAMeasureResponse measures;
42 protected List<String> measuredChannels = new ArrayList<>();
45 * If this channel value is provided as a measure, then add it
46 * in the getMeasure parameter list
48 protected void addChannel(ChannelUID channelUID) {
49 String channel = channelUID.getId();
50 if (MEASURABLE_CHANNELS.contains(channel)) {
51 measuredChannels.add(channel);
56 * If this channel value is provided as a measure, then delete
57 * it in the getMeasure parameter list
59 protected void removeChannel(ChannelUID channelUID) {
60 String channel = channelUID.getId();
61 measuredChannels.remove(channel);
64 protected Optional<State> getNAThingProperty(String channelId) {
65 int index = measuredChannels.indexOf(channelId);
66 NAMeasureResponse theMeasures = measures;
67 if (index != -1 && theMeasures != null) {
68 List<NAMeasureBodyElem> body = nonNullList(theMeasures.getBody());
69 if (!body.isEmpty()) {
70 List<List<Float>> valueList = nonNullList(body.get(0).getValue());
71 if (!valueList.isEmpty()) {
72 List<Float> values = nonNullList(valueList.get(0));
73 if (values.size() >= index) {
74 Float value = values.get(index);
75 return Optional.of(ChannelTypeUtils.toDecimalType(value));
80 return Optional.empty();
83 public Optional<List<String>> getMeasuredChannels() {
84 if (!measuredChannels.isEmpty()) {
85 return Optional.of(measuredChannels);
87 return Optional.empty();
90 public void setMeasures(NAMeasureResponse measures) {
91 this.measures = measures;