]> git.basschouten.com Git - openhab-addons.git/blob
b333fa8fb58624739d63b79d0f41b67a73ca7216
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.enocean.internal.eep.generic;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16 import static org.openhab.binding.enocean.internal.messages.ESP3Packet.*;
17
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;
23
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;
48
49 /**
50  *
51  * @author Daniel Weber - Initial contribution
52  */
53 @NonNullByDefault
54 public class GenericEEP extends EEP {
55
56     final List<Class<? extends State>> supportedStates = Collections
57             .unmodifiableList(new LinkedList<Class<? extends State>>() {
58                 private static final long serialVersionUID = 1L;
59
60                 {
61                     add(DateTimeType.class);
62                     add(DecimalType.class);
63                     add(HSBType.class);
64                     add(OnOffType.class);
65                     add(OpenClosedType.class);
66                     add(PercentType.class);
67                     add(PlayPauseType.class);
68                     add(PointType.class);
69                     add(RewindFastforwardType.class);
70                     add(StringListType.class);
71                     add(StringType.class);
72                     add(UpDownType.class);
73                 }
74             });
75
76     public GenericEEP() {
77         super();
78     }
79
80     public GenericEEP(ERP1Message packet) {
81         super(packet);
82     }
83
84     @Override
85     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
86             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
87         if (config == null) {
88             logger.error("Cannot handle command {}, when transformation configuration is null", command.toString());
89             return;
90         }
91         EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
92
93         String input = channelId + "|" + command.toString();
94         String output = Transformation.transform(transformationInfo.transformationType,
95                 transformationInfo.transformationFunction, input);
96
97         if (output != null && !output.isEmpty() && !input.equals(output)) {
98             try {
99                 setData(HexUtils.hexToBytes(output));
100             } catch (IllegalArgumentException e) {
101                 logger.debug("Command {} could not transformed", command.toString());
102                 throw e;
103             }
104         }
105     }
106
107     @Override
108     protected State convertToStateImpl(String channelId, String channelTypeId,
109             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
110         EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
111
112         String payload = HexUtils.bytesToHex(bytes);
113         String input = channelId + "|" + payload;
114         String output = Transformation.transform(transformationInfo.transformationType,
115                 transformationInfo.transformationFunction, input);
116
117         if (output != null && !output.isEmpty() && !input.equals(output)) {
118             String[] parts = output.split("\\|");
119
120             if (parts.length == 2) {
121                 @Nullable
122                 Class<? extends State> state = supportedStates.stream().filter(s -> s.getName().contains(parts[0]))
123                         .findFirst().orElse(null);
124
125                 if (state != null) {
126                     if (state.isEnum()) {
127                         State[] states;
128                         if ((states = state.getEnumConstants()) != null) {
129                             for (State s : states) {
130                                 if (s.toString().equalsIgnoreCase(parts[1])) {
131                                     return s;
132                                 }
133                             }
134                         }
135                         logger.debug("Could not find value '{}' for state '{}'", parts[1], parts[0]);
136                     } else {
137                         try {
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]);
142                         }
143                     }
144                 } else {
145                     logger.debug("State '{}' not found", parts[0]);
146                 }
147             } else {
148                 logger.debug("Transformation result malformed: {}", output);
149             }
150         }
151
152         return UnDefType.UNDEF;
153     }
154
155     @Override
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;
160         } else {
161             return bytes.length;
162         }
163     }
164
165     @Override
166     protected boolean validateData(byte[] bytes) {
167         return true;
168     }
169
170     @Override
171     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
172         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
173                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
174     }
175 }