]> git.basschouten.com Git - openhab-addons.git/blob
b69ec05c4b3ab787b501100f4f660f8b2e776dfa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
46
47 /**
48  *
49  * @author Daniel Weber - Initial contribution
50  */
51 public class GenericEEP extends EEP {
52
53     final List<Class<? extends State>> supportedStates = Collections
54             .unmodifiableList(new LinkedList<Class<? extends State>>() {
55
56                 private static final long serialVersionUID = 1L;
57
58                 {
59                     add(DateTimeType.class);
60                     add(DecimalType.class);
61                     add(HSBType.class);
62                     add(OnOffType.class);
63                     add(OpenClosedType.class);
64                     add(PercentType.class);
65                     add(PlayPauseType.class);
66                     add(PointType.class);
67                     add(RewindFastforwardType.class);
68                     add(StringListType.class);
69                     add(StringType.class);
70                     add(UpDownType.class);
71                 }
72             });
73
74     public GenericEEP() {
75         super();
76     }
77
78     public GenericEEP(ERP1Message packet) {
79         super(packet);
80     }
81
82     @Override
83     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
84             Function<String, State> getCurrentStateFunc, Configuration config) {
85         if (config != null) {
86             EnOceanChannelTransformationConfig transformationInfo = config.as(EnOceanChannelTransformationConfig.class);
87
88             String input = channelId + "|" + command.toString();
89             String output = Transformation.transform(transformationInfo.transformationType,
90                     transformationInfo.transformationFunction, input);
91
92             if (output != null && !output.isEmpty() && !input.equals(output)) {
93                 try {
94                     setData(HexUtils.hexToBytes(output));
95                 } catch (Exception e) {
96                     logger.debug("Command {} could not transformed", command.toString());
97                 }
98             }
99         }
100     }
101
102     @Override
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);
107
108             String payload = HexUtils.bytesToHex(bytes);
109             String input = channelId + "|" + payload;
110             String output = Transformation.transform(transformationInfo.transformationType,
111                     transformationInfo.transformationFunction, input);
112
113             if (output != null && !output.isEmpty() && !input.equals(output)) {
114                 String[] parts = output.split("\\|");
115
116                 if (parts.length == 2) {
117                     Class<? extends State> state = supportedStates.stream().filter(s -> s.getName().contains(parts[0]))
118                             .findFirst().orElse(null);
119
120                     if (state != null) {
121                         if (state.isEnum()) {
122                             for (State s : state.getEnumConstants()) {
123                                 if (s.toString().equalsIgnoreCase(parts[1])) {
124                                     return s;
125                                 }
126                             }
127                             logger.debug("Could not find value '{}' for state '{}'", parts[1], parts[0]);
128                         } else {
129                             try {
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]);
134                             }
135                         }
136                     } else {
137                         logger.debug("State '{}' not found", parts[0]);
138                     }
139                 } else {
140                     logger.debug("Transformation result malformed: {}", output);
141                 }
142             }
143         }
144
145         return UnDefType.UNDEF;
146     }
147
148     @Override
149     protected int getDataLength() {
150         if (packet != null) {
151             return packet.getPayload().length - ESP3_SENDERID_LENGTH - ESP3_RORG_LENGTH - ESP3_STATUS_LENGTH;
152         } else {
153             return bytes.length;
154         }
155     }
156
157     @Override
158     protected boolean validateData(byte[] bytes) {
159         return true;
160     }
161
162     @Override
163     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
164         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
165                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
166     }
167 }