diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-08-08 12:25:55 +0100 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-08-08 12:25:55 +0100 |
commit | 30a23711626c5afe7f86ca34fbc489d7979b71d9 (patch) | |
tree | 73d131c789c9e6a06d00dfd46ef258e41d704db1 | |
parent | 901397ffe83cbf1aed01e6a77e00db57e9440dac (diff) | |
download | ADSBDecoder-30a23711626c5afe7f86ca34fbc489d7979b71d9.tar.gz ADSBDecoder-30a23711626c5afe7f86ca34fbc489d7979b71d9.zip |
Live data works
-rw-r--r-- | ADSBDecoder.xcodeproj/project.pbxproj | 24 | ||||
-rw-r--r-- | ADSBDecoder.xcodeproj/xcuserdata/jackyjack.xcuserdatad/xcschemes/xcschememanagement.plist | 28 | ||||
-rw-r--r-- | LearnMapKit/ADSBDataQueue.swift | 122 | ||||
-rw-r--r-- | LearnMapKit/ADSBFileRunner.swift (renamed from LearnMapKit/ADSBRunner.swift) | 119 | ||||
-rw-r--r-- | LearnMapKit/ADSBNetRunner.swift | 111 | ||||
-rw-r--r-- | LearnMapKit/FlighState.swift | 126 | ||||
-rw-r--r-- | Net1090/NetADSBDecoder.swift | 46 | ||||
-rw-r--r-- | Net1090/main.swift | 36 |
8 files changed, 449 insertions, 163 deletions
diff --git a/ADSBDecoder.xcodeproj/project.pbxproj b/ADSBDecoder.xcodeproj/project.pbxproj index 7790139..0e0c319 100644 --- a/ADSBDecoder.xcodeproj/project.pbxproj +++ b/ADSBDecoder.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 8D8480EF2C56344B007ACD95 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 8D8480EE2C56344B007ACD95 /* ArgumentParser */; }; + 8D8480F12C60C79E007ACD95 /* ADSBDataQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D8480F02C60C79E007ACD95 /* ADSBDataQueue.swift */; }; + 8D8480F32C60C7E6007ACD95 /* ADSBNetRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D8480F22C60C7E6007ACD95 /* ADSBNetRunner.swift */; }; 8D93B64C2C32030B00C91865 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D93B64B2C32030B00C91865 /* Utils.swift */; }; 8D93B64F2C3206DD00C91865 /* AirplaneTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DB347C42C242BAC00F3C020 /* AirplaneTracker.swift */; }; 8D93B6502C3206E700C91865 /* PositionDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DB347BE2C1C501200F3C020 /* PositionDecoder.swift */; }; @@ -37,7 +40,7 @@ 8DB347BF2C1C501200F3C020 /* PositionDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DB347BE2C1C501200F3C020 /* PositionDecoder.swift */; }; 8DB347C32C1C53ED00F3C020 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = 8DB347C22C1C53ED00F3C020 /* Collections */; }; 8DB347C52C242BAC00F3C020 /* AirplaneTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DB347C42C242BAC00F3C020 /* AirplaneTracker.swift */; }; - 8DEC6F2E2C2E9C3000AC6209 /* ADSBRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DEC6F2D2C2E9C3000AC6209 /* ADSBRunner.swift */; }; + 8DEC6F2E2C2E9C3000AC6209 /* ADSBFileRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DEC6F2D2C2E9C3000AC6209 /* ADSBFileRunner.swift */; }; 8DEC6F302C2E9F0E00AC6209 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = 8DEC6F2F2C2E9F0E00AC6209 /* Collections */; }; 8DEC6F322C31530C00AC6209 /* FlighState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DEC6F312C31530C00AC6209 /* FlighState.swift */; }; 8DF756632C0595AA008E8DFF /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DF756622C0595AA008E8DFF /* main.swift */; }; @@ -112,6 +115,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 8D8480F02C60C79E007ACD95 /* ADSBDataQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBDataQueue.swift; sourceTree = "<group>"; }; + 8D8480F22C60C7E6007ACD95 /* ADSBNetRunner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBNetRunner.swift; sourceTree = "<group>"; }; 8D93B64B2C32030B00C91865 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; }; 8D93B6522C32095300C91865 /* ADSBRegEx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBRegEx.swift; sourceTree = "<group>"; }; 8D93B6562C3D27A000C91865 /* Configs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configs.swift; sourceTree = "<group>"; }; @@ -124,7 +129,7 @@ 8D93B69D2C4A788F00C91865 /* NetADSBDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetADSBDecoder.swift; sourceTree = "<group>"; }; 8DB347BE2C1C501200F3C020 /* PositionDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionDecoder.swift; sourceTree = "<group>"; }; 8DB347C42C242BAC00F3C020 /* AirplaneTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirplaneTracker.swift; sourceTree = "<group>"; }; - 8DEC6F2D2C2E9C3000AC6209 /* ADSBRunner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBRunner.swift; sourceTree = "<group>"; }; + 8DEC6F2D2C2E9C3000AC6209 /* ADSBFileRunner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBFileRunner.swift; sourceTree = "<group>"; }; 8DEC6F312C31530C00AC6209 /* FlighState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlighState.swift; sourceTree = "<group>"; }; 8DF7565F2C0595AA008E8DFF /* ADSBDecoder */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ADSBDecoder; sourceTree = BUILT_PRODUCTS_DIR; }; 8DF756622C0595AA008E8DFF /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; }; @@ -183,6 +188,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 8D8480EF2C56344B007ACD95 /* ArgumentParser in Frameworks */, 8DEC6F302C2E9F0E00AC6209 /* Collections in Frameworks */, 8D93B6812C3FEA5600C91865 /* NIO in Frameworks */, ); @@ -292,7 +298,9 @@ 8DF756832C10556A008E8DFF /* LearnMapKit */ = { isa = PBXGroup; children = ( - 8DEC6F2D2C2E9C3000AC6209 /* ADSBRunner.swift */, + 8DEC6F2D2C2E9C3000AC6209 /* ADSBFileRunner.swift */, + 8D8480F22C60C7E6007ACD95 /* ADSBNetRunner.swift */, + 8D8480F02C60C79E007ACD95 /* ADSBDataQueue.swift */, 8DF756862C10556A008E8DFF /* ContentView.swift */, 8D93B6992C4A665B00C91865 /* NetConfigView.swift */, 8DF756842C10556A008E8DFF /* LearnMapKitApp.swift */, @@ -428,6 +436,7 @@ packageProductDependencies = ( 8DEC6F2F2C2E9F0E00AC6209 /* Collections */, 8D93B6802C3FEA5600C91865 /* NIO */, + 8D8480EE2C56344B007ACD95 /* ArgumentParser */, ); productName = LearnMapKit; productReference = 8DF756822C10556A008E8DFF /* LearnMapKit.app */; @@ -617,13 +626,15 @@ 8D93B69C2C4A727300C91865 /* NetworkConfigure.swift in Sources */, 8DF756872C10556A008E8DFF /* ContentView.swift in Sources */, 8D93B6552C32099B00C91865 /* Utils.swift in Sources */, - 8DEC6F2E2C2E9C3000AC6209 /* ADSBRunner.swift in Sources */, + 8DEC6F2E2C2E9C3000AC6209 /* ADSBFileRunner.swift in Sources */, 8D93B69F2C4A7A0700C91865 /* NetADSBDecoder.swift in Sources */, + 8D8480F32C60C7E6007ACD95 /* ADSBNetRunner.swift in Sources */, 8D93B6502C3206E700C91865 /* PositionDecoder.swift in Sources */, 8D93B6582C3D290C00C91865 /* Configs.swift in Sources */, 8D93B6512C3206EB00C91865 /* Decoder.swift in Sources */, 8DEC6F322C31530C00AC6209 /* FlighState.swift in Sources */, 8DF756852C10556A008E8DFF /* LearnMapKitApp.swift in Sources */, + 8D8480F12C60C79E007ACD95 /* ADSBDataQueue.swift in Sources */, 8D93B6542C32095800C91865 /* ADSBRegEx.swift in Sources */, 8D93B69A2C4A665B00C91865 /* NetConfigView.swift in Sources */, ); @@ -1093,6 +1104,11 @@ /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ + 8D8480EE2C56344B007ACD95 /* ArgumentParser */ = { + isa = XCSwiftPackageProductDependency; + package = 8DF7566A2C0595F6008E8DFF /* XCRemoteSwiftPackageReference "swift-argument-parser" */; + productName = ArgumentParser; + }; 8D93B67E2C3FEA0000C91865 /* NIO */ = { isa = XCSwiftPackageProductDependency; package = 8D93B67D2C3FEA0000C91865 /* XCRemoteSwiftPackageReference "swift-nio" */; diff --git a/ADSBDecoder.xcodeproj/xcuserdata/jackyjack.xcuserdatad/xcschemes/xcschememanagement.plist b/ADSBDecoder.xcodeproj/xcuserdata/jackyjack.xcuserdatad/xcschemes/xcschememanagement.plist index decefeb..70a9ecd 100644 --- a/ADSBDecoder.xcodeproj/xcuserdata/jackyjack.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/ADSBDecoder.xcodeproj/xcuserdata/jackyjack.xcuserdatad/xcschemes/xcschememanagement.plist @@ -9,6 +9,34 @@ <key>orderHint</key> <integer>0</integer> </dict> + <key>ADSBStats.xcscheme_^#shared#^_</key> + <dict> + <key>orderHint</key> + <integer>3</integer> + </dict> + <key>LearnMapKit.xcscheme_^#shared#^_</key> + <dict> + <key>orderHint</key> + <integer>1</integer> + </dict> + <key>Net1090.xcscheme_^#shared#^_</key> + <dict> + <key>orderHint</key> + <integer>4</integer> + </dict> + <key>Test.xcscheme_^#shared#^_</key> + <dict> + <key>orderHint</key> + <integer>2</integer> + </dict> + </dict> + <key>SuppressBuildableAutocreation</key> + <dict> + <key>8DF7565E2C0595AA008E8DFF</key> + <dict> + <key>primary</key> + <true/> + </dict> </dict> </dict> </plist> diff --git a/LearnMapKit/ADSBDataQueue.swift b/LearnMapKit/ADSBDataQueue.swift new file mode 100644 index 0000000..ada0ab5 --- /dev/null +++ b/LearnMapKit/ADSBDataQueue.swift @@ -0,0 +1,122 @@ +// +// ADSBDataQueue.swift +// LearnMapKit +// +// Created by Jacky Jack on 05/08/2024. +// + +import Foundation +import Collections + +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 +} + +enum DataStreamType { + case EMPTY + case ADSB_ICAO + case ADSB_LOCATION + case ADSB_ALTITUDE +} + +//get stream of decoded data, to tagged stream so all can be processed in sequence +class ADSBDataQueue { + //var icaoQueue: Deque<ADSBICAOname> = [] + var icaoArray: Array<ADSBICAOname> = [] + var altQueue: Deque<ADSBAltitude> = [] + var locQueue: Deque<ADSBLocation> = [] + //var tagQueue: Deque<> = [] + var tagArray: Array<DataStreamType> = [] + + func getNextTag() -> DataStreamType { + if tagArray.count < 0 { + return DataStreamType.EMPTY + } + return tagArray[tagArray.count-1] + } + + func addIcaoName(_ address: Int, _ icaoname: String) { + tagArray.append(DataStreamType.ADSB_ICAO) + icaoArray.append(ADSBICAOname(address: address, ICAOname: icaoname)) + } + + func addAltitude(_ address: Int, _ altitude: Int) { + tagArray.append(DataStreamType.ADSB_ALTITUDE) + altQueue.append(ADSBAltitude(address: address, altitude: altitude)) + } + + func addLocation(_ address: Int, _ lat: Double, _ long: Double) { + tagArray.append(DataStreamType.ADSB_LOCATION) + locQueue.append(ADSBLocation(address: address, lat: lat, long: long)) + } + + func getIcaoName() -> ADSBICAOname { + if tagArray.count < 1 { + print("ADSB tag Queue is empty") + return ADSBICAOname(address:0,ICAOname: "TEmpty") + } + let tag = tagArray[tagArray.count-1] + if tag != DataStreamType.ADSB_ICAO { + print("ADSB Queue empty") + return ADSBICAOname(address:0,ICAOname: "QEmpty") + } + tagArray.removeLast() + var ret_icao_name = ADSBICAOname(address:0, ICAOname: "Default") + if let last_icao_name = icaoArray.popLast() { + ret_icao_name = last_icao_name + } + return ret_icao_name + } + + func getAltitude() -> ADSBAltitude { + if tagArray.count < 1 { + print("ADSB tag Queue is empty") + return ADSBAltitude(address:0,altitude:0) + } + let tag = tagArray[tagArray.count-1] + if tag != DataStreamType.ADSB_ALTITUDE { + print("ADSB Queue empty") + return ADSBAltitude(address:0,altitude:0) + } + tagArray.removeLast() + return altQueue.popLast()! + } + + func getLocation() -> ADSBLocation { + if tagArray.count < 1 { + print("ADSB tag Queue is empry") + return ADSBLocation(address:0,lat:0.0,long:0.0) + } + let tag = tagArray[tagArray.count-1] + if tag != DataStreamType.ADSB_LOCATION { + print("ADSB Queue empty") + return ADSBLocation(address:0,lat:0.0,long:0.0) + } + tagArray.removeLast() + return locQueue.popLast()! + } + + func haveNum(_ num: Int) -> Bool { + if (tagArray.count > num) { + return true + } + return false + } + + func getCount() -> Int { + return tagArray.count + } +} diff --git a/LearnMapKit/ADSBRunner.swift b/LearnMapKit/ADSBFileRunner.swift index e05978c..d61b91b 100644 --- a/LearnMapKit/ADSBRunner.swift +++ b/LearnMapKit/ADSBFileRunner.swift @@ -8,121 +8,7 @@ import Foundation import Collections -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 -} - -enum DataStreamType { - case EMPTY - case ADSB_ICAO - case ADSB_LOCATION - case ADSB_ALTITUDE -} - -//get stream of decoded data, to tagged stream so all can be processed in sequence -class ADSBDataQueue { - //var icaoQueue: Deque<ADSBICAOname> = [] - var icaoArray: Array<ADSBICAOname> = [] - var altQueue: Deque<ADSBAltitude> = [] - var locQueue: Deque<ADSBLocation> = [] - //var tagQueue: Deque<> = [] - var tagArray: Array<DataStreamType> = [] - - func getNextTag() -> DataStreamType { - if tagArray.count < 0 { - return DataStreamType.EMPTY - } - return tagArray[tagArray.count-1] - } - - func addIcaoName(_ address: Int, _ icaoname: String) { - tagArray.append(DataStreamType.ADSB_ICAO) - icaoArray.append(ADSBICAOname(address: address, ICAOname: icaoname)) - } - - func addAltitude(_ address: Int, _ altitude: Int) { - tagArray.append(DataStreamType.ADSB_ALTITUDE) - altQueue.append(ADSBAltitude(address: address, altitude: altitude)) - } - - func addLocation(_ address: Int, _ lat: Double, _ long: Double) { - tagArray.append(DataStreamType.ADSB_LOCATION) - locQueue.append(ADSBLocation(address: address, lat: lat, long: long)) - } - - func getIcaoName() -> ADSBICAOname { - if tagArray.count < 1 { - print("ADSB tag Queue is empty") - return ADSBICAOname(address:0,ICAOname: "TEmpty") - } - let tag = tagArray[tagArray.count-1] - if tag != DataStreamType.ADSB_ICAO { - print("ADSB Queue empty") - return ADSBICAOname(address:0,ICAOname: "QEmpty") - } - tagArray.removeLast() - var ret_icao_name = ADSBICAOname(address:0, ICAOname: "Default") - if let last_icao_name = icaoArray.popLast() { - ret_icao_name = last_icao_name - } - return ret_icao_name - } - - func getAltitude() -> ADSBAltitude { - if tagArray.count < 1 { - print("ADSB tag Queue is empry") - return ADSBAltitude(address:0,altitude:0) - } - let tag = tagArray[tagArray.count-1] - if tag != DataStreamType.ADSB_ALTITUDE { - print("ADSB Queue empty") - return ADSBAltitude(address:0,altitude:0) - } - tagArray.removeLast() - return altQueue.popLast()! - } - - func getLocation() -> ADSBLocation { - if tagArray.count < 1 { - print("ADSB tag Queue is empry") - return ADSBLocation(address:0,lat:0.0,long:0.0) - } - let tag = tagArray[tagArray.count-1] - if tag != DataStreamType.ADSB_LOCATION { - print("ADSB Queue empty") - return ADSBLocation(address:0,lat:0.0,long:0.0) - } - tagArray.removeLast() - return locQueue.popLast()! - } - - func haveNum(_ num: Int) -> Bool { - if (tagArray.count > num) { - return true - } - return false - } - - func getCount() -> Int { - return tagArray.count - } -} - class ADSBFileRunner { - var filename: URL //track all airplanes var tracker = AirPlaneTracker() @@ -169,7 +55,7 @@ class ADSBFileRunner { print("If there anything new in file") } - func decode() { + func decodeFromFile() { for line in self.adsb_source.components(separatedBy: .newlines) { var found=false do { @@ -201,7 +87,6 @@ class ADSBFileRunner { adsb_tag_stream.addAltitude(d17.AddressAnnounced, tracker.getAltitude(d17.AddressAnnounced)!) let location = tracker.getPosition(d17.AddressAnnounced)! adsb_tag_stream.addLocation(d17.AddressAnnounced, location.0, location.1) - } } } @@ -260,3 +145,5 @@ class ADSBFileRunner { return self.adsb_tag_stream.getCount() } } + + diff --git a/LearnMapKit/ADSBNetRunner.swift b/LearnMapKit/ADSBNetRunner.swift new file mode 100644 index 0000000..7125615 --- /dev/null +++ b/LearnMapKit/ADSBNetRunner.swift @@ -0,0 +1,111 @@ +// +// ADSBNetRunner.swift +// LearnMapKit +// +// Created by Jacky Jack on 05/08/2024. +// + +import Foundation + +class ADSBNetRunner { + var address = "" + var port = 0 + var tracker = AirPlaneTracker() + var adsb_tag_stream = ADSBDataQueue() + var ADSBClient:NetADSBDecoder! + var timer: Timer? + + init(address:String, port:Int) { + self.address = address + self.port = port + } + + func start() { + var found: Bool = false + let adsb_net_decoder = NetADSBDecoder(host: self.address, port: self.port) + //var _adsb_tag_stream = ADSBDataQueue() + //var _tracker = AirPlaneTracker() + print("_start ADSBNetRunner") + timer = Timer.scheduledTimer( + withTimeInterval: 1, + repeats: true + ) { _ in + 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) + for _ in 0..<adsb_net_decoder.msgarray.message_array.count-1 { + found = false + //print(adsb_net_decoder.msgarray.message_array.popLast()!,terminator: "") + if let msg = adsb_net_decoder.msgarray.message_array.popLast() { + //print("msg:[\(msg)]") + do { + if let tokenMatch = try matchADSBLong.prefixMatch(in: msg) { + //print("token output \(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 { + self.tracker.addDF17Indentification(d17.AddressAnnounced, indentification.ICAOName) + self.adsb_tag_stream.addIcaoName(d17.AddressAnnounced, self.tracker.getICAOname(d17.AddressAnnounced)!) + } + } else if (d17.TypeCode >= 9 && d17.TypeCode <= 18) { + if let airbornposition = d17.messageAirbornPositon { + self.tracker.addDF17AirBornPosition( + d17.AddressAnnounced, + airbornposition.Latitude, + airbornposition.Longitude, + airbornposition.Altitude, + airbornposition.CPRFormat == 0 + ) + if let position = self.tracker.getPosition(d17.AddressAnnounced) { + 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) + } + } + } + } + } + }; + } catch { + print("Error") + } + + if (found == false) { + print("Unknown adsb data line [\(msg)]") + } + } + } + } + + } + + DispatchQueue.global(qos: .background).async { + do { + try adsb_net_decoder.start() + } catch let error { + print("Error: \(error.localizedDescription)") + adsb_net_decoder.stop() + } + } + } + + func getDataOut() { + + } + + func stop() { + + } + + func getCount() -> Int { + return self.adsb_tag_stream.getCount() + } +} diff --git a/LearnMapKit/FlighState.swift b/LearnMapKit/FlighState.swift index cfb34f1..446b491 100644 --- a/LearnMapKit/FlighState.swift +++ b/LearnMapKit/FlighState.swift @@ -8,6 +8,7 @@ import Foundation import Collections + class FlightTracker { var last_time_seen: Int = 0 var ICAOName_avaliable = false @@ -27,60 +28,121 @@ 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 + let sourceDump1090Server = true + let dump1090address = "192.168.4.201" + let dump1090port = 30002 + init() { var count = 0 //let ADSBtask = ADSBFileRunner(filename: "") - let adsb_file = ADSBFileRunner(filename: self.default_file_path) + //let adsb_net = ADSBNetRunner(address: dump1090address, port: dump1090port) - 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.decode() - print("Stop decoding data") + 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") + } + } + } } - //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 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() if nextTag == DataStreamType.ADSB_ALTITUDE { - let _ = data.getAltitude() + let _ = ADSBClient.adsb_tag_stream.getAltitude() #warning("Implement this") } else if (nextTag == DataStreamType.ADSB_ICAO) { - let icao = data.getIcaoName() - print("Tag icao \(icao) count:\(data.icaoArray.count)") + 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 = data.getLocation() + let loc = ADSBClient.adsb_tag_stream.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() + } + }*/ + do { + print("Start") + try ADSBClient.start() + } catch let error { + print("Error: \(error.localizedDescription)") + ADSBClient.stop() + } + } } diff --git a/Net1090/NetADSBDecoder.swift b/Net1090/NetADSBDecoder.swift index 065bf91..335a303 100644 --- a/Net1090/NetADSBDecoder.swift +++ b/Net1090/NetADSBDecoder.swift @@ -9,10 +9,16 @@ import Foundation import Network import NIO -class ADSBHandlder: ChannelInboundHandler { +protocol ADSBQueueDelegate: AnyObject { + var message_array:Array<String> {get set} +} + +class NetADSBHandlder: ChannelInboundHandler { typealias InboundIn = ByteBuffer typealias OutboundOut = ByteBuffer + var messageDelegate: ADSBQueueDelegate? + func channelActive(context: ChannelHandlerContext) { print("Channel is active") } @@ -21,7 +27,31 @@ class ADSBHandlder: ChannelInboundHandler { var buffer = unwrapInboundIn(data) let readableBytes = buffer.readableBytes if let received = buffer.readString(length: readableBytes) { - print(received,terminator: "") + //print(received,terminator: "") + /* + if let message_queue = message_queue { + message_queue.append(String(received)) + print("Add new messsge") + }*/ + //if self.message_array != nil { + // message_array?.append(String(received)) + // print("\(message_array?.count)") + //} + if self.messageDelegate != nil { + if received.count == 17 { + let trimmed = received.trimmingCharacters(in: .newlines).lowercased() + messageDelegate?.message_array.append(String(trimmed)) + + } else if received.count == 31 { + let trimmed = received.trimmingCharacters(in: .newlines).lowercased() + messageDelegate?.message_array.append(String(trimmed)) + } else { + for line in received.components(separatedBy: .newlines) { + let trimmed = line.trimmingCharacters(in: .newlines).lowercased() + messageDelegate?.message_array.append(String(trimmed)) + } + } + } } } @@ -31,14 +61,24 @@ class ADSBHandlder: ChannelInboundHandler { } } +class ADSBQueue: ADSBQueueDelegate { + var message_array:Array<String> = [] +} + class NetADSBDecoder { let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) var host: String var port: Int + var msgarray = ADSBQueue() + var handler = NetADSBHandlder() + //var msg_array:Array<String> = [] + init(host: String, port: Int) { self.host = host self.port = port + //regiter delegate + handler.messageDelegate = msgarray } func start() throws { @@ -47,7 +87,7 @@ class NetADSBDecoder { .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .channelInitializer{channel in //channel.pipeline.add(handler: ADSBHandlder()) - channel.pipeline.addHandlers([ADSBHandlder()]) + channel.pipeline.addHandlers([self.handler]) }.connect(host: self.host, port: self.port) .wait() try channel.closeFuture.wait() diff --git a/Net1090/main.swift b/Net1090/main.swift index d142613..06c2652 100644 --- a/Net1090/main.swift +++ b/Net1090/main.swift @@ -10,16 +10,36 @@ import Network import NIO - +var timer: Timer? +let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) print("Hello, World!") -print("Start listening client") +print("Start listening client 2") -let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) -do { - try ADSBClient.start() -} catch let error { - print("Error: \(error.localizedDescription)") - ADSBClient.stop() +//var timer:Timer? +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("adsb-message:\(ADSBClient.msgarray.message_array.popLast()!)") + } + } } +print("start") + +DispatchQueue.global(qos: .background).async { + do { + try ADSBClient.start() + } catch let error { + print("Error: \(error.localizedDescription)") + ADSBClient.stop() + } +} +RunLoop.current.run() +print("end") |