]> git.basschouten.com Git - openhab-addons.git/blob
d0a8bbe831eacfc6d2556b1e8d36f90f17485b62
[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 java.time.DateTimeException;
16 import java.time.LocalDateTime;
17 import java.time.format.DateTimeParseException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.nobohub.internal.NoboHubBindingConstants;
22
23 /**
24  * Helper class for converting data to/from Nobø Hub.
25  *
26  * @author Jørgen Austvik - Initial contribution
27  * @author Espen Fossen - Initial contribution
28  */
29 @NonNullByDefault
30 public final class ModelHelper {
31
32     /**
33      * Converts a String returned form Nobø hub to a normal Java string.
34      *
35      * @param noboString String where Char 160 (nobr space is used for space)
36      * @return String with normal spaces.
37      */
38     static String toJavaString(final String noboString) {
39         return noboString.replace((char) 160, ' ');
40     }
41
42     /**
43      * Converts a String in java to a string the Nobø hub can understand (fix spaces).
44      *
45      * @param javaString String to send to Nobø hub
46      * @return String with Nobø hub spaces
47      */
48     static String toHubString(final String javaString) {
49         return javaString.replace(' ', (char) 160);
50     }
51
52     /**
53      * Creates a Java date string from a date string returned from the Nobø Hub.
54      *
55      * @param noboDateString Date string from Nobø, like '202001221832' or '-1'
56      * @return Java date for the returned string (or null if -1 is returned)
57      */
58     @Nullable
59     static LocalDateTime toJavaDate(final String noboDateString) throws NoboDataException {
60         if ("-1".equals(noboDateString)) {
61             return null;
62         }
63
64         try {
65             return LocalDateTime.parse(noboDateString, NoboHubBindingConstants.DATE_FORMAT_MINUTES);
66         } catch (DateTimeParseException pe) {
67             throw new NoboDataException(String.format("Failed parsing string %s", noboDateString), pe);
68         }
69     }
70
71     static String toHubDateMinutes(final @Nullable LocalDateTime date) {
72         if (null == date) {
73             return "-1";
74         }
75
76         try {
77             return date.format(NoboHubBindingConstants.DATE_FORMAT_MINUTES);
78         } catch (DateTimeException dte) {
79             return "-1";
80         }
81     }
82 }