summaryrefslogtreecommitdiff
path: root/ADSBDecoderFramework
diff options
context:
space:
mode:
Diffstat (limited to 'ADSBDecoderFramework')
-rw-r--r--ADSBDecoderFramework/ADSBRegEx.swift12
-rw-r--r--ADSBDecoderFramework/AirplaneTracker.swift120
-rw-r--r--ADSBDecoderFramework/Decoder.swift52
-rw-r--r--ADSBDecoderFramework/PositionDecoder.swift52
-rw-r--r--ADSBDecoderFramework/Query.swift68
5 files changed, 222 insertions, 82 deletions
diff --git a/ADSBDecoderFramework/ADSBRegEx.swift b/ADSBDecoderFramework/ADSBRegEx.swift
index 2841467..4e554c0 100644
--- a/ADSBDecoderFramework/ADSBRegEx.swift
+++ b/ADSBDecoderFramework/ADSBRegEx.swift
@@ -21,7 +21,9 @@ let matchADSBLong = Regex {
";"
}*/
-class MatchADSBLong {
+public class MatchADSBLong {
+ public init() {}
+
let matchADSBLong = Regex {
Anchor.startOfLine
"*"
@@ -36,7 +38,7 @@ class MatchADSBLong {
";"
}
- func prefixMatch(_ line: String) -> Regex<Regex<Substring>.RegexOutput>.Match? {
+ public func prefixMatch(_ line: String) -> Regex<Regex<Substring>.RegexOutput>.Match? {
do {
if let match = try matchADSBLong.prefixMatch(in: line) {
return match
@@ -61,7 +63,9 @@ let matchADSBShort = Regex {
";"
}*/
-class MatchADSBShort {
+public class MatchADSBShort {
+ public init() {}
+
let matchADSBShort = Regex {
Anchor.startOfLine
"*"
@@ -74,7 +78,7 @@ class MatchADSBShort {
";"
}
- func prefixMatch(_ line: String) -> Regex<Regex<Substring>.RegexOutput>.Match? {
+ public func prefixMatch(_ line: String) -> Regex<Regex<Substring>.RegexOutput>.Match? {
do {
if let match = try matchADSBShort.prefixMatch(in: line) {
return match
diff --git a/ADSBDecoderFramework/AirplaneTracker.swift b/ADSBDecoderFramework/AirplaneTracker.swift
new file mode 100644
index 0000000..6c09953
--- /dev/null
+++ b/ADSBDecoderFramework/AirplaneTracker.swift
@@ -0,0 +1,120 @@
+//
+// AirplaneTracker.swift
+// ADSBDecoder
+//
+// Created by Jacky Jack on 20/06/2024.
+//
+
+import Foundation
+
+struct Airplane {
+ var addressReady:Bool = false
+ var Address:Int
+ var ICAOready:Bool = false
+ var ICAOname:String = ""
+ var locationReady:Bool = false
+ var lat:Double = 0.0
+ var long:Double = 0.0
+ var altitudeReady:Bool = false
+ var altitude:Int = 0
+ var altitudeCount:Int = 0
+ var positionDecoder:PositionDecoder = PositionDecoder()
+}
+
+public class AirPlaneTracker {
+
+ var airplanes:[Int:Airplane] = [:]
+
+ public init () {
+
+ }
+
+ public func addDF17Indentification(_ address: Int, _ ICAOname: String) {
+ if (airplanes[address] == nil) {
+ airplanes[address] = Airplane(addressReady: true, Address: address,ICAOready: true, ICAOname: ICAOname)
+
+ } else {
+ if (airplanes[address]?.ICAOname == "") {
+ airplanes[address]?.ICAOname = ICAOname
+ airplanes[address]?.ICAOready = true
+ }
+ }
+ }
+
+ public func addDF17AirBornPosition(_ address: Int, _ cpr_lat: Int, _ cpr_long: Int, _ alt: Int, _ even: Bool) {
+
+ if airplanes[address] == nil {
+ return
+ }
+
+ //deal with altitude
+ airplanes[address]?.altitude = alt
+ airplanes[address]?.altitudeCount += 1
+ airplanes[address]?.altitudeReady = true
+ //do the airborn position
+ if (even) {
+ airplanes[address]?.positionDecoder.addEvenPosition(UInt32(cpr_lat), UInt32(cpr_long), mstime())
+ } else {
+
+ airplanes[address]?.positionDecoder.addOddPosition(UInt32(cpr_lat), UInt32(cpr_long), mstime())
+ }
+
+ }
+
+ public func getPosition(_ address: Int) -> (Double,Double)? {
+ if (airplanes[address] == nil) {
+ return nil
+ }
+
+ if let airplane = airplanes[address] {
+ if (airplane.positionDecoder.calcPosition()) {
+ return airplane.positionDecoder.getPosition()
+ }
+ }
+
+ return nil
+ }
+
+ public func getAltitude(_ address: Int) -> Int? {
+ if (airplanes[address] == nil) {
+ return nil
+ }
+ if let airplane = airplanes[address] {
+ if airplane.altitudeReady {
+ return airplane.altitude
+ } else {
+ return nil
+ }
+ } else {
+ return nil
+ }
+ }
+
+ public func getICAOname(_ address: Int) -> String? {
+ if (airplanes[address] == nil) {
+ return nil
+ }
+ if let airplane = airplanes[address] {
+ if airplane.ICAOready {
+ return airplane.ICAOname
+ } else {
+ return nil
+ }
+ } else {
+ return nil
+ }
+ }
+
+ public func printAllICAOnames() {
+ let extra = false
+ for (address,plane) in airplanes {
+ if plane.ICAOready {
+ print(String("\(plane.ICAOname) "), terminator: "")
+ }
+ if (extra) {
+ print("Alitude \(plane.altitudeCount)")
+ }
+ }
+ }
+
+}
diff --git a/ADSBDecoderFramework/Decoder.swift b/ADSBDecoderFramework/Decoder.swift
index a9724b6..17fce52 100644
--- a/ADSBDecoderFramework/Decoder.swift
+++ b/ADSBDecoderFramework/Decoder.swift
@@ -20,11 +20,11 @@ func BarometricAltitudeFeat(_ altitude: UInt16) -> Int {
return Int(altitude)*100-1000
}
-class Decoder {
+public class Decoder {
var adsb_data: String = ""
- var DataFormat: UInt32 = 0;
-
- init (_ adsb_data: String) {
+ public var DataFormat: UInt32 = 0;
+
+ public init (_ adsb_data: String) {
//print(adsb_data)
self.adsb_data = adsb_data
//get the first 8 bits as integer
@@ -41,7 +41,7 @@ class Decoder {
//print("Data Format \(DataFormat)")
}
- func getDataFormat17() -> DataFormat17? {
+ public func getDataFormat17() -> DataFormat17? {
if (DataFormat != 17) {
return nil
@@ -77,12 +77,12 @@ func ICAO2String(_ b1: UInt8, _ b2: UInt8, _ b3: UInt8, _ b4: UInt8, _ b5: UInt8
return ICAOAlphabet(b1)+ICAOAlphabet(b2)+ICAOAlphabet(b3)+ICAOAlphabet(b4)+ICAOAlphabet(b5)+ICAOAlphabet(b6)+ICAOAlphabet(b7)+ICAOAlphabet(b8)
}
-class DataFormat17 {
-
+public class DataFormat17 {
+
let DataFormat=17 //0:5
var Capability=0 //5:7
- var AddressAnnounced=0 //8:31
- var TypeCode=0 //32:36
+ public var AddressAnnounced=0 //8:31
+ public var TypeCode=0 //32:36
var MovementField=0//37:43
var HeadingBit=0//44
var HeadingField=0//45:51
@@ -92,10 +92,10 @@ class DataFormat17 {
var ParityIntegrity=0//88-111
//all avaliable message types
- var messageIdentification:ADSBTypeCodeIndentification? = nil
- var messageAirbornPositon:ADSBTypeCodeAirbonePositon? = nil
-
- init(_ adsb_data: String) {
+ public var messageIdentification:ADSBTypeCodeIndentification? = nil
+ public var messageAirbornPositon:ADSBTypeCodeAirbonePositon? = nil
+
+ public init(_ adsb_data: String) {
//print("Dataformat: 17!")
//print(adsb_data)
@@ -167,12 +167,12 @@ class DataFormat17 {
}
}
-class ADSBTypeCodeIndentification: CustomStringConvertible {
+public class ADSBTypeCodeIndentification: CustomStringConvertible {
var TypeCode:Int = 4
var Category:Int = 0
- var ICAOName:String
-
- init(_ bindata:ArraySlice<UInt8>) {
+ public var ICAOName:String
+
+ public init(_ bindata:ArraySlice<UInt8>) {
let cat = bindata[4]&0x7
Category = Int(cat)
let char_0 = bindata[5]>>2
@@ -189,24 +189,24 @@ class ADSBTypeCodeIndentification: CustomStringConvertible {
//print("ICAO name \(ICAOName)")
}
- var description: String {
+ public var description: String {
let description = "TypeCode \(TypeCode) Cat \(Category) Flight name \(ICAOName)"
return description
}
}
//DF17 message types codes 8 to 18
-class ADSBTypeCodeAirbonePositon:CustomStringConvertible {
+public class ADSBTypeCodeAirbonePositon:CustomStringConvertible {
var TypeCode:Int = 11
var SurveillanceStatus: Int = 0
var SingleAntennaFlag: Int = 0
- var Altitude: Int = 0
+ public var Altitude: Int = 0
var Time: Int = 0
- var CPRFormat: Int = 0
- var Latitude: Int = 0
- var Longitude: Int = 0
-
- init(_ bindata:ArraySlice<UInt8>) {
+ public var CPRFormat: Int = 0
+ public var Latitude: Int = 0
+ public var Longitude: Int = 0
+
+ public init(_ bindata:ArraySlice<UInt8>) {
let ss = (bindata[4]>>1)&(0x3)
SurveillanceStatus = Int(ss)
let saf = bindata[5]&0x1
@@ -224,7 +224,7 @@ class ADSBTypeCodeAirbonePositon:CustomStringConvertible {
Longitude = Int(lon)
}
- var description: String {
+ public var description: String {
var description = "SS \(SurveillanceStatus) SAF \(SingleAntennaFlag) Altitude \(Altitude)ft \n"
description += "Time \(Time) CPR \(CPRFormat)"
if (CPRFormat == 0) {
diff --git a/ADSBDecoderFramework/PositionDecoder.swift b/ADSBDecoderFramework/PositionDecoder.swift
index 85b9d49..a8d03de 100644
--- a/ADSBDecoderFramework/PositionDecoder.swift
+++ b/ADSBDecoderFramework/PositionDecoder.swift
@@ -112,7 +112,7 @@ func mstime() -> Int64 {
return mst;
}
-class PositionDecoder {
+public class PositionDecoder {
var long: Double = 0.0
var lat: Double = 0.0
@@ -120,18 +120,22 @@ class PositionDecoder {
var newposition: Bool = false
//initialise first values so this values is nonsence
- var queue: Deque<PositionData> = []
+ //var queue: Deque<PositionData> = []
+ var position_even: PositionData? = nil
+ var position_odd: PositionData? = nil
init() {
}
func addEvenPosition(_ cpr_lat: UInt32,_ cpr_long: UInt32, _ cprtime: Int64) {
- queue.append(PositionData(cpr_lat:cpr_lat, cpr_long:cpr_long, even:true, cprtime: cprtime))
+ //queue.append(PositionData(cpr_lat:cpr_lat, cpr_long:cpr_long, even:true, cprtime: cprtime))
+ self.position_even = PositionData(cpr_lat:cpr_lat, cpr_long:cpr_long, even:true, cprtime: cprtime)
}
func addOddPosition(_ cpr_lat: UInt32,_ cpr_long: UInt32, _ cprtime: Int64) {
- queue.append(PositionData(cpr_lat:cpr_lat,cpr_long:cpr_long,even:false, cprtime: cprtime))
+ //queue.append(PositionData(cpr_lat:cpr_lat,cpr_long:cpr_long,even:false, cprtime: cprtime))
+ self.position_odd = PositionData(cpr_lat:cpr_lat,cpr_long:cpr_long,even:false, cprtime: cprtime)
}
func getPosition() -> (Double, Double)? {
@@ -143,24 +147,36 @@ class PositionDecoder {
//TODO: allways adds to queue newer frees the queue
func calcPosition() -> Bool {
- if (queue.count > 2) {
- let el1 = queue[queue.count-1]
- let el2 = queue[queue.count-2]
- if (el1.even == el2.even) {
- //ready = false
- return false
- }
- } else {
- ready = false
- print("Position queue to short to calculate values")
+ //if (queue.count > 2) {
+ //let el1 = queue[queue.count-1]
+ //let el2 = queue[queue.count-2]
+ //if (el1.even == el2.even) {
+ // //ready = false
+ // return false
+ //}
+ //} else {
+ // ready = false
+ // print("Position queue to short to calculate values")
+ // return false
+ //}
+ if position_odd == nil {
+ return false
+ }
+ if position_even == nil {
return false
}
//last to elements are evan and odd
- let el1 = queue[queue.count-1]
- let el2 = queue[queue.count-2]
- let cpr_even = el1.even ? el1 : el2
- let cpr_odd = (!el1.even) ? el1 : el2
+ //let el1 = queue[queue.count-1]
+ //let el2 = queue[queue.count-2]
+ guard let cpr_even = self.position_even else {
+ print("Cant unwrap even position")
+ return false
+ }
+ guard let cpr_odd = self.position_odd else {
+ print("Cant unwrap odd position")
+ return false
+ }
//print("Position queue is ready to calculate location \(cpr_even) \(cpr_odd)")
// from here https://github.com/antirez/dump1090/blob/master/dump1090.c#L1718
diff --git a/ADSBDecoderFramework/Query.swift b/ADSBDecoderFramework/Query.swift
index a92d919..0ae6212 100644
--- a/ADSBDecoderFramework/Query.swift
+++ b/ADSBDecoderFramework/Query.swift
@@ -7,20 +7,20 @@
import Foundation
-class QueryDF: CustomStringConvertible {
-
+public class QueryDF: CustomStringConvertible {
+
//max I see is 24
var DFlist:[Int] = Array(repeating: 0, count: 30)
-
- init() {
+
+ public init() {
}
- func addDF(_ dataformat: UInt32) {
+ public func addDF(_ dataformat: UInt32) {
DFlist[Int(dataformat)] += 1
}
-
- func showStat() {
+
+ public func showStat() {
print("DataFormat Count")
for i in 0...(DFlist.count)-1 {
if DFlist[i] != 0 {
@@ -29,7 +29,7 @@ class QueryDF: CustomStringConvertible {
}
}
- var description: String {
+ public var description: String {
var description = "DataFormat Count\n"
for i in 0...(DFlist.count)-1 {
if DFlist[i] != 0 {
@@ -40,18 +40,18 @@ class QueryDF: CustomStringConvertible {
}
}
-class QueryDF17TC: CustomStringConvertible {
+public class QueryDF17TC: CustomStringConvertible {
var TClist:[Int] = Array(repeating: 0, count: 32)
-
- init() {
-
+
+ public init() {
+
}
-
- func addTC(_ typecode: Int) {
+
+ public func addTC(_ typecode: Int) {
TClist[typecode] += 1
}
- var description: String {
+ public var description: String {
var description = "DF17 TypeCode Count\n"
for i in 0...(TClist.count-1) {
if TClist[i] != 0 {
@@ -62,25 +62,25 @@ class QueryDF17TC: CustomStringConvertible {
}
}
-class QueryDecodedMessages: CustomStringConvertible {
-
+public class QueryDecodedMessages: CustomStringConvertible {
+
var decoded:Int = 0
var total:Int = 0
-
- init() {
-
+
+ public init() {
+
}
-
- func addDecoded() {
+
+ public func addDecoded() {
decoded += 1
total += 1
}
-
- func addUndecoded() {
+
+ public func addUndecoded() {
total = total + 1
}
- var description: String {
+ public var description: String {
var description = "Total messages:\n"
description += "Decoded:\(decoded)\n"
description += "Total :\(total) \(String(format:"%02.01f",Float(Float(decoded)/Float(total)*100.0)))%\n"
@@ -90,24 +90,24 @@ class QueryDecodedMessages: CustomStringConvertible {
}
-class QueryDF17TC_decoded: CustomStringConvertible {
+public class QueryDF17TC_decoded: CustomStringConvertible {
var TClist_decoded:[Int] = Array(repeating: 0, count: 32)
var TClist_total:[Int] = Array(repeating: 0, count: 32)
-
- init() {
-
+
+ public init() {
+
}
-
- func addDecoded(_ typecode: Int) {
+
+ public func addDecoded(_ typecode: Int) {
TClist_decoded[typecode] += 1
TClist_total[typecode] += 1
}
-
- func addUndecode(_ typecode: Int) {
+
+ public func addUndecode(_ typecode: Int) {
TClist_total[typecode] += 1
}
- var description: String {
+ public var description: String {
var description = ""
var decoded:Int = 0
var total:Int = 0