]> git.basschouten.com Git - openhab-addons.git/blob
059f9ef52ebfa8f152821b9d1aa01c1d5d410a51
[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.smartmeter.internal.iec62056;
14
15 import javax.measure.Quantity;
16 import javax.measure.Unit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.types.util.UnitUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Converts a unit from IEC62056-21 protocol to a {@link Unit}
26  *
27  * @author Matthias Steigenberger - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class Iec62056_21UnitConversion {
32
33     private static final Logger logger = LoggerFactory.getLogger(Iec62056_21UnitConversion.class);
34
35     @SuppressWarnings("unchecked")
36     public static @Nullable <Q extends Quantity<Q>> Unit<Q> getUnit(String unit) {
37         if (!unit.isEmpty()) {
38             try {
39                 return (Unit<Q>) UnitUtils.parseUnit(" " + unit);
40             } catch (Exception e) {
41                 logger.warn("Failed to parse unit {}: {}", unit, e.getMessage());
42                 return null;
43             }
44         }
45         return null;
46     }
47 }