feat: add classes for Trip parsing

This commit is contained in:
dancingCycle 2022-02-02 05:33:37 -05:00
parent 80ad6e1c56
commit 5cc3378420
27 changed files with 433 additions and 157 deletions

View File

@ -1,22 +0,0 @@
package de.swingbe.ifleet.model;
public class LocationMessage {
private final Position position;
public LocationMessage(Position position) {
this.position = position;
}
public Position getPosition() {
return position;
}
@Override
public String toString() {
String positionString = "LocationMessage{";
positionString += position != null ? "position=" + position : "";
positionString += '}';
return positionString;
}
}

View File

@ -0,0 +1,39 @@
package de.swingbe.ifleet.model;
public class LocationMsg {
private final Position position;
private final String driverNumber;
private final Trip trip;
public LocationMsg(final Position position, final String driverNumber,
final Trip trip) {
this.position = position;
this.driverNumber = driverNumber;
this.trip = trip;
}
@Override
public String toString() {
String tString = "LocationMsg{" +
"position=" + position +
", driverNumber='" + driverNumber + '\'';
tString += trip != null ? ", trip=" + trip + '\'' : "" + '\'';
tString += '}';
return tString;
}
public String getDriverNumber() {
return driverNumber;
}
public Trip getTrip() {
return trip;
}
public Position getPosition() {
return position;
}
}

View File

