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