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

WindhoverLabs / yamcs-cfs / #162

28 Nov 2024 05:03PM UTC coverage: 0.0%. Remained the same
#162

push

lorenzo-gomez-windhover
-Updates for yamcs 5.9.8

0 of 4 new or added lines in 1 file covered. (0.0%)

614 existing lines in 11 files now uncovered.

0 of 6789 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/main/java/com/windhoverlabs/yamcs/tctm/SerialStreamInOutProvider.java
1
package com.windhoverlabs.yamcs.tctm;
2

3
import com.fazecast.jSerialComm.SerialPort;
4
import com.google.common.util.concurrent.RateLimiter;
5
import java.io.IOException;
6
import java.io.OutputStream;
7
import java.nio.ByteBuffer;
8
import java.nio.channels.Channels;
9
import java.nio.channels.WritableByteChannel;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.List;
13
import java.util.concurrent.TimeUnit;
14
import java.util.concurrent.atomic.AtomicBoolean;
15
import java.util.concurrent.atomic.AtomicLong;
16
import org.yamcs.AbstractYamcsService;
17
import org.yamcs.ConfigurationException;
18
import org.yamcs.InitException;
19
import org.yamcs.Spec;
20
import org.yamcs.TmPacket;
21
import org.yamcs.YConfiguration;
22
import org.yamcs.YamcsServer;
23
import org.yamcs.events.EventProducer;
24
import org.yamcs.events.EventProducerFactory;
25
import org.yamcs.logging.Log;
26
import org.yamcs.parameter.ParameterValue;
27
import org.yamcs.parameter.SystemParametersProducer;
28
import org.yamcs.parameter.SystemParametersService;
29
import org.yamcs.protobuf.Yamcs.Value.Type;
30
import org.yamcs.tctm.Link;
31
import org.yamcs.tctm.PacketInputStream;
32
import org.yamcs.tctm.PacketTooLongException;
33
import org.yamcs.time.SimulationTimeService;
34
import org.yamcs.time.TimeService;
35
import org.yamcs.utils.DataRateMeter;
36
import org.yamcs.utils.YObjectLoader;
37
import org.yamcs.xtce.Parameter;
38
import org.yamcs.yarch.ColumnDefinition;
39
import org.yamcs.yarch.DataType;
40
import org.yamcs.yarch.Stream;
41
import org.yamcs.yarch.StreamSubscriber;
42
import org.yamcs.yarch.Tuple;
43
import org.yamcs.yarch.TupleDefinition;
44
import org.yamcs.yarch.YarchDatabase;
45
import org.yamcs.yarch.YarchDatabaseInstance;
46

47
/**
48
 * Receives telemetry fames via UDP. One UDP datagram = one TM frame.
49
 *
50
 * @author nm
51
 */
