]> git.basschouten.com Git - openhab-addons.git/blob
7da3de2282e0f17e8f9c142e5e6dd68f1d651856
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.proteusecometer.internal.ecometers;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Parse the bytes from the device
24  *
25  * @author Matthias Herrmann - Initial contribution
26  *
27  */
28 @NonNullByDefault
29 class ProteusEcoMeterSParser {
30
31     private final Logger logger = LoggerFactory.getLogger(ProteusEcoMeterSParser.class);
32
33     /**
34      * @param bytes Raw bytes send from the device
35      * @return A structured version of the bytes, if possible
36      */
37     public Optional<ProteusEcoMeterSReply> parseFromBytes(final byte[] bytes) {
38         return Optional.ofNullable(bytes).flatMap(b -> {
39             final String hexString = HexUtils.bytesToHex(b);
40             logger.trace("Received hex string: {}", hexString);
41
42             if (hexString.length() < 4) {
43                 return Optional.empty();
44             } else {
45                 final String marker = hexString.substring(0, 4);
46                 if (!"5349".equals(marker)) {
47                     logger.trace("Marker is not {} but {}", "5349", marker);
48                     return Optional.empty();
49                 } else if (hexString.length() < 40) {
50                     logger.trace("hexString is of length {}, expected >= 40", hexString.length());
51                     return Optional.empty();
52                 } else {
53                     try {
54                         return Optional
55                                 .of(new ProteusEcoMeterSReply(parseInt(hexString.substring(26, 28), "tempInFahrenheit"),
56                                         parseInt(hexString.substring(28, 32), "sensorLevelInCm"),
57                                         parseInt(hexString.substring(32, 36), "usableLevelInLiter"),
58                                         parseInt(hexString.substring(36, 40), "totalCapacityInLiter")));
59                     } catch (final NumberFormatException e) {
60                         logger.debug("Error while parsing numbers", e);
61                         return Optional.empty();
62                     }
63                 }
64             }
65         });
66     }
67
68     private Integer parseInt(final String toParse, final String fieldName) throws NumberFormatException {
69         try {
70             return Integer.parseInt(toParse, 16);
71         } catch (final NumberFormatException e) {
72             logger.trace("Unable to parse field {}", fieldName, e);
73             throw e;
74         }
75     }
76 }