diff options
Diffstat (limited to 'LearnMapKit')
-rw-r--r-- | LearnMapKit/ADSBNetRunner.swift | 12 | ||||
-rw-r--r-- | LearnMapKit/ContentView.swift | 69 | ||||
-rw-r--r-- | LearnMapKit/FlighState.swift | 152 | ||||
-rw-r--r-- | LearnMapKit/LearnMapKitApp.swift | 56 | ||||
-rw-r--r-- | LearnMapKit/NetConfigView.swift | 16 | ||||
-rw-r--r-- | LearnMapKit/NetworkConfigure.swift | 1 |
6 files changed, 217 insertions, 89 deletions
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..<adsb_net_decoder.msgarray.message_array.count-1 { found = false //print(adsb_net_decoder.msgarray.message_array.popLast()!,terminator: "") @@ -64,7 +64,7 @@ class ADSBNetRunner { airbornposition.CPRFormat == 0 ) if let position = self.tracker.getPosition(d17.AddressAnnounced) { - print("position: \(position)") + //print("position: \(position)") self.adsb_tag_stream.addAltitude(d17.AddressAnnounced, self.tracker.getAltitude(d17.AddressAnnounced)!) let location = self.tracker.getPosition(d17.AddressAnnounced)! self.adsb_tag_stream.addLocation(d17.AddressAnnounced, location.0, location.1) @@ -79,7 +79,7 @@ class ADSBNetRunner { } if (found == false) { - print("Unknown adsb data line [\(msg)]") + //print("Unknown adsb data line [\(msg)]") } } } diff --git a/LearnMapKit/ContentView.swift b/LearnMapKit/ContentView.swift index 9f18e30..cbe178f 100644 --- a/LearnMapKit/ContentView.swift +++ b/LearnMapKit/ContentView.swift @@ -8,9 +8,7 @@ import SwiftUI import MapKit import Collections - - - +/* struct FlightView: View { var evilClass: FlightState @@ -34,19 +32,17 @@ struct FlightView: View { } } } -} +}*/ struct ContentView: View { @State private var region = MKCoordinateRegion() @State private var isImporting = false - @Binding var pos_queue: Deque<ADSBLocation> @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<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) } } + */ } - } diff --git a/LearnMapKit/LearnMapKitApp.swift b/LearnMapKit/LearnMapKitApp.swift index 8132c92..b10764d 100644 --- a/LearnMapKit/LearnMapKitApp.swift +++ b/LearnMapKit/LearnMapKitApp.swift @@ -19,29 +19,31 @@ struct CommandLineArgs: ParsableCommand { @Flag(name: .shortAndLong) var version:Bool = false } +//@EnvironmentObject var flightState: FlightState + @main struct LearnMapKitApp: App { var network_mode = false var file_mode = false @State var queue: Deque<ADSBLocation> = [] @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" |