summaryrefslogtreecommitdiff
path: root/src/main/java/com/benburwell/planes/sbs/SBSPacket.java
blob: 568480381219a192248d74648759d114307d9b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.benburwell.planes.sbs;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ben on 11/15/16.
 */
public class SBSPacket {
    private MessageType messageType;
    private TransmissionType transmissionType = null;
    private String sessionId = null;
    private String aircraftId = null;
    private String hexIdent = null;
    private String flightId = null;
    private Date dateGenerated = null;
    private Date dateLogged = null;
    private String callsign = null;
    private Double altitude = null;
    private Double groundSpeed = null;
    private Double track = null;
    private Double latitude = null;
    private Double longitude = null;
    private Double verticalRate = null;
    private String squawk = null;
    private Boolean alert = null;
    private Boolean emergency = null;
    private Boolean spi = null;
    private Boolean isOnGround = null;

    public SBSPacket(String packet) throws MalformedPacketException {
        this.parse(packet);
    }

    public void parse(String packet) throws MalformedPacketException {
        String[] segments = packet.split(",", -1);
        if (segments.length < 11) {
            throw new MalformedPacketException("Packet must have at least 11 fields, but has only " + segments.length);
        }

        // get the message type
        try {
            this.messageType = MessageType.parse(segments[0]);
        } catch (UnrecognizedMessageTypeException e) {
            throw new MalformedPacketException("Packet has an unrecognized message type: " + e.getType());
        }

        // get the transmission type
        if (this.messageType.equals(MessageType.TRANSMISSION)) {
            try {
                this.transmissionType = TransmissionType.parse(segments[1]);
            } catch (UnrecognizedTransmissionTypeException e) {
                throw new MalformedPacketException("Packet has an unrecognized transmission type code: " + e.getCode());
            }
        }

        this.sessionId = segments[2];
        this.aircraftId = segments[3];
        this.hexIdent = segments[4];
        this.flightId = segments[5];
        this.dateGenerated = this.parseDateAndTime(segments[6], segments[7]);
        this.dateLogged = this.parseDateAndTime(segments[8], segments[9]);
        this.callsign = segments[10];

        if (this.messageType.equals(MessageType.TRANSMISSION)) {
            if (segments.length < 22) {
                throw new MalformedPacketException("Packet is a message (22 fields), but only has " + segments.length);
            }
            if (segments[11].length() > 0) {
                this.altitude = Double.parseDouble(segments[11]);
            }
            if (segments[12].length() > 0) {
                this.groundSpeed = Double.parseDouble(segments[12]);
            }
            if (segments[13].length() > 0) {
                this.track = Double.parseDouble(segments[13]);
            }
            if (segments[14].length() > 0) {
                this.latitude = Double.parseDouble(segments[14]);
            }
            if (segments[15].length() > 0) {
                this.longitude = Double.parseDouble(segments[15]);
            }
            if (segments[16].length() > 0) {
                this.verticalRate = Double.parseDouble(segments[16]);
            }
            this.squawk = segments[17];
            if (segments[18].length() > 0) {
                this.alert = segments[18].equals("1");
            }
            if (segments[19].length() > 0) {
                this.emergency = segments[19].equals("1");
            }
            if (segments[20].length() > 0) {
                this.spi = segments[20].equals("1");
            }
            if (segments[21].length() > 0) {
                this.isOnGround = segments[21].equals("1");
            }
        }
    }

    public Date parseDateAndTime(String date, String time) {
        String combined = "";
        Calendar now = Calendar.getInstance();
        if (date == null || date.isEmpty()) {
            combined += now.get(Calendar.YEAR) + "/" + now.get(Calendar.MONTH) + "/" + now.get(Calendar.DAY_OF_MONTH);
        } else {
            combined += date;
        }

        combined += " ";

        if (time == null || time.isEmpty()) {
            combined += now.get(Calendar.HOUR_OF_DAY) + ":" +
                    now.get(Calendar.MINUTE) + ":" +
                    now.get(Calendar.SECOND) + "." +
                    now.get(Calendar.MILLISECOND);
        } else {
            combined += time;
        }

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.sss");
        try {
            return fmt.parse(combined);
        } catch (ParseException e) {
            return null;
        }
    }

    public String toString() {
        return this.messageType.name();
    }

    public MessageType getMessageType() {
        return messageType;
    }

    public TransmissionType getTransmissionType() {
        return transmissionType;
    }

    public String getSessionId() {
        return sessionId;
    }

    public String getAircraftId() {
        return aircraftId;
    }

    public String getHexIdent() {
        return hexIdent;
    }

    public String getFlightId() {
        return flightId;
    }

    public Date getDateGenerated() {
        return dateGenerated;
    }

    public Date getDateLogged() {
        return dateLogged;
    }

    public String getCallsign() {
        return callsign;
    }

    public Double getAltitude() {
        return altitude;
    }

    public Double getGroundSpeed() {
        return groundSpeed;
    }

    public Double getTrack() {
        return track;
    }

    public Double getLatitude() {
        return latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public Double getVerticalRate() {
        return verticalRate;
    }

    public String getSquawk() {
        return squawk;
    }

    public Boolean isAlert() {
        return alert;
    }

    public Boolean isEmergency() {
        return emergency;
    }

    public Boolean isSpi() {
        return spi;
    }

    public Boolean isOnGround() {
        return isOnGround;
    }
}