2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nobohub.internal.model;
15 import java.time.DayOfWeek;
16 import java.time.LocalDateTime;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.nobohub.internal.NoboHubBindingConstants;
22 * The normal week profile (used when no {@link OverridePlan}s exist).
24 * @author Jørgen Austvik - Initial contribution
27 public final class WeekProfile {
30 private final String name;
31 private final String profile;
33 public WeekProfile(int id, String name, String profile) {
36 this.profile = profile;
39 public static WeekProfile fromH03(String h03) throws NoboDataException {
40 String[] parts = h03.split(" ", 4);
42 if (parts.length != 4) {
43 throw new NoboDataException(
44 String.format("Unexpected number of parts from hub on H3 call: %d", parts.length));
47 return new WeekProfile(Integer.parseInt(parts[1]), ModelHelper.toJavaString(parts[2]),
48 ModelHelper.toJavaString(parts[3]));
55 public String getName() {
59 public String getProfile() {
64 * Returns the current status on the week profile (unless there is an override).
66 * @param time The current time
67 * @return The current status (according to the week profile)
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(",");
76 for (int i = 0; i < parts.length; i++) {
77 String current = parts[i];
78 if (current.startsWith("0000")) {
82 if (current.length() != 5) {
83 throw new NoboDataException("Illegal week profile entry: " + current);
86 if (dayNumber == dayCounter) {
87 String next = "24000";
88 if (i + 1 < parts.length) {
89 if (!parts[i + 1].startsWith("0000")) {
94 if (next.length() != 5) {
95 throw new NoboDataException("Illegal week profile entry for next entry: " + next);
99 String currentTime = current.substring(0, 4);
100 String nextTime = next.substring(0, 4);
101 if (currentTime.compareTo(timeString) <= 0 && timeString.compareTo(nextTime) < 0) {
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);
108 } catch (IndexOutOfBoundsException oobe) {
109 throw new NoboDataException("Illegal time string" + current + ", " + next, oobe);
114 throw new NoboDataException(
115 String.format("Failed to calculate %s for day %d in '%s'", timeString, dayNumber, profile));