• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / phoebus / #77

03 Sep 2023 05:01PM UTC coverage: 16.544% (-0.02%) from 16.566%
#77

push

web-flow
Merge pull request #87 from WindhoverLabs/param_export

Param export

50 of 50 new or added lines in 3 files covered. (100.0%)

17740 of 107229 relevant lines covered (16.54%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/commander-core/src/main/java/com/windhoverlabs/yamcs/script/Yamcs.java
1
package com.windhoverlabs.yamcs.script;
2

3
import com.windhoverlabs.pv.yamcs.YamcsPVFactory;
4
import com.windhoverlabs.pv.yamcs.YamcsPlugin;
5
import com.windhoverlabs.yamcs.commanding.CommandParser;
6
import com.windhoverlabs.yamcs.commanding.CommandParser.ParseResult;
7
import com.windhoverlabs.yamcs.core.CMDR_YamcsInstance;
8
import com.windhoverlabs.yamcs.core.CMDR_YamcsInstance.CommandOption;
9
import com.windhoverlabs.yamcs.core.YamcsObjectManager;
10
import com.windhoverlabs.yamcs.core.YamcsServer;
11
import java.util.Map;
12
import java.util.Map.Entry;
13
import java.util.logging.Logger;
14
import javafx.collections.ObservableList;
15
import org.csstudio.display.builder.model.Widget;
16
import org.phoebus.framework.macros.MacroHandler;
17
import org.phoebus.framework.macros.Macros;
18
import org.yamcs.client.processor.ProcessorClient;
19
import org.yamcs.client.processor.ProcessorClient.CommandBuilder;
20
import org.yamcs.protobuf.IssueCommandRequest.Assignment;
21
import org.yamcs.protobuf.Yamcs.Value;
22

23
public class Yamcs {
×
24

25
  public static Logger log = Logger.getLogger(Yamcs.class.getName());
×
26

27
  private static CMDR_YamcsInstance getInstance(String serverName, String instanceName) {
28
    YamcsServer server = YamcsObjectManager.getServerFromName(serverName);
×
29

30
    if (server == null) {
×
31
      log.warning("Server \"" + serverName + "\" not found.");
×
32
      return null;
×
33
    }
34
    CMDR_YamcsInstance instance =
×
35
        YamcsObjectManager.getServerFromName(serverName).getInstance(instanceName);
×
36

37
    if (instance == null) {
×
38
      log.warning("Instance \"" + instanceName + "\" not found.");
×
39
      return null;
×
40
    }
41

42
    return YamcsObjectManager.getServerFromName(serverName).getInstance(instanceName);
×
43
  }
44

45
  /**
46
   * Sample use:
47
   *
48
   * <p>Yamcs.issueCommand('sitl:yamcs-cfs/YSS/SIMULATOR/SWITCH_VOLTAGE_ON(voltage_num: 1)');
49
   */
50
  public static void issueCommand(String commandText) {
51
    ParseResult parsed = CommandParser.parseCommand(commandText);
×
52

53
    ProcessorClient processor =
×
54
        getInstance(parsed.getServer(), parsed.getInstance()).getYamcsProcessor();
×
55

56
    // Eventually this could be overridden by scripts
57
    ObservableList<CommandOption> options =
×
58
        getInstance(parsed.getServer(), parsed.getInstance()).getOptionsList();
×
59

60
    if (processor == null) {
×
61
      log.warning("No active processor");
×
62
      return;
×
63
    }
64

65
    CommandBuilder builder =
×
66
        processor
67
            .prepareCommand(parsed.getQualifiedName())
×
68
            .withSequenceNumber(YamcsPlugin.nextCommandSequenceNumber());
×
69
    for (Assignment arg : parsed.getAssignments()) {
×
70
      builder.withArgument(arg.getName(), arg.getValue());
×
71
    }
×
72

73
    for (var op : options) {
×
74
      String v = op.getValue();
×
75

76
      // I know, know -- this is hideous. I'll fix it. Just want this to work for now.
77
      Value yamcsValue =
78
          Value.newBuilder()
×
79
              .setType(org.yamcs.protobuf.Yamcs.Value.Type.BOOLEAN)
×
80
              .setBooleanValue(false)
×
81
              .build();
×
82
      if (v.trim().equals("true")) {
×
83
        yamcsValue =
84
            Value.newBuilder()
×
85
                .setType(org.yamcs.protobuf.Yamcs.Value.Type.BOOLEAN)
×
86
                .setBooleanValue(true)
×
87
                .build();
×
88
      }
89
      builder.withExtra(op.getId(), yamcsValue);
×
90
    }
×
91
    builder.issue();
×
92
  }
×
93

94
  /**
95
   * Sample use:
96
   *
97
   * <p>Yamcs.issueCommand('sitl:yamcs-cfs/YSS/SIMULATOR/SWITCH_VOLTAGE_ON', {"voltage_num": 1});
98
   * Yamcs.issueCommand('/YSS/SIMULATOR/SWITCH_VOLTAGE_ON', {"voltage_num": 1});
99
   */
100
  public static void issueCommand(String command, Map<String, Object> args) {
101

102
    if (command == null) {
×
103
      return;
×
104
    }
105

106
    command = command.trim();
×
107

108
    String serverName = null;
×
109
    String instanceName = null;
×
110
    try {
111
      // TODO: Clean this up a bit
112
      serverName = command.split(":")[0];
×
113
      if (serverName.contains("/")) {
×
114
        serverName = serverName.substring(serverName.indexOf("/") + 1);
×
115
      }
116
      instanceName = command.split(":")[1].split("/")[0];
×
117
    } catch (Exception e) {
×
118
      log.warning(
×
119
          "Error on expression \""
120
              + command
121
              + "\". Verify that the instance and server names exist.");
122
      return;
×
123
    }
×
124

125
    command = command.split(":")[1].substring(command.split(":")[1].indexOf('/'));
×
126

127
    // Eventually this could be overridden by scripts
128
    ObservableList<CommandOption> options = getInstance(serverName, instanceName).getOptionsList();
×
129
    ProcessorClient processor = getInstance(serverName, instanceName).getYamcsProcessor();
×
130

131
    if (processor == null) {
×
132
      log.warning("No active processor");
×
133
      return;
×
134
    }
135

136
    CommandBuilder builder =
×
137
        processor
138
            .prepareCommand(command)
×
139
            .withSequenceNumber(YamcsPlugin.nextCommandSequenceNumber());
×
140

141
    if (args != null) {
×
142
      for (Entry<String, Object> arg : args.entrySet()) {
×
143
        builder.withArgument(arg.getKey(), String.valueOf(arg.getValue()));
×
144
      }
×
145
    }
146

147
    for (var op : options) {
×
148
      String v = op.getValue();
×
149

150
      // I know, know -- this is hideous. I'll fix it. Just want this to work for now.
151
      Value yamcsValue =
152
          Value.newBuilder()
×
153
              .setType(org.yamcs.protobuf.Yamcs.Value.Type.BOOLEAN)
×
154
              .setBooleanValue(false)
×
155
              .build();
×
156
      if (v.trim().equals("true")) {
×
157
        yamcsValue =
158
            Value.newBuilder()
×
159
                .setType(org.yamcs.protobuf.Yamcs.Value.Type.BOOLEAN)
×
160
                .setBooleanValue(true)
×
161
                .build();
×
162
      }
163
      builder.withExtra(op.getId(), yamcsValue);
×
164
    }
×
165
    builder.issue();
×
166
  }
×
167

168
  public static void issueCommand(Widget widget, String commandText) {
169
    /* TODO: Finish this. */
170
    try {
171
      Macros macros = widget.getEffectiveMacros();
×
172
      String expanded_commandText = MacroHandler.replace(macros, commandText);
×
173

174
      issueCommand(expanded_commandText);
×
175
    } catch (Exception e) {
×
176
      log.warning("FINISH HIM!-->" + e.toString());
×
177
    }
×
178
  }
×
179

180
  /* TODO: Expand the macros too. */
181
  public static void issueCommand(Widget widget, String commandText, Map<String, Object> args) {
182
    /* TODO: Finish this. */
183
    try {
184
      Macros macros = widget.getEffectiveMacros();
×
185
      String expanded_commandText = MacroHandler.replace(macros, commandText);
×
186
      expanded_commandText = YamcsPVFactory.sanitizePVName(expanded_commandText);
×
187

188
      issueCommand(expanded_commandText, args);
×
189
    } catch (Exception e) {
×
190
      // TODO
191
      log.warning("FINISH HIM!-->" + e.toString());
×
192
    }
×
193
  }
×
194

195
  public static void issueEvent(Widget widget, String eventText) {
196
    /* TODO: Finish this.
197
     * At the moment this only works for default instance.
198
     * */
199
    //    try {
200
    //      Macros macros = widget.getEffectiveMacros();
201
    //      String expanded_eventText = MacroHandler.replace(macros, eventText);
202
    //      expanded_eventText = YamcsPVFactory.sanitizePVName("/" + expanded_eventText);
203
    //
204
    //      if (expanded_eventText == null) {
205
    //        return;
206
    //      }
207
    //
208
    //      expanded_eventText = expanded_eventText.trim();
209
    //
210
    //      String serverName = null;
211
    //      String instanceName = null;
212
    //      try {
213
    //        // TODO: Clean this up a bit
214
    //        serverName = expanded_eventText.split(":")[0];
215
    //        if (serverName.contains("/")) {
216
    //          serverName = serverName.substring(serverName.indexOf("/") + 1);
217
    //        }
218
    //        instanceName = expanded_eventText.split(":")[1].split("/")[0];
219
    //      } catch (Exception e) {
220
    //        log.warning(
221
    //            "Error on expression \""
222
    //                + expanded_eventText
223
    //                + "\". Verify that the instance and server names exist.");
224
    //        return;
225
    //      }
226

227
    // Eventually this could be overridden by scripts
228
    //      YamcsServer s = YamcsObjectManager.getServerFromName(serverName);
229
    //      if (s == null) {
230
    //        log.warning("Failed to find a server:" + serverName);
231
    //        return;
232
    //      }
233
    //          catch (Exception e) {
234
    //              // TODO
235
    //              log.warning("FINISH HIM!-->" + e.toString());
236
    //            }
237
    //          }
238

239
    YamcsServer s = YamcsObjectManager.getDefaultServer();
×
240
    if (s == null) {
×
241
      log.warning("Failed to find default server");
×
242
      return;
×
243
    }
244
    CMDR_YamcsInstance instance = YamcsObjectManager.getDefaultInstance();
×
245
    if (instance == null) {
×
246
      log.warning("Failed to find default instance");
×
247
      return;
×
248
    }
249
    instance.publishEvent(eventText, s.getYamcsClient());
×
250
  }
×
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc