]> git.basschouten.com Git - openhab-addons.git/blob
8c43257443b04828f7d89773551ee30283f09cc0
[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.unmodifiableList(new LinkedList<>() {
57         private static final long serialVersionUID = 1L;
58
59         {
60             add(DateTimeType.class);
61             add(DecimalType.class);
62             add(HSBType.class);
63             add(OnOffType.class);
64             add(OpenClosedType.class);
65             add(PercentType.class);
66             add(PlayPauseType.class);
67             add(PointType.class);
68             add(RewindFastforwardType.class);
69             add(StringListType.class);
70             add(StringType.class);
71             add(UpDownType.class);
72         }
73     });
74
75     public GenericEEP() {
76         super();
77     }
78
79     public GenericEEP(ERP1Message packet) {
80         super(packet);
81     }
82
83     @Override
84     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
85             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
86         if (config == null) {
87             logger.error("Cannot handle command {}, when transformation configuration is null", command.toString());
88             return;
89         }
90         EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
91
92         String input = channelId + "|" + command.toString();
93         String output = Transformation.transform(transformationInfo.transformationType,
94                 transformationInfo.transformationFunction, input);
95
96         if (output != null && !output.isEmpty() && !input.equals(output)) {
97             try {
98                 setData(HexUtils.hexToBytes(output));
99             } catch (IllegalArgumentException e) {
100                 logger.debug("Command {} could not transformed", command.toString());
101                 throw e;
102             }
103         }
104     }
105
106     @Override
107     protected State convertToStateImpl(String channelId, String channelTypeId,
108             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
109         EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
110
111         String payload = HexUtils.bytesToHex(bytes);
112         String input = channelId + "|" + payload;
113         String output = Transformation.transform(transformationInfo.transformationType,
114                 transformationInfo.transformationFunction, input);
115
116         if (output != null && !output.isEmpty() && !input.equals(output)) {
117             String[] parts = output.split("\\|");
118
119             if (parts.length == 2) {
120                 @Nullable
121                 Class<? extends State> state = supportedStates.stream().filter(s -> s.getName().contains(parts[0]))
122                         .findFirst().orElse(null);
123
124                 if (state != null) {
125                     if (state.isEnum()) {
126                         State[] states;
127                         if ((states = state.getEnumConstants()) != null) {
128                             for (State s : states) {
129                                 if (s.toString().equalsIgnoreCase(parts[1])) {
130                                     return s;
131                                 }
132                             }
133                         }
134                         logger.debug("Could not find value '{}' for state '{}'", parts[1], parts[0]);
135                     } else {
136                         try {
137                             return state.getConstructor(String.class).newInstance(parts[1]);
138                         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
139                                 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
140                             logger.debug("Could not create state '{}' with value '{}'", parts[0], parts[1]);
141                         }
142                     }
143                 } else {
144                     logger.debug("State '{}' not found", parts[0]);
145                 }
146             } else {
147                 logger.debug("Transformation result malformed: {}", output);
148             }
149         }
150
151         return UnDefType.UNDEF;
152     }
153
154     @Override
155     protected int getDataLength() {
156         ERP1Message localPacket = packet;
157         if (localPacket != null) {
158             return localPacket.getPayload().length - ESP3_SENDERID_LENGTH - ESP3_RORG_LENGTH - ESP3_STATUS_LENGTH;
159         } else {
160             return bytes.length;
161         }
162     }
163
164     @Override
165     protected boolean validateData(byte[] bytes) {
166         return true;
167     }
168
169     @Override
170     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
171         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
172                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
173     }
174 }