diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-07-02 10:07:49 +0100 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-07-02 10:07:49 +0100 |
commit | cc5272cd4be2ab55fa8411790082be408b586be9 (patch) | |
tree | 8436c54cdeb94e6383607fe0028b09a19790e8d2 /LearnMapKit | |
parent | 58d9d561df35f88884b2959d2cf322f1ee69e3cd (diff) | |
download | ADSBDecoder-cc5272cd4be2ab55fa8411790082be408b586be9.tar.gz ADSBDecoder-cc5272cd4be2ab55fa8411790082be408b586be9.zip |
Make ADSBDecoder more modular. Integrate into LearnMapKit
Diffstat (limited to 'LearnMapKit')
-rw-r--r-- | LearnMapKit/ADSBRunner.swift | 105 | ||||
-rw-r--r-- | LearnMapKit/ContentView.swift | 29 | ||||
-rw-r--r-- | LearnMapKit/FlighState.swift | 45 | ||||
-rw-r--r-- | LearnMapKit/LearnMapKitApp.swift | 45 |
4 files changed, 218 insertions, 6 deletions
diff --git a/LearnMapKit/ADSBRunner.swift b/LearnMapKit/ADSBRunner.swift new file mode 100644 index 0000000..0f09759 --- /dev/null +++ b/LearnMapKit/ADSBRunner.swift @@ -0,0 +1,105 @@ +// +// ADSBRunner.swift +// LearnMapKit +// +// Created by Jacky Jack on 28/06/2024. +// + +import Foundation + +struct ADSBLocation { + let address: Int + let lat: Double + let long: Double + //let alt: Int +} + +struct ADSBAltitude { + let address: Int + let altitude: Int +} + +struct ADSBICAOname { + let address: Int + let ICAOname: String +} + +class ADSBFileRunner { + + var filename: URL + //track all airplanes + var tracker = AirPlaneTracker() + var adsb_source: String = "" + + init(filename:String) { + self.filename = URL(fileURLWithPath:filename) + } + + func openFile() { + print("File location [\(filename.absoluteString)]") + + //check if file excists + if (checkIfFileExists(filename.path) == false) { + print("Supplied path \(filename.path) doesnt exists") + exit(1) + } + } + + func readFile() { + //load the file with adsb data + do { + adsb_source = try String(contentsOfFile: filename.path) + print("Loaded \(adsb_source.count) bytes") + } catch { + print("Couldn't load text from a file \(filename.path)") + exit(1) + } + print("If there anything new in file") + } + + func decode() { + for line in self.adsb_source.components(separatedBy: .newlines) { + var found=false + do { + if let tokenMatch = try matchADSBLong.prefixMatch(in: line) { + //print("\(String(tokenMatch.output))") + found = true + let str = String(tokenMatch.output) + let startIndex = str.index(str.startIndex, offsetBy: 1) + let endIndex = str.index(str.endIndex, offsetBy: -2) + let decoder = Decoder(String(str[startIndex...endIndex])) + if decoder.DataFormat == 17 { + if let d17 = decoder.getDataFormat17() { + if (d17.TypeCode == 4) { + if let indentification = d17.messageIdentification { + tracker.addDF17Indentification(d17.AddressAnnounced, indentification.ICAOName) + } + } else if (d17.TypeCode >= 9 && d17.TypeCode <= 18) { + if let airbornposition = d17.messageAirbornPositon { + tracker.addDF17AirBornPosition( + d17.AddressAnnounced, + airbornposition.Latitude, + airbornposition.Longitude, + airbornposition.Altitude, + airbornposition.CPRFormat == 0 + ) + if let position = tracker.getPosition(d17.AddressAnnounced) { + print("position: \(position)") + } + } + } + } + } + }; + } catch { + print("Error") + } + + if (found == false) { + print("Unknown adsb data line \(line)") + } + } + //try to free the string after decoded + //adsb_source = "" + } +} diff --git a/LearnMapKit/ContentView.swift b/LearnMapKit/ContentView.swift index d9e3377..08f4192 100644 --- a/LearnMapKit/ContentView.swift +++ b/LearnMapKit/ContentView.swift @@ -7,11 +7,15 @@ import SwiftUI import MapKit +import Collections struct ContentView: View { - @State private var region = MKCoordinateRegion() + @State private var isImporting = false + + @Binding var pos_queue: Deque<ADSBLocation> + @EnvironmentObject var evilClass: FlightState let initialPosition: MapCameraPosition = { let center = CLLocationCoordinate2D(latitude: 55.90159, longitude:-3.53154) @@ -49,6 +53,7 @@ struct ContentView: View { } Button("7") { print("Pressed 7") + print(evilClass.update_postions.count) } } .border(.blue) @@ -105,6 +110,22 @@ struct ContentView: View { .border(.green) .layoutPriority(1) .mapStyle(.hybrid(elevation: .realistic)) + .toolbar { + ToolbarItem() { + Button { + isImporting = true + } label: { + Label("Import file", systemImage: "square.and.arrow.down") + } + } + } .fileImporter(isPresented: $isImporting, allowedContentTypes: [.text], allowsMultipleSelection: false) { + result in switch result { + case .success(let files): + print(files) + case .failure(let error): + print(error) + } + } } @@ -114,6 +135,6 @@ struct ContentView: View { } } -#Preview { - ContentView() -} +//#Preview { + //ContentView(pos_queue: pos_queue) +//} diff --git a/LearnMapKit/FlighState.swift b/LearnMapKit/FlighState.swift new file mode 100644 index 0000000..f3895cb --- /dev/null +++ b/LearnMapKit/FlighState.swift @@ -0,0 +1,45 @@ +// +// FlighState.swift +// LearnMapKit +// +// Created by Jacky Jack on 30/06/2024. +// + +import Foundation +import Collections + +class FlightState: ObservableObject { + @Published var name = "Some Name" + @Published var isEnabled = false + var timer: Timer? + var update_postions: Deque<ADSBLocation> = [] + var fromFile: Bool = false + init() { + var count = 0 + + let ADSBtask = ADSBFileRunner(filename: "") + timer = Timer.scheduledTimer( + withTimeInterval: 1, + repeats: true + ) { _ in + + //queue.append(MapADSBData(lat: 0.0, long: 0.0, alt: 1)) + + //let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in + print("Evil object \(count)") + //ADSBtask.runFromFile() + self.update_postions.append(ADSBLocation(address: 0, lat: 0.0, long: 0.0)) + // print(update_postions.count) + count += 1 + //} + } + } + init(filename: String) { + + } + + func loadFromFile() { + + } +} + diff --git a/LearnMapKit/LearnMapKitApp.swift b/LearnMapKit/LearnMapKitApp.swift index eebb4b8..5fa170a 100644 --- a/LearnMapKit/LearnMapKitApp.swift +++ b/LearnMapKit/LearnMapKitApp.swift @@ -6,12 +6,53 @@ // import SwiftUI +import Collections + +//https://www.hackingwithswift.com/quick-start/swiftui/how-to-run-code-when-your-app-launches + + @main struct LearnMapKitApp: App { + + @State var queue: Deque<ADSBLocation> = [] + + @StateObject private var flightState = FlightState() + + init() { + print("Init app") + var update_postions: Deque<ADSBLocation> = [] + DispatchQueue.global(qos: .background).sync { + print("Dispatch") + //var count = 0 + + + //let ADSBtask = ADSBRunner() + //queue.append(MapADSBData(lat: 0.0, long: 0.0, alt: 1)) + /* + let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in + print("Timer fired! \(count)") + ADSBtask.runFromFile() + update_postions.append(MapADSBData(lat: 0.0, long: 0.0, alt: 1)) + print(update_postions.count) + count += 1 + }*/ + print("Exit dispatch") + } + + //push all new data to state queue from a runner + DispatchQueue.global(qos: .background).sync { + // Update the UI on the main thread + let c = queue.count + } + } + + + var body: some Scene { WindowGroup { - ContentView() - } + ContentView(pos_queue: $queue) + }.environmentObject(flightState) } + } |