]> git.basschouten.com Git - openhab-addons.git/blob
9e1c3b2b57d60cb1e522c3e58f13b8beb16e50e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.capability;
14
15 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
16
17 import java.time.ZonedDateTime;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
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;
37
38 /**
39  * The {@link MeasureCapability} is the base class for handler able to handle user defined measures
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  *
43  */
44 @NonNullByDefault
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<>();
48
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);
56     }
57
58     @Override
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);
64         return List.of();
65     }
66
67     private void updateMeasures(WeatherApi api, String deviceId, @Nullable String moduleId) {
68         measures.clear();
69         handler.getActiveChannels().filter(channel -> !channel.getConfiguration().getProperties().isEmpty())
70                 .forEach(channel -> {
71                     ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
72                     if (channelTypeUID == null) {
73                         return;
74                     }
75
76                     MeasureConfiguration measureDef = channel.getConfiguration().as(MeasureConfiguration.class);
77                     String descriptor = channelTypeUID.getId().split("-")[0];
78                     try {
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)
87                                                         : UnDefType.UNDEF));
88                     } catch (NetatmoException e) {
89                         logger.warn("Error getting measures for channel {}, check configuration", channel.getLabel());
90                     }
91                 });
92     }
93 }