summaryrefslogtreecommitdiff
path: root/src/main/java/com/benburwell/planes/data/Aircraft.java
blob: 280c6bd3d1ca442ef0184aa5716c47aa2414718d (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
package com.benburwell.planes.data;

import com.benburwell.planes.sbs.SBSPacket;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * @author ben
 */
public class Aircraft implements Comparable<Aircraft> {
    private final String hexIdent;
    private Position currentPosition = new Position();
    private List<Position> positionHistory = new ArrayList<>();
    private String callsign = "";
    private long packetCount = 0;
    private double track;
    private double groundSpeed;
    private double verticalRate;

    public Aircraft(String hexIdent) {
        this.hexIdent = hexIdent;
    }

    public void handleUpdate(SBSPacket packet) {
        this.packetCount++;

        Position newPosition = new Position();
        newPosition.setAltitude(this.currentPosition.getAltitude());
        newPosition.setLatitude(this.currentPosition.getLatitude());
        newPosition.setLongitude(this.currentPosition.getLongitude());
        newPosition.setTimestamp(this.currentPosition.getTimestamp());

        if (packet.getAltitude() != null) {
            newPosition.setAltitude(packet.getAltitude());
            newPosition.setTimestamp(new Date(System.currentTimeMillis()));
        }
        if (packet.getLatitude() != null) {
            newPosition.setLatitude(packet.getLatitude());
            newPosition.setTimestamp(new Date(System.currentTimeMillis()));
        }
        if (packet.getLongitude() != null) {
            newPosition.setLongitude(packet.getLongitude());
            newPosition.setTimestamp(new Date(System.currentTimeMillis()));
        }
        if (packet.getCallsign() != null && !packet.getCallsign().isEmpty()) {
            this.callsign = packet.getCallsign();
        }
        if (packet.getSquawk() != null && !packet.getSquawk().isEmpty()) {
            this.callsign = packet.getSquawk();
        }
        if (packet.getTrack() != null) {
            this.track = packet.getTrack();
        }
        if (packet.getGroundSpeed() != null) {
            this.groundSpeed = packet.getGroundSpeed();
        }
        if (packet.getVerticalRate() != null) {
            this.verticalRate = packet.getVerticalRate();
        }

        if (newPosition.getTimestamp().after(this.currentPosition.getTimestamp())) {
            this.positionHistory.add(currentPosition);
            this.currentPosition = newPosition;
        }
    }

    public Position getCurrentPosition() {
        return currentPosition;
    }

    public String getCallsign() {
        return callsign;
    }

    public Long getPacketCount() {
        return packetCount;
    }

    public String getHexIdent() {
        return this.hexIdent;
    }

    public double getTrack() {
        return this.track;
    }

    public double getGroundSpeed() {
        return this.groundSpeed;
    }

    public double getVerticalRate() {
        return this.verticalRate;
    }

    public List<Position> getPositionHistory() {
        return positionHistory;
    }

    @Override
    public int compareTo(Aircraft that) {
        return this.getHexIdent().compareTo(that.getHexIdent());
    }
}