summaryrefslogtreecommitdiff
path: root/LearnMapKit/ADSBDataQueue.swift
blob: d3777ec3ed47fbcac4fc27c69ef4c47f2df600b4 (plain) (blame)
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
//
//  ADSBDataQueue.swift
//  LearnMapKit
//
//  Created by Jacky Jack on 05/08/2024.
//

import Foundation
import Collections

///Tag for location
struct ADSBLocation {
    let address: Int
    let lat: Double
    let long: Double
    //let alt: Int
}
/// Tag for Altitude
struct ADSBAltitude {
    let address: Int
    let altitude: Int
}

/// Tag for ICAO name
struct ADSBICAOname {
    let address: Int
    let ICAOname: String
}
/// Tag enum for data stream
///
enum DataStreamType {
    case EMPTY
    case ADSB_ICAO
    case ADSB_LOCATION
    case ADSB_ALTITUDE
}

/// get stream of decoded data, to tagged stream so all can be processed in sequence
class ADSBDataQueue {
    //var icaoQueue: Deque<ADSBICAOname> = []
    var icaoArray: Array<ADSBICAOname> = []
    var altQueue: Deque<ADSBAltitude> = []
    var locQueue: Deque<ADSBLocation> = []
    //var tagQueue: Deque<> = []
    var tagArray: Array<DataStreamType> = []
    
    ///get the last tag from a queue
    ///user handles what todo with this data
    func getNextTag() -> DataStreamType {
        if tagArray.count < 0 {
            return DataStreamType.EMPTY
        }
        return tagArray[tagArray.count-1]
    }
    
    
    func addIcaoName(_ address: Int, _ icaoname: String) {
        tagArray.append(DataStreamType.ADSB_ICAO)
        icaoArray.append(ADSBICAOname(address: address, ICAOname: icaoname))
    }
    
    func addAltitude(_ address: Int, _ altitude: Int) {
        tagArray.append(DataStreamType.ADSB_ALTITUDE)
        altQueue.append(ADSBAltitude(address: address, altitude: altitude))
    }
    
    func addLocation(_ address: Int, _ lat: Double, _ long: Double) {
        tagArray.append(DataStreamType.ADSB_LOCATION)
        locQueue.append(ADSBLocation(address: address, lat: lat, long: long))
    }
    
    ///let read next tag in a queue, that is ICAO name
    ///user handles order in a queue for now
    func getIcaoName() -> ADSBICAOname {
        if tagArray.count < 1 {
            print("ADSB tag Queue is empty")
            return ADSBICAOname(address:0,ICAOname: "TEmpty")
        }
        let tag = tagArray[tagArray.count-1]
        if tag != DataStreamType.ADSB_ICAO {
            print("ADSB Queue empty")
            return ADSBICAOname(address:0,ICAOname: "QEmpty")
        }
        tagArray.removeLast()
        var ret_icao_name = ADSBICAOname(address:0, ICAOname: "Default")
        if let last_icao_name = icaoArray.popLast() {
            ret_icao_name = last_icao_name
        }
        return ret_icao_name
    }
    
    ///let read next tag in a queue, that is Altitude
    ///user handles order in a queue for now
    func getAltitude() -> ADSBAltitude {
        if tagArray.count < 1 {
            print("ADSB tag Queue is empty")
            return ADSBAltitude(address:0,altitude:0)
        }
        let tag = tagArray[tagArray.count-1]
        if tag != DataStreamType.ADSB_ALTITUDE {
            print("ADSB Queue empty")
            return ADSBAltitude(address:0,altitude:0)
        }
        tagArray.removeLast()
        return altQueue.popLast()!
    }
    
    ///let read next tag in a queue, that is the location
    ///user handles order in a queue for now
    func getLocation() -> ADSBLocation {
        if tagArray.count < 1 {
            print("ADSB tag Queue is empry")
            return ADSBLocation(address:0,lat:0.0,long:0.0)
        }
        let tag = tagArray[tagArray.count-1]
        if tag != DataStreamType.ADSB_LOCATION {
            print("ADSB Queue empty")
            return ADSBLocation(address:0,lat:0.0,long:0.0)
        }
        tagArray.removeLast()
        return locQueue.popLast()!
    }
    
    //check if there is more then specified number in queue
    func haveNum(_ num: Int) -> Bool {
        if (tagArray.count > num) {
            return true
        }
        return false
    }
    
    ///get number in the queue
    func getCount() -> Int {
        return tagArray.count
    }
}