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