summaryrefslogtreecommitdiff
path: root/LearnMapKit/FlighState.swift
blob: cfb34f1be3ff2559f4e00ab543abca3ac2323832 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
//  FlighState.swift
//  LearnMapKit
//
//  Created by Jacky Jack on 30/06/2024.
//

import Foundation
import Collections

class FlightTracker {
    var last_time_seen: Int = 0
    var ICAOName_avaliable = false
    @Published var ICAOName = ""
    var Position_avaliable = false
    @Published var long:Double = 0.0
    @Published var lat:Double = 0.0
    var FromTo_avaliable = false
    var flightFrom:String = ""
    var flightTo:String = ""
}

class FlightState: ObservableObject {
    var timer: Timer?
    //default location currently for testing
    var fromFile: Bool = false
    @Published var flight:[Int:FlightTracker] = [:]
    
    //configuration options
    let default_file_path = "/Users/jackyjack/Downloads/2024_05_27_raw_adsb.txt"
    let process_per_second = 120
    
    init() {
        var count = 0
        
        //let ADSBtask = ADSBFileRunner(filename: "")
        let adsb_file = ADSBFileRunner(filename: self.default_file_path)
        
        
        DispatchQueue.global(qos: .background).sync {
            print("Open file")
            adsb_file.openFile()
            adsb_file.readFile()
        }
        
        DispatchQueue.global(qos: .background).async {
            print("Start decoding data")
            adsb_file.decode()
            print("Stop decoding data")
        }
        
        //once a second read some data from decoded queue
        timer = Timer.scheduledTimer(
            withTimeInterval: 1,
            repeats: true
        ) { _ in
            //get the 10 entries if there is
            if adsb_file.jobDone() {
                print("Decoding done let get some data \(adsb_file.getCount())")
                //if adsb_file
                if adsb_file.getCount() > self.process_per_second {
                    let data = adsb_file.getPlainData(self.process_per_second)
                    //print(data.getCount())
                    for idx in 0..<data.getCount() {
                        let nextTag = data.getNextTag()
                        if nextTag == DataStreamType.ADSB_ALTITUDE {
                            let _ = data.getAltitude()
#warning("Implement this")
                        } else if (nextTag == DataStreamType.ADSB_ICAO) {
                            let icao = data.getIcaoName()
                            print("Tag icao \(icao) count:\(data.icaoArray.count)")
                            self.addIcaoName(icao.address, icao.ICAOname)
                        } else if (nextTag == DataStreamType.ADSB_LOCATION) {
                            print("tag location")
                            let loc = data.getLocation()
                            self.addLocation(loc.address, loc.lat, loc.long)
                        }
                    }
                    
                } else {
                    print("Data stream is empty")
                }
            }
        }
    }
    
    init(filename: String) {
        #warning("not implemented at all")
    }
    
    func addLocation(_ address: Int, _ lat: Double, _ long: Double) {
        if flight[address] == nil {
            flight[address] = FlightTracker()
            flight[address]?.last_time_seen = Int(Date().timeIntervalSince1970)
            flight[address]?.Position_avaliable = true
            flight[address]?.lat = lat
            flight[address]?.long = long
            print("new location")
            return
        } else {
            if let f = flight[address] {
                f.last_time_seen = Int(Date().timeIntervalSince1970)
                f.Position_avaliable = true
                f.lat = lat
                f.long = long
                print("Update location \(flight.count)")
                return
            }
            
        }
        print("No update?")
    }
    
    func addIcaoName(_ address: Int, _ icaoname: String) {
        if flight[address] == nil {
            flight[address] = FlightTracker()
            flight[address]?.last_time_seen = Int(Date().timeIntervalSince1970)
            flight[address]?.ICAOName_avaliable = true
            flight[address]?.ICAOName = icaoname
            print("new flight name added \(icaoname)")
            return
        } else {
            if let f = flight[address] {
                f.last_time_seen = Int(Date().timeIntervalSince1970)
                if  f.ICAOName_avaliable == false{
                    f.ICAOName_avaliable = true
                    f.ICAOName = icaoname
                    print("flight timestamp updated")
                    return
                }
            }
        }
        print("no update?!")
    }
    
    func addNewFlight() {
        
    }
    
    //loop over and if expired then remove
    func removeExpiredFlight() {
        for (address,el) in self.flight {
            //if on the map more then 1 minute
            if el.last_time_seen+60 < Int(Date().timeIntervalSince1970) {
                self.flight.removeValue(forKey: address)
            }
        }
    }
    
}