52
public class SerialStreamInOutProvider extends AbstractYamcsService
×
53
    implements Link, StreamSubscriber, SystemParametersProducer, Runnable {
54
  protected String deviceName;
55
  protected int baudRate;
56
  protected int dataBits;
57
  protected String stopBits;
58
  protected String parity;
59
  protected String flowControl;
60
  protected long initialDelay;
61
  String packetInputStreamClassName;
62
  YConfiguration packetInputStreamArgs;
63
  PacketInputStream packetInputStream;
64
  SerialPort serialPort = null;
×
65
  OutputStream outputStream = null;
×
66

67
  Thread thread;
68
  RateLimiter outRateLimiter;
69
  protected YConfiguration config;
70
  protected String linkName;
71
  protected AtomicBoolean disabled = new AtomicBoolean(false);
×
72
  protected Log log;
73
  protected EventProducer eventProducer;
74
  protected TimeService timeService;
75
  protected AtomicLong inPacketCount = new AtomicLong(0);
×
76
  protected AtomicLong outPacketCount = new AtomicLong(0);
×
77
  protected AtomicLong inDataCount = new AtomicLong(0);
×
78
  protected AtomicLong outDataCount = new AtomicLong(0);
×
79
  protected boolean updateSimulationTime;
80
  DataRateMeter inPacketRateMeter = new DataRateMeter();
×
81
  DataRateMeter outPacketRateMeter = new DataRateMeter();
×
82
  DataRateMeter inDataRateMeter = new DataRateMeter();
×
83
  DataRateMeter outDataRateMeter = new DataRateMeter();
×
84
  protected PacketPreprocessor packetPreprocessor;
85
  protected boolean hasPreprocessor = false;
×
86
  protected Stream inStream;
87
  protected Stream outStream;
88

89
  private Parameter spLinkStatus;
90
  private Parameter spInPacketCount;
91
  private Parameter spOutPacketCount;
92
  private Parameter spInPacketRate;
93
  private Parameter spOutPacketRate;
94
  private Parameter spInDataRate;
95
  private Parameter spOutDataRate;
96
  private Parameter spInDataCount;
97
  private Parameter spOutDataCount;
98

99
  static TupleDefinition gftdef;
100

101
  static final String RECTIME_CNAME = "rectime";
102
  static final String DATA_CNAME = "data";
103

104
  static {
105
    gftdef = new TupleDefinition();
×
106
    gftdef.addColumn(new ColumnDefinition(RECTIME_CNAME, DataType.TIMESTAMP));
×
107
    gftdef.addColumn(new ColumnDefinition(DATA_CNAME, DataType.BINARY));
×
108
  }
×
109

110
  static final String CFG_PREPRO_CLASS = "packetPreprocessorClassName";
111

112
  static final int MAX_LENGTH = 1500;
113
  private int leftTrim;
114
  private int rightTrim;
115
  int maxLength;
116

117
  String packetPreprocessorClassName;
118
  Object packetPreprocessorArgs;
119

120
  /**
121
   * Creates a new UDP Frame Data Link
122
   *
123
   * @throws ConfigurationException if port is not defined in the configuration
124
   */
125
  public void init(String instance, String name, YConfiguration config) {
126
    log = new Log(getClass(), instance);
×
127
    log.setContext(name);
×
128
    log.info("Initializing");
×
129

130
    try {
131
      super.init(instance, name, config);
×
132
    } catch (InitException e1) {
×
133
      // TODO Auto-generated catch block
134
      e1.printStackTrace();
×
135
    }
×
136

137
    this.deviceName = config.getString("device", "/dev/ttyUSB0");
×
138
    this.baudRate = config.getInt("baudRate", 57600);
×
139
    this.initialDelay = config.getLong("initialDelay", -1);
×
140
    this.dataBits = config.getInt("dataBits", 8);
×
141
    this.stopBits = config.getString("stopBits", "1");
×
142
    this.parity = config.getString("parity", "NONE");
×
143
    this.flowControl = config.getString("flowControl", "NONE");
×
144

145
    if (!("NONE".equalsIgnoreCase(this.parity)
×
146
        || "EVEN".equalsIgnoreCase(this.parity)
×
147
        || "ODD".equalsIgnoreCase(this.parity)
×
148
        || "MARK".equalsIgnoreCase(this.parity)
×
149
        || "SPACE".equalsIgnoreCase(this.parity))) {
×
150
      throw new ConfigurationException("Invalid Parity (NONE, EVEN, ODD, MARK or SPACE)");
×
151
    }
152

153
    if (!("NONE".equalsIgnoreCase(this.flowControl)
×
154
        || "RTS_CTS".equalsIgnoreCase(this.flowControl)
×
155
        || "XON_XOFF".equalsIgnoreCase(this.flowControl))) {
×
156
      throw new ConfigurationException("Invalid Flow Control (NONE, RTS_CTS, or XON_XOFF)");
×
157
    }
158

159
    if (!(this.dataBits == 5 || this.dataBits == 6 || this.dataBits == 7 || this.dataBits == 8)) {
×
160
      throw new ConfigurationException("Invalid Data Bits (5, 6, 7, or 8)");
×
161
    }
162

163
    if (!("1".equalsIgnoreCase(this.stopBits)
×
164
        || "1.5".equalsIgnoreCase(this.stopBits)
×
165
        || "2".equalsIgnoreCase(this.stopBits))) {
×
166
      throw new ConfigurationException("Invalid Stop Bits (1, 1.5, or 2)");
×
167
    }
168

169
    if (config.containsKey("packetInputStreamClassName")) {
×
170
      this.packetInputStreamClassName = config.getString("packetInputStreamClassName");
×
171
      this.packetInputStreamArgs = config.getConfig("packetInputStreamArgs");
×
172
    } else {
173
      this.packetInputStreamClassName = CcsdsPacketInputStream.class.getName();
×
174
      this.packetInputStreamArgs = YConfiguration.emptyConfig();
×
175
    }
176

177
    this.linkName = name;
×
178
    this.config = config;
×
179
    leftTrim = config.getInt("leftTrim", 0);
×
180
    rightTrim = config.getInt("rightTrim", 0);
×
181
    maxLength = config.getInt("maxFrameLength", MAX_LENGTH);
×
182
    eventProducer = EventProducerFactory.getEventProducer(instance, name, 10000);
×
183
    this.timeService = YamcsServer.getTimeService(instance);
×
184
    String inStreamName = config.getString("in_stream");
×
185
    String outStreamName = config.getString("out_stream");
×
186

187
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(instance);
×
188
    this.inStream = getStream(ydb, inStreamName);
×
189
    this.outStream = getStream(ydb, outStreamName);
×
190

191
    this.inStream.addSubscriber(this);
×
192

193
    if (config.containsKey("frameMaxRate")) {
×
194
      outRateLimiter = RateLimiter.create(config.getDouble("frameMaxRate"), 1, TimeUnit.SECONDS);
×
195
    }
196

197
    if (config.containsKey(CFG_PREPRO_CLASS)) {
×
198
      this.hasPreprocessor = true;
×
199

200
      this.packetPreprocessorClassName = config.getString(CFG_PREPRO_CLASS);
×
201

202
      if (config.containsKey("packetPreprocessorArgs")) {
×
203
        this.packetPreprocessorArgs = config.getConfig("packetPreprocessorArgs");
×
204
      }
205

206
      try {
207
        if (packetPreprocessorArgs != null) {
×
208
          packetPreprocessor =
×
209
              YObjectLoader.loadObject(
×
210
                  packetPreprocessorClassName, instance, packetPreprocessorArgs);
211
        } else {
212
          packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClassName, instance);
×
213
        }
214
      } catch (ConfigurationException e) {
×
215
        log.error("Cannot instantiate the packet preprocessor", e);
×
216
        throw e;
×
217
      }
×
218
    }
219

220
    updateSimulationTime = config.getBoolean("updateSimulationTime", false);
×
221
    if (updateSimulationTime) {
×
222
      if (timeService instanceof SimulationTimeService) {
×
223
        SimulationTimeService sts = (SimulationTimeService) timeService;
×
224
        sts.setTime0(0);
×
225
      } else {
×
226
        throw new ConfigurationException(
×
227
            "updateSimulationTime can only be used together with SimulationTimeService "
228
                + "(add 'timeService: org.yamcs.time.SimulationTimeService' in yamcs.<instance>.yaml)");
229
      }
230
    }
231

232
    SystemParametersService collector = SystemParametersService.getInstance(yamcsInstance);
×
233
    setupSystemParameters(collector);
×
234
    collector.registerProducer((SystemParametersProducer) this);
×
235
  }
