2 * Copyright (c) 2010-2023 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.openhab.binding.enocean.internal.config.EnOceanChannelTransformationConfig;
25 import org.openhab.binding.enocean.internal.eep.EEP;
26 import org.openhab.binding.enocean.internal.messages.ERP1Message;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.library.types.DateTimeType;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.OpenClosedType;
34 import org.openhab.core.library.types.PercentType;
35 import org.openhab.core.library.types.PlayPauseType;
36 import org.openhab.core.library.types.PointType;
37 import org.openhab.core.library.types.RewindFastforwardType;
38 import org.openhab.core.library.types.StringListType;
39 import org.openhab.core.library.types.StringType;
40 import org.openhab.core.library.types.UpDownType;
41 import org.openhab.core.transform.actions.Transformation;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.State;
44 import org.openhab.core.types.UnDefType;
45 import org.openhab.core.util.HexUtils;
49 * @author Daniel Weber - Initial contribution
51 public class GenericEEP extends EEP {
53 final List<Class<? extends State>> supportedStates = Collections
54 .unmodifiableList(new LinkedList<Class<? extends State>>() {
56 private static final long serialVersionUID = 1L;
59 add(DateTimeType.class);
60 add(DecimalType.class);
63 add(OpenClosedType.class);
64 add(PercentType.class);
65 add(PlayPauseType.class);
67 add(RewindFastforwardType.class);
68 add(StringListType.class);
69 add(StringType.class);
70 add(UpDownType.class);
78 public GenericEEP(ERP1Message packet) {
83 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
84 Function<String, State> getCurrentStateFunc, Configuration config) {
86 EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
88 String input = channelId + "|" + command.toString();
89 String output = Transformation.transform(transformationInfo.transformationType,
90 transformationInfo.transformationFunction, input);
92 if (output != null && !output.isEmpty() && !input.equals(output)) {
94 setData(HexUtils.hexToBytes(output));
95 } catch (Exception e) {
96 logger.debug("Command {} could not transformed", command.toString());
103 protected State convertToStateImpl(String channelId, String channelTypeId,
104 Function<String, State> getCurrentStateFunc, Configuration config) {
105 if (config != null) {
106 EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
108 String payload = HexUtils.bytesToHex(bytes);
109 String input = channelId + "|" + payload;
110 String output = Transformation.transform(transformationInfo.transformationType,
111 transformationInfo.transformationFunction, input);
113 if (output != null && !output.isEmpty() && !input.equals(output)) {
114 String[] parts = output.split("\\|");
116 if (parts.length == 2) {
117 Class<? extends State> state = supportedStates.stream().filter(s -> s.getName().contains(parts[0]))
118 .findFirst().orElse(null);
121 if (state.isEnum()) {
122 for (State s : state.getEnumConstants()) {
123 if (s.toString().equalsIgnoreCase(parts[1])) {
127 logger.debug("Could not find value '{}' for state '{}'", parts[1], parts[0]);
130 return state.getConstructor(String.class).newInstance(parts[1]);
131 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
132 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
133 logger.debug("Could not create state '{}' with value '{}'", parts[0], parts[1]);
137 logger.debug("State '{}' not found", parts[0]);
140 logger.debug("Transformation result malformed: {}", output);
145 return UnDefType.UNDEF;
149 protected int getDataLength() {
150 if (packet != null) {
151 return packet.getPayload().length - ESP3_SENDERID_LENGTH - ESP3_RORG_LENGTH - ESP3_STATUS_LENGTH;
158 protected boolean validateData(byte[] bytes) {
163 public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
164 discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
165 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());