]> git.basschouten.com Git - openhab-addons.git/blob
ed4de3e361b439126dcc000ff08aebab1271dd13
[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.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_SIGNAL_LEVEL;
16
17 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
18 import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
21 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26
27 /**
28  * A base class for all device messages, so this is not about things as interface messages
29  *
30  * @author Martin van Wingerden - Initial contribution
31  */
32 abstract class RFXComDeviceMessageImpl<T> extends RFXComBaseMessage implements RFXComDeviceMessage<T> {
33     byte signalLevel;
34
35     RFXComDeviceMessageImpl(PacketType packetType) {
36         super(packetType);
37     }
38
39     RFXComDeviceMessageImpl() {
40         // deliberately empty
41     }
42
43     @Override
44     public void setConfig(RFXComDeviceConfiguration config) throws RFXComException {
45         RFXComGenericDeviceConfiguration genericConfig = (RFXComGenericDeviceConfiguration) config;
46         this.setSubType(convertSubType(genericConfig.subType));
47         this.setDeviceId(genericConfig.deviceId);
48     }
49
50     @Override
51     public Command convertToCommand(String channelId, DeviceState deviceState)
52             throws RFXComUnsupportedChannelException {
53         return (Command) convertToState(channelId, deviceState);
54     }
55
56     @Override
57     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
58         switch (channelId) {
59             case CHANNEL_SIGNAL_LEVEL:
60                 return convertSignalLevelToSystemWideLevel(signalLevel);
61
62             default:
63                 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
64         }
65     }
66
67     @Override
68     public void addDevicePropertiesTo(DiscoveryResultBuilder discoveryResultBuilder) throws RFXComException {
69         String subTypeString = convertSubType(String.valueOf(subType)).toString();
70         String label = getPacketType() + "-" + getDeviceId();
71
72         discoveryResultBuilder.withLabel(label)
73                 .withProperty(RFXComGenericDeviceConfiguration.DEVICE_ID_LABEL, getDeviceId())
74                 .withProperty(RFXComGenericDeviceConfiguration.SUB_TYPE_LABEL, subTypeString);
75     }
76
77     /**
78      * Convert internal signal level (0-15) to system wide signal level (0-4).
79      *
80      * @param signalLevel Internal signal level
81      * @return Signal level in system wide level
82      */
83     private State convertSignalLevelToSystemWideLevel(int signalLevel) {
84         int newLevel;
85
86         /*
87          * RFXCOM signal levels are always between 0-15.
88          *
89          * Use switch case to make level adaption easier in future if needed.
90          */
91
92         switch (signalLevel) {
93             case 0:
94             case 1:
95                 newLevel = 0;
96                 break;
97
98             case 2:
99             case 3:
100             case 4:
101                 newLevel = 1;
102                 break;
103
104             case 5:
105             case 6:
106             case 7:
107                 newLevel = 2;
108                 break;
109
110             case 8:
111             case 9:
112             case 10:
113             case 11:
114                 newLevel = 3;
115                 break;
116
117             case 12:
118             case 13:
119             case 14:
120             case 15:
121             default:
122                 newLevel = 4;
123         }
124
125         return new DecimalType(newLevel);
126     }
127 }