@ -2,42 +2,66 @@ package de.swingbe.ifleet.model;
public class Position {
private final long longitude;
private final long latitude;
private final int velocity;
private final int heading;
public final static int POSITION_FIELD_NO = 7;
public Position(long longitude, long latitude, int velocity, int heading) {
private final String netPoint;
private final String relPosition;
private final String longitude;
private final String latitude;
private final String offRoute;
private final String velocity;
private final String heading;
public Position(final String netPoint, final String relPosition, final String longitude,
final String latitude, final String offRoute,
final String velocity, final String heading) {
this.netPoint = netPoint;
this.relPosition = relPosition;
this.longitude = longitude;
this.latitude = latitude;
this.offRoute = offRoute;
this.velocity = velocity;
this.heading = heading;
}
public long getLongitude() {
return longitude;
}
public long getLatitude() {
return latitude;
}
public int getVelocity() {
return velocity;
}
public int getHeading() {
return heading;
}
@Override
public String toString() {
return "Position{" +
"longitude=" + longitude +
", latitude=" + latitude +
", velocity=" + velocity +
", heading=" + heading +
"netPoint='" + netPoint + '\'' +
", relPosition='" + relPosition + '\'' +
", longitude='" + longitude + '\'' +
", latitude='" + latitude + '\'' +
", offRoute='" + offRoute + '\'' +
", velocity='" + velocity + '\'' +
", heading='" + heading + '\'' +
'}';
}
public String getOffRoute() {
return offRoute;
}
public String getNetPoint() {
return netPoint;
}
public String getRelPosition() {
return relPosition;
}
public String getLongitude() {
return longitude;
}
public String getLatitude() {
return latitude;
}
public String getVelocity() {
return velocity;
}
public String getHeading() {
return heading;
}
}

View File

@ -15,7 +15,7 @@ public class Receiver {
@Override
public String toString() {
String rString = "Receiver{";
rString += receiver != null ? "receiver='" + receiver + '\'' : "";
rString += receiver != null ? "receiver='" + receiver + '\'' : "" + '\'';
rString += '}';
return rString;
}

View File

@ -15,7 +15,7 @@ public class Sender {
@Override
public String toString() {
String sString = "Sender{";
sString += sender != null ? "sender='" + sender + '\'' : "";
sString += sender != null ? "sender='" + sender + '\'' : "" + '\'';
sString += '}';
return sString;
}

View File

@ -3,26 +3,26 @@ package de.swingbe.ifleet.model;
public class Telegram {
private final TelegramHdr teleHeader;
private final LocationMessage locationMessage;
private final LocationMsg locationMsg;
public Telegram(TelegramHdr teleHeader, LocationMessage locationMessage) {
public Telegram(TelegramHdr teleHeader, LocationMsg locationMsg) {
this.teleHeader = teleHeader;
this.locationMessage = locationMessage;
this.locationMsg = locationMsg;
}
public TelegramHdr getTeleHeader() {
return teleHeader;
}
public LocationMessage getLocationMessage() {
return locationMessage;
public LocationMsg getLocationMessage() {
return locationMsg;
}
@Override
public String toString() {
String telegramString = "Telegram{";
telegramString += teleHeader != null ? "teleHeader=" + teleHeader : "";
telegramString += locationMessage != null ? ", locationMessage=" + locationMessage : "";
telegramString += teleHeader != null ? "teleHeader=" + teleHeader + '\'' : "" + '\'';
telegramString += locationMsg != null ? ", locationMsg=" + locationMsg + '\'' : "" + '\'';
telegramString += '}';
return telegramString;
}

View File

@ -2,17 +2,19 @@ package de.swingbe.ifleet.model;
public class TelegramHdr {
private final int teleType;
private final String teleVersion;
private final int teleId;
public final static int TELEGRAM_HDR_FIELD_NO = 3;
public TelegramHdr(int teleType, String teleVersion, int teleId) {
private final String teleType;
private final String teleVersion;
private final String teleId;
public TelegramHdr(String teleType, String teleVersion, String teleId) {
this.teleType = teleType;
this.teleVersion = teleVersion;
this.teleId = teleId;
}
public int getTeleType() {
public String getTeleType() {
return teleType;
}
@ -20,16 +22,16 @@ public class TelegramHdr {
return teleVersion;
}
public int getTeleId() {
public String getTeleId() {
return teleId;
}
@Override
public String toString() {
return "TelegramHdr{" +
"teleType=" + teleType +
"teleType='" + teleType + '\'' +
", teleVersion='" + teleVersion + '\'' +
", teleId=" + teleId +
", teleId='" + teleId + '\'' +
'}';
}
}

View File

@ -0,0 +1,74 @@
package de.swingbe.ifleet.model;
public class Trip {
public final static int TRIP_FIELD_NO = 8;
private final String blockNo;
private final String lineNo;
private final String tripNo;
private final String routeNo;
private final String deviation;
private final String loadDegree;
private final String destinationNo;
private final String tripType;
public Trip(final String blockNo, final String lineNo, final String tripNo,
final String routeNo, final String deviation, final String loadDegree,
final String destinationNo, final String tripType) {
this.blockNo = blockNo;
this.lineNo = lineNo;
this.tripNo = tripNo;
this.routeNo = routeNo;
this.deviation = deviation;
this.loadDegree = loadDegree;
this.destinationNo = destinationNo;
this.tripType = tripType;
}
@Override
public String toString() {
return "Trip{" +
"blockNo='" + blockNo + '\'' +
", lineNo='" + lineNo + '\'' +
", tripNo='" + tripNo + '\'' +
", routeNo='" + routeNo + '\'' +
", deviation='" + deviation + '\'' +
", loadDegree='" + loadDegree + '\'' +
", destinationNo='" + destinationNo + '\'' +
", tripType='" + tripType + '\'' +
'}';
}
public String getBlockNo() {
return blockNo;
}
public String getLineNo() {
return lineNo;
}
public String getTripNo() {
return tripNo;
}
public String getRouteNo() {
return routeNo;
}
public String getDeviation() {
return deviation;
}
public String getLoadDegree() {
return loadDegree;
}
public String getDestinationNo() {
return destinationNo;
}
public String getTripType() {
return tripType;
}
}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Communication;
interface ComParser {
/**
* Returns a new {@link Communication} that holds the parses telegram.
* Returns a new {@link Communication} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link Communication}

View File

@ -3,12 +3,12 @@ package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.Communication;
import de.swingbe.ifleet.model.Header;
import de.swingbe.ifleet.model.Telegram;
import de.swingbe.ifleet.utils.TelegramUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
class ComParserImpl implements ComParser {
private final static Logger LOG = LoggerFactory.getLogger(ComParserImpl.class);
private static final String SEP = "#";
private static final String END = System.getProperty("line.separator");
@ -16,27 +16,6 @@ class ComParserImpl implements ComParser {
ComParserImpl() {
}
public static String popFieldFromCom(final String com, final int numberOfFields) {
String comNew = com;
String comSup = null;
for (int i = 0; i < numberOfFields; i++) {
//TODO Method call indexOf may produce NullPointerException!
int indexChar;
if (comNew != null) {
indexChar = comNew.indexOf('#');
try {
comSup = comNew.substring(indexChar + 1);
} catch (IndexOutOfBoundsException e) {
LOG.error("poping field from communication failed, message: " + e.getMessage() + ", trace: " + Arrays.toString(e.getStackTrace()));
}
comNew = comSup;
} else {
LOG.error("input unavailable");
}
}
return comNew;
}
@Override
public Communication parse(final String input) {
@ -52,7 +31,7 @@ class ComParserImpl implements ComParser {
header = HeaderParserFactory.createHeaderParser().parse(input);
//parse Telegram
String inputSupPop = popFieldFromCom(inputSup, 4);
String inputSupPop = TelegramUtils.popField(inputSup, 4);
telegram = TelegramParserFactory.createTelegramParser().parse(inputSupPop);
} else {
LOG.warn("telegram NOT included");

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Entity;
public interface EntityParser {
/**
* Returns a new {@link Entity} that holds the parses telegram.
* Returns a new {@link Entity} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link Entity}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Header;
interface HeaderParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.Header} that holds the parses telegram.
* Returns a new {@link de.swingbe.ifleet.model.Header} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.Header}

View File

@ -1,10 +1,9 @@
package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.Header;
import de.swingbe.ifleet.model.Receiver;
import de.swingbe.ifleet.model.Sender;
import static de.swingbe.ifleet.parser.ComParserImpl.popFieldFromCom;
import static de.swingbe.ifleet.utils.TelegramUtils.popField;
class HeaderParserImpl implements HeaderParser {
@ -19,7 +18,7 @@ class HeaderParserImpl implements HeaderParser {
sender = SenderParserFactory.createSenderParser().parse(input);
//parse Receiver
String inputPop = popFieldFromCom(input, 2);
String inputPop = popField(input, 2);
return new Header(sender, ReceiverParserFactory.createReceiverParser().parse(inputPop));
}

View File

@ -1,17 +1,17 @@
package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.LocationMessage;
import de.swingbe.ifleet.model.LocationMsg;
/**
* Parses a {@link de.swingbe.ifleet.model.LocationMessage}.
* Parses a {@link LocationMsg}.
*/
interface LocationMsgParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.LocationMessage} that holds the parses telegram.
* Returns a new {@link LocationMsg} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.LocationMessage}
* @return the parsed {@link LocationMsg}
*/
LocationMessage parse(final String input);
LocationMsg parse(final String input);
}

View File

@ -1,10 +1,39 @@
package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.LocationMessage;
import de.swingbe.ifleet.model.LocationMsg;
import de.swingbe.ifleet.model.Position;
import de.swingbe.ifleet.model.Trip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static de.swingbe.ifleet.model.Position.POSITION_FIELD_NO;
import static de.swingbe.ifleet.utils.TelegramUtils.popField;
class LocationMsgParserImpl implements LocationMsgParser {
public LocationMessage parse(String input) {
return new LocationMessage(PositionParserFactory.createPositionParser().parse(input));
public final static int DRIVER_NO_FIELD_NO = 1;
private final static Logger LOG = LoggerFactory.getLogger(LocationMsgParserImpl.class);
public LocationMsg parse(String input) {
//parse position
Position position = PositionParserFactory.createPositionParser().parse(input);
//parse driver number
String inputNew = popField(input, POSITION_FIELD_NO);
String[] splits = inputNew.split("#");
String driverNo = "";
if (splits.length > 0) {
driverNo = splits[0];
} else {
LOG.warn("driverNo unavailable");
}
//parse trip
inputNew = popField(input, DRIVER_NO_FIELD_NO);
return new LocationMsg(position, driverNo, TripParserFactory.createTripParser().parse(inputNew));
}
}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Position;
interface PositionParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.Position} that holds the parses telegram.
* Returns a new {@link de.swingbe.ifleet.model.Position} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.Position}

View File

@ -9,6 +9,7 @@ import java.util.Arrays;
import static de.swingbe.ifleet.parser.TelegramHdrParserImpl.isNumeric;
public class PositionParserImpl implements PositionParser {
private final static Logger LOG = LoggerFactory.getLogger(PositionParserImpl.class);
@Override
@ -19,51 +20,61 @@ public class PositionParserImpl implements PositionParser {
String[] splits = input.split("#");
//parse NetPoint
//parse RelPosition
//parse lat
String lat = "";
if (splits.length > 2) {
lat = splits[2];
String netPoint = "";
if (splits.length > 0) {
netPoint = splits[0];
} else {
LOG.warn("lat unavailable");
LOG.warn("netPoint unavailable");
}
//parse RelPosition
String relPosition = "";
if (splits.length > 1) {
relPosition = splits[1];
} else {
LOG.warn("relPosition unavailable");
}
//parse lon
String lon = "";
if (splits.length > 3) {
lon = splits[3];
if (splits.length > 2) {
lon = splits[2];
} else {
LOG.warn("lon unavailable");
}
//parse lat
String lat = "";
if (splits.length > 3) {
lat = splits[3];
} else {
LOG.warn("lat unavailable");
}
//parse offRoute
String offRoute = "";
if (splits.length > 4) {
offRoute = splits[4];
} else {
LOG.warn("offRoute unavailable");
}
//parse vel
String vel = "";
if (splits.length > 4) {
vel = splits[4];
if (splits.length > 5) {
vel = splits[5];
} else {
LOG.warn("vel unavailable");
}
//parse hdg
String hdg = "";
if (splits.length > 5) {
hdg = splits[5];
if (splits.length > 6) {
hdg = splits[6];
} else {
LOG.warn("hdg unavailable");
}
boolean latIsNumeric = isNumeric(lat);
boolean lonIsNumeric = isNumeric(lon);
boolean velIsNumeric = isNumeric(vel);
boolean hdgIsNumeric = isNumeric(hdg);
if (latIsNumeric && lonIsNumeric && velIsNumeric && hdgIsNumeric) {
try {
position = new Position(Long.parseLong(lat), Long.parseLong(lon), Integer.parseInt(vel), Integer.parseInt(hdg));
} catch (NumberFormatException e) {
LOG.error("parsing position failed, message: " + e.getMessage() + ", trace: " + Arrays.toString(e.getStackTrace()));
}
}
return position;
return new Position(netPoint, relPosition, lon, lat, offRoute, vel, hdg);
}
}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Receiver;
interface ReceiverParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.Receiver} that holds the parses telegram.
* Returns a new {@link de.swingbe.ifleet.model.Receiver} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.Receiver}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Sender;
interface SenderParser {
/**
* Returns a new {@link Sender} that holds the parses telegram.
* Returns a new {@link Sender} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link Sender}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.TelegramHdr;
interface TelegramHdrParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.TelegramHdr} that holds the parses telegram.
* Returns a new {@link de.swingbe.ifleet.model.TelegramHdr} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.TelegramHdr}

View File

@ -4,8 +4,6 @@ import de.swingbe.ifleet.model.TelegramHdr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
class TelegramHdrParserImpl implements TelegramHdrParser {
private final static Logger LOG = LoggerFactory.getLogger(TelegramHdrParserImpl.class);
@ -19,11 +17,9 @@ class TelegramHdrParserImpl implements TelegramHdrParser {
public TelegramHdr parse(final String input) {
TelegramHdr teleHeader = null;
String[] splits = input.split("#");
//parse id
//parse type
String type = null;
if (splits.length > 0) {
type = splits[0];
@ -35,29 +31,13 @@ class TelegramHdrParserImpl implements TelegramHdrParser {
version = splits[1];
}
//parse ID
//parse id
String id = null;
if (splits.length > 2) {
id = splits[2];
}
if (type != null && version != null && id != null) {
boolean typeIsNumeric = isNumeric(type);
boolean idIsNumeric = isNumeric(id);
if (typeIsNumeric && idIsNumeric) {
try {
teleHeader = new TelegramHdr(Integer.parseInt(type), version,
Integer.parseInt(id));
} catch (NumberFormatException e) {
LOG.error("parsing telegram header failed, message: "
+ e.getMessage() + ", trace: "
+ Arrays.toString(e.getStackTrace()));
}
}
}
return teleHeader;
return new TelegramHdr(type, version, id);
}
}

View File

@ -8,7 +8,7 @@ import de.swingbe.ifleet.model.Telegram;
interface TelegramParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.Telegram} that holds the parses telegram.
* Returns a new {@link de.swingbe.ifleet.model.Telegram} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.Telegram}

View File

@ -3,7 +3,8 @@ package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.Telegram;
import de.swingbe.ifleet.model.TelegramHdr;
import static de.swingbe.ifleet.parser.ComParserImpl.popFieldFromCom;
import static de.swingbe.ifleet.model.TelegramHdr.TELEGRAM_HDR_FIELD_NO;
import static de.swingbe.ifleet.utils.TelegramUtils.popField;
class TelegramParserImpl implements TelegramParser {
@ -14,7 +15,7 @@ class TelegramParserImpl implements TelegramParser {
TelegramHdr teleHeader = TelegramHdrParserFactory.createTelegramHdrParser().parse(input);
String inputNew = popFieldFromCom(input, 3);
String inputNew = popField(input, TELEGRAM_HDR_FIELD_NO);
return new Telegram(teleHeader,
LocationMsgParserFactory.createLocationMsgParer().parse(inputNew));

View File

@ -0,0 +1,17 @@
package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.Trip;
/**
* Parses a {@link de.swingbe.ifleet.model.Trip}.
*/
interface TripParser {
/**
* Returns a new {@link de.swingbe.ifleet.model.Trip} that holds the parses fields.
*
* @param input the {@link String} to be parsed
* @return the parsed {@link de.swingbe.ifleet.model.Trip}
*/
Trip parse(final String input);
}

View File

@ -0,0 +1,20 @@
package de.swingbe.ifleet.parser;
/**
* Factory for creating instances of {@link TripParser}.
*/
class TripParserFactory {
private TripParserFactory() {
}
/**
* Creates an instance of {@link TripParser}.
*
* @return the new instance
*/
public static TripParser createTripParser() {
return new TripParserImpl();
}
}

View File

@ -0,0 +1,84 @@
package de.swingbe.ifleet.parser;
import de.swingbe.ifleet.model.Trip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TripParserImpl implements TripParser {
private final static Logger LOG = LoggerFactory.getLogger(TripParserImpl.class);
@Override
public Trip parse(final String input) {
Trip trip = null;
String[] splits = input.split("#");
//parse blockNo
String blockNo = "";
if (splits.length > 0) {
blockNo = splits[0];
} else {
LOG.warn("blockNo unavailable");
}
//parse lineNo
String lineNo = "";
if (splits.length > 1) {
lineNo = splits[1];
} else {
LOG.warn("lineNo unavailable");
}
//parse tripNo
String tripNo = "";
if (splits.length > 2) {
tripNo = splits[2];
} else {
LOG.warn("tripNo unavailable");
}
//parse routeNo
String routeNo = "";
if (splits.length > 3) {
routeNo = splits[3];
} else {
LOG.warn("routeNo unavailable");
}
//parse deviation
String deviation = "";
if (splits.length > 4) {
deviation = splits[4];
} else {
LOG.warn("deviation unavailable");
}
//parse loadDegree
String loadDegree = "";
if (splits.length > 5) {
loadDegree = splits[5];
} else {
LOG.warn("loadDegree unavailable");
}
//parse destinationNo
String destinationNo = "";
if (splits.length > 6) {
destinationNo = splits[6];
} else {
LOG.warn("destinationNo unavailable");
}
//parse tripType
String tripType = "";
if (splits.length > 6) {
tripType = splits[6];
} else {
LOG.warn("tripType unavailable");
}
return new Trip(blockNo, lineNo, tripNo, routeNo, deviation, loadDegree, destinationNo, tripType);
}
}

View File

@ -0,0 +1,39 @@
package de.swingbe.ifleet.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
public class TelegramUtils {
private final static Logger LOG = LoggerFactory.getLogger(TelegramUtils.class);
/**
* Returns a sub {@link String} from the input {@link String} reduces by a certain number of fields
*
* @param input the {@link String} to be reduced by a certain number of fields
* @param numberOfFields the number of fields to be popped from the input {@link String}
* @return the sub {@link String} not containing the popped fields
*/
public static String popField(final String input, final int numberOfFields) {
String inputNew = input;
String inputSub = null;
for (int i = 0; i < numberOfFields; i++) {
//TODO Method call indexOf may produce NullPointerException!
int indexHash;
if (inputNew != null) {
indexHash = inputNew.indexOf('#');
try {
inputSub = inputNew.substring(indexHash + 1);
} catch (IndexOutOfBoundsException e) {
LOG.error("popping field failed, message: " + e.getMessage() + ", trace: " + Arrays.toString(e.getStackTrace()));
}
inputNew = inputSub;
} else {
LOG.error("input unavailable");
}
}
return inputNew;
}
}