create static method

AISMessage create(
  1. String input,
  2. bool logging,
  3. bool legacy,
  4. bool isPayload,
)

Implementation

static AISMessage create(String input, bool logging, bool legacy, bool isPayload) {
  String encoded = '';

  //region sanitizing

  //legacy method to create binary String from input String
  String makeBinaryString(String input) {
    String binaryOutput = '';
    for (String char in input.split('')) {
      binaryOutput += convertCharToBinary(char);
    }
    return binaryOutput;
  }

  if(input.isEmpty) {
    throw InvalidBinaryDataException(
        "Supplied String is empty or undefined!");
  }

  if(input.contains("!AIVDM") || input.contains("!AIVDO")) {

    List<String> fields = input.split(',');

    // When logging is enabled print logging
    if(logging) DebugPrinter(fields: fields).debugPrint();

    // Legacy Mode:
    if(legacy) {
      encoded = isPayload ? input : makeBinaryString(fields[5]);
    } else {
      encoded = isPayload ? input : fields[5];
    }

  } else {
    // fallback if no AIVDM String is found aka is payload
    encoded = input;
  }

  // minimum length for mmsi part of sentence
  if(legacy && encoded.length < 38) {
    throw InvalidBinaryDataException("Supplied binary String too short (${encoded.length} bits)!");
  }
  //endregion

  try {
    // get message type
    int messageType = legacy ? int.parse(encoded.substring(0,6), radix: 2) : getUintDirect(encoded, 0, 6);
    int messagePart = legacy ? int.parse(encoded.substring(38, 40), radix: 2) : getUintDirect(encoded, 38, 40);
    if(messageType == 24) { messagePart = legacy ? int.parse(encoded.substring(38, 40), radix: 2) : getUintDirect(encoded, 38, 40); }

    // switch to correct message type handling scenario
    if(!legacy) {

      return switch (messageType) {

      // Position reports
        1 || 2 || 3 => PositionMessage.fromEncoded(encoded),
        18 => StandardClassBCSPositionReport.fromEncoded(encoded),
        19 => ExtendedClassBCSPositionReport.fromEncoded(encoded),
        27 => LongRangeAISBroadcastMessage.fromEncoded(encoded),
        9 => SarAircraftPositionReport.fromEncoded(encoded),

      // Static data
        5 => StaticAndVoyageRelatedData.fromEncoded(encoded),
        24 => messagePart == 0
            ? StaticDataReportA.fromEncoded(encoded)
            : StaticDataReportB.fromEncoded(encoded),

      // Safety messages
        12 => AddressedSafetyRelatedMessage.fromEncoded(encoded),
        13 => SafetyRelatedAcknowledgement.fromEncoded(encoded),
        14 => SafetyRelatedBroadcastMessage.fromEncoded(encoded),

      // Specialized
        4 => BaseStationReport.fromEncoded(encoded),
        21 => AidToNavigationReport.fromEncoded(encoded),

      // Binary messages
        6 => BinaryAddressedMessage.fromEncoded(encoded),
        7 => BinaryAcknowledge.fromEncoded(encoded),
        8 => BinaryBroadcastMessage.fromEncoded(encoded),
        25 => SingleSlotBinaryMessage.fromEncoded(encoded),
        26 => MultipleSlotBinaryMessage.fromEncoded(encoded),

      // Network messages
        15 => InterrogationMessage.fromEncoded(encoded),
        16 => AssignmentModeCommand.fromEncoded(encoded),
        17 => DgnssBroadcastBinaryMessage.fromEncoded(encoded),
        20 => DataLinkManagementMessage.fromEncoded(encoded),
        22 => ChannelManagementMessage.fromEncoded(encoded),
        23 => GroupAssignmentCommand.fromEncoded(encoded),

      // Time messages
        10 => UtcDateInquiry.fromEncoded(encoded),
        11 => UtcDateResponse.fromEncoded(encoded),

        _ => throw UnsupportedMessageTypeException(messageType),
      };
    } else {
      return switch (messageType) {

      // Position reports
        1 || 2 || 3 => PositionMessage.fromBinary(encoded),
        18 => StandardClassBCSPositionReport.fromBinary(encoded),
        19 => ExtendedClassBCSPositionReport.fromBinary(encoded),
        27 => LongRangeAISBroadcastMessage.fromBinary(encoded),

      // Static data
        5 => StaticAndVoyageRelatedData.fromBinary(encoded),
        24 =>
        messagePart == 0
            ? StaticDataReportA.fromBinary(encoded)
            : StaticDataReportB.fromBinary(encoded),

      // Specialized
        4 => BaseStationReport.fromBinary(encoded),

        _ => throw UnsupportedMessageTypeExceptionLegacy(messageType),
      };
    }


  } catch (e) {
    throw Exception(e);
  }
}