import java.util.*;
public class SentenceFactory implements SentenceFactoryInterface {
public ADSBMessage fromWebdisJson(String json) {
String rawSentence, dfca, icao, payload, parity, type;
HashMap<String, String> map;
// ...
// variables for position message
int altitude, longitude, latitude, lat0, lat1, lon0, lon1;
char qbit, format;
String hashIcao;
// check whether we got a correct JSON Sentence
// {"subscribe":["message","ads.sentence","1381236091.8042176!ADS-B*8D3C6DD25819B0699A075E167303;\r\n"]}
rawSentence = json.substring(json.indexOf("*") + 1, json.indexOf("*") + 29);
// get the indexes of the different parts of the sentence
int dfcaIndex = json.indexOf("*") + 1;
int icaoIndex = dfcaIndex + 2;
int payloadIndex = icaoIndex + 6;
// create hashmap
if(map == null) {
}
else {
map = new HashMap<String, String>();
}
//check if it is a ads b sentence
if (json.contains("!ADS-B*")) {
//create the substrings
icao = json.substring(icaoIndex, icaoIndex + 6);
payload = json.substring(payloadIndex, payloadIndex + 14);
//convert the payload to bin and calculate the type
String payloadBin = hexToBinary(payload);
String payloadFirstFive = payloadBin.substring(0, 5);
int payloadTypeValue = binaryToDecimal(payloadFirstFive);
// ...
// Airborne Position Message
if (payloadTypeValue >= 9 && payloadTypeValue <= 18 || payloadTypeValue >= 20 && payloadTypeValue <= 22 ) {
// get format
format = payloadBin.charAt(21);
// check if other message is in hashmap
if(map.containsKey(icao)){
// get the payload of the other message
hashIcao = map.get(icao);
//check the format of both messages
if(format == hashIcao.charAt(21)) {
//Überschreiben
}
if(format != hashIcao.charAt(21)) {
//calculate the altitude (newer message)
altitude = binaryToDecimal(payloadBin.substring(8,15) + payloadBin.substring(16,20));
qbit = payloadBin.charAt(15);
if(qbit == 0) {
altitude = (altitude * 25) - 1000;
}
if(qbit == 1) {
altitude = (altitude * 100) - 1000;
}
// get the even and odd longitude and latitude
// when my new message is even
if(format == 0) {
lat0 = binaryToDecimal(payloadBin.substring(22,39));
lon0 = binaryToDecimal(payloadBin.substring(39));
lat1 = binaryToDecimal(hashIcao.substring(22,39));
lon1 = binaryToDecimal(hashIcao.substring(39));
}
// when my new message is odd
if(format == 1) {
lat0 = binaryToDecimal(hashIcao.substring(22,39));
lon0 = binaryToDecimal(hashIcao.substring(39));
lat1 = binaryToDecimal(payloadBin.substring(22,39));
lon1 = binaryToDecimal(payloadBin.substring(39));
}
// alte nachricht aus hash löschen
return new ADSBMessage (icao, payloadTypeValue, altitude, longitude, latitude);
}
}
// wenn keine nachricht mit demselben icao -> einfügen
}
//...
// Other Message
return new ADSBMessage (icao, payloadTypeValue);
}
// Fallback
return new ADSBMessage();
}
//...
}