×
236

237
  private static Stream getStream(YarchDatabaseInstance ydb, String streamName) {
238
    Stream stream = ydb.getStream(streamName);
×
239
    if (stream == null) {
×
240
      try {
241
        ydb.execute("create stream " + streamName + gftdef.getStringDefinition());
×
242
        // ydb.execute("create stream " + streamName);
243
      } catch (Exception e) {
×
244
        throw new ConfigurationException(e);
×
245
      }
×
246
      stream = ydb.getStream(streamName);
×
247
    }
248
    return stream;
×
249
  }
250

251
  @Override
252
  public YConfiguration getConfig() {
253
    return config;
×
254
  }
255

256
  @Override
257
  public String getName() {
258
    return linkName;
×
259
  }
260

261
  @Override
262
  public void resetCounters() {
263
    inPacketCount.set(0);
×
264
    outPacketCount.set(0);
×
265
  }
×
266

267
  @Override
268
  public void doStart() {
269
    if (!isDisabled()) {
×
270
      new Thread(this).start();
×
271
    }
272
    notifyStarted();
×
273
  }
×
274

275
  @Override
276
  public void doStop() {
277
    notifyStopped();
×
278
  }
×
279

280
  public boolean isRunningAndEnabled() {
281
    State state = state();
×
282
    return (state == State.RUNNING || state == State.STARTING) && !disabled.get();
×
283
  }
