From 901397ffe83cbf1aed01e6a77e00db57e9440dac Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 25 Jul 2024 12:13:51 +0100 Subject: Receiving ADSB stream from dump1090 works in LearnMapKit --- ADSBDecoder.xcodeproj/project.pbxproj | 10 +++++ LearnMapKit/ContentView.swift | 1 + LearnMapKit/LearnMapKit.entitlements | 2 + LearnMapKit/LearnMapKitApp.swift | 27 ++++++++++---- LearnMapKit/NetConfigView.swift | 11 ++++-- LearnMapKit/NetworkConfigure.swift | 13 +++++++ Net1090/NetADSBDecoder.swift | 69 +++++++++++++++++++++++++++++++++++ Net1090/main.swift | 59 +----------------------------- 8 files changed, 124 insertions(+), 68 deletions(-) create mode 100644 LearnMapKit/NetworkConfigure.swift create mode 100644 Net1090/NetADSBDecoder.swift diff --git a/ADSBDecoder.xcodeproj/project.pbxproj b/ADSBDecoder.xcodeproj/project.pbxproj index f8a27db..7790139 100644 --- a/ADSBDecoder.xcodeproj/project.pbxproj +++ b/ADSBDecoder.xcodeproj/project.pbxproj @@ -31,6 +31,9 @@ 8D93B6962C45992F00C91865 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 8D93B6952C45992F00C91865 /* ArgumentParser */; }; 8D93B6982C45993600C91865 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = 8D93B6972C45993600C91865 /* Collections */; }; 8D93B69A2C4A665B00C91865 /* NetConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D93B6992C4A665B00C91865 /* NetConfigView.swift */; }; + 8D93B69C2C4A727300C91865 /* NetworkConfigure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D93B69B2C4A727300C91865 /* NetworkConfigure.swift */; }; + 8D93B69E2C4A788F00C91865 /* NetADSBDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D93B69D2C4A788F00C91865 /* NetADSBDecoder.swift */; }; + 8D93B69F2C4A7A0700C91865 /* NetADSBDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D93B69D2C4A788F00C91865 /* NetADSBDecoder.swift */; }; 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 */; }; @@ -117,6 +120,8 @@ 8D93B6872C45986700C91865 /* ADSBStats */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ADSBStats; sourceTree = BUILT_PRODUCTS_DIR; }; 8D93B6892C45986700C91865 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 8D93B6992C4A665B00C91865 /* NetConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetConfigView.swift; sourceTree = ""; }; + 8D93B69B2C4A727300C91865 /* NetworkConfigure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkConfigure.swift; sourceTree = ""; }; + 8D93B69D2C4A788F00C91865 /* NetADSBDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetADSBDecoder.swift; sourceTree = ""; }; 8DB347BE2C1C501200F3C020 /* PositionDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionDecoder.swift; sourceTree = ""; }; 8DB347C42C242BAC00F3C020 /* AirplaneTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirplaneTracker.swift; sourceTree = ""; }; 8DEC6F2D2C2E9C3000AC6209 /* ADSBRunner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADSBRunner.swift; sourceTree = ""; }; @@ -204,6 +209,7 @@ isa = PBXGroup; children = ( 8D93B6782C3FE6F400C91865 /* main.swift */, + 8D93B69D2C4A788F00C91865 /* NetADSBDecoder.swift */, ); path = Net1090; sourceTree = ""; @@ -294,6 +300,7 @@ 8DF756882C10556C008E8DFF /* Assets.xcassets */, 8DF7568D2C10556C008E8DFF /* LearnMapKit.entitlements */, 8DF7568A2C10556C008E8DFF /* Preview Content */, + 8D93B69B2C4A727300C91865 /* NetworkConfigure.swift */, ); path = LearnMapKit; sourceTree = ""; @@ -557,6 +564,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 8D93B69E2C4A788F00C91865 /* NetADSBDecoder.swift in Sources */, 8D93B6792C3FE6F400C91865 /* main.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -606,9 +614,11 @@ buildActionMask = 2147483647; files = ( 8D93B64F2C3206DD00C91865 /* AirplaneTracker.swift in Sources */, + 8D93B69C2C4A727300C91865 /* NetworkConfigure.swift in Sources */, 8DF756872C10556A008E8DFF /* ContentView.swift in Sources */, 8D93B6552C32099B00C91865 /* Utils.swift in Sources */, 8DEC6F2E2C2E9C3000AC6209 /* ADSBRunner.swift in Sources */, + 8D93B69F2C4A7A0700C91865 /* NetADSBDecoder.swift in Sources */, 8D93B6502C3206E700C91865 /* PositionDecoder.swift in Sources */, 8D93B6582C3D290C00C91865 /* Configs.swift in Sources */, 8D93B6512C3206EB00C91865 /* Decoder.swift in Sources */, diff --git a/LearnMapKit/ContentView.swift b/LearnMapKit/ContentView.swift index 9f7fd29..e85c3b2 100644 --- a/LearnMapKit/ContentView.swift +++ b/LearnMapKit/ContentView.swift @@ -42,6 +42,7 @@ struct ContentView: View { @State private var isImporting = false @Binding var pos_queue: Deque + @Binding var net_config: NetworkConfigure @EnvironmentObject var evilClass: FlightState @Environment(\.openWindow) private var openWindow diff --git a/LearnMapKit/LearnMapKit.entitlements b/LearnMapKit/LearnMapKit.entitlements index 61b4b47..ca15375 100644 --- a/LearnMapKit/LearnMapKit.entitlements +++ b/LearnMapKit/LearnMapKit.entitlements @@ -8,5 +8,7 @@ com.apple.security.files.user-selected.read-only + com.apple.security.network.client + diff --git a/LearnMapKit/LearnMapKitApp.swift b/LearnMapKit/LearnMapKitApp.swift index 6c46f3b..62a902f 100644 --- a/LearnMapKit/LearnMapKitApp.swift +++ b/LearnMapKit/LearnMapKitApp.swift @@ -15,26 +15,39 @@ import ArgumentParser @main struct LearnMapKitApp: App { - - - + @State var queue: Deque = [] + @State var netconfig: NetworkConfigure = NetworkConfigure() @StateObject private var flightState = FlightState() init() { + print("Init app") + let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) + /*do { + try ADSBClient.start() + } catch let error { + print("Error: \(error.localizedDescription)") + ADSBClient.stop() + }*/ + DispatchQueue.global(qos: .background).async { + do { + try ADSBClient.start() + } catch let error { + print("Error: \(error.localizedDescription)") + ADSBClient.stop() + } + } } - - var body: some Scene { WindowGroup { - ContentView(pos_queue: $queue) + ContentView(pos_queue: $queue, net_config: $netconfig) }.environmentObject(flightState) WindowGroup("Network", id: "net-config") { - NetConfigView() + NetConfigView(net_config: $netconfig) } } diff --git a/LearnMapKit/NetConfigView.swift b/LearnMapKit/NetConfigView.swift index 06fc5c2..0216f8a 100644 --- a/LearnMapKit/NetConfigView.swift +++ b/LearnMapKit/NetConfigView.swift @@ -12,6 +12,8 @@ struct NetConfigView: View { @State private var server_port: String = "" @Environment(\.dismissWindow) private var dismissWindow + @Binding var net_config: NetworkConfigure + var body: some View { VStack { HStack { @@ -31,6 +33,9 @@ struct NetConfigView: View { } Button(action: { print("Save config") + net_config.servername = server_name + // is there better way? + net_config.serverport = Int(server_port)! dismissWindow(id:"net-config") }) { Text("Save") @@ -40,6 +45,6 @@ struct NetConfigView: View { } } -#Preview { - NetConfigView() -} +//#Preview { + //NetConfigView(net_config: net_config) +//} diff --git a/LearnMapKit/NetworkConfigure.swift b/LearnMapKit/NetworkConfigure.swift new file mode 100644 index 0000000..a566f8b --- /dev/null +++ b/LearnMapKit/NetworkConfigure.swift @@ -0,0 +1,13 @@ +// +// File.swift +// LearnMapKit +// +// Created by Jacky Jack on 19/07/2024. +// + +import Foundation + +class NetworkConfigure { + var servername: String = "192.168.4.201" + var serverport: Int = 30002 +} diff --git a/Net1090/NetADSBDecoder.swift b/Net1090/NetADSBDecoder.swift new file mode 100644 index 0000000..065bf91 --- /dev/null +++ b/Net1090/NetADSBDecoder.swift @@ -0,0 +1,69 @@ +// +// NetADSBDecoder.swift +// Net1090 +// +// Created by Jacky Jack on 19/07/2024. +// + +import Foundation +import Network +import NIO + +class ADSBHandlder: ChannelInboundHandler { + typealias InboundIn = ByteBuffer + typealias OutboundOut = ByteBuffer + + func channelActive(context: ChannelHandlerContext) { + print("Channel is active") + } + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + var buffer = unwrapInboundIn(data) + let readableBytes = buffer.readableBytes + if let received = buffer.readString(length: readableBytes) { + print(received,terminator: "") + } + } + + func errorCaught(context: ChannelHandlerContext, error: any Error) { + print("error: \(error.localizedDescription)") + context.close(promise: nil) + } +} + +class NetADSBDecoder { + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + var host: String + var port: Int + + init(host: String, port: Int) { + self.host = host + self.port = port + } + + func start() throws { + do { + let channel = try ClientBootstrap(group: group) + .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .channelInitializer{channel in + //channel.pipeline.add(handler: ADSBHandlder()) + channel.pipeline.addHandlers([ADSBHandlder()]) + }.connect(host: self.host, port: self.port) + .wait() + try channel.closeFuture.wait() + } catch let error { + print(error) + throw error + } + } + + func stop() { + do { + try group.syncShutdownGracefully() + } catch let error { + print("Error shutting down \(error.localizedDescription)") + exit(0) + } + print("Connection closed") + } +} diff --git a/Net1090/main.swift b/Net1090/main.swift index 04d0384..d142613 100644 --- a/Net1090/main.swift +++ b/Net1090/main.swift @@ -10,69 +10,12 @@ import Network import NIO -class ADSBHandlder: ChannelInboundHandler { - typealias InboundIn = ByteBuffer - typealias OutboundOut = ByteBuffer - - func channelActive(context: ChannelHandlerContext) { - print("Channel is active") - } - - func channelRead(context: ChannelHandlerContext, data: NIOAny) { - var buffer = unwrapInboundIn(data) - let readableBytes = buffer.readableBytes - if let received = buffer.readString(length: readableBytes) { - print(received,terminator: "") - } - } - - func errorCaught(context: ChannelHandlerContext, error: any Error) { - print("error: \(error.localizedDescription)") - context.close(promise: nil) - } -} -class TCPClient { - let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - var host: String - var port: Int - - init(host: String, port: Int) { - self.host = host - self.port = port - } - - func start() throws { - do { - let channel = try ClientBootstrap(group: group) - .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .channelInitializer{channel in - //channel.pipeline.add(handler: ADSBHandlder()) - channel.pipeline.addHandlers([ADSBHandlder()]) - }.connect(host: self.host, port: self.port) - .wait() - try channel.closeFuture.wait() - } catch let error { - print(error) - throw error - } - } - - func stop() { - do { - try group.syncShutdownGracefully() - } catch let error { - print("Error shutting down \(error.localizedDescription)") - exit(0) - } - print("Connection closed") - } -} print("Hello, World!") print("Start listening client") -let ADSBClient = TCPClient(host: "192.168.4.201", port: 30002) +let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) do { try ADSBClient.start() } catch let error { -- cgit v1.2.3