]> git.basschouten.com Git - openhab-addons.git/blob
654d5525edcbb527da119a911a78ae2825e79f5b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.station;
14
15 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
17
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.netatmo.internal.handler.NetatmoModuleHandler;
26 import org.openhab.core.i18n.TimeZoneProvider;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.State;
29
30 import io.swagger.client.model.NADashboardData;
31 import io.swagger.client.model.NAStationModule;
32
33 /**
34  * {@link NAModule3Handler} is the class used to handle the Rain Gauge
35  * capable of measuring precipitation
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
39  *
40  */
41 @NonNullByDefault
42 public class NAModule3Handler extends NetatmoModuleHandler<NAStationModule> {
43     private Map<String, Float> channelMeasurements = new ConcurrentHashMap<>();
44
45     public NAModule3Handler(Thing thing, final TimeZoneProvider timeZoneProvider) {
46         super(thing, timeZoneProvider);
47     }
48
49     @Override
50     protected void updateProperties(NAStationModule moduleData) {
51         updateProperties(moduleData.getFirmware(), moduleData.getType());
52     }
53
54     @Override
55     public void updateMeasurements() {
56         List<String> types = Arrays.asList(SUM_RAIN);
57
58         if (isLinked(CHANNEL_SUM_RAIN_THIS_WEEK)) {
59             getMeasurements(getParentId(), getId(), ONE_WEEK, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_WEEK),
60                     channelMeasurements);
61         }
62
63         if (isLinked(CHANNEL_SUM_RAIN_THIS_MONTH)) {
64             getMeasurements(getParentId(), getId(), ONE_MONTH, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_MONTH),
65                     channelMeasurements);
66         }
67     }
68
69     @Override
70     protected State getNAThingProperty(String channelId) {
71         NADashboardData dashboardData = getModule().map(m -> m.getDashboardData()).orElse(null);
72         if (dashboardData != null) {
73             switch (channelId) {
74                 case CHANNEL_RAIN:
75                     return toQuantityType(dashboardData.getRain(), API_RAIN_UNIT);
76                 case CHANNEL_SUM_RAIN1:
77                     return toQuantityType(dashboardData.getSumRain1(), API_RAIN_UNIT);
78                 case CHANNEL_SUM_RAIN24:
79                     return toQuantityType(dashboardData.getSumRain24(), API_RAIN_UNIT);
80                 case CHANNEL_TIMEUTC:
81                     return toDateTimeType(dashboardData.getTimeUtc(), timeZoneProvider.getTimeZone());
82             }
83         }
84
85         switch (channelId) {
86             case CHANNEL_SUM_RAIN_THIS_WEEK:
87             case CHANNEL_SUM_RAIN_THIS_MONTH:
88                 return toQuantityType(channelMeasurements.get(channelId), API_RAIN_UNIT);
89         }
90
91         return super.getNAThingProperty(channelId);
92     }
93
94     @Override
95     protected boolean isReachable() {
96         boolean result = false;
97         Optional<NAStationModule> module = getModule();
98         if (module.isPresent()) {
99             Boolean reachable = module.get().isReachable();
100             result = reachable != null ? reachable.booleanValue() : false;
101         }
102         return result;
103     }
104 }