2 * Copyright (c) 2010-2023 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.capability;
15 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
17 import java.time.Duration;
18 import java.time.ZonedDateTime;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.NetatmoException;
26 import org.openhab.binding.netatmo.internal.api.WeatherApi;
27 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
28 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
29 import org.openhab.binding.netatmo.internal.config.MeasureConfiguration;
30 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
31 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
32 import org.openhab.binding.netatmo.internal.handler.channelhelper.MeasuresChannelHelper;
33 import org.openhab.core.thing.type.ChannelTypeUID;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link MeasureCapability} is the base class for handler able to handle user defined measures
42 * @author Gaƫl L'hopital - Initial contribution
46 public class MeasureCapability extends CacheWeatherCapability {
47 private final Logger logger = LoggerFactory.getLogger(MeasureCapability.class);
48 private final Map<String, State> measures = new HashMap<>();
50 public MeasureCapability(CommonInterface handler, List<ChannelHelper> helpers) {
51 super(handler, Duration.ofMinutes(30));
52 MeasuresChannelHelper measureChannelHelper = (MeasuresChannelHelper) helpers.stream()
53 .filter(c -> c instanceof MeasuresChannelHelper).findFirst()
54 .orElseThrow(() -> new IllegalArgumentException(
55 "MeasureCapability must find a MeasuresChannelHelper, please file a bug report."));
56 measureChannelHelper.setMeasures(measures);
59 private void updateMeasures(WeatherApi api, String deviceId, @Nullable String moduleId) {
61 handler.getActiveChannels().filter(channel -> !channel.getConfiguration().getProperties().isEmpty())
63 ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
64 if (channelTypeUID == null) {
68 MeasureConfiguration measureDef = channel.getConfiguration().as(MeasureConfiguration.class);
69 String descriptor = channelTypeUID.getId().split("-")[0];
71 Object result = measureDef.limit.isBlank()
72 ? api.getMeasures(deviceId, moduleId, measureDef.period, descriptor)
73 : api.getMeasures(deviceId, moduleId, measureDef.period, descriptor, measureDef.limit);
74 MeasureClass.AS_SET.stream().filter(mc -> mc.apiDescriptor.equals(descriptor))
75 .reduce((first, second) -> second)
76 .ifPresent(mc -> measures.put(channel.getUID().getIdWithoutGroup(),
77 result instanceof ZonedDateTime zonedDateTime ? toDateTimeType(zonedDateTime)
78 : result instanceof Double ? toQuantityType((Double) result, mc)
80 } catch (NetatmoException e) {
81 logger.warn("Error getting measures for channel {}, check configuration", channel.getLabel());
87 protected List<NAObject> getFreshData(WeatherApi api) {
88 String bridgeId = handler.getBridgeId();
89 String deviceId = bridgeId != null ? bridgeId : handler.getId();
90 String moduleId = bridgeId != null ? handler.getId() : null;
91 updateMeasures(api, deviceId, moduleId);