]> git.basschouten.com Git - openhab-addons.git/blob
eead1771168454efeed181fad5ee71161f18215a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.handler;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.MEASURABLE_CHANNELS;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.netatmo.internal.ChannelTypeUtils;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.State;
26
27 import io.swagger.client.CollectionFormats.CSVParams;
28 import io.swagger.client.model.NAMeasureResponse;
29
30 /**
31  * {@link MeasurableChannels} is a helper class designed to handle
32  * manipulation of requests and responses provided by calls to
33  * someNetatmoApi.getMeasures(....)
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class MeasurableChannels {
40     protected @Nullable NAMeasureResponse measures;
41     protected List<String> measuredChannels = new ArrayList<>();
42
43     /*
44      * If this channel value is provided as a measure, then add it
45      * in the getMeasure parameter list
46      */
47     protected void addChannel(ChannelUID channelUID) {
48         String channel = channelUID.getId();
49         if (MEASURABLE_CHANNELS.contains(channel)) {
50             measuredChannels.add(channel);
51         }
52     }
53
54     /*
55      * If this channel value is provided as a measure, then delete
56      * it in the getMeasure parameter list
57      */
58     protected void removeChannel(ChannelUID channelUID) {
59         String channel = channelUID.getId();
60         measuredChannels.remove(channel);
61     }
62
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));
74                     }
75                 }
76             }
77         }
78         return Optional.empty();
79     }
80
81     public Optional<CSVParams> getAsCsv() {
82         if (!measuredChannels.isEmpty()) {
83             return Optional.of(new CSVParams(measuredChannels));
84         }
85         return Optional.empty();
86     }
87
88     public void setMeasures(NAMeasureResponse measures) {
89         this.measures = measures;
90     }
91 }