]> git.basschouten.com Git - openhab-addons.git/blob
dfe1fd0411c9843391399da81f6cb5703d97392b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miele.internal;
14
15 import java.nio.charset.StandardCharsets;
16
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;
21
22 /**
23  * The {@link ExtendedDeviceStateUtil} class contains utility methods for parsing
24  * ExtendedDeviceState information
25  *
26  * @author Jacob Laursen - Added power/water consumption channels
27  */
28 public class ExtendedDeviceStateUtil {
29     private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
30     private static final String TEMPERATURE_UNDEFINED = "32768";
31
32     /**
33      * Convert byte array to hex representation.
34      */
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];
41         }
42
43         return new String(hexChars, StandardCharsets.UTF_8);
44     }
45
46     /**
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.
51      */
52     public static byte[] stringToBytes(String input) {
53         return input.getBytes(StandardCharsets.ISO_8859_1);
54     }
55
56     /**
57      * Convert string to Number:Temperature state with unit Celcius
58      */
59     public static State getTemperatureState(String s) throws NumberFormatException {
60         if (TEMPERATURE_UNDEFINED.equals(s)) {
61             return UnDefType.UNDEF;
62         }
63         int temperature = Integer.parseInt(s);
64         return new QuantityType<>(temperature, SIUnits.CELSIUS);
65     }
66 }