AisLib: A Java Library for Efficiently Handling AIS Messages
Are you looking for a robust solution to handle AIS (Automatic Identification System) messages in your Java applications? Look no further – AisLib is here to streamline the process of reading, decoding, filtering, and encoding AIS messages. Whether you need to integrate AIS data into your maritime applications, analyze vessel movements, or develop advanced tracking systems, AisLib provides the tools and functionalities you need.
Introduction
AisLib is a powerful Java library that offers a comprehensive set of features for handling AIS messages. Here are some of the key functionalities it provides:
- Reading from AIS sources such as serial connections, TCP connections, or files
- Handling proprietary source tagging sentences
- Message filtering, including doublet filtering and downsampling
- Decoding AIS sentences and messages
- Encoding sentences and AIS messages
- Sending AIS messages #6, #8, #12, and #14
- Handling application-specific messages
This library also includes test code and utility applications that demonstrate the use of AisLib.
Prerequisites
Before using AisLib, ensure that you have the following prerequisites:
- Java 8
- Maven 3
Building and Running
Building and running your applications with AisLib is a breeze. Simply follow these steps:
- Clone the AisLib repository from GitHub.
- Open a terminal or command prompt and navigate to the cloned repository.
- Run the command
mvn clean install
to build the library. - Run the command
mvn test
to run the tests.
For additional details on the build process, refer to the repository’s README.
Developing in Eclipse
If you prefer using Eclipse as your IDE, you can easily set up your AisLib project. Follow these steps:
- Install the M2 plugin in Eclipse.
- Open a terminal or command prompt and navigate to the cloned AisLib repository.
- Run the command
mvn eclipse:eclipse
to generate Eclipse project files. - Import the project into Eclipse as a regular project.
Now, you can start developing your applications with AisLib in Eclipse.
Contributing
AisLib is an open-source project, and contributions are always welcome. If you want to contribute to the development of AisLib, follow these steps:
- Fork the AisLib repository on GitHub.
- Make the necessary changes or additions to the code.
- Submit a pull request to the main repository.
Your contributions will be reviewed and considered for inclusion in future releases of AisLib.
License
AisLib is licensed under the Apache License, Version 2.0. This means that you are free to use, modify, and distribute AisLib in both commercial and non-commercial projects.
Examples
Let’s dive into some examples to illustrate how AisLib simplifies the handling of AIS messages:
Simple Read and Message Handling
Reading an AIS message from a sentence can be done easily with the Vdm object provided by AisLib. Whether it’s a single message or multiple messages, the process is straightforward. Here’s an example:
``java
E46I<@9n@059l,0*30″;
String aisSentence = "!AIVDM,1,1,,A,181:Jqh02c1Qra
Vdm vdm = new Vdm();
vdm.parse(aisSentence);
AisMessage aisMessage = AisMessage.getInstance(vdm);
“`
Reading Raw Message Packets
If you prefer working with unparsed raw message packets instead of AISMessage objects, AisLib has you covered. You can register a packet consumer in a reader and process packets as needed. Here’s an example:
“`java
AisReader reader = AisReaders.createReader(“localhost”, 4001);
reader.registerPacketHandler(new Consumer() {
@Override
public void accept(AisPacket packet) {
try {
AisMessage message = packet.getAisMessage();
// Handle the AIS message
} catch (AisMessageException | SixbitException e) {
// Handle exceptions
}
// Alternative approach: Try to get the AIS message without throwing exceptions
AisMessage message = packet.tryGetAisMessage();
Date timestamp = packet.getTimestamp();
CommentBlock cb = packet.getVdm().getCommentBlock();
// Process the timestamp and comment block as needed
}
});
reader.start();
reader.join();
“`
Multiple Sources and Round Robin Reading
AisLib allows you to handle messages from multiple sources and implement round-robin reading for redundancy. You can easily configure a reader group to handle this scenario. Here’s an example:
“`java
List sources = new ArrayList<>();
sources.add(“host1:port1,host2:port2,host3:port3”);
AisReaderGroup group = AisReaders.createGroup(“name”, sources);
// Further configuration and processing of the reader group
“`
Message Filtering
With AisLib, you can apply various filters to AIS messages based on your specific requirements. Whether you need to filter messages by attributes, position, or other criteria, AisLib provides flexible filtering options. Here’s an example of down-sampling filtering:
“`java
AisReader reader = AisReaders.createReader(“ais163.sealan.dk:4712”);
reader.registerHandler(new Consumer() {
DownSampleFilter downSampleFilter = new DownSampleFilter(60);
@Override
public void accept(AisMessage aisMessage) {
if (downSampleFilter.rejectedFilter(aisMessage)) {
return;
}
// Handle the filtered message
}
});
reader.start();
reader.join();
“`
Sending Messages
AisLib also enables you to send AIS messages. You can easily send addressed text messages or even application-specific messages. Here’s an example of sending an addressed text message:
“`java
AisReader aisReader = AisReaders.createReader(host, port);
aisReader.start();
// Create an AIS message
AisMessage12 msg12 = new AisMessage12();
msg12.setDestination(destination);
msg12.setMessage(message);
// Send the message and get the acknowledgment
Abk abk = aisReader.send(msg12, 1, destination);
if (abk.isSuccess()) {
// Message successfully sent
} else {
// Error sending the message
}
“`
These examples only scratch the surface of what AisLib can do. Explore the library and its extensive documentation to learn more about its capabilities and how it can simplify AIS message handling in your applications.
Conclusion
AisLib is a Java library that empowers developers to efficiently handle AIS messages in their applications. With its comprehensive set of features, ease of use, and flexibility, AisLib is the go-to solution for integrating AIS data, analyzing vessel movements, and building advanced tracking systems. Get started with AisLib today and unlock the full potential of AIS message handling in your Java applications.
Remember to contribute to the AisLib open-source project if you find value in it, and don’t forget to explore the AisLib repository on GitHub for more information and examples.
Check out the AisLib repository on GitHub: https://github.com/dma-ais/AisLib
Leave a Reply