]> git.basschouten.com Git - openhab-addons.git/blob
73e5a02de58ae78f0646a2f5a424155c2142928e
[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.channelhelper;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.toQuantityType;
17
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27
28 /**
29  * The {@link SignalChannelHelper} handles specific behavior of WIFI or RF devices and modules
30  *
31  * @author GaĆ«l L'hopital - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class SignalChannelHelper extends ChannelHelper {
36
37     public SignalChannelHelper(Set<String> providedGroups) {
38         super(providedGroups);
39     }
40
41     @Override
42     protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
43         int status = naThing.getRadioStatus();
44         if (status != -1) {
45             switch (channelId) {
46                 case CHANNEL_SIGNAL_STRENGTH:
47                     return new DecimalType(getSignalStrength(status, naThing.getType().getSignalLevels()));
48                 case CHANNEL_VALUE:
49                     return toQuantityType(status, Units.DECIBEL_MILLIWATTS);
50             }
51         }
52         return null;
53     }
54
55     private int getSignalStrength(int signalLevel, int[] levels) {
56         int level;
57         for (level = 0; level < levels.length; level++) {
58             if (signalLevel > levels[level]) {
59                 break;
60             }
61         }
62         return level;
63     }
64 }