]> git.basschouten.com Git - openhab-addons.git/blob
952fa1e2a6e7d6de5a28ab43ba844cb16e6581a1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nobohub.internal.model;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * Nobø serial numbers are 12 digits where 3 and 3 digits form 2 bytes as decimal. In total 32 bits.
19  *
20  * @author Jørgen Austvik - Initial contribution
21  */
22 @NonNullByDefault
23 public final class Temperature {
24
25     private final SerialNumber serialNumber;
26     private final double temperature;
27
28     public Temperature(SerialNumber serialNumber, double temperature) {
29         this.serialNumber = serialNumber;
30         this.temperature = temperature;
31     }
32
33     public static Temperature fromY02(String y02) throws NoboDataException {
34         String parts[] = y02.split(" ", 3);
35         if (parts.length != 3) {
36             throw new NoboDataException(
37                     String.format("Unexpected number of parts from hub on Y02 call: %d", parts.length));
38         }
39
40         if (parts[2] == null) {
41             throw new NoboDataException("Missing temperature data");
42         }
43
44         SerialNumber serialNumber = new SerialNumber(parts[1]);
45         double temp = Double.NaN;
46
47         if (!"N/A".equals(parts[2])) {
48             try {
49                 temp = Double.parseDouble(parts[2]);
50             } catch (NumberFormatException nfe) {
51                 throw new NoboDataException(
52                         String.format("Failed to parse temperature %s: %s", parts[2], nfe.getMessage()), nfe);
53             }
54         }
55
56         return new Temperature(serialNumber, temp);
57     }
58
59     public SerialNumber getSerialNumber() {
60         return serialNumber;
61     }
62
63     public double getTemperature() {
64         return temperature;
65     }
66 }