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