2 * Copyright (c) 2010-2021 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.station;
15 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
18 import java.util.Arrays;
19 import java.util.List;
21 import java.util.Optional;
22 import java.util.concurrent.ConcurrentHashMap;
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;
30 import io.swagger.client.model.NADashboardData;
31 import io.swagger.client.model.NAStationModule;
34 * {@link NAModule3Handler} is the class used to handle the Rain Gauge
35 * capable of measuring precipitation
37 * @author Gaƫl L'hopital - Initial contribution
38 * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
42 public class NAModule3Handler extends NetatmoModuleHandler<NAStationModule> {
43 private Map<String, Float> channelMeasurements = new ConcurrentHashMap<>();
45 public NAModule3Handler(Thing thing, final TimeZoneProvider timeZoneProvider) {
46 super(thing, timeZoneProvider);
50 protected void updateProperties(NAStationModule moduleData) {
51 updateProperties(moduleData.getFirmware(), moduleData.getType());
55 public void updateMeasurements() {
56 List<String> types = Arrays.asList(SUM_RAIN);
58 if (isLinked(CHANNEL_SUM_RAIN_THIS_WEEK)) {
59 getMeasurements(getParentId(), getId(), ONE_WEEK, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_WEEK),
63 if (isLinked(CHANNEL_SUM_RAIN_THIS_MONTH)) {
64 getMeasurements(getParentId(), getId(), ONE_MONTH, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_MONTH),
70 protected State getNAThingProperty(String channelId) {
71 NADashboardData dashboardData = getModule().map(m -> m.getDashboardData()).orElse(null);
72 if (dashboardData != null) {
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);
81 return toDateTimeType(dashboardData.getTimeUtc(), timeZoneProvider.getTimeZone());
86 case CHANNEL_SUM_RAIN_THIS_WEEK:
87 case CHANNEL_SUM_RAIN_THIS_MONTH:
88 return toQuantityType(channelMeasurements.get(channelId), API_RAIN_UNIT);
91 return super.getNAThingProperty(channelId);
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;