Search in sources :

Example 1 with ExecuteScriptActionInfo

use of org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo in project org.csstudio.display.builder by kasemir.

the class JFXActionsDialogDemo method start.

@Override
public void start(final Stage stage) {
    final Widget widget = new Widget("demo");
    final Macros macros = new Macros();
    macros.add("S", "Test");
    macros.add("N", "17");
    final ActionInfos actions = new ActionInfos(Arrays.asList(new OpenDisplayActionInfo("Related Display", "../opi/some_file.opi", macros, Target.TAB), new WritePVActionInfo("Reset", "Test:CS:Reset", "1"), new ExecuteScriptActionInfo("Script", new ScriptInfo(ScriptInfo.EMBEDDED_PYTHON, "print 'hi'", false, Collections.emptyList()))));
    final ActionsDialog dialog = new ActionsDialog(widget, actions);
    final Optional<ActionInfos> result = dialog.showAndWait();
    if (result.isPresent()) {
        if (result.get().isExecutedAsOne())
            System.out.println("Execute all commands as one:");
        for (ActionInfo info : result.get().getActions()) {
            if (info instanceof ExecuteScriptActionInfo) {
                final ExecuteScriptActionInfo action = (ExecuteScriptActionInfo) info;
                System.out.println("Execute " + action.getDescription() + ", " + action.getInfo() + ": " + action.getInfo().getText());
            } else
                System.out.println(info);
        }
    } else
        System.out.println("Cancelled");
}
Also used : ActionsDialog(org.csstudio.display.builder.representation.javafx.ActionsDialog) ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) Widget(org.csstudio.display.builder.model.Widget) ActionInfos(org.csstudio.display.builder.model.properties.ActionInfos) ActionInfo(org.csstudio.display.builder.model.properties.ActionInfo) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) OpenDisplayActionInfo(org.csstudio.display.builder.model.properties.OpenDisplayActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) Macros(org.csstudio.display.builder.model.macros.Macros) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo) OpenDisplayActionInfo(org.csstudio.display.builder.model.properties.OpenDisplayActionInfo)

Example 2 with ExecuteScriptActionInfo

use of org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo in project org.csstudio.display.builder by kasemir.

the class WidgetRuntime method stop.

/**
 * Stop: Disconnect PVs, ...
 */
public void stop() {
    awaitStartup();
    widget.propClass().removePropertyListener(update_widget_class);
    final List<RuntimePV> safe_pvs = writable_pvs;
    if (safe_pvs != null) {
        for (final RuntimePV pv : safe_pvs) {
            removePV(pv);
            PVFactory.releasePV(pv);
        }
        writable_pvs = null;
    }
    final PVNameToValueBinding binding = pv_name_binding.getAndSet(null);
    if (binding != null)
        binding.dispose();
    final Map<ExecuteScriptActionInfo, Script> actions = action_scripts;
    if (actions != null) {
        actions.clear();
        action_scripts = null;
    }
    final List<RuntimeScriptHandler> handlers = script_handlers;
    if (handlers != null) {
        for (final RuntimeScriptHandler handler : handlers) handler.shutdown();
        script_handlers = null;
    }
    if (runtime_pvs != null) {
        final Collection<RuntimePV> pvs = runtime_pvs.getPVs();
        if (!pvs.isEmpty())
            logger.log(Level.SEVERE, widget + " has unreleased PVs: " + pvs);
    }
    // Close script support that might have been created
    // by RuntimeScriptHandlers or action-invoked scripts
    final ScriptSupport scripting = widget.getUserData(Widget.USER_DATA_SCRIPT_SUPPORT);
    if (scripting != null)
        scripting.close();
    // Prepare for another start()
    started = new CountDownLatch(1);
}
Also used : RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) Script(org.csstudio.display.builder.runtime.script.internal.Script) RuntimeScriptHandler(org.csstudio.display.builder.runtime.script.internal.RuntimeScriptHandler) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) CountDownLatch(java.util.concurrent.CountDownLatch) ScriptSupport(org.csstudio.display.builder.runtime.script.internal.ScriptSupport)

Example 3 with ExecuteScriptActionInfo

use of org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo in project org.csstudio.display.builder by kasemir.

the class WidgetRuntime method startScripts.

/**
 * Start Scripts
 */
