]> git.basschouten.com Git - openhab-addons.git/blob
39b312372e906eb5e4e45036b4f71988de8c9a66
[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.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                         MeasureConfiguration measureDef = channel.getConfiguration().as(MeasureConfiguration.class);
74                         String descriptor = channelTypeUID.getId().split("-")[0];
75                         try {
76                             Object result = measureDef.limit.isBlank()
77                                     ? api.getMeasures(deviceId, moduleId, measureDef.period, descriptor)
78                                     : api.getMeasures(deviceId, moduleId, measureDef.period, descriptor,
79                                             measureDef.limit);
80                             MeasureClass.AS_SET.stream().filter(mc -> mc.apiDescriptor.equals(descriptor)).findFirst()
81                                     .ifPresent(mc -> {
82                                         State state = result instanceof ZonedDateTime
83                                                 ? toDateTimeType((ZonedDateTime) result)
84                                                 : result instanceof Double ? toQuantityType((Double) result, mc)
85                                                         : UnDefType.UNDEF;
86                                         measures.put(channel.getUID().getIdWithoutGroup(), state);
87                                     });
88                         } catch (NetatmoException e) {
89                             logger.warn("Error getting measures for channel {}, check configuration",
90                                     channel.getLabel());
91                         }
92                     }
93                 });
94     }
95 }