284

285
  /**
286
   * Sends the packet downstream for processing.
287
   *
288
   * <p>Starting in Yamcs 5.2, if the updateSimulationTime option is set on the link configuration,
289
   *
290
   * <ul>
291
   *   <li>the timeService is expected to be SimulationTimeService
292
   *   <li>at initialization, the time0 is set to 0
293
   *   <li>upon each packet received, the generationTime (as set by the pre-processor) is used to
294
   *       update the simulation elapsed time
295
   * </ul>
296
   *
297
   * <p>Should be called by all sub-classes (instead of directly calling {@link
298
   * TmSink#processPacket(TmPacket)}
299
   *
300
   * @param tmpkt
301
   */
302
  protected void processPacket(TmPacket tmpkt) {
303
    long rectime = tmpkt.getReceptionTime();
×
304
    byte byteArray[] = tmpkt.getPacket();
×
305

306
    byte[] trimmedByteArray =
×
307
        Arrays.copyOfRange(byteArray, this.leftTrim, byteArray.length - this.rightTrim);
×
308

309
    outStream.emitTuple(new Tuple(gftdef, Arrays.asList(rectime, trimmedByteArray)));
×
310

311
    if (updateSimulationTime) {
×
312
      SimulationTimeService sts = (SimulationTimeService) timeService;
×
313
      if (!tmpkt.isInvalid()) {
×
314
        sts.setSimElapsedTime(tmpkt.getGenerationTime());
×
315
      }
316
    }
317
  }
×
318

319
  @Override
320
  public void run() {
321
    log.info("Running");
×
322
    if (initialDelay > 0) {
×
323
      try {
324
        Thread.sleep(initialDelay);
×
325
        initialDelay = -1;
×
326
      } catch (InterruptedException e) {
×
327
        Thread.currentThread().interrupt();
×
328
        return;
×
329
      }
×
330
    }
331

332
    while (isRunningAndEnabled()) {
×
333
      TmPacket tmpkt = getNextPacket();
×
334
      if (tmpkt == null) {
×
335
        break;
×
336
      }
337
      processPacket(tmpkt);
×
338
    }
×
339
  }
×
340

341
  /**
342
   * called when a new packet is received to update the statistics
343
   *
344
   * @param packetSize
345
   */
346
  protected void updateInStats(int packetSize) {
347
    inPacketCount.getAndIncrement();
×
348
    inPacketRateMeter.mark(1);
×
349
    inDataRateMeter.mark(packetSize);
×
350
    inDataCount.getAndAdd(packetSize);
×
351
  }
×
352

353
  /**
354
   * called when a new packet is sent to update the statistics
355
   *
356
   * @param packetSize
357
   */
358
  protected void updateOutStats(int packetSize) {
359
    outPacketCount.getAndIncrement();
×
360
    outPacketRateMeter.mark(1);
×
361
    outDataRateMeter.mark(packetSize);
×
362
    outDataCount.getAndAdd(packetSize);
×
363
  }
×
364

365
  /**
366
   * Called to retrieve the next packet. It blocks in reading on the multicast socket
367
   *
368
   * @return anything that looks as a valid packet, just the size is taken into account to decide if
369
   *     it's valid or not
370
   */
371
  public TmPacket getNextPacket() {
372
    TmPacket pkt = null;
×
373

374
    while (isRunningAndEnabled()) {
×
375
      try {
376
        openDevice();
×
377
        byte[] packet = packetInputStream.readPacket();
×
378

379
        // log.info("Got packet");
380
        // for(int i = 0; i < packet.length; ++i) {
381
        //  System.console().printf("%02x ", packet[i]);
382
        // }
383
        // System.console().printf("\n");
384
        updateInStats(packet.length);
×
385
        pkt = new TmPacket(timeService.getMissionTime(), packet);
×
386
        pkt.setEarthRceptionTime(timeService.getHresMissionTime());
×
387
        break;
×
388
      } catch (IOException e) {
×
389
        if (isRunningAndEnabled()) {
×
390
          log.info(
×
391
              "Cannot open or read serial device {}::{}:{}'. Retrying in 10s",
392
              deviceName,
393
              e.getMessage(),
×
394
              e.toString());
×
395
          try {
396
            Thread.sleep(10000);
×
397
          } catch (InterruptedException e1) {
×
398
            // TODO Auto-generated catch block
399
            e1.printStackTrace();
×
400
          }
×
401
        }
402
        try {
403
          serialPort.closePort();
×
404
        } catch (Exception e2) {
×
405
        }
×
406
        serialPort = null;
×
407
        for (int ii = 0; ii < 10; ii++) {
×
408
          if (!isRunningAndEnabled()) {
×
409
            break;
×
410
          }
411
          try {
412
            Thread.sleep(10);
×
413
          } catch (InterruptedException e1) {
×
414
            Thread.currentThread().interrupt();
×
415
            return null;
×
416
          }
×
417
        }
418
      } catch (PacketTooLongException e) {
×
419
        log.warn(e.toString());
×
420
        try {
421
          serialPort.closePort();
×
422
        } catch (Exception e2) {
×
423
        }
×
424
        serialPort = null;
×
425
      }
×
426
    }
427
    return pkt;
×
428
  }
