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.NetatmoBindingConstants.MEASURABLE_CHANNELS;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.types.State;
25 import org.openhab.binding.netatmo.internal.ChannelTypeUtils;
27 import io.swagger.client.CollectionFormats.CSVParams;
28 import io.swagger.client.model.NAMeasureResponse;
31 * {@link MeasurableChannels} is a helper class designed to handle
32 * manipulation of requests and responses provided by calls to
33 * someNetatmoApi.getMeasures(....)
35 * @author Gaƫl L'hopital - Initial contribution
39 public class MeasurableChannels {
40 protected @Nullable NAMeasureResponse measures;
41 protected List<String> measuredChannels = new ArrayList<>();
44 * If this channel value is provided as a measure, then add it
45 * in the getMeasure parameter list
47 protected void addChannel(ChannelUID channelUID) {
48 String channel = channelUID.getId();
49 if (MEASURABLE_CHANNELS.contains(channel)) {
50 measuredChannels.add(channel);
55 * If this channel value is provided as a measure, then delete
56 * it in the getMeasure parameter list
58 protected void removeChannel(ChannelUID channelUID) {
59 String channel = channelUID.getId();
60 measuredChannels.remove(channel);
63 protected Optional<State> getNAThingProperty(String channelId) {
64 int index = measuredChannels.indexOf(channelId);
65 NAMeasureResponse theMeasures = measures;
66 if (index != -1 && theMeasures != null) {
67 if (!theMeasures.getBody().isEmpty()) {
68 List<List<Float>> valueList = theMeasures.getBody().get(0).getValue();
69 if (!valueList.isEmpty()) {
70 List<Float> values = valueList.get(0);
71 if (values.size() >= index) {
72 Float value = values.get(index);
73 return Optional.of(ChannelTypeUtils.toDecimalType(value));
78 return Optional.empty();
81 public Optional<CSVParams> getAsCsv() {
82 if (!measuredChannels.isEmpty()) {
83 return Optional.of(new CSVParams(measuredChannels));
85 return Optional.empty();
88 public void setMeasures(NAMeasureResponse measures) {
89 this.measures = measures;