2 * Copyright (c) 2010-2024 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.enocean.internal.eep.generic;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16 import static org.openhab.binding.enocean.internal.messages.ESP3Packet.*;
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.function.Function;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.enocean.internal.config.EnOceanChannelTransformationConfig;
27 import org.openhab.binding.enocean.internal.eep.EEP;
28 import org.openhab.binding.enocean.internal.messages.ERP1Message;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.library.types.DateTimeType;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.HSBType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.OpenClosedType;
36 import org.openhab.core.library.types.PercentType;
37 import org.openhab.core.library.types.PlayPauseType;
38 import org.openhab.core.library.types.PointType;
39 import org.openhab.core.library.types.RewindFastforwardType;
40 import org.openhab.core.library.types.StringListType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.library.types.UpDownType;
43 import org.openhab.core.transform.actions.Transformation;
44 import org.openhab.core.types.Command;
45 import org.openhab.core.types.State;
46 import org.openhab.core.types.UnDefType;
47 import org.openhab.core.util.HexUtils;
51 * @author Daniel Weber - Initial contribution
54 public class GenericEEP extends EEP {
56 final List<Class<? extends State>> supportedStates = Collections
57 .unmodifiableList(new LinkedList<Class<? extends State>>() {
58 private static final long serialVersionUID = 1L;
61 add(DateTimeType.class);
62 add(DecimalType.class);
65 add(OpenClosedType.class);
66 add(PercentType.class);
67 add(PlayPauseType.class);
69 add(RewindFastforwardType.class);
70 add(StringListType.class);
71 add(StringType.class);
72 add(UpDownType.class);
80 public GenericEEP(ERP1Message packet) {
85 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
86 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
88 logger.error("Cannot handle command {}, when transformation configuration is null", command.toString());
91 EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
93 String input = channelId + "|" + command.toString();
94 String output = Transformation.transform(transformationInfo.transformationType,
95 transformationInfo.transformationFunction, input);
97 if (output != null && !output.isEmpty() && !input.equals(output)) {
99 setData(HexUtils.hexToBytes(output));
100 } catch (IllegalArgumentException e) {
101 logger.debug("Command {} could not transformed", command.toString());
108 protected State convertToStateImpl(String channelId, String channelTypeId,
109 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
110 EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
112 String payload = HexUtils.bytesToHex(bytes);
113 String input = channelId + "|" + payload;
114 String output = Transformation.transform(transformationInfo.transformationType,
115 transformationInfo.transformationFunction, input);
117 if (output != null && !output.isEmpty() && !input.equals(output)) {
118 String[] parts = output.split("\\|");
120 if (parts.length == 2) {
122 Class<? extends State> state = supportedStates.stream().filter(s -> s.getName().contains(parts[0]))
123 .findFirst().orElse(null);
126 if (state.isEnum()) {
128 if ((states = state.getEnumConstants()) != null) {
129 for (State s : states) {
130 if (s.toString().equalsIgnoreCase(parts[1])) {
135 logger.debug("Could not find value '{}' for state '{}'", parts[1], parts[0]);
138 return state.getConstructor(String.class).newInstance(parts[1]);
139 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
140 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
141 logger.debug("Could not create state '{}' with value '{}'", parts[0], parts[1]);
145 logger.debug("State '{}' not found", parts[0]);
148 logger.debug("Transformation result malformed: {}", output);
152 return UnDefType.UNDEF;
156 protected int getDataLength() {
157 ERP1Message localPacket = packet;
158 if (localPacket != null) {
159 return localPacket.getPayload().length - ESP3_SENDERID_LENGTH - ESP3_RORG_LENGTH - ESP3_STATUS_LENGTH;
166 protected boolean validateData(byte[] bytes) {
171 public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
172 discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
173 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());