429

430
  protected void openDevice() throws IOException {
431
    if (this.serialPort == null) {
×
432
      log.info("Opening device {}", deviceName);
×
433
      this.serialPort = SerialPort.getCommPort(this.deviceName);
×
434
      this.serialPort.openPort();
×
435
      this.serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
×
436

437
      switch (this.flowControl) {
×
438
        case "NONE":
439
          log.info("Set Flow Control to NONE");
×
440
          this.serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
×
441
          break;
×
442

443
        case "RTS_CTS":
444
          log.info("Set Flow Control to RTS_CTS");
×
445
          this.serialPort.setFlowControl(
×
446
              SerialPort.FLOW_CONTROL_CTS_ENABLED | SerialPort.FLOW_CONTROL_RTS_ENABLED);
447
          break;
×
448

449
        case "XON_XOFF":
450
          log.info("Set Flow Control to XON_XOFF");
×
451
          this.serialPort.setFlowControl(SerialPort.FLOW_CONTROL_XONXOFF_IN_ENABLED);
×
452
          break;
453
      }
454

455
      switch (this.parity) {
×
456
        case "NONE":
457
          log.info("Set Parity to NONE");
×
458
          this.serialPort.setParity(SerialPort.NO_PARITY);
×
459
          break;
×
460

461
        case "ODD":
462
          log.info("Set Parity to ODD_PARITY");
×
463
          this.serialPort.setParity(SerialPort.ODD_PARITY);
×
464
          break;
×
465

466
        case "EVEN":
467
          log.info("Set Parity to EVEN_PARITY");
×
468
          this.serialPort.setParity(SerialPort.EVEN_PARITY);
×
469
          break;
×
470

471
        case "MARK":
472
          log.info("Set Parity to MARK_PARITY");
×
473
          this.serialPort.setParity(SerialPort.MARK_PARITY);
×
474
          break;
×
475

476
        case "SPACE":
477
          log.info("Set Parity to SPACE_PARITY");
×
478
          this.serialPort.setParity(SerialPort.SPACE_PARITY);
×
479
          break;
480
      }
481

482
      if (this.serialPort.setNumDataBits(this.dataBits) == false) {
×
483
        throw new IOException("Invalid dataBits");
×
484
      }
485
      ;
486

487
      switch (this.stopBits) {
×
488
        case "1":
489
          log.info("Set Stop Bits to 1");
×
490
          if (this.serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT) == false) {
×
491
            throw new IOException("Invalid stopBits");
×
492
          }
493
          ;
494
          break;
495

496
        case "1.5":
497
          log.info("Set Stop Bits to 1.5");
×
498
          if (this.serialPort.setNumStopBits(SerialPort.ONE_POINT_FIVE_STOP_BITS) == false) {
×
499
            throw new IOException("Invalid stopBits");
×
500
          }
501
          ;
502
          break;
503

504
        case "2":
505
          log.info("Set Stop Bits to 2");
×
506
          if (this.serialPort.setNumStopBits(SerialPort.TWO_STOP_BITS) == false) {
×
507
            throw new IOException("Invalid stopBits");
×
508
          }
509
          ;
510
          break;
511

512
        default:
513
          log.error("Invalid stopBits");
×
514
          throw new IOException("Invalid stopBits");
×
515
      }
516

517
      if (this.serialPort.setBaudRate(this.baudRate) == false) {
×
518
        throw new IOException("Invalid baudRate");
×
519
      }
520
      ;
521

522
      log.info("Opened device {}", deviceName);
×
523
    }