private void startScripts() {
    // Start scripts triggered by PVs
    final List<ScriptInfo> script_infos = widget.propScripts().getValue();
    final List<RuleInfo> rule_infos = widget.propRules().getValue();
    if ((script_infos.size() > 0) || (rule_infos.size() > 0)) {
        final List<RuntimeScriptHandler> handlers = new ArrayList<>(script_infos.size() + rule_infos.size());
        for (final ScriptInfo script_info : script_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, script_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_info.getPath());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        for (final RuleInfo rule_info : rule_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, rule_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Rule failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(rule_info.getName());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        script_handlers = handlers;
    }
    // Compile scripts invoked by actions
    final List<ActionInfo> actions = widget.propActions().getValue().getActions();
    if (actions.size() > 0) {
        final Map<ExecuteScriptActionInfo, Script> scripts = new HashMap<>();
        for (ActionInfo action_info : actions) {
            if (!(action_info instanceof ExecuteScriptActionInfo))
                continue;
            final ExecuteScriptActionInfo script_action = (ExecuteScriptActionInfo) action_info;
            try {
                final MacroValueProvider macros = widget.getMacrosOrProperties();
                final Script script = RuntimeScriptHandler.compileScript(widget, macros, script_action.getInfo());
                scripts.put(script_action, script);
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script for action failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_action);
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        if (scripts.size() > 0)
            action_scripts = scripts;
    }
    // Signal that start() has completed
    started.countDown();
}
Also used : Script(org.csstudio.display.builder.runtime.script.internal.Script) HashMap(java.util.HashMap) ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) ArrayList(java.util.ArrayList) ActionInfo(org.csstudio.display.builder.model.properties.ActionInfo) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider) DisplayModel(org.csstudio.display.builder.model.DisplayModel) RuntimeScriptHandler(org.csstudio.display.builder.runtime.script.internal.RuntimeScriptHandler) RuleInfo(org.csstudio.display.builder.model.rules.RuleInfo)

Example 4 with ExecuteScriptActionInfo

use of org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo in project org.csstudio.display.builder by kasemir.

the class WidgetRuntime method executeScriptAction.

/**
 * Execute script
 *  @param action_info Which script-based action to execute
 *  @throws NullPointerException if action_info is not valid, runtime not initialized
 */
public void executeScriptAction(final ExecuteScriptActionInfo action_info) throws NullPointerException {
    awaitStartup();
    final Map<ExecuteScriptActionInfo, Script> actions = Objects.requireNonNull(action_scripts);
    final Script script = Objects.requireNonNull(actions.get(action_info));
    script.submit(widget);
}
Also used : Script(org.csstudio.display.builder.runtime.script.internal.Script) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo)

Example 5 with ExecuteScriptActionInfo

use of org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo in project org.csstudio.display.builder by kasemir.

the class ActionsDialog method getExecuteScriptAction.

/**
 * @return {@link ExecuteScriptActionInfo} from sub pane
 */
private ExecuteScriptActionInfo getExecuteScriptAction() {
    final String file = execute_script_file.getText();
    final String text = (file.equals(ScriptInfo.EMBEDDED_PYTHON) || file.equals(ScriptInfo.EMBEDDED_JAVASCRIPT)) ? execute_script_text.getText() : null;
    return new ExecuteScriptActionInfo(execute_script_description.getText(), new ScriptInfo(file, text, false, Collections.emptyList()));
}
Also used : ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo)

Aggregations

ExecuteScriptActionInfo (org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo)5 ScriptInfo (org.csstudio.display.builder.model.properties.ScriptInfo)3 Script (org.csstudio.display.builder.runtime.script.internal.Script)3 ActionInfo (org.csstudio.display.builder.model.properties.ActionInfo)2 WritePVActionInfo (org.csstudio.display.builder.model.properties.WritePVActionInfo)2 RuntimeScriptHandler (org.csstudio.display.builder.runtime.script.internal.RuntimeScriptHandler)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DisplayModel (org.csstudio.display.builder.model.DisplayModel)1 Widget (org.csstudio.display.builder.model.Widget)1 MacroValueProvider (org.csstudio.display.builder.model.macros.MacroValueProvider)1 Macros (org.csstudio.display.builder.model.macros.Macros)1 ActionInfos (org.csstudio.display.builder.model.properties.ActionInfos)1 OpenDisplayActionInfo (org.csstudio.display.builder.model.properties.OpenDisplayActionInfo)1 RuleInfo (org.csstudio.display.builder.model.rules.RuleInfo)1 ActionsDialog (org.csstudio.display.builder.representation.javafx.ActionsDialog)1 RuntimePV (org.csstudio.display.builder.runtime.pv.RuntimePV)1 ScriptSupport (org.csstudio.display.builder.runtime.script.internal.ScriptSupport)1