summaryrefslogtreecommitdiff
path: root/LearnMapKit/FlighState.swift
diff options
context:
space:
mode:
Diffstat (limited to 'LearnMapKit/FlighState.swift')
-rw-r--r--LearnMapKit/FlighState.swift152
1 files changed, 138 insertions, 14 deletions
diff --git a/LearnMapKit/FlighState.swift b/LearnMapKit/FlighState.swift
index 9f42369..a86a660 100644
--- a/LearnMapKit/FlighState.swift
+++ b/LearnMapKit/FlighState.swift
@@ -7,25 +7,47 @@
import Foundation
import Collections
+import SwiftUI
-
-class FlightTracker {
+class FlightTracker: Identifiable {
var last_time_seen: Int = 0
+ var ICAOaddress = 0
var ICAOName_avaliable = false
- @Published var ICAOName = ""
+ //@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 = ""
+ var id: String { String(ICAOaddress) }
+
+
+ enum CodingKeys: String, CodingKey {
+ case ICAOName
+ case long
+ case lat
+ }
+
+ func encode(to encoder: any Encoder) throws {
+ var container = encoder.container(keyedBy: CodingKeys.self)
+ try container.encode(self.ICAOName, forKey: .ICAOName)
+ try container.encode(self.long, forKey: .long)
+ try container.encode(self.lat, forKey: .lat)
+ }
}
+
+
class FlightState: ObservableObject {
var timer: Timer?
//default location currently for testing
var fromFile: Bool = false
- @Published var flight:[Int:FlightTracker] = [:]
+ var idx_flight:[Int:Int] = [:]
+ @Published var flight:[FlightTracker] = []
//configuration options
var sourceFile = false
@@ -33,7 +55,7 @@ class FlightState: ObservableObject {
var process_per_second = 120
var sourceDump1090Server = true
- var dump1090address = "192.168.4.201"
+ var dump1090address = "192.168.4.233"
var dump1090port = 30002
init() {
@@ -71,6 +93,14 @@ class FlightState: ObservableObject {
self.sourceFile = false
}
+ func networkMode(netconfig: Binding<NetworkConfigure>) {
+ print("network mode")
+ self.dump1090address = netconfig.servername.wrappedValue
+ self.dump1090port = netconfig.serverport.wrappedValue
+ self.sourceDump1090Server = true
+ self.sourceFile = false
+ }
+
func fileMode(filepath: String) {
print("file mode")
self.default_file_path = filepath
@@ -84,6 +114,14 @@ class FlightState: ObservableObject {
self.sourceFile = false
}
+ func defaultMode(hostname: String, port: Int) {
+ print("default mode net")
+ self.sourceDump1090Server = true
+ self.dump1090address = hostname
+ self.dump1090port = port
+ self.sourceFile = false
+ }
+
func startSourceFile() {
print("Start reading ADSB messages from \(self.default_file_path)")
let adsb_file = ADSBFileRunner(filename: self.default_file_path)
@@ -111,7 +149,7 @@ class FlightState: ObservableObject {
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() {
+ for _ in 0..<data.getCount() {
let nextTag = data.getNextTag()
if nextTag == DataStreamType.ADSB_ALTITUDE {
let _ = data.getAltitude()
@@ -152,8 +190,8 @@ class FlightState: ObservableObject {
}
}*/
if ADSBClient.adsb_tag_stream.getCount() > 0 {
- print("Process onse a second")
- for idx in 0..<ADSBClient.adsb_tag_stream.getCount() {
+ //print("Process onse a second")
+ for _ in 0..<ADSBClient.adsb_tag_stream.getCount() {
let nextTag = ADSBClient.adsb_tag_stream.getNextTag()
if nextTag == DataStreamType.ADSB_ALTITUDE {
let _ = ADSBClient.adsb_tag_stream.getAltitude()
@@ -163,7 +201,7 @@ class FlightState: ObservableObject {
print("Tag icao \(icao) count:\(ADSBClient.adsb_tag_stream.icaoArray.count)")
self.addIcaoName(icao.address, icao.ICAOname)
} else if (nextTag == DataStreamType.ADSB_LOCATION) {
- print("tag location")
+ print("Tag location")
let loc = ADSBClient.adsb_tag_stream.getLocation()
self.addLocation(loc.address, loc.lat, loc.long)
}
@@ -198,6 +236,8 @@ class FlightState: ObservableObject {
}
func addLocation(_ address: Int, _ lat: Double, _ long: Double) {
+ /*
+ //usign as dictionary
if flight[address] == nil {
flight[address] = FlightTracker()
flight[address]?.last_time_seen = Int(Date().timeIntervalSince1970)
@@ -212,15 +252,77 @@ class FlightState: ObservableObject {
f.Position_avaliable = true
f.lat = lat
f.long = long
- print("Update location \(flight.count)")
+ if f.ICAOName_avaliable {
+ print("Update location name: \(f.ICAOName) lat:\(f.lat) long:\(f.long)")
+ } else {
+ print("Update location addr: \(address) lat:\(f.lat) long:\(f.long)")
+ }
return
}
+ }*/
+ //using as array
+ /*
+ if self.idx_flight[address] == nil {
+ let f = FlightTracker()
+ f.ICAOaddress = address
+ f.last_time_seen = Int(Date().timeIntervalSince1970)
+ f.Position_avaliable = true
+ f.lat = lat
+ f.long = long
+ flight.append(f)
+ print(idx_flight[address])
+ idx_flight[address] = flight.count-1
+ print("add new location \(address) ")
+ } else {
+ if let idx = idx_flight[address] {
+ let f = flight[idx]
+ f.last_time_seen = Int(Date().timeIntervalSince1970)
+ f.Position_avaliable = true
+ f.lat = lat
+ f.long = long
+ if f.ICAOName_avaliable {
+ print("Update location name: \(f.ICAOName) lat:\(f.lat) long:\(f.long)")
+ } else {
+ print("Update location addr: \(address) lat:\(f.lat) long:\(f.long)")
+ }
+ return
+ }
+ }*/
+ if self.idx_flight[address] == nil {
+ let f = FlightTracker()
+ f.ICAOaddress = address
+ f.last_time_seen = Int(Date().timeIntervalSince1970)
+ f.Position_avaliable = true
+ f.lat = lat
+ f.long = long
+ flight.append(f)
+ //print(idx_flight[address])
+ idx_flight[address] = flight.count-1
+ print("add new location \(address) ")
+ } else {
+ if let idx = idx_flight[address] {
+ print("Flights loc \(flight.count)")
+ let f = flight[idx]
+ f.last_time_seen = Int(Date().timeIntervalSince1970)
+ f.Position_avaliable = true
+ f.lat = lat
+ f.long = long
+ if f.ICAOName_avaliable {
+ print("Update location name: \(f.ICAOName) lat:\(f.lat) long:\(f.long)")
+ } else {
+ print("Update location addr: \(address) lat:\(f.lat) long:\(f.long)")
+ }
+ flight.append(f)
+ return
+ }
}
- print("No update?")
+ //using as List
+ //print("No update?")
}
func addIcaoName(_ address: Int, _ icaoname: String) {
+ /*
if flight[address] == nil {
flight[address] = FlightTracker()
flight[address]?.last_time_seen = Int(Date().timeIntervalSince1970)
@@ -231,15 +333,36 @@ class FlightState: ObservableObject {
} else {
if let f = flight[address] {
f.last_time_seen = Int(Date().timeIntervalSince1970)
- if f.ICAOName_avaliable == false{
+ if f.ICAOName_avaliable == false {
f.ICAOName_avaliable = true
f.ICAOName = icaoname
print("flight timestamp updated")
return
}
}
+ }*/
+ if idx_flight[address] == nil {
+ let f = FlightTracker()
+ f.ICAOaddress = address
+ f.last_time_seen = Int(Date().timeIntervalSince1970)
+ f.ICAOName_avaliable = true
+ f.ICAOName = icaoname
+ flight.append(f)
+ idx_flight[address] = flight.count-1
+ } else {
+ if let idx = idx_flight[address] {
+ print("Flights \(flight.count)")
+ //let f = flight[idx]
+ /*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?!")
+ print("icao name")
}
func addNewFlight() {
@@ -248,13 +371,14 @@ 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)
}
}
+ */
}
-
}