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

Alarm UI macros #2137

Merged
merged 2 commits into from
Feb 15, 2022
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018-2021 Oak Ridge National Laboratory.
* Copyright (c) 2018-2022 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,6 +14,9 @@
import java.util.logging.Logger;

import org.phoebus.applications.alarm.client.IdentificationHelper;
import org.phoebus.framework.macros.MacroOrSystemProvider;
import org.phoebus.framework.macros.MacroValueProvider;
import org.phoebus.framework.macros.Macros;
import org.phoebus.framework.preferences.AnnotatedPreferences;
import org.phoebus.framework.preferences.Preference;
import org.phoebus.framework.preferences.PreferencesReader;
Expand Down Expand Up @@ -133,6 +136,9 @@ public class AlarmSystem
/** "Disable until.." shortcuts */
@Preference public static String[] shelving_options;

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

static
{
final PreferencesReader prefs = AnnotatedPreferences.initialize(AlarmSystem.class, "/alarm_preferences.properties");
Expand Down Expand Up @@ -167,6 +173,16 @@ public class AlarmSystem
}
}

try
{
macros = new MacroOrSystemProvider(Macros.fromSimpleSpec(prefs.get("macros")));
}
catch (Exception ex)
{
logger.log(Level.WARNING, "Invalid macros '" + prefs.get("macros") + "'", ex);
macros = new MacroOrSystemProvider(new Macros());
}

IdentificationHelper.initialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ disable_notify_visible=false
# Comma separated, each option needs to comply with TimeParser.parseTemporalAmount():
# 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

# Macros for UI display, command or web links
#
# Format: M1=Value1, M2=Value2
macros=TOP=/home/controls/displays,WEBROOT=http://localhost/controls/displays
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Oak Ridge National Laboratory.
* Copyright (c) 2018-2022 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,6 +12,7 @@
import org.phoebus.applications.alarm.model.TitleDetail;
import org.phoebus.framework.jobs.CommandExecutor;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.macros.MacroHandler;
import org.phoebus.ui.javafx.ImageCache;

import javafx.scene.control.MenuItem;
Expand All @@ -32,7 +33,8 @@ public ExecuteCommandAction(final AlarmTreeItem<?> item, final TitleDetail comma
{
JobManager.schedule(command.title, monitor ->
{
final CommandExecutor executor = new CommandExecutor(command.detail, AlarmSystem.command_directory);
final String expanded = MacroHandler.replace(AlarmSystem.macros, command.detail);
final CommandExecutor executor = new CommandExecutor(expanded, AlarmSystem.command_directory);
executor.call();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Oak Ridge National Laboratory.
* Copyright (c) 2018-2022 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,6 +14,7 @@
import org.phoebus.applications.alarm.model.TitleDetail;
import org.phoebus.framework.jobs.CommandExecutor;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.macros.MacroHandler;
import org.phoebus.framework.spi.AppResourceDescriptor;
import org.phoebus.framework.util.ResourceParser;
import org.phoebus.ui.application.ApplicationLauncherService;
Expand All @@ -40,12 +41,23 @@ public OpenDisplayAction(final Node node, final AlarmTreeItem<?> item, final Tit
super(display.title, ImageCache.getImageView(AlarmSystem.class, "/icons/related_display.png"));
setOnAction(event ->
{
// Expand macros
final String expanded;
try
{
expanded = MacroHandler.replace(AlarmSystem.macros, display.detail);
}
catch (Exception ex)
{
ExceptionDetailsErrorDialog.openError(node, "Display Error", "Cannot expand macros in display '" + display.detail + "'", ex);
return;
}
// Open display as resource,
// which includes http://server.site/path/to/display.bob
// where the file type is handled by an application
try
{
final URI resource = ResourceParser.createResourceURI(display.detail);
final URI resource = ResourceParser.createResourceURI(expanded);
final AppResourceDescriptor app = ApplicationLauncherService.findApplication(resource, false, null);
if (app != null)
{
Expand All @@ -55,22 +67,22 @@ public OpenDisplayAction(final Node node, final AlarmTreeItem<?> item, final Tit
}
catch (Exception ex)
{
ExceptionDetailsErrorDialog.openError(node, "Display Error", "Cannot open " + display.detail, ex);
ExceptionDetailsErrorDialog.openError(node, "Display Error", "Cannot open " + expanded, ex);
return;
}

// For web pages, fall back to web browser
if (display.detail.startsWith("http:") ||
display.detail.startsWith("https:"))
if (expanded.startsWith("http:") ||
expanded.startsWith("https:"))
{
Platform.runLater(() -> PhoebusApplication.INSTANCE.getHostServices().showDocument(display.detail));
Platform.runLater(() -> PhoebusApplication.INSTANCE.getHostServices().showDocument(expanded));
return;
}

// Execute external command
JobManager.schedule(display.title, monitor ->
{
final CommandExecutor executor = new CommandExecutor(display.detail, AlarmSystem.command_directory);
final CommandExecutor executor = new CommandExecutor(expanded, AlarmSystem.command_directory);
executor.call();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/*******************************************************************************
* Copyright (c) 2018 Oak Ridge National Laboratory.
* Copyright (c) 2018-2022 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.phoebus.applications.alarm.ui;

import static org.phoebus.applications.alarm.AlarmSystem.logger;

import java.util.logging.Level;

import org.phoebus.applications.alarm.AlarmSystem;
import org.phoebus.applications.alarm.model.AlarmTreeItem;
import org.phoebus.applications.alarm.model.TitleDetail;
import org.phoebus.framework.macros.MacroHandler;
import org.phoebus.ui.dialog.DialogHelper;
import org.phoebus.ui.javafx.ImageCache;

Expand Down Expand Up @@ -37,7 +43,17 @@ public ShowGuidanceAction(final Node node, final AlarmTreeItem<?> item, final Ti
dialog.setTitle("Guidance for " + item.getName());
dialog.setHeaderText(guidance.title);

final TextArea details = new TextArea(guidance.detail);
String expanded;
try
{
expanded = MacroHandler.replace(AlarmSystem.macros, guidance.detail);
}
catch (Exception ex)
{
logger.log(Level.WARNING, "Cannot expand macros in alarm guidance '" + guidance.detail + "'");
expanded = guidance.detail;
}
final TextArea details = new TextArea(expanded);
details.setEditable(false);
details.setWrapText(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2022 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.phoebus.framework.macros;

/** Macro provider that falls back to Java system properties or environment variables
* @author Kay Kasemir
*/
public class MacroOrSystemProvider implements MacroValueProvider
{
final private MacroValueProvider macros;

/** @param macros Base macros */
public MacroOrSystemProvider(final MacroValueProvider macros)
{
this.macros = macros;
}

/** Get value for macro
* @param name Name of the macro
* @return Value of the macro or <code>null</code> if not defined
*/
@Override
public String getValue(final String name)
{
String value = macros.getValue(name);
if (value != null)
return value;

// Fall back to Java system properties
value = System.getProperty(name);
if (value != null)
return value;

// Finally, fall back to environment variables
return System.getenv(name);
}
}