Skip to content
Open
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 @@ -8,8 +8,8 @@
package org.phoebus.applications.alarm;

import java.io.File;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAmount;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;

import org.phoebus.applications.alarm.client.IdentificationHelper;
Expand All @@ -20,7 +20,7 @@
import org.phoebus.framework.preferences.Preference;
import org.phoebus.framework.preferences.PreferencesReader;
import org.phoebus.util.time.SecondsParser;
import org.phoebus.util.time.TimeParser;


/** Common alarm system code
* @author Kay Kasemir
Expand Down Expand Up @@ -121,11 +121,11 @@
/** Enable context menu for adding PVs to alarm configuration */
@Preference public static boolean enable_add_to_alarm_context_menu;

/** "Disable until.." shortcuts */
@Preference public static String[] shelving_options;

@Preference public static int max_block_ms;

/** Presets for the re-enable time when disabling an alarm */
public static final Map<String, DisableDurationPreset> disable_duration_presets;

/** Macros used in UI display/command/web links */
public static MacroValueProvider macros;

Expand All @@ -146,23 +146,6 @@
}
nag_period_ms = Math.round(Math.max(0, secs) * 1000.0);

// Check if the provided options can be parsed to avoid later runtime errors
final LocalDateTime now = LocalDateTime.now();
for (String option : shelving_options)
{
try
{
final TemporalAmount amount = TimeParser.parseTemporalAmount(option);
final LocalDateTime end = now.plus(amount);
if (! end.isAfter(now))
throw new Exception("Invalid 'shelving_options' value '" + option + "'");
}
catch (Throwable ex)
{
logger.log(Level.WARNING, "Error in 'shelving_options'", ex);
}
}

try
{
macros = new MacroOrSystemProvider(Macros.fromSimpleSpec(prefs.get("macros")));
Expand All @@ -173,6 +156,19 @@
macros = new MacroOrSystemProvider(new Macros());
}

disable_duration_presets = new LinkedHashMap<>();

Check warning on line 159 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make member "disable_duration_presets" "protected".

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzlD8j7TuxcaYF4f&open=AaHFMzlD8j7TuxcaYF4f&pullRequest=4013
for (final String preset : prefs.get("disable_duration_presets").split(","))
{
try
{
disable_duration_presets.put(preset.strip(), DisableDurationPreset.parse(preset));
}
catch (Exception ex)
{
logger.log(Level.WARNING, "Invalid disable duration preset '" + preset + "'", ex);

Check warning on line 168 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Lambda should be used to defer string concatenation.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzlD8j7TuxcaYF4g&open=AaHFMzlD8j7TuxcaYF4g&pullRequest=4013
}
}

IdentificationHelper.initialize();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.phoebus.applications.alarm;

import org.phoebus.util.time.TimeParser;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalAmount;
import java.util.Map;


