]> git.basschouten.com Git - openhab-addons.git/blob
779da2d1033989e4b050639f66bc49a2c46a2cf6
[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.http.internal.transform;
14
15 import java.lang.ref.WeakReference;
16 import java.util.Optional;
17 import java.util.function.Function;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.transform.TransformationException;
22 import org.openhab.core.transform.TransformationService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * A transformation for a value used in {@HttpChannel}.
28  *
29  * @author David Graeff - Initial contribution
30  * @author Jan N. Klug - adapted from MQTT binding to HTTP binding
31  */
32 @NonNullByDefault
33 public class SingleValueTransformation implements ValueTransformation {
34     private final Logger logger = LoggerFactory.getLogger(SingleValueTransformation.class);
35     private final Function<String, @Nullable TransformationService> transformationServiceSupplier;
36     private WeakReference<@Nullable TransformationService> transformationService = new WeakReference<>(null);
37     private final String pattern;
38     private final String serviceName;
39
40     /**
41      * Creates a new channel state transformer.
42      *
43      * @param pattern A transformation pattern, starting with the transformation service
44      *            name, followed by a colon and the transformation itself.
45      * @param transformationServiceSupplier
46      */
47     public SingleValueTransformation(String pattern,
48             Function<String, @Nullable TransformationService> transformationServiceSupplier) {
49         this.transformationServiceSupplier = transformationServiceSupplier;
50         int index = pattern.indexOf(':');
51         if (index == -1) {
52             throw new IllegalArgumentException(
53                     "The transformation pattern must consist of the type and the pattern separated by a colon");
54         }
55         this.serviceName = pattern.substring(0, index).toUpperCase();
56         this.pattern = pattern.substring(index + 1);
57     }
58
59     @Override
60     public Optional<String> apply(String value) {
61         TransformationService transformationService = this.transformationService.get();
62         if (transformationService == null) {
63             transformationService = transformationServiceSupplier.apply(serviceName);
64             if (transformationService == null) {
65                 logger.warn("Transformation service {} for pattern {} not found!", serviceName, pattern);
66                 return Optional.empty();
67             }
68             this.transformationService = new WeakReference<>(transformationService);
69         }
70
71         try {
72             String result = transformationService.transform(pattern, value);
73             if (result == null) {
74                 logger.debug("Transformation {} returned empty result when applied to {}.", this, value);
75                 return Optional.empty();
76             }
77             return Optional.of(result);
78         } catch (TransformationException e) {
79             logger.warn("Executing transformation {} failed: {}", this, e.getMessage());
80         }
81
82         return Optional.empty();
83     }
84
85     @Override
86     public String toString() {
87         return "ChannelStateTransformation{pattern='" + pattern + "', serviceName='" + serviceName + "'}";
88     }
89 }