From 9070c9e6b7c7ce5671f4fad85e4aefa8c27aad28 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Wed, 4 Sep 2024 10:24:38 +0100 Subject: Plain tracking UI works much better --- ADSBDecoder.xcodeproj/project.pbxproj | 2 +- ADSBDecoder/AirplaneTracker.swift | 2 +- ADSBDecoder/Configs.swift | 2 +- LearnMapKit/ADSBNetRunner.swift | 12 +-- LearnMapKit/ContentView.swift | 69 ++++++++------- LearnMapKit/FlighState.swift | 152 ++++++++++++++++++++++++++++++---- LearnMapKit/LearnMapKitApp.swift | 56 ++++++------- LearnMapKit/NetConfigView.swift | 16 ++-- LearnMapKit/NetworkConfigure.swift | 1 + Net1090/main.swift | 2 +- 10 files changed, 221 insertions(+), 93 deletions(-) diff --git a/ADSBDecoder.xcodeproj/project.pbxproj b/ADSBDecoder.xcodeproj/project.pbxproj index e185afa..06c5a00 100644 --- a/ADSBDecoder.xcodeproj/project.pbxproj +++ b/ADSBDecoder.xcodeproj/project.pbxproj @@ -301,10 +301,10 @@ 8DEC6F2D2C2E9C3000AC6209 /* ADSBFileRunner.swift */, 8D8480F22C60C7E6007ACD95 /* ADSBNetRunner.swift */, 8D8480F02C60C79E007ACD95 /* ADSBDataQueue.swift */, + 8DF756842C10556A008E8DFF /* LearnMapKitApp.swift */, 8DF756862C10556A008E8DFF /* ContentView.swift */, 8D93B6992C4A665B00C91865 /* NetConfigView.swift */, 8D93B69B2C4A727300C91865 /* NetworkConfigure.swift */, - 8DF756842C10556A008E8DFF /* LearnMapKitApp.swift */, 8DEC6F312C31530C00AC6209 /* FlighState.swift */, 8DF756882C10556C008E8DFF /* Assets.xcassets */, 8DF7568D2C10556C008E8DFF /* LearnMapKit.entitlements */, diff --git a/ADSBDecoder/AirplaneTracker.swift b/ADSBDecoder/AirplaneTracker.swift index 96c4d5f..3c1edcf 100644 --- a/ADSBDecoder/AirplaneTracker.swift +++ b/ADSBDecoder/AirplaneTracker.swift @@ -113,7 +113,7 @@ class AirPlaneTracker { } func printAllICAOnames() { - let extra = true + let extra = false var once=false for (address,plane) in airplanes { if plane.ICAOready { diff --git a/ADSBDecoder/Configs.swift b/ADSBDecoder/Configs.swift index 97ab31c..d0357c9 100644 --- a/ADSBDecoder/Configs.swift +++ b/ADSBDecoder/Configs.swift @@ -7,4 +7,4 @@ import Foundation -var decoder_debug_mode = true +var decoder_debug_mode = false diff --git a/LearnMapKit/ADSBNetRunner.swift b/LearnMapKit/ADSBNetRunner.swift index 7125615..3307c84 100644 --- a/LearnMapKit/ADSBNetRunner.swift +++ b/LearnMapKit/ADSBNetRunner.swift @@ -25,15 +25,15 @@ class ADSBNetRunner { let adsb_net_decoder = NetADSBDecoder(host: self.address, port: self.port) //var _adsb_tag_stream = ADSBDataQueue() //var _tracker = AirPlaneTracker() - print("_start ADSBNetRunner") + //print("_start ADSBNetRunner") timer = Timer.scheduledTimer( withTimeInterval: 1, repeats: true ) { _ in - print("Timer drain queue") - print("\(adsb_net_decoder.msgarray.message_array.count)") + //print("Timer drain queue") + //print("\(adsb_net_decoder.msgarray.message_array.count)") if adsb_net_decoder.msgarray.message_array.count > 0 { - print(adsb_net_decoder.msgarray.message_array.count) + //print(adsb_net_decoder.msgarray.message_array.count) for _ in 0.. @Binding var net_config: NetworkConfigure - @EnvironmentObject var evilClass: FlightState + @EnvironmentObject var flightState: FlightState @Environment(\.openWindow) private var openWindow - let initialPosition: MapCameraPosition = { let center = CLLocationCoordinate2D(latitude: 55.90159, longitude:-3.53154) let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1) @@ -54,44 +50,59 @@ struct ContentView: View { return .region(region) }() - let position1 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.53154) - let position2 = CLLocationCoordinate2D(latitude: 55.99159, longitude:-3.53154) - let position3 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.43154) - let position4 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.63154) + //let position1 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.53154) + //let position2 = CLLocationCoordinate2D(latitude: 55.99159, longitude:-3.53154) + //let position3 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.43154) + //let position4 = CLLocationCoordinate2D(latitude: 55.80159, longitude:-3.63154) var body: some View { VStack { Map(initialPosition: initialPosition) { - ForEach(self.evilClass.flight.sorted(by: { $0.key < $1.key} ), id:\.key) { k in - Annotation("\(k.key)", coordinate: CLLocationCoordinate2D(latitude: self.evilClass.flight[k.key]!.lat, longitude:self.evilClass.flight[k.key]!.long)) { - VStack { - ZStack { - RoundedRectangle(cornerRadius: 10) - .fill(.background) - RoundedRectangle(cornerRadius: 10) - .stroke(.secondary,lineWidth: 5) - Image(systemName:"airplane.circle.fill") - .resizable() - .frame(width:20,height: 20) - } - Text("\(k.value.ICAOName)") + ForEach(flightState.flight) { flight in + Annotation(flight.ICAOName, + coordinate: CLLocationCoordinate2D( + latitude: flight.lat, + longitude:flight.long) + ) { + VStack { + /* + ZStack { + RoundedRectangle(cornerRadius: 10) + .fill(.background) + RoundedRectangle(cornerRadius: 10) + .stroke(.secondary,lineWidth: 5) + //Image(systemName:"airplane.circle.fill") + // .resizable() + // .frame(width:20,height: 20) + Text("\(flight.ICAOName)").font(.body) + }*/ + ZStack { + RoundedRectangle(cornerRadius: 10) + .fill(.background) + RoundedRectangle(cornerRadius: 10) + .stroke(.secondary,lineWidth: 5) + Image(systemName:"airplane.circle.fill") + .resizable() + .frame(width:20,height: 20) } - }.annotationTitles(.hidden) + Text("\(flight.ICAOName)").font(.body) + } + }.annotationTitles(.hidden) } } .padding() .border(.green) .layoutPriority(1) .mapStyle(.hybrid(elevation: .realistic)) - .toolbar { + /*.toolbar { ToolbarItem() { Button { isImporting = true } label: { Label("Import file", systemImage: "square.and.arrow.down") } - } + }*//* ToolbarItem { Button { openWindow(id: "net-config") @@ -99,14 +110,14 @@ struct ContentView: View { Label("Network config", systemImage: "network") } } - } .fileImporter(isPresented: $isImporting, allowedContentTypes: [.text], allowsMultipleSelection: false) { + }*/ /*.fileImporter(isPresented: $isImporting, allowedContentTypes: [.text], allowsMultipleSelection: false) { result in switch result { case .success(let files): print(files) case .failure(let error): print(error) } - } + }*/ } 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) { + 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.. 0 { - print("Process onse a second") - for idx in 0.. = [] @State var netconfig: NetworkConfigure = NetworkConfigure() + @State var new_config: Bool = false + + //@StateObject private var flightState:FlightState = FlightState() + //@State var flightState:FlightState = FlightState() + var flightState:FlightState = FlightState() + //@Published var list_of_plains:[FlightTracker]? + //@StateObject var flightState:FlightState = FlightState() - @StateObject private var flightState:FlightState = FlightState() - var default_hostname = "192.168.4.201" + var default_hostname = "192.168.4.233" var default_port = 30002 var default_input_file = "" + + init() { - print("Init app") - //parse arguments - //let args = CommandLineArgs.parseOrExit() - //flightState.defaultMode() - let args = CommandLineArgs.parseOrExit() - print(args) - print(args.inputfile) - //exit(0) - //if (true)// + print("Init ") if let args = CommandLineArgs.parseNotExit() { if args.hostname != nil { @@ -57,44 +59,34 @@ struct LearnMapKitApp: App { default_input_file = args.inputfile! file_mode = true } - print("Set this") - } - //flightState.run() - - - /*let ADSBClient = NetADSBDecoder(host: default_hostname, port: default_port) - DispatchQueue.global(qos: .background).async { - do { - try ADSBClient.start() - } catch let error { - print("Error: \(error.localizedDescription)") - ADSBClient.stop() - } - }*/ + if network_mode { + netconfig.servername = default_hostname + netconfig.serverport = default_port + } } var body: some Scene { WindowGroup { - ContentView(pos_queue: $queue, net_config: $netconfig).onAppear(perform: { + ContentView(net_config: $netconfig).onAppear(perform: { if network_mode { - print("network mode") + print("Network mode") flightState.networkMode(hostname: self.default_hostname, port: self.default_port) } else if (file_mode){ - print("RUn file mode") + print("Run file mode") flightState.fileMode(filepath: self.default_input_file) } else { print("Run default mode") - flightState.defaultMode() + flightState.defaultMode(hostname: default_hostname, port: default_port) } - flightState.run() + flightState.run() }) }.environmentObject(flightState) - WindowGroup("Network", id: "net-config") { + /*WindowGroup("Network", id: "net-config") { NetConfigView(net_config: $netconfig) - } + }*/ } } diff --git a/LearnMapKit/NetConfigView.swift b/LearnMapKit/NetConfigView.swift index 0216f8a..63bb336 100644 --- a/LearnMapKit/NetConfigView.swift +++ b/LearnMapKit/NetConfigView.swift @@ -8,21 +8,21 @@ import SwiftUI struct NetConfigView: View { - @State private var server_name: String = "" - @State private var server_port: String = "" - @Environment(\.dismissWindow) private var dismissWindow - @Binding var net_config: NetworkConfigure + @State private var server_name: String = "127.0.0.1" + @State private var server_port: String = "8080" + @Environment(\.dismissWindow) private var dismissWindow + var body: some View { VStack { HStack { - Text("Server") - TextField("Server", text: $server_name) + Text("Server") + TextField("Server", text: $server_name) } HStack { - Text("Port") - TextField("Port", text: $server_port) + Text("Port") + TextField("Port", text: $server_port) } HStack { Button(action:{ diff --git a/LearnMapKit/NetworkConfigure.swift b/LearnMapKit/NetworkConfigure.swift index 2baf6ad..c1c8629 100644 --- a/LearnMapKit/NetworkConfigure.swift +++ b/LearnMapKit/NetworkConfigure.swift @@ -6,6 +6,7 @@ // import Foundation +import SwiftUI class NetworkConfigure { var servername: String = "192.168.4.201" diff --git a/Net1090/main.swift b/Net1090/main.swift index 06c2652..b0deb2f 100644 --- a/Net1090/main.swift +++ b/Net1090/main.swift @@ -11,7 +11,7 @@ import NIO var timer: Timer? -let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) +let ADSBClient = NetADSBDecoder(host: "192.168.4.233", port: 30002) print("Hello, World!") print("Start listening client 2") -- cgit v1.2.3