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

WindhoverLabs / phoebus / #70

28 Aug 2023 12:40AM UTC coverage: 16.512% (-0.02%) from 16.527%
#70

push

lorenzo-gomez-windhover
-Add links viewer. Functional.
-TODO:Need to handle edge case when count drops.

17705 of 107226 relevant lines covered (16.51%)

0.17 hits per line

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

47.22
/core/commander-core/src/main/java/com/windhoverlabs/yamcs/core/CMDR_YamcsInstance.java
1
package com.windhoverlabs.yamcs.core;
2

3
import com.windhoverlabs.pv.yamcs.YamcsPV;
4
import com.windhoverlabs.pv.yamcs.YamcsSubscriptionService;
5
import java.time.Duration;
6
import java.time.Instant;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.concurrent.ExecutionException;
11
import java.util.function.Consumer;
12
import java.util.logging.Logger;
13
import javafx.collections.FXCollections;
14
import javafx.collections.ObservableList;
15
import org.yamcs.client.EventSubscription;
16
import org.yamcs.client.LinkSubscription;
17
import org.yamcs.client.Page;
18
import org.yamcs.client.YamcsClient;
19
import org.yamcs.client.archive.ArchiveClient;
20
import org.yamcs.client.mdb.MissionDatabaseClient.ListOptions;
21
import org.yamcs.client.processor.ProcessorClient;
22
import org.yamcs.protobuf.CreateEventRequest;
23
import org.yamcs.protobuf.GetServerInfoResponse;
24
import org.yamcs.protobuf.GetServerInfoResponse.CommandOptionInfo;
25
import org.yamcs.protobuf.Mdb.ParameterInfo;
26
import org.yamcs.protobuf.Pvalue.ParameterValue;
27
import org.yamcs.protobuf.SubscribeEventsRequest;
28
import org.yamcs.protobuf.links.LinkInfo;
29
import org.yamcs.protobuf.links.SubscribeLinksRequest;
30

31
// import org.yamcs.protobuf.Event;
32

