2 * Copyright (c) 2010-2021 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.channelhelper;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
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;
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;
33 * The {@link RadioHelper} handle specific behavior
34 * of WIFI or RF devices and modules
36 * @author Gaƫl L'hopital - Initial contribution
40 public class RadioHelper {
41 private final Logger logger = LoggerFactory.getLogger(RadioHelper.class);
42 private final List<Integer> signalThresholds;
43 private @Nullable Object module;
45 public RadioHelper(String signalLevels) {
46 signalThresholds = Stream.of(signalLevels.split(",")).map(Integer::parseInt).collect(Collectors.toList());
49 private int getSignalStrength(int signalLevel) {
51 for (level = 0; level < signalThresholds.size(); level++) {
52 if (signalLevel > signalThresholds.get(level)) {
59 public void setModule(Object module) {
63 public Optional<State> getNAThingProperty(String channelId) {
64 Object module = this.module;
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)));
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);
83 return Optional.empty();