2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.smartmeter.internal.iec62056;
15 import javax.measure.Quantity;
16 import javax.measure.Unit;
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;
25 * Converts a unit from IEC62056-21 protocol to a {@link Unit}
27 * @author Matthias Steigenberger - Initial contribution
31 public class Iec62056_21UnitConversion {
33 private static final Logger logger = LoggerFactory.getLogger(Iec62056_21UnitConversion.class);
35 @SuppressWarnings("unchecked")
36 public static @Nullable <Q extends Quantity<Q>> Unit<Q> getUnit(String unit) {
37 if (!unit.isEmpty()) {
39 return (Unit<Q>) UnitUtils.parseUnit(" " + unit);
40 } catch (Exception e) {
41 logger.warn("Failed to parse unit {}: {}", unit, e.getMessage());