/**
* Represents a disable duration that the user can select when disabling an alarm.
*
* Presets can be relative (e.g. 5 hours, 10 days) or occurrences (e.g. 17:00, Monday 3:50).
* Occurrences simply select the next occurrence of the given time or day.
*
* Occurrences can be minutes, hours:minutes, or weekday hours:minutes.
*/
public sealed interface DisableDurationPreset permits DisableDurationPreset.Relative, DisableDurationPreset.Occurrence {

record Relative(TemporalAmount amount) implements DisableDurationPreset {

public Relative(final TemporalAmount amount) {

Check warning on line 25 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant constructor which is the same as a default one.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzil8j7TuxcaYF4c&open=AaHFMzil8j7TuxcaYF4c&pullRequest=4013
this.amount = amount;
}

@Override
public LocalDateTime getNextDisableDateTime(final LocalDateTime now) {
return now.plus(amount);
}
}

record Occurrence(DayOfWeek day, Integer hour, int minute) implements DisableDurationPreset {

public Occurrence(final DayOfWeek day, final Integer hour, final int minute) {

Check warning on line 37 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant constructor which is the same as a default one.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzil8j7TuxcaYF4d&open=AaHFMzil8j7TuxcaYF4d&pullRequest=4013
this.day = day;
this.hour = hour;
this.minute = minute;

if (hour == null && day != null) {
throw new IllegalArgumentException("A day and minute was provided, but no hour");
}

if (hour != null && hour > 23) {
throw new IllegalArgumentException("Invalid hour " + hour);
}

if (minute > 59) {
throw new IllegalArgumentException("Invalid minute " + minute);
}
}

@Override
public LocalDateTime getNextDisableDateTime(final LocalDateTime now) {
LocalDateTime nextDisableTime = now.withMinute(minute).truncatedTo(ChronoUnit.MINUTES);

if (hour == null) {
// only minute
if (nextDisableTime.isBefore(now)) {
nextDisableTime = nextDisableTime.plusHours(1);
}
return nextDisableTime;
}

nextDisableTime = nextDisableTime.withHour(hour);
if (day == null) {
// hour and minute
if (nextDisableTime.isBefore(now)) {
nextDisableTime = nextDisableTime.plusDays(1);
}
return nextDisableTime;
}

// day, hour, and minute
nextDisableTime = nextDisableTime.with(TemporalAdjusters.nextOrSame(day));
if (nextDisableTime.isBefore(now)) {
nextDisableTime = nextDisableTime.plusWeeks(1);
}
return nextDisableTime;
}
}

LocalDateTime getNextDisableDateTime(final LocalDateTime now);

static DisableDurationPreset parse(final String string) {
final String stripped = string.strip();

final Map<String, DayOfWeek> weekdaysMap = Map.of(
"Mon", DayOfWeek.MONDAY,
"Tue", DayOfWeek.TUESDAY,
"Wed", DayOfWeek.WEDNESDAY,
"Thu", DayOfWeek.THURSDAY,
"Fri", DayOfWeek.FRIDAY,
"Sat", DayOfWeek.SATURDAY,
"Sun", DayOfWeek.SUNDAY
);
final String weekdaysRegex = "(" + String.join("|", weekdaysMap.keySet()) + ")";

if (stripped.matches("xx:\\d\\d"))
{
return new DisableDurationPreset.Occurrence(
null,
null,
Integer.parseInt(stripped.substring(3))
);
} else if (stripped.matches("\\d\\d:\\d\\d"))
{
return new Occurrence(
null,
Integer.parseInt(stripped.substring(0, 2)),
Integer.parseInt(stripped.substring(3))
);
} else if (stripped.matches(weekdaysRegex + " \\d\\d:\\d\\d")) {
return new Occurrence(
weekdaysMap.get(stripped.substring(0, 3)),
Integer.parseInt(stripped.substring(4, 6)),
Integer.parseInt(stripped.substring(7))
);
}

final TemporalAmount amount = TimeParser.parseTemporalAmount(stripped);
final LocalDateTime now = LocalDateTime.now();

Check warning on line 124 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Explicitly specify the time zone by passing a ZoneId or a Clock to the .now() method.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzil8j7TuxcaYF4e&open=AaHFMzil8j7TuxcaYF4e&pullRequest=4013
final LocalDateTime end = now.plus(amount);
if (!end.isAfter(now))
throw new RuntimeException("Invalid 'disable_duration_presets' value '" + string + "'");

Check warning on line 127 in app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace generic exceptions with specific library exceptions or a custom exception.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzik8j7TuxcaYF4b&open=AaHFMzik8j7TuxcaYF4b&pullRequest=4013
return new DisableDurationPreset.Relative(amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EnabledState
/** Time to (re-)enable */
public final LocalDateTime enabled_date;

/** Plain enablement */
/** Plain enablement - always false if enabled_date is not null */
public final boolean enabled;

private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
Expand Down
11 changes: 7 additions & 4 deletions app/alarm/model/src/main/resources/alarm_preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ disable_notify_visible=false
# PVs to the alarm tree.
enable_add_to_alarm_context_menu=true

# Options for the "Disable until.." shortcuts in the PV config dialog.
# Duration presets for the "Disable" shortcut in the alarm context menu.
#
# :format:
# comma separated, each option needs to comply with `TimeParser.parseTemporalAmount()`:
# Comma separated list of presets. Presets can be relative (5 minutes,
# 6 hours, 1 day, etc) or occurrences. There are three formats for
# occurrences: xx:15 (the next time it's quarter past any hour),
# 17:43, and Tue 14:30 (this or the next Tuesday at 14:30).
#
# 30 seconds, 5 minutes, 1 hour, 6 hours, 1 day, 30 days, ...
shelving_options=1 hour, 6 hours, 12 hours, 1 day, 7 days, 30 days
# Weekdays can be Mon, Tue, Wed, Thu, Fri, Sat, Sun
disable_duration_presets=1 hour, 6 hours, 12 hours, 1 day, 7 days, 30 days

# Macros for UI display, command or web links
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.phoebus.applications.alarm;

import org.hamcrest.Matchers;

Check warning on line 3 in app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'org.hamcrest.Matchers'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzgJ8j7TuxcaYF4a&open=AaHFMzgJ8j7TuxcaYF4a&pullRequest=4013
import org.junit.jupiter.api.Test;
import org.phoebus.util.time.TimeDuration;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;


public class DisableDurationPresetTest {

Check warning on line 17 in app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove redundant visibility modifiers from the methods of this test class.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzgJ8j7TuxcaYF4Z&open=AaHFMzgJ8j7TuxcaYF4Z&pullRequest=4013

@Test
public void testParse() {
assertThat(DisableDurationPreset.parse("5 min"), equalTo(new DisableDurationPreset.Relative(TimeDuration.ofMinutes(5))));
assertThat(DisableDurationPreset.parse("xx:16"), equalTo(new DisableDurationPreset.Occurrence(null, null, 16)));
assertThat(DisableDurationPreset.parse("12:16"), equalTo(new DisableDurationPreset.Occurrence(null, 12, 16)));
assertThat(DisableDurationPreset.parse("Mon 18:16"), equalTo(new DisableDurationPreset.Occurrence(DayOfWeek.MONDAY, 18, 16)));
assertThrows(Exception.class, () -> DisableDurationPreset.parse("25:00"));
assertThrows(Exception.class, () -> DisableDurationPreset.parse("xx:70"));
}

@Test
public void testGetNextDisableDateTime() {
final LocalDateTime now = LocalDateTime.of(2050, 12, 25, 6, 9);

assertThat(
new DisableDurationPreset.Relative(Duration.of(5, ChronoUnit.DAYS)).getNextDisableDateTime(now),
equalTo(now.plusDays(5))
);

assertThat(
new DisableDurationPreset.Occurrence(null, null, 50).getNextDisableDateTime(now),
equalTo(now.withMinute(50))
);
assertThat(
new DisableDurationPreset.Occurrence(null, null, 5).getNextDisableDateTime(now),
equalTo(now.withMinute(5).plusHours(1))
);

assertThat(
new DisableDurationPreset.Occurrence(null, 12, 5).getNextDisableDateTime(now),
equalTo(now.withHour(12).withMinute(5))
);
assertThat(
new DisableDurationPreset.Occurrence(null, 3, 5).getNextDisableDateTime(now),
equalTo(now.withHour(3).withMinute(5).plusDays(1))
);

assertThat(
new DisableDurationPreset.Occurrence(now.getDayOfWeek(), 12, 5).getNextDisableDateTime(now),
equalTo(now.withHour(12).withMinute(5))
);
assertThat(
new DisableDurationPreset.Occurrence(now.getDayOfWeek(), 3, 5).getNextDisableDateTime(now),
equalTo(now.withHour(3).withMinute(5).plusWeeks(1))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Messages
public static String Annunciator;
public static String commands;
public static String configure;
public static String custom;
public static String delayTooltip0;
public static String delayTooltip1;
public static String delayTooltip2;
Expand All @@ -45,12 +46,13 @@ public class Messages
public static String indefinitely;
public static String MenuPath;
public static String moveItemFailed;
public static String promptTitle;
public static String removeComponentFailed;
public static String renameItemFailed;
public static String selectedDateInThePast;
public static String today;
public static String tomorrow;
public static String totalPVs;
public static String unacknowledgeFailed;
public static String withEnableDate;

static
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import javafx.beans.NamedArg;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;
Expand Down Expand Up @@ -39,7 +41,7 @@
}

/** Constructor */
public DateTimePicker() {
public DateTimePicker(@NamedArg(value = "allowPastDateTimes", defaultValue = "false") final boolean allowPastDateTimes) {

Check failure on line 44 in app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DateTimePicker.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHFMzQi8j7TuxcaYF4U&open=AaHFMzQi8j7TuxcaYF4U&pullRequest=4013
getStyleClass().add("datetime-picker");
setFormat(DefaultFormat);
setConverter(new InternalConverter());
Expand Down Expand Up @@ -80,6 +82,15 @@
simulateEnterPressed();
});

if (!allowPastDateTimes) {
setDayCellFactory(_ -> new DateCell() {
@Override
public void updateItem(final LocalDate date, final boolean empty) {
super.updateItem(date, empty);
setDisable(empty || LocalDate.now().isAfter(date));
}
});
}
}

private void simulateEnterPressed() {
Expand Down
Loading
Loading