]> git.basschouten.com Git - openhab-addons.git/blob
d1f64507ad54c07d20db1f9217d55bcbc381bc56
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.channelhelper;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 import java.util.List;
20 import java.util.Optional;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link RadioHelper} handle specific behavior
34  * of WIFI or RF devices and modules
35  *
36  * @author GaĆ«l L'hopital - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class RadioHelper {
41     private final Logger logger = LoggerFactory.getLogger(RadioHelper.class);
42     private final List<Integer> signalThresholds;
43     private @Nullable Object module;
44
45     public RadioHelper(String signalLevels) {
46         signalThresholds = Stream.of(signalLevels.split(",")).map(Integer::parseInt).collect(Collectors.toList());
47     }
48
49     private int getSignalStrength(int signalLevel) {
50         int level;
51         for (level = 0; level < signalThresholds.size(); level++) {
52             if (signalLevel > signalThresholds.get(level)) {
53                 break;
54             }
55         }
56         return level;
57     }
58
59     public void setModule(Object module) {
60         this.module = module;
61     }
62
63     public Optional<State> getNAThingProperty(String channelId) {
64         Object module = this.module;
65         if (module != null) {
66             try {
67                 switch (channelId) {
68                     case CHANNEL_RF_STATUS:
69                         Method getRfStatus = module.getClass().getMethod("getRfStatus");
70                         Integer rfStatus = (Integer) getRfStatus.invoke(module);
71                         return Optional.of(new DecimalType(getSignalStrength(rfStatus)));
72                     case CHANNEL_WIFI_STATUS:
73                         Method getWifiStatus = module.getClass().getMethod("getWifiStatus");
74                         Integer wifiStatus = (Integer) getWifiStatus.invoke(module);
75                         return Optional.of(new DecimalType(getSignalStrength(wifiStatus)));
76                 }
77             } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
78                     | InvocationTargetException e) {
79                 logger.warn("The module has no method to access {} property : {}", channelId, e.getMessage());
80                 return Optional.of(UnDefType.NULL);
81             }
82         }
83         return Optional.empty();
84     }
85 }