33
public class CMDR_YamcsInstance extends YamcsObject<YamcsObject<?>> {
34
  public static final Logger logger = Logger.getLogger(CMDR_YamcsInstance.class.getPackageName());
1✔
35
  public static String OBJECT_TYPE = "instance";
1✔
36
  private ProcessorClient yamcsProcessor = null;
1✔
37
  private YamcsSubscriptionService paramSubscriptionService;
38
  private EventSubscription eventSubscription;
39
  private LinkSubscription linkSubscription;
40
  private MissionDatabase missionDatabase;
41
  //  private EventSubscription eventSubscription;
42
  private ArchiveClient yamcsArchiveClient;
43
  private CMDR_YamcsInstanceState instanceState;
44

45
  // TODO:Not sure if we want to have this on every instance and their server...just want it to work
46
  // for now.
47
  // Useful for "special" command link arguments such as cop1Bypass
48
  private HashMap<String, CommandOptionInfo> extraCommandArgs =
1✔
49
      new HashMap<String, CommandOptionInfo>();
50

51
  private ObservableList<CommandOption> optionsList = FXCollections.observableArrayList();
1✔
52
  private ObservableList<CMDR_Event> events = FXCollections.observableArrayList();
1✔
53
  private ObservableList<LinkInfo> links = FXCollections.observableArrayList();
1✔
54

55
  private HashMap<String, LinkInfo> linksMap = new HashMap<String, LinkInfo>();
1✔
56

57
  private HashMap<String, Boolean> activeInLinks = new HashMap<String, Boolean>();
1✔
58

59
  private HashMap<String, Instant> LastUpdateLinks = new HashMap<String, Instant>();
1✔
60

61
  public HashMap<String, Boolean> getActiveInLinks() {
62
    return activeInLinks;
×
63
  }
64

65
  private HashMap<String, Boolean> activeOutLinks = new HashMap<String, Boolean>();
1✔
66

67
  public ObservableList<LinkInfo> getLinks() {
68
    return links;
×
69
  }
70

71
  public CMDR_YamcsInstanceState getInstanceState() {
72
    return instanceState;
×
73
  }
74

75
  // Make this class generic?
76
  public class CommandOption {
77
    private String id;
78
    private String value;
79

80
    public CommandOption(String newId, String value) {
×
81
      this.id = newId;
×
82
      this.value = value;
×
83
    }
×
84

85
    public String getValue() {
86
      return this.value;
×
87
    }
88

89
    public String getId() {
90
      return this.id;
×
91
    }
92

93
    public void setValue(String newValue) {
94
      this.value = newValue;
×
95
    }
×
96
  }
97

98
  public ObservableList<CommandOption> getOptionsList() {
99
    return optionsList;
×
100
  }
101

102
  public HashMap<String, CommandOptionInfo> getExtraCommandArgs() {
103
    return extraCommandArgs;
×
104
  }
105

106
  public ArchiveClient getYamcsArchiveClient() {
107
    return yamcsArchiveClient;
1✔
108
  }
109

110
  public ObservableList<CMDR_Event> getEvents() {
111
    return events;
1✔
112
  }
113

114
  public ProcessorClient getYamcsProcessor() {
115
    return yamcsProcessor;
1✔
116
  }
117

118
  public CMDR_YamcsInstance(String name) {
119
    super(name);
1✔
120
  }
1✔
121

122
  @Override
123
  public ObservableList<YamcsObject<?>> getItems() {
124
    return FXCollections.emptyObservableList();
1✔
125
  }
126

127
  @Override
128
  public void createAndAddChild(String name) {
129
    throw new IllegalStateException("CMDR_YamcsInstance does not allow child items");
1✔
130
  }
131

132
  @Override
133
  public String getObjectType() {
134
    return OBJECT_TYPE;
1✔
135
  }
136

137
  protected void initProcessorClient(YamcsClient yamcsClient) {
138
    yamcsProcessor = yamcsClient.createProcessorClient(getName(), "realtime");
1✔
139
    //    yamcsClient.listProcessors(OBJECT_TYPE)
140
  }
1✔
141

142
  protected void initYamcsSubscriptionService(
143
      YamcsClient yamcsClient, String serverName, String procesor) {
144
    paramSubscriptionService =
1✔
145
        new YamcsSubscriptionService(
146
            yamcsClient.createParameterSubscription(), serverName, this.getName(), procesor);
1✔
147
  }
1✔
148

149
  protected void initLinkSubscription(YamcsClient yamcsClient, String serverName) {
150
    linkSubscription = yamcsClient.createLinkSubscription();
1✔
151
    linkSubscription.addMessageListener(
1✔
152
        linkEvent -> {
153
          switch (linkEvent.getType()) {
1✔
154
            case REGISTERED:
155
            case UPDATED:
156
              {
157
                var link = linkEvent.getLinkInfo();
1✔
158
                LinkInfo linkFromList = null;
1✔
159

160
                LastUpdateLinks.put(link.getName(), Instant.now());
1✔
161

162
                boolean linkExistsInlList = false;
1✔
163

164
                for (var l : links) {
1✔
165
                  if (l.getName().equals(link.getName())) {
×
166
                    linkFromList = l;
×
167
                    linkExistsInlList = true;
×
168
                  }
169
                }
×
170

171
                if (linkExistsInlList) {
1✔
172
                  links.remove(linkFromList);
×
173
                }
174
                links.add(linksMap.get(link.getName()));
1✔
175
              }
176

177
              break;
1✔
178
            case UNREGISTERED:
179
              //               TODO but not currently sent by Yamcs
180
          }
181
        });
1✔
182

183
    linkSubscription.sendMessage(SubscribeLinksRequest.newBuilder().setInstance(getName()).build());
1✔
184
  }
1✔
185

186
  protected void initEventSubscription(YamcsClient yamcsClient, String serverName) {
187
    eventSubscription = yamcsClient.createEventSubscription();
1✔
188
    eventSubscription.addMessageListener(
1✔
189
        event -> {
190
          events.add(
×
191
              new CMDR_Event(
192
                  event.getMessage(),
×
193
                  Instant.ofEpochSecond(
×
194
                      event.getGenerationTime().getSeconds(), event.getGenerationTime().getNanos()),
×
195
                  event.getSeverity(),
×
196
                  event.getType(),
×
197
                  Instant.ofEpochSecond(
×
198
                      event.getReceptionTime().getSeconds(), event.getReceptionTime().getNanos()),
×
199
                  event.getSource(),
×
200
                  this.getName()));
×
201
        });
×
202

203
    yamcsArchiveClient = yamcsClient.createArchiveClient(getName());
1✔
204

205
    eventSubscription.sendMessage(
1✔
206
        SubscribeEventsRequest.newBuilder().setInstance(getName()).build());
1✔
207
  }
1✔
208

209
  private MissionDatabase loadMissionDatabase(YamcsClient client) {
210
    var missionDatabase = new MissionDatabase();
1✔
211

212
    var mdbClient = client.createMissionDatabaseClient(getName());
1✔
213
    try {
214
      var page = mdbClient.listParameters(ListOptions.limit(500)).get();
1✔
215
      page.iterator().forEachRemaining(missionDatabase::addParameter);
1✔
216
      while (page.hasNextPage()) {
1✔
217
        page = page.getNextPage().get();
×
218
        page.iterator().forEachRemaining(missionDatabase::addParameter);
×
219
      }
220

221
      var commandPage = mdbClient.listCommands(ListOptions.limit(200)).get();
1✔
222
      commandPage.iterator().forEachRemaining(missionDatabase::addCommand);
1✔
223
      while (commandPage.hasNextPage()) {
1✔
224
        commandPage = commandPage.getNextPage().get();
1✔
225
        commandPage.iterator().forEachRemaining(missionDatabase::addCommand);
1✔
226
      }
227
    } catch (Exception e) {
×
228
      e.printStackTrace();
×
229
      //          throw new Exception("Failed to load mission database", e);
230
    }
1✔
231
    return missionDatabase;
1✔
232
  }
233

234
  protected void initMDBParameterRDequest(YamcsClient yamcsClient, String serverName) {
235
    var mdb = yamcsClient.createMissionDatabaseClient(getName()).listParameters();
1✔
236
    Page<ParameterInfo> paramsPage = null;
1✔
237
    try {
238
      paramsPage = mdb.get();
1✔
239
    } catch (InterruptedException | ExecutionException e) {
×
240
      // TODO Auto-generated catch block
241
      e.printStackTrace();
×
242
    }
1✔
243
    var it = paramsPage.iterator();
1✔
244
    it.forEachRemaining(
1✔
245
        p -> {
246
          System.out.println("p-->" + p.getQualifiedName());
1✔
247

248
          for (var m : p.getType().getMemberList()) {
1✔
249
            System.out.println("p member-->" + m.getName());
1✔
250
          }
1✔
251
        });
1✔
252
    while (paramsPage.hasNextPage()) {
1✔
253
      //      var it = paramsPage.iterator();
254
      try {
255
        paramsPage = paramsPage.getNextPage().get();
×
256
      } catch (InterruptedException | ExecutionException e) {
×
257
        // TODO Auto-generated catch block
258
        e.printStackTrace();
×
259
      }
×
260
      it = paramsPage.iterator();
×
261
      it.forEachRemaining(
×
262
          p -> {
263
            System.out.println("p-->" + p.getQualifiedName());
×
264

265
            for (var m : p.getType().getMemberList()) {
×
266
              System.out.println("p member-->" + m.getName());
×
267
            }
×
268
          });
×
269
    }
270
  }
1✔
271

272
  /**
273
   * Initializes all of the subscriptions to the servers such as event and parameter subscriptions.
274
   * Always call this AFTER the websocket connection to YAMCS has been established. Ideally inside
275
   * the connected() method of a org.yamcs.client.ConnectionListener. Otherwise, one might cause a
276
   * race between the time we "connect" via the websocket and the time we create these
277
   * subscriptions.
278
   *
279
   * @param yamcsClient
280
   * @param serverName
281
   */
282
  // TODO:This shoud return whether or not the instance activated successfully.
283
  public void activate(YamcsClient yamcsClient, String serverName) {
284
    initProcessorClient(yamcsClient);
1✔
285
    initYamcsSubscriptionService(yamcsClient, serverName, "realtime");
1✔
286
    initEventSubscription(yamcsClient, serverName);
1✔
287
    initLinkSubscription(yamcsClient, serverName);
1✔
288
    initMDBParameterRDequest(yamcsClient, serverName);
1✔
289
    missionDatabase = loadMissionDatabase(yamcsClient);
1✔
290

291
    try {
292
      initCommandOptions(yamcsClient);
1✔
293
    } catch (InterruptedException e) {
×
294
      // TODO Auto-generated catch block
295
      e.printStackTrace();
×
296
      return;
×
297
    } catch (ExecutionException e) {
×
298
      // TODO Auto-generated catch block
299
      e.printStackTrace();
×
300
      return;
×
301
    }
1✔
302
    instanceState = CMDR_YamcsInstanceState.ACTIVATED;
1✔
303
  }
1✔
304

305
  public MissionDatabase getMissionDatabase() {
306
    return missionDatabase;
×
307
  }
308

309
  private void initCommandOptions(YamcsClient yamcsClient)
310
      throws InterruptedException, ExecutionException {
311
    GetServerInfoResponse info = yamcsClient.getServerInfo().get();
1✔
312
    System.out.println("initCommandOptions-->1");
1✔
313
    for (CommandOptionInfo o : info.getCommandOptionsList()) {
1✔
314
      extraCommandArgs.put(o.getId(), o);
×
315

316
      // Eventually check the type and create Commandoption accordingly
317
      optionsList.add(new CommandOption(o.getId(), ""));
×
318
      System.out.println("initCommandOptions-->2" + optionsList);
×
319
    }
×
320
    System.out.println("initCommandOptions-->3");
1✔
321
  }
1✔
322

323
  public void deActivate(YamcsClient yamcsClient, String serverName) {
324
    // TODO:unInit resources...
325
    instanceState = CMDR_YamcsInstanceState.DEACTIVATED;
×
326
    if (eventSubscription != null) {
×
327
      eventSubscription.cancel(true);
×
328
      paramSubscriptionService.destroy();
×
329
    }
330
  }
×
331

332
  public EventSubscription getEventSubscription() {
333
    return eventSubscription;
1✔
334
  }
335

336
  public void subscribePV(YamcsPV pv) {
337
    // TODO:Have to let the caller know whether were able to successfully subscribe
338
    // to this pv or not.
339
    paramSubscriptionService.register(pv);
×
340
  }
×
341

342
  /** Creates and publishes an event to YAMCS instance. */
343
  public void publishEvent(String message, YamcsClient yamcsClient) {
344
    yamcsClient.createEvent(
×
345
        CreateEventRequest.newBuilder()
×
346
            .setInstance(getName())
×
347
            .setMessage(message)
×
348
            .setSource("Commander")
×
349
            .build());
×
350
  }
×
351

352
  public ArrayList<String> getProcessors(YamcsClient yamcsClient) {
353

354
    ArrayList<String> processors = new ArrayList<String>();
×
355
    try {
356
      yamcsClient
×
357
          .listProcessors(getName())
×
358
          .get()
×
359
          .forEach(
×
360
              p -> {
361
                processors.add(p.getName());
×
362
              });
×
363
    } catch (InterruptedException | ExecutionException e) {
×
364
      // TODO Auto-generated catch block
365
      e.printStackTrace();
×
366
    }
×
367

368
    return processors;
×
369
  }
370

371
  public void switchProcessor(YamcsClient yamcsClient, String serverName, String processorName) {
372
    //          This seems redundant....
373
    paramSubscriptionService.destroy();
×
374
    initYamcsSubscriptionService(yamcsClient, serverName, processorName);
×
375
  }
×
376

377
  public void getParameters(
378
      YamcsClient yamcsClient,
379
      List<String> parameters,
380
      Instant start,
381
      Instant end,
382
      Consumer<ArrayList<Page<ParameterValue>>> consumer) {
383

384
    //    this.getYamcsArchiveClient().streamValues(parameters, consumer, start, end);
385
    ArrayList<Page<ParameterValue>> pages = new ArrayList<Page<ParameterValue>>();
×
386
    for (var p : parameters) {
×
387
      try {
388
        pages.add(this.getYamcsArchiveClient().listValues(p, start, end).get());
×
389
      } catch (InterruptedException | ExecutionException e) {
×
390
        // TODO Auto-generated catch block
391
        e.printStackTrace();
×
392
      }
×
393
    }
×
394

395
    consumer.accept(pages);
×
396
  }
×
397

398
  public boolean isLinkActive(String linkName) {
399
    return Duration.between(Instant.now(), LastUpdateLinks.get(linkName)).toMillis() < 1000;
×
400
  }
401
}
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