]> git.basschouten.com Git - openhab-addons.git/blob
3dd63013c76aa0470e81f229b852e0c5558b800d
[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.mikrotik.internal.util;
14
15 import java.time.LocalDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.format.DateTimeParseException;
18 import java.time.temporal.ChronoField;
19 import java.util.Locale;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25
26 /**
27  * The {@link Converter} is a utility class having functions to convert RouterOS-specific data representation strings
28  * to regular Java types.
29  *
30  * @author Oleg Vivtash - Initial contribution
31  */
32 @NonNullByDefault
33 public class Converter {
34     private static final DateTimeFormatter ROUTEROS_FORMAT = DateTimeFormatter.ofPattern("MMM/dd/yyyy kk:mm:ss",
35             Locale.ENGLISH);
36     private static final DateTimeFormatter ROUTEROS_FORMAT_NEW = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss",
37             Locale.ENGLISH);
38
39     private static final Pattern PERIOD_PATTERN = Pattern.compile("(\\d+)([a-z]+){1,3}");
40
41     public @Nullable static LocalDateTime fromRouterosTime(@Nullable String dateTimeString) {
42         if (dateTimeString == null || dateTimeString.length() < 19) {
43             return null;
44         }
45         try {
46             // As of Firmware 7.10 the date format has changed to "yyyy-MM-dd HH:mm:ss"
47             if (dateTimeString.length() == 19) {
48                 return LocalDateTime.parse(dateTimeString, ROUTEROS_FORMAT_NEW);
49             } else {
50                 String fixedTs = dateTimeString.substring(0, 1).toUpperCase() + dateTimeString.substring(1);
51                 return LocalDateTime.parse(fixedTs, ROUTEROS_FORMAT);
52             }
53         } catch (DateTimeParseException e) {
54             return null;
55         }
56     }
57
58     public @Nullable static LocalDateTime routerosPeriodBack(@Nullable String durationString) {
59         return routerosPeriodBack(durationString, LocalDateTime.now());
60     }
61
62     public @Nullable static LocalDateTime routerosPeriodBack(@Nullable String durationString,
63             LocalDateTime fromDateTime) {
64         if (durationString == null) {
65             return null;
66         }
67
68         Matcher m = PERIOD_PATTERN.matcher(durationString);
69         LocalDateTime ts = fromDateTime;
70         while (m.find()) {
71             int amount = Integer.parseInt(m.group(1));
72             String periodKey = m.group(2);
73             switch (periodKey) {
74                 case "y":
75                     ts = ts.minusYears(amount);
76                     break;
77                 case "w":
78                     ts = ts.minusWeeks(amount);
79                     break;
80                 case "d":
81                     ts = ts.minusDays(amount);
82                     break;
83                 case "h":
84                     ts = ts.minusHours(amount);
85                     break;
86                 case "m":
87                     ts = ts.minusMinutes(amount);
88                     break;
89                 case "s":
90                     ts = ts.minusSeconds(amount);
91                     break;
92                 case "ms":
93                     ts = ts.plus(amount, ChronoField.MILLI_OF_SECOND.getBaseUnit());
94                     break;
95                 default:
96                     throw new IllegalArgumentException(
97                             String.format("Unable to parse duration %s - %s is unknown", durationString, periodKey));
98             }
99         }
100         return ts;
101     }
102 }