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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_SIGNAL_LEVEL;
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;
27 * A base class for all device messages, so this is not about things as interface messages
29 * @author Martin van Wingerden - Initial contribution
31 abstract class RFXComDeviceMessageImpl<T> extends RFXComBaseMessage implements RFXComDeviceMessage<T> {
34 RFXComDeviceMessageImpl(PacketType packetType) {
38 RFXComDeviceMessageImpl() {
43 public void setConfig(RFXComDeviceConfiguration config) throws RFXComException {
44 this.setSubType(convertSubType(config.subType));
45 this.setDeviceId(config.deviceId);
49 public Command convertToCommand(String channelId, DeviceState deviceState)
50 throws RFXComUnsupportedChannelException {
51 return (Command) convertToState(channelId, deviceState);
55 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
57 case CHANNEL_SIGNAL_LEVEL:
58 return convertSignalLevelToSystemWideLevel(signalLevel);
61 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
66 public void addDevicePropertiesTo(DiscoveryResultBuilder discoveryResultBuilder) throws RFXComException {
67 String subTypeString = convertSubType(String.valueOf(subType)).toString();
68 String label = getPacketType() + "-" + getDeviceId();
70 discoveryResultBuilder.withLabel(label).withProperty(RFXComDeviceConfiguration.DEVICE_ID_LABEL, getDeviceId())
71 .withProperty(RFXComDeviceConfiguration.SUB_TYPE_LABEL, subTypeString);
75 * Convert internal signal level (0-15) to system wide signal level (0-4).
77 * @param signalLevel Internal signal level
78 * @return Signal level in system wide level
80 private State convertSignalLevelToSystemWideLevel(int signalLevel) {
84 * RFXCOM signal levels are always between 0-15.
86 * Use switch case to make level adaption easier in future if needed.
89 switch (signalLevel) {
122 return new DecimalType(newLevel);