]> git.basschouten.com Git - openhab-addons.git/blob
92b55c66e1a78fd65e1aee8c59a671c1f86948a8
[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.transform.bin2json.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.transform.TransformationException;
18 import org.openhab.core.transform.TransformationService;
19 import org.osgi.service.component.annotations.Component;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The implementation of {@link TransformationService} which transforms the
25  * hexa string formatted binary data by Binary Block Parser syntax to JSON format.
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 @NonNullByDefault
30 @Component(property = { "openhab.transform=BIN2JSON" })
31 public class Bin2JsonTransformationService implements TransformationService {
32
33     private Logger logger = LoggerFactory.getLogger(Bin2JsonTransformationService.class);
34
35     /**
36      * Transforms the input <code>source</code> by Java Binary Block Parser syntax.
37      *
38      * @param syntax Java Binary Block Parser syntax.
39      * @param source the input to transform
40      */
41     @Override
42     public @Nullable String transform(String syntax, String source) throws TransformationException {
43         final long startTime = System.currentTimeMillis();
44         logger.debug("About to transform '{}' by the Bin2Json syntax '{}'", source, syntax);
45
46         String result = "";
47
48         try {
49             result = String.valueOf(new Bin2Json(syntax).convert(source));
50             logger.debug("transformation resulted '{}'", result);
51             return result;
52         } catch (ConversionException e) {
53             throw new TransformationException("An error occurred while executing the converter. " + e.getMessage(), e);
54         } finally {
55             logger.trace("Bin2Json execution elapsed {} ms. Result: {}", System.currentTimeMillis() - startTime,
56                     result);
57         }
58     }
59 }