524

525
    if (packetInputStream == null) {
×
526
      try {
527
        log.info("Loading PacketInputStream {}", packetInputStreamClassName);
×
528
        packetInputStream = YObjectLoader.loadObject(packetInputStreamClassName);
×
529
        log.info("PacketInputStream {} loaded", packetInputStreamClassName);
×
530
      } catch (ConfigurationException e) {
×
531
        log.error("Cannot instantiate the packetInput stream", e);
×
532
        throw e;
×
533
      }
×
534

535
      log.info("Initializing {}", packetInputStreamClassName);
×
536
      packetInputStream.init(this.serialPort.getInputStream(), packetInputStreamArgs);
×
537
      log.info("{} initialized", packetInputStreamClassName);
×
538
    }
539

540
    this.outputStream = this.serialPort.getOutputStream();
×
541
  }
×
542

543
  @Override
544
  public void disable() {
545
    boolean b = disabled.getAndSet(true);
×
546
    if (!b) {
×
547
      try {
548
        /* TODO */
549
        // doDisable();
550
      } catch (Exception e) {
551
        disabled.set(false);
552
        log.warn("Failed to disable link", e);
553
      }
554
    }
555
  }
×
556

557
  @Override
558
  public void enable() {
559
    boolean b = disabled.getAndSet(false);
×
560
    if (b) {
×
561
      try {
562
        /* TODO */
563
        // doEnable();
564
      } catch (Exception e) {
565
        disabled.set(true);
566
        log.warn("Failed to enable link", e);
567
      }
568
    }
569
  }
×
570

571
  @Override
572
  public long getDataInCount() {
573
    return inPacketCount.get();
×
574
  }
575

576
  @Override
577
  public long getDataOutCount() {
578
    return outPacketCount.get();
×
579
  }
580

581
  @Override
582
  public Status getLinkStatus() {
583
    if (isDisabled()) {
×
584
      return Status.DISABLED;
×
585
    }
586
    if (state() == State.FAILED) {
×
587
      return Status.FAILED;
×
588
    }
589

590
    return connectionStatus();
×
591
  }
592

593
  @Override
594
  public boolean isDisabled() {
595
    return disabled.get();
×
596
  }
597

598
  protected Status connectionStatus() {
599
    return Status.OK;
×
600
  }
601

602
  @Override
603
  public Spec getSpec() {
604
    // TODO Auto-generated method stub
605
    return super.getSpec();
×
606
  }
607

608
  @Override
609
  public void onTuple(Stream arg0, Tuple tuple) {
610
    if (isRunningAndEnabled()) {
×
611
      byte[] pktData = tuple.getColumn(DATA_CNAME);
×
612
      long recTime = tuple.getColumn(RECTIME_CNAME);
×
613
      if (pktData == null) {
×
614
        throw new ConfigurationException("no column named '%s' in the tuple", DATA_CNAME);
×
615
      } else {
616
        int retries = 5;
×
617
        boolean sent = false;
×
618

619
        ByteBuffer bb = ByteBuffer.wrap(pktData);
×
620
        bb.rewind();
×
621
        String reason = null;
×
622
        while (!sent && (retries > 0)) {
×
623
          try {
624
            if (serialPort == null) {
×
625
              openDevice();
×
626
            }
627
            WritableByteChannel channel = Channels.newChannel(outputStream);
×
628
            channel.write(bb);
×
629
            updateOutStats(pktData.length);
×
630
            sent = true;
×
631
          } catch (IOException e) {
×
632
            reason =
×
633
                String.format("Error writing to TC device to %s : %s", deviceName, e.getMessage());
×
634
            log.warn(reason);
×
635
            //        try {
636
            //          if (serialPort != null) {
637
            ////            serialPort.close();
638
            //          }
639
            ////          serialPort = null;
640
            //        } catch (IOException e1) {
641
            //          e1.printStackTrace();
642
            //        }
643
          }
×
644
          retries--;
×
645
          if (!sent && (retries > 0)) {
×
646
            try {
647
              log.warn("Command not sent, retrying in 2 seconds");
×
648
              Thread.sleep(2000);
×
649
            } catch (InterruptedException e) {
×
650
              log.warn("exception {} thrown when sleeping 2 sec", e.toString());
×
651
              Thread.currentThread().interrupt();
×
652
            }
×
653
          }
654
        }
655
      }
656
    }
657
  }
×
658

659
  @Override
660
  public void setupSystemParameters(SystemParametersService sysParamCollector) {
661
    spLinkStatus =
×
662
        sysParamCollector.createEnumeratedSystemParameter(
×
663
            linkName + "/status", Status.class, "The current status of this interface");
664

665
    spInPacketCount =
×
666
        sysParamCollector.createSystemParameter(
×
667
            linkName + "/packetInCount",
668
            Type.UINT64,
669
            "The total number of packets that have been received via this interface");
670

671
    spOutPacketCount =
×
672
        sysParamCollector.createSystemParameter(
×
673
            linkName + "/packetOutCount",
674
            Type.UINT64,
675
            "The total number of packets that have been transmitted via this interface");
676

677
    spInPacketRate =
×
678
        sysParamCollector.createSystemParameter(
×
679
            linkName + "/packetInRate",
680
            Type.UINT64,
681
            "The rate packets that have been received via this interface");
682

683
    spOutPacketRate =
×
684
        sysParamCollector.createSystemParameter(
×
685
            linkName + "/packetOutRate",
686
            Type.UINT64,
687
            "The rate packets that have been transmitted via this interface");
688

689
    spInDataCount =
×
690
        sysParamCollector.createSystemParameter(
×
691
            linkName + "/dataInCount",
692
            Type.UINT64,
693
            "The total number of bytes that have been received via this interface");
694

695
    spOutDataCount =
×
696
        sysParamCollector.createSystemParameter(
×
697
            linkName + "/dataOutCount",
698
            Type.UINT64,
699
            "The total number of bytes that have been transmitted via this interface");
700

701
    spInDataRate =
×
702
        sysParamCollector.createSystemParameter(
×
703
            linkName + "/dataInRate",
704
            Type.UINT64,
705
            "The bit rate of data that has been received via this interface");
706

707
    spOutDataRate =
×
708
        sysParamCollector.createSystemParameter(
×
709
            linkName + "/dataOutRate",
710
            Type.UINT64,
711
            "The bit rate of data that has been transmitted via this interface");
712
  }
×
713

714
  @Override
715
  public List<ParameterValue> getSystemParameters(long gentime) {
716
    ArrayList<ParameterValue> list = new ArrayList<>();
×
717
    try {
718
      collectSystemParameters(gentime, list);
×
UNCOV
719
    } catch (Exception e) {
×
720
      log.error("Exception caught when collecting link system parameters", e);
×
721
    }
×
722
    return list;
×
723
  }
724

725
  /**
726
   * adds system parameters link status and data in/out to the list.
727
   *
728
   * <p>The inheriting classes should call super.collectSystemParameters and then add their own
729
   * parameters to the list
730
   *
731
   * @param time
732
   * @param list
733
   */
734
  protected void collectSystemParameters(long time, List<ParameterValue> list) {
UNCOV
735
    list.add(SystemParametersService.getPV(spLinkStatus, time, getLinkStatus()));
×
UNCOV
736
    list.add(SystemParametersService.getPV(spInPacketCount, time, inPacketCount.get()));
×
737
    list.add(SystemParametersService.getPV(spOutPacketCount, time, outPacketCount.get()));
×
738
    list.add(
×
739
        SystemParametersService.getPV(
×
740
            spInPacketRate, time, inPacketRateMeter.getFiveSecondsRate()));
×
741
    list.add(
×
742
        SystemParametersService.getPV(
×
743
            spOutPacketRate, time, outPacketRateMeter.getFiveSecondsRate()));
×
744
    list.add(
×
745
        SystemParametersService.getPV(spInDataRate, time, inDataRateMeter.getFiveSecondsRate()));
×
746
    list.add(
×
747
        SystemParametersService.getPV(spOutDataRate, time, outDataRateMeter.getFiveSecondsRate()));
×
748
    list.add(SystemParametersService.getPV(spInDataCount, time, inDataCount.get()));
×
749
    list.add(SystemParametersService.getPV(spOutDataCount, time, outDataCount.get()));
×
750
  }
×
751
}
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