Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ephemeris] Evolutions of the Ephemeris module #1169

Merged
merged 5 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.automation.internal.module.handler;

import java.time.ZonedDateTime;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class EphemerisConditionHandler extends BaseModuleHandler<Condition> impl

private final EphemerisManager ephemerisManager;
private final @Nullable String dayset;
private final int offset;
private final ZonedDateTime target;

public EphemerisConditionHandler(Condition condition, EphemerisManager ephemerisManager) {
super(condition);
Expand All @@ -49,7 +50,8 @@ public EphemerisConditionHandler(Condition condition, EphemerisManager ephemeris
this.dayset = DAYSET_MODULE_TYPE_ID.equals(module.getTypeUID())
? getValidStringConfigParameter(DAYSET, module.getConfiguration(), module.getId())
: null;
this.offset = getValidIntegerConfigParameter(OFFSET, module.getConfiguration(), module.getId());
int offset = getValidIntegerConfigParameter(OFFSET, module.getConfiguration(), module.getId());
target = ZonedDateTime.now().plusDays(offset);
}

private static int getValidIntegerConfigParameter(String parameter, Configuration config, String moduleId) {
Expand All @@ -76,15 +78,15 @@ private static String getValidStringConfigParameter(String parameter, Configurat
public boolean isSatisfied(Map<String, Object> inputs) {
switch (module.getTypeUID()) {
case HOLIDAY_MODULE_TYPE_ID:
return ephemerisManager.isBankHoliday(offset);
return ephemerisManager.isBankHoliday(target);
case WEEKEND_MODULE_TYPE_ID:
return ephemerisManager.isWeekend(offset);
return ephemerisManager.isWeekend(target);
case WEEKDAY_MODULE_TYPE_ID:
return !ephemerisManager.isWeekend(offset);
return !ephemerisManager.isWeekend(target);
case DAYSET_MODULE_TYPE_ID:
final String dayset = this.dayset;
if (dayset != null) {
return ephemerisManager.isInDayset(dayset, offset);
return ephemerisManager.isInDayset(dayset, target);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package org.eclipse.smarthome.core.ephemeris;

import java.io.FileNotFoundException;
import java.net.URL;
import java.time.ZonedDateTime;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -27,57 +29,145 @@
public interface EphemerisManager {

/**
* Tests given day (related to today) status against configured weekend days
* Tests given day status against configured weekend days
*
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @param date observed day
* @return whether the day is on weekend
*/
boolean isWeekend(int offset);
boolean isWeekend(ZonedDateTime date);

/**
* Tests given day (related to today) status against configured dayset
* Tests given day status against configured dayset
*
* @param daysetName name of the requested dayset, without prefix
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @return whether the day is on weekend
* @param date observed day
* @return whether the day is in the dayset
*/
boolean isInDayset(String daysetName, ZonedDateTime date);

/**
* Tests given day status
*
* @param date observed day
* @return whether the day is bank holiday or not
*/
boolean isInDayset(String daysetName, int offset);
boolean isBankHoliday(ZonedDateTime date);

/**
* Tests given day status against official bank holidays
* Tests given day status against given userfile
*
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @param date observed day
* @param url bundle resource file containing holiday definitions
* @return whether the day is bank holiday or not
*/
boolean isBankHoliday(int offset);
boolean isBankHoliday(ZonedDateTime date, URL resource);

/**
* Tests given day status against given userfile
*
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @param filename absolute or relative path to the file on local file system
* @param date observed day
* @param source absolute or relative path to the file on local file system
* @return whether the day is bank holiday or not
* @throws FileNotFoundException if given file does not exist
* @throws FileNotFoundException
*/
boolean isBankHoliday(ZonedDateTime date, String filename) throws FileNotFoundException;

/**
* Get given day name from given userfile
*
* @param date observed day
* @return name of the day or null if no corresponding entry
*/
boolean isBankHoliday(int offset, String filename) throws FileNotFoundException;
@Nullable
String getBankHolidayName(ZonedDateTime date);

/**
* Get given day bank holiday name
* Get given day name from given userfile
*
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @return name of the bank holiday or null if no bank holiday
* @param date observed day
* @param url bundle resource file containing holiday definitions
* @return name of the day or null if no corresponding entry
*/
@Nullable
String getBankHolidayName(int offset);
String getBankHolidayName(ZonedDateTime date, URL resource);

/**
* Get given day name from given userfile
*
* @param offset Today +/- offset days (+1 = tomorrow, -1 = yesterday)
* @param filename absolute or relative path to the file on local file system
* @param date observed day
* @param source absolute or relative path to the file on local file system
* @return name of the day or null if no corresponding entry
* @throws FileNotFoundException if given file does not exist
* @throws FileNotFoundException
*/
@Nullable
String getBankHolidayName(ZonedDateTime date, String filename) throws FileNotFoundException;

/**
* Gets the first next to come holiday in a 1 year time window
*
* @param startDate first day of the time window
* @return next coming holiday
*/
@Nullable
String getNextBankHoliday(ZonedDateTime startDate);

/**
* Gets the first next to come holiday in a 1 year time window
*
* @param startDate first day of the time window
* @param url bundle resource file containing holiday definitions
* @return next coming holiday
*/
@Nullable
String getNextBankHoliday(ZonedDateTime startDate, URL resource);

/**
* Gets the first next to come holiday in a 1 year time window
*
* @param startDate first day of the time window
* @param source absolute or relative path to the file on local file system
* @return next coming holiday
* @throws FileNotFoundException
*/
@Nullable
String getNextBankHoliday(ZonedDateTime startDate, String filename) throws FileNotFoundException;

/**
* Gets the localized holiday description
*
* @param holidayName code of searched holiday
* @return localized holiday description
*/
@Nullable
String getBankHolidayName(int offset, String filename) throws FileNotFoundException;
String getHolidayDescription(@Nullable String holiday);

/**
* Gets the number of days until searchedHoliday
*
* @param from first day of the time window
* @param searchedHoliday name of the searched holiday
* @return difference in days, -1 if not found
*/
long getDaysUntil(ZonedDateTime from, String searchedHoliday);

/**
* Gets the number of days until searchedHoliday in user file
*
* @param from first day of the time window
* @param searchedHoliday name of the searched holiday
* @param url bundle resource file containing holiday definitions
* @return difference in days, -1 if not found
*/
long getDaysUntil(ZonedDateTime from, String searchedHoliday, URL resource);

/**
* Gets the number of days until searchedHoliday in user file
*
* @param from first day of the time window
* @param searchedHoliday name of the searched holiday
* @param source absolute or relative path to the file on local file system
* @return difference in days, -1 if not found
* @throws FileNotFoundException
*/
long getDaysUntil(ZonedDateTime from, String searchedHoliday, String filename) throws FileNotFoundException;
}
Loading