org.opensky.example.ExampleDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of libadsb Show documentation
Show all versions of libadsb Show documentation
A Mode S and ADS-B decoding library for Java
package org.opensky.example;
/**
* This file is part of org.opensky.libadsb.
*
* org.opensky.libadsb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* org.opensky.libadsb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with org.opensky.libadsb. If not, see .
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import org.opensky.libadsb.Decoder;
import org.opensky.libadsb.Position;
import org.opensky.libadsb.PositionDecoder;
import org.opensky.libadsb.tools;
import org.opensky.libadsb.exceptions.BadFormatException;
import org.opensky.libadsb.exceptions.UnspecifiedFormatError;
import org.opensky.libadsb.msgs.AirbornePositionMsg;
import org.opensky.libadsb.msgs.AirspeedHeadingMsg;
import org.opensky.libadsb.msgs.AllCallReply;
import org.opensky.libadsb.msgs.AltitudeReply;
import org.opensky.libadsb.msgs.CommBAltitudeReply;
import org.opensky.libadsb.msgs.CommBIdentifyReply;
import org.opensky.libadsb.msgs.CommDExtendedLengthMsg;
import org.opensky.libadsb.msgs.EmergencyOrPriorityStatusMsg;
import org.opensky.libadsb.msgs.ExtendedSquitter;
import org.opensky.libadsb.msgs.IdentificationMsg;
import org.opensky.libadsb.msgs.IdentifyReply;
import org.opensky.libadsb.msgs.LongACAS;
import org.opensky.libadsb.msgs.MilitaryExtendedSquitter;
import org.opensky.libadsb.msgs.ModeSReply;
import org.opensky.libadsb.msgs.OperationalStatusMsg;
import org.opensky.libadsb.msgs.ShortACAS;
import org.opensky.libadsb.msgs.SurfacePositionMsg;
import org.opensky.libadsb.msgs.TCASResolutionAdvisoryMsg;
import org.opensky.libadsb.msgs.VelocityOverGroundMsg;
/**
* ADS-B decoder example: It reads STDIN line-by-line. It should be fed with
* comma-separated timestamp and message. Example input:
*
* 1,8d4b19f39911088090641010b9b0
* 2,8d4ca513587153a8184a2fb5adeb
* 3,8d3413c399014e23c80f947ce87c
* 4,5d4ca88c079afe
* 5,a0001838ca3e51f0a8000047a36a
* 6,8d47a36a58c38668ffb55f000000
* 7,5d506c28000000
* 8,a8000102fe81c1000000004401e3
* 9,a0001839000000000000004401e3
*
* @author Matthias Schäfer ([email protected])
*/
public class ExampleDecoder {
// we store the position decoder for each aircraft
HashMap decs;
private PositionDecoder dec;
public ExampleDecoder() {
decs = new HashMap();
}
public void decodeMsg(double timestamp, String raw, String icao) throws Exception {
ModeSReply msg;
try {
msg = Decoder.genericDecoder(raw);
} catch (BadFormatException e) {
System.out.println("Malformed message! Skipping it. Message: "+e.getMessage());
return;
} catch (UnspecifiedFormatError e) {
System.out.println("Unspecified message! Skipping it...");
return;
}
String icao24 = tools.toHexString(msg.getIcao24());
if (icao != null && !icao.toLowerCase().equals(icao24)) return;
// check for erroneous messages; some receivers set
// parity field to the result of the CRC polynomial division
if (tools.isZero(msg.getParity()) || msg.checkParity()) { // CRC is ok
// cleanup decoders every 100.000 messages to avoid excessive memory usage
// therefore, remove decoders which have not been used for more than one hour.
List to_remove = new ArrayList();
for (String key : decs.keySet())
if (decs.get(key).getLastUsedTime() 0) {
icao = args[0];
System.err.println("Set filter to ICAO 24-bit ID '"+icao+"'.");
}
// iterate over STDIN
Scanner sc = new Scanner(System.in, "UTF-8");
ExampleDecoder dec = new ExampleDecoder();
while(sc.hasNext()) {
String[] values = sc.nextLine().split(",");
dec.decodeMsg(Double.parseDouble(values[0]), values[1], icao);
}
sc.close();
}
}