summaryrefslogtreecommitdiff
path: root/LearnMapKit/FlighState.swift
diff options
context:
space:
mode:
Diffstat (limited to 'LearnMapKit/FlighState.swift')
-rw-r--r--LearnMapKit/FlighState.swift43
1 files changed, 25 insertions, 18 deletions
diff --git a/LearnMapKit/FlighState.swift b/LearnMapKit/FlighState.swift
index 9bb6782..9f26578 100644
--- a/LearnMapKit/FlighState.swift
+++ b/LearnMapKit/FlighState.swift
@@ -13,13 +13,10 @@ class FlightTracker: Identifiable {
var last_time_seen: Int = 0
var ICAOaddress = 0
var ICAOName_avaliable = false
- //@Published var ICAOName = ""
var ICAOName = ""
var Position_avaliable = false
@Published var long:Double = 0.0
- //var long:Double = 0.0
@Published var lat:Double = 0.0
- //var lat:Double = 0.0
var FromTo_avaliable = false
var flightFrom:String = ""
var flightTo:String = ""
@@ -52,14 +49,18 @@ class FlightState: ObservableObject {
@Published var flight:[FlightTracker] = []
//configuration options
- var sourceFile = false
+ var sourceFile = true
var default_file_path = "/Users/jackyjack/Downloads/2024_05_27_raw_adsb.txt"
var process_per_second = 120
- var sourceDump1090Server = true
+ var sourceDump1090Server = false
var dump1090address = "X.X.X.X"
var dump1090port = 12345
+ //flight expiration
+ var staleAfterSeconds: Int = 120
+ var expireAfterSeconds: Int = 300
+
init() {
print("Init")
var count = 0
@@ -185,12 +186,7 @@ class FlightState: ObservableObject {
) { _ in
//print("Timer drain queue")
//print("\(ADSBClient.msgarray.message_array.count)")
- /*if ADSBClient.msgarray.message_array.count > 0 {
- print(ADSBClient.msgarray.message_array.count)
- for i in 0..<ADSBClient.msgarray.message_array.count {
- print(ADSBClient.msgarray.message_array.popLast()!,terminator: "")
- }
- }*/
+ //when there is data drain the queue and update flight states
if ADSBClient.adsb_tag_stream.getCount() > 0 {
//print("Process onse a second")
for _ in 0..<ADSBClient.adsb_tag_stream.getCount() {
@@ -209,6 +205,8 @@ class FlightState: ObservableObject {
}
}
}
+ //if there is no data in queue, do tasks like remove expired flights
+ self.removeExpiredFlight()
}
/*
DispatchQueue.global(qos: .background).async {
@@ -327,14 +325,23 @@ class FlightState: ObservableObject {
//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)
- }
+ let now = Int(Date().timeIntervalSince1970)
+ flight.removeAll { now - $0.last_time_seen > expireAfterSeconds }
+ idx_flight.removeAll(keepingCapacity: true)
+
+ //rebuild index
+ for (index, f) in flight.enumerated() {
+ idx_flight[f.ICAOaddress] = index
}
- */
+ }
+
+ func flightAge(_ ft: FlightTracker) -> Int {
+ let now: Int = Int(Date().timeIntervalSince1970)
+ return max(0, now - ft.last_time_seen)
+ }
+
+ func isStaled(_ ft: FlightTracker) -> Bool {
+ return flightAge(ft) >= self.staleAfterSeconds
}
}