Exemple #1
0
  @Override
  public void accept(AisPacket packet) {
    if (out == null) {
      // Open new output stream
      out = getNextOutputStram();
    }

    end = System.currentTimeMillis();
    if (start == 0) {
      start = end;
    }

    for (IPacketFilter filter : filters) {
      if (filter.rejectedByFilter(packet)) {
        return;
      }
    }

    Integer baseMMSI = -1;
    String country = "";
    String region = "";

    Vdm vdm = packet.getVdm();
    if (vdm == null) {
      return;
    }

    // Get source tag properties
    IProprietarySourceTag sourceTag = vdm.getSourceTag();
    if (sourceTag != null) {
      baseMMSI = sourceTag.getBaseMmsi();
      if (sourceTag.getCountry() != null) {
        country = sourceTag.getCountry().getTwoLetter();
      }
      if (sourceTag.getRegion() != null) {
        region = sourceTag.getRegion();
      }
    }
    if (region.equals("")) {
      region = "0";
    }

    // Maybe check for start date
    Date timestamp = vdm.getTimestamp();
    if (filter.getStartDate() != null && timestamp != null) {
      if (timestamp.before(filter.getStartDate())) {
        return;
      }
    }

    // Maybe check for end date
    if (filter.getEndDate() != null && timestamp != null) {
      if (timestamp.after(filter.getEndDate())) {
        System.exit(0);
      }
    }

    // Maybe check for base station MMSI
    if (filter.getBaseStations().size() > 0) {
      if (!filter.getBaseStations().contains(baseMMSI)) {
        return;
      }
    }

    // Maybe check for country
    if (filter.getCountries().size() > 0) {
      if (!filter.getCountries().contains(country)) {
        return;
      }
    }

    // Maybe check for region
    if (filter.getRegions().size() > 0) {
      if (!filter.getRegions().contains(region)) {
        return;
      }
    }

    if (stop) {
      return;
    }

    // Count message
    msgCount++;

    // Print tag line packet
    out.print(packet.getStringMessage() + "\r\n");

    // Count bytes
    bytes += packet.getStringMessage().length() + 2;
    currentFileBytes += packet.getStringMessage().length() + 2;

    // Maybe print parsed
    if (dumpParsed) {
      if (timestamp != null) {
        out.println("+ timetamp " + timestampFormat.format(timestamp));
      }
      if (vdm.getTags() != null) {
        for (IProprietaryTag tag : vdm.getTags()) {
          out.println("+ " + tag.toString());
        }
      }
      AisMessage aisMessage = packet.tryGetAisMessage();
      if (aisMessage != null) {
        out.println("+ " + aisMessage.toString());
      } else {
        out.println("+ AIS message could not be parsed");
      }

      // Check for binary message
      if (aisMessage instanceof AisBinaryMessage) {
        AisBinaryMessage binaryMessage = (AisBinaryMessage) aisMessage;
        try {
          AisApplicationMessage appMessage = binaryMessage.getApplicationMessage();
          out.println(appMessage);
        } catch (SixbitException e) {
        }
      }
      out.println("---------------------------");
    }

    // Maybe time for new outfile
    if (splitSize != null && currentFileBytes >= splitSize) {
      out.close();
      out = null;
      currentFileBytes = 0;
    }
  }
  /**
   * Test encoding of binary route information message
   *
   * @throws SentenceException
   * @throws AisMessageException
   * @throws BitExhaustionException
   */
  @Test
  public void addressedRouteEncode()
      throws SentenceException, SixbitException, AisMessageException {
    int userId = 992199007;
    int destination = 219015063;

    // Make ASM
    AddressedRouteInformation route = new AddressedRouteInformation();
    route.setSenderClassification(1);
    route.setRouteType(RouteInformation.RouteType.RECOMMENDED.ordinal());
    route.setStartMonth(1);
    route.setStartDay(13);
    route.setStartHour(16);
    route.setStartMin(50);
    route.setDuration(35);
    route.setMsgLinkId(10);
    // Add waypoints
    List<AisPosition> waypoints = new ArrayList<>();
    waypoints.add(new AisPosition(Position.create(55.845283333333334, 12.704933333333333)));
    waypoints.add(new AisPosition(Position.create(55.913383333333336, 12.6453)));
    waypoints.add(new AisPosition(Position.create(55.93476666666667, 12.644016666666667)));
    waypoints.add(new AisPosition(Position.create(55.97728333333333, 12.7015)));
    waypoints.add(new AisPosition(Position.create(56.00, 12.8)));
    waypoints.add(new AisPosition(Position.create(56.10, 12.9)));
    for (AisPosition aisPosition : waypoints) {
      route.addWaypoint(aisPosition);
    }

    // Make addressed binary AIS message
    AisMessage6 msg6 = new AisMessage6();
    msg6.setUserId(userId);
    msg6.setDestination(destination);
    msg6.setRetransmit(0);
    msg6.setAppMessage(route);

    System.out.println("Route message 6: " + msg6);

    // Make ABM sentences
    Abm abm = new Abm();
    abm.setTalker("AI");
    abm.setTotal(1);
    abm.setNum(1);
    abm.setDestination(destination);
    abm.setBinaryData(msg6);
    abm.setSequence(0);
    String encoded = abm.getEncoded();
    System.out.println("Route ABM encoded: " + encoded);

    // Make VDM sentences
    String[] vdms = Vdm.createSentences(msg6, 0);
    System.out.println("Route VDM encoded:\n" + StringUtils.join(vdms, "\r\n"));

    // Decode VDM sentences
    Vdm vdm = new Vdm();
    for (int i = 0; i < vdms.length; i++) {
      int result = vdm.parse(new SentenceLine(vdms[i]));
      if (i < vdms.length - 1) {
        Assert.assertEquals(result, 1);
      } else {
        Assert.assertEquals(result, 0);
      }
    }
    // Extract AisMessage6
    msg6 = (AisMessage6) AisMessage.getInstance(vdm);
    System.out.println("msg6 decoded: " + msg6);
    // Get the ASM
    AisApplicationMessage appMsg = msg6.getApplicationMessage();
    AddressedRouteInformation parsedRoute = (AddressedRouteInformation) appMsg;
    System.out.println("sentenceStr 6 application: " + appMsg);

    // Assert if mathes original
    Assert.assertEquals(parsedRoute.getWaypointCount(), waypoints.size());
    Assert.assertEquals(route.getMsgLinkId(), parsedRoute.getMsgLinkId());
    Assert.assertEquals(route.getSenderClassification(), parsedRoute.getSenderClassification());
    Assert.assertEquals(route.getRouteType(), parsedRoute.getRouteType());
    Assert.assertEquals(route.getDuration(), parsedRoute.getDuration());
    Assert.assertEquals(route.getStartMonth(), parsedRoute.getStartMonth());
    Assert.assertEquals(route.getStartDay(), parsedRoute.getStartDay());
    Assert.assertEquals(route.getStartHour(), parsedRoute.getStartHour());
    Assert.assertEquals(route.getStartMin(), parsedRoute.getStartMin());
    for (int i = 0; i < parsedRoute.getWaypointCount(); i++) {
      AisPosition parsedWp = parsedRoute.getWaypoints().get(i);
      AisPosition orgWp = waypoints.get(i);
      Assert.assertTrue(parsedWp.equals(orgWp));
    }
  }
Exemple #3
0
 public AisMessage19(Vdm vdm) throws AisMessageException, SixbitException {
   super(vdm);
   parse(vdm.getBinArray());
 }