summaryrefslogtreecommitdiff
path: root/LearnMapKit/FlighState.swift
diff options
context:
space:
mode:
Diffstat (limited to 'LearnMapKit/FlighState.swift')
-rw-r--r--LearnMapKit/FlighState.swift221
1 files changed, 134 insertions, 87 deletions
diff --git a/LearnMapKit/FlighState.swift b/LearnMapKit/FlighState.swift
index 446b491..9f42369 100644
--- a/LearnMapKit/FlighState.swift
+++ b/LearnMapKit/FlighState.swift
@@ -28,126 +28,173 @@ class FlightState: ObservableObject {
@Published var flight:[Int:FlightTracker] = [:]
//configuration options
- let sourceFile = false
- let default_file_path = "/Users/jackyjack/Downloads/2024_05_27_raw_adsb.txt"
- let process_per_second = 120
+ var sourceFile = false
+ var default_file_path = "/Users/jackyjack/Downloads/2024_05_27_raw_adsb.txt"
+ var process_per_second = 120
- let sourceDump1090Server = true
- let dump1090address = "192.168.4.201"
- let dump1090port = 30002
+ var sourceDump1090Server = true
+ var dump1090address = "192.168.4.201"
+ var dump1090port = 30002
init() {
+ print("Init")
var count = 0
//let ADSBtask = ADSBFileRunner(filename: "")
//let adsb_net = ADSBNetRunner(address: dump1090address, port: dump1090port)
- if sourceFile {
- 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.decodeFromFile()
- 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")
- }
- }
- }
+ //if sourceFile {
+ // startSourceFile()
+ //}
+
+ //if sourceDump1090Server {
+ // startDump1090()
+ //}
+ }
+
+ init(filename: String) {
+ fileMode(filepath: filename)
+ startSourceFile()
+ }
+
+ init(hostname: String, port: Int) {
+ networkMode(hostname: hostname, port: port)
+ startDump1090()
+ }
+
+ func networkMode(hostname: String, port: Int) {
+ print("network mode")
+ self.dump1090address = hostname
+ self.dump1090port = port
+ self.sourceDump1090Server = true
+ self.sourceFile = false
+ }
+
+ func fileMode(filepath: String) {
+ print("file mode")
+ self.default_file_path = filepath
+ self.sourceFile = true
+ self.sourceDump1090Server = false
+ }
+
+ func defaultMode() {
+ print("default mode")
+ self.sourceDump1090Server = true
+ self.sourceFile = false
+ }
+
+ func startSourceFile() {
+ print("Start reading ADSB messages from \(self.default_file_path)")
+ let adsb_file = ADSBFileRunner(filename: self.default_file_path)
+ DispatchQueue.global(qos: .background).sync {
+ print("Open file")
+ adsb_file.openFile()
+ adsb_file.readFile()
}
- if sourceDump1090Server {
- //let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002)
- let ADSBClient = ADSBNetRunner(address: "192.168.4.201", port: 30002)
- timer = Timer.scheduledTimer(
- withTimeInterval: 1,
- repeats: true
- ) { _ 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: "")
- }
- }*/
- if ADSBClient.adsb_tag_stream.getCount() > 0 {
- print("Process onse a second")
- for idx in 0..<ADSBClient.adsb_tag_stream.getCount() {
- let nextTag = ADSBClient.adsb_tag_stream.getNextTag()
+ DispatchQueue.global(qos: .background).async {
+ print("Start decoding data")
+ adsb_file.decodeFromFile()
+ 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 _ = ADSBClient.adsb_tag_stream.getAltitude()
+ let _ = data.getAltitude()
#warning("Implement this")
} else if (nextTag == DataStreamType.ADSB_ICAO) {
- let icao = ADSBClient.adsb_tag_stream.getIcaoName()
- print("Tag icao \(icao) count:\(ADSBClient.adsb_tag_stream.icaoArray.count)")
+ 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 = ADSBClient.adsb_tag_stream.getLocation()
+ let loc = data.getLocation()
self.addLocation(loc.address, loc.lat, loc.long)
}
}
+
+ } else {
+ print("Data stream is empty")
}
}
- /*
- DispatchQueue.global(qos: .background).async {
- do {
- try ADSBClient.start()
- } catch let error {
- print("Error: \(error.localizedDescription)")
- ADSBClient.stop()
+ }
+ }
+
+ func startDump1090() {
+ print("Start reading ADSB messages from \(dump1090address):\(dump1090port)")
+ //let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002)
+ //let ADSBClient = ADSBNetRunner(address: "192.168.4.201", port: 30002)
+ let ADSBClient = ADSBNetRunner(address: dump1090address, port: dump1090port)
+ timer = Timer.scheduledTimer(
+ withTimeInterval: 1,
+ repeats: true
+ ) { _ 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: "")
}
}*/
+ if ADSBClient.adsb_tag_stream.getCount() > 0 {
+ print("Process onse a second")
+ for idx 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()
+#warning("Implement this")
+ } else if (nextTag == DataStreamType.ADSB_ICAO) {
+ let icao = ADSBClient.adsb_tag_stream.getIcaoName()
+ 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")
+ let loc = ADSBClient.adsb_tag_stream.getLocation()
+ self.addLocation(loc.address, loc.lat, loc.long)
+ }
+ }
+ }
+ }
+ /*
+ DispatchQueue.global(qos: .background).async {
do {
- print("Start")
try ADSBClient.start()
} catch let error {
print("Error: \(error.localizedDescription)")
ADSBClient.stop()
}
-
+ }*/
+ do {
+ print("Start")
+ try ADSBClient.start()
+ } catch let error {
+ print("Error: \(error.localizedDescription)")
+ ADSBClient.stop()
}
}
- init(filename: String) {
- #warning("not implemented at all")
+ func run() {
+ print("run")
+ if self.sourceFile {
+ startSourceFile()
+ } else if self.sourceDump1090Server {
+ startDump1090()
+ }
}
func addLocation(_ address: Int, _ lat: Double, _ long: Double) {