]> git.basschouten.com Git - openhab-addons.git/blob
0cde97301ad2e41fb8f42879872ab806c048d192
[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.DayOfWeek;
16 import java.time.LocalDateTime;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.nobohub.internal.NoboHubBindingConstants;
20
21 /**
22  * The normal week profile (used when no {@link OverridePlan}s exist).
23  *
24  * @author Jørgen Austvik - Initial contribution
25  */
26 @NonNullByDefault
27 public final class WeekProfile {
28
29     private final int id;
30     private final String name;
31     private final String profile;
32
33     public WeekProfile(int id, String name, String profile) {
34         this.id = id;
35         this.name = name;
36         this.profile = profile;
37     }
38
39     public static WeekProfile fromH03(String h03) throws NoboDataException {
40         String[] parts = h03.split(" ", 4);
41
42         if (parts.length != 4) {
43             throw new NoboDataException(
44                     String.format("Unexpected number of parts from hub on H3 call: %d", parts.length));
45         }
46
47         return new WeekProfile(Integer.parseInt(parts[1]), ModelHelper.toJavaString(parts[2]),
48                 ModelHelper.toJavaString(parts[3]));
49     }
50
51     public int getId() {
52         return id;
53     }
54
55     public String getName() {
56         return name;
57     }
58
59     public String getProfile() {
60         return profile;
61     }
62
63     /**
64      * Returns the current status on the week profile (unless there is an override).
65      *
66      * @param time The current time
67      * @return The current status (according to the week profile)
68      */
69     public WeekProfileStatus getStatusAt(LocalDateTime time) throws NoboDataException {
70         final DayOfWeek weekDay = time.getDayOfWeek();
71         final int dayNumber = weekDay.getValue();
72         final String timeString = time.format(NoboHubBindingConstants.TIME_FORMAT_MINUTES);
73         String[] parts = profile.split(",");
74
75         int dayCounter = 0;
76         for (int i = 0; i < parts.length; i++) {
77             String current = parts[i];
78             if (current.startsWith("0000")) {
79                 dayCounter++;
80             }
81
82             if (current.length() != 5) {
83                 throw new NoboDataException("Illegal week profile entry: " + current);
84             }
85
86             if (dayNumber == dayCounter) {
87                 String next = "24000";
88                 if (i + 1 < parts.length) {
89                     if (!parts[i + 1].startsWith("0000")) {
90                         next = parts[i + 1];
91                     }
92                 }
93
94                 if (next.length() != 5) {
95                     throw new NoboDataException("Illegal week profile entry for next entry: " + next);
96                 }
97
98                 try {
99                     String currentTime = current.substring(0, 4);
100                     String nextTime = next.substring(0, 4);
101                     if (currentTime.compareTo(timeString) <= 0 && timeString.compareTo(nextTime) < 0) {
102                         try {
103                             return WeekProfileStatus.getByNumber(Integer.parseInt(String.valueOf(current.charAt(4))));
104                         } catch (NumberFormatException nfe) {
105                             throw new NoboDataException("Failed parsing week profile entry: " + current, nfe);
106                         }
107                     }
108                 } catch (IndexOutOfBoundsException oobe) {
109                     throw new NoboDataException("Illegal time string" + current + ", " + next, oobe);
110                 }
111             }
112         }
113
114         throw new NoboDataException(
115                 String.format("Failed to calculate %s for day %d in '%s'", timeString, dayNumber, profile));
116     }
117 }