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.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;
28 * A base class for all device messages, so this is not about things as interface messages
30 * @author Martin van Wingerden - Initial contribution
32 abstract class RFXComDeviceMessageImpl<T> extends RFXComBaseMessage implements RFXComDeviceMessage<T> {
35 RFXComDeviceMessageImpl(PacketType packetType) {
39 RFXComDeviceMessageImpl() {
44 public void setConfig(RFXComDeviceConfiguration config) throws RFXComException {
45 RFXComGenericDeviceConfiguration genericConfig = (RFXComGenericDeviceConfiguration) config;
46 this.setSubType(convertSubType(genericConfig.subType));
47 this.setDeviceId(genericConfig.deviceId);
51 public Command convertToCommand(String channelId, DeviceState deviceState)
52 throws RFXComUnsupportedChannelException {
53 return (Command) convertToState(channelId, deviceState);
57 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
59 case CHANNEL_SIGNAL_LEVEL:
60 return convertSignalLevelToSystemWideLevel(signalLevel);
63 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
68 public void addDevicePropertiesTo(DiscoveryResultBuilder discoveryResultBuilder) throws RFXComException {
69 String subTypeString = convertSubType(String.valueOf(subType)).toString();
70 String label = getPacketType() + "-" + getDeviceId();
72 discoveryResultBuilder.withLabel(label)
73 .withProperty(RFXComGenericDeviceConfiguration.DEVICE_ID_LABEL, getDeviceId())
74 .withProperty(RFXComGenericDeviceConfiguration.SUB_TYPE_LABEL, subTypeString);
78 * Convert internal signal level (0-15) to system wide signal level (0-4).
80 * @param signalLevel Internal signal level
81 * @return Signal level in system wide level
83 private State convertSignalLevelToSystemWideLevel(int signalLevel) {
87 * RFXCOM signal levels are always between 0-15.
89 * Use switch case to make level adaption easier in future if needed.
92 switch (signalLevel) {
125 return new DecimalType(newLevel);