2 * Copyright (c) 2010-2021 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.miele.internal;
15 import java.nio.charset.StandardCharsets;
17 import org.openhab.core.library.types.QuantityType;
18 import org.openhab.core.library.unit.SIUnits;
19 import org.openhab.core.types.State;
20 import org.openhab.core.types.UnDefType;
23 * The {@link ExtendedDeviceStateUtil} class contains utility methods for parsing
24 * ExtendedDeviceState information
26 * @author Jacob Laursen - Added power/water consumption channels
28 public class ExtendedDeviceStateUtil {
29 private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
30 private static final String TEMPERATURE_UNDEFINED = "32768";
33 * Convert byte array to hex representation.
35 public static String bytesToHex(byte[] bytes) {
36 byte[] hexChars = new byte[bytes.length * 2];
37 for (int j = 0; j < bytes.length; j++) {
38 int v = bytes[j] & 0xFF;
39 hexChars[j * 2] = HEX_ARRAY[v >>> 4];
40 hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
43 return new String(hexChars, StandardCharsets.UTF_8);
47 * Convert string consisting of 8 bit characters to byte array.
48 * Note: This simple operation has been extracted and put here to document
49 * and ensure correct behavior for 8 bit characters that should be turned
50 * into single bytes without any UTF-8 encoding.
52 public static byte[] stringToBytes(String input) {
53 return input.getBytes(StandardCharsets.ISO_8859_1);
57 * Convert string to Number:Temperature state with unit Celcius
59 public static State getTemperatureState(String s) throws NumberFormatException {
60 if (TEMPERATURE_UNDEFINED.equals(s)) {
61 return UnDefType.UNDEF;
63 int temperature = Integer.parseInt(s);
64 return new QuantityType<>(temperature, SIUnits.CELSIUS);