2 * Copyright (c) 2010-2022 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.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;
29 * A base class for all device messages, so this is not about things as interface messages
31 * @author Martin van Wingerden - Initial contribution
33 abstract class RFXComDeviceMessageImpl<T> extends RFXComBaseMessage implements RFXComDeviceMessage<T> {
36 RFXComDeviceMessageImpl(PacketType packetType) {
40 RFXComDeviceMessageImpl() {
45 public void setConfig(RFXComDeviceConfiguration config) throws RFXComException {
46 RFXComGenericDeviceConfiguration genericConfig = (RFXComGenericDeviceConfiguration) config;
47 this.setSubType(convertSubType(genericConfig.subType));
48 this.setDeviceId(genericConfig.deviceId);
52 public Command convertToCommand(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
53 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
54 return (Command) convertToState(channelId, config, deviceState);
58 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
59 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
61 case CHANNEL_SIGNAL_LEVEL:
62 return convertSignalLevelToSystemWideLevel(signalLevel);
65 throw new RFXComUnsupportedChannelException("Nothing relevant for " + channelId);
70 public void addDevicePropertiesTo(DiscoveryResultBuilder discoveryResultBuilder) throws RFXComException {
71 String subTypeString = convertSubType(String.valueOf(subType)).toString();
72 String label = getPacketType() + "-" + getDeviceId();
74 discoveryResultBuilder.withLabel(label)
75 .withProperty(RFXComGenericDeviceConfiguration.DEVICE_ID_LABEL, getDeviceId())
76 .withProperty(RFXComGenericDeviceConfiguration.SUB_TYPE_LABEL, subTypeString);
80 * Convert internal signal level (0-15) to system wide signal level (0-4).
82 * @param signalLevel Internal signal level
83 * @return Signal level in system wide level
85 private State convertSignalLevelToSystemWideLevel(int signalLevel) {
89 * RFXCOM signal levels are always between 0-15.
91 * Use switch case to make level adaption easier in future if needed.
94 switch (signalLevel) {
127 return new DecimalType(newLevel);