From 30a23711626c5afe7f86ca34fbc489d7979b71d9 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 8 Aug 2024 12:25:55 +0100 Subject: Live data works --- Net1090/NetADSBDecoder.swift | 46 +++++++++++++++++++++++++++++++++++++++++--- Net1090/main.swift | 36 ++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 11 deletions(-) (limited to 'Net1090') 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 {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 = [] +} + class NetADSBDecoder { let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) var host: String var port: Int + var msgarray = ADSBQueue() + var handler = NetADSBHandlder() + //var msg_array:Array = [] + 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..