]> git.basschouten.com Git - openhab-addons.git/blob
1df657b79546fd49cab4caf0041f7bd51b04d930
[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.APIUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.MEASURABLE_CHANNELS;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Optional;
21
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;
27
28 import io.swagger.client.model.NAMeasureBodyElem;
29 import io.swagger.client.model.NAMeasureResponse;
30
31 /**
32  * {@link MeasurableChannels} is a helper class designed to handle
33  * manipulation of requests and responses provided by calls to
34  * someNetatmoApi.getMeasures(....)
35  *
36  * @author GaĆ«l L'hopital - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class MeasurableChannels {
41     protected @Nullable NAMeasureResponse measures;
42     protected List<String> measuredChannels = new ArrayList<>();
43
44     /*
45      * If this channel value is provided as a measure, then add it
46      * in the getMeasure parameter list
47      */
48     protected void addChannel(ChannelUID channelUID) {
49         String channel = channelUID.getId();
50         if (MEASURABLE_CHANNELS.contains(channel)) {
51             measuredChannels.add(channel);
52         }
53     }
54
55     /*
56      * If this channel value is provided as a measure, then delete
57      * it in the getMeasure parameter list
58      */
59     protected void removeChannel(ChannelUID channelUID) {
60         String channel = channelUID.getId();
61         measuredChannels.remove(channel);
62     }
63
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));
76                     }
77                 }
78             }
79         }
80         return Optional.empty();
81     }
82
83     public Optional<List<String>> getMeasuredChannels() {
84         if (!measuredChannels.isEmpty()) {
85             return Optional.of(measuredChannels);
86         }
87         return Optional.empty();
88     }
89
90     public void setMeasures(NAMeasureResponse measures) {
91         this.measures = measures;
92     }
93 }