summaryrefslogtreecommitdiff
path: root/LearnMapKit/ADSBRunner.swift
blob: 0f09759abab6e3938c95be0bd0e292b8b0b3f9e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
//  ADSBRunner.swift
//  LearnMapKit
//
//  Created by Jacky Jack on 28/06/2024.
//

import Foundation

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
}

class ADSBFileRunner {
    
    var filename: URL
    //track all airplanes
    var tracker = AirPlaneTracker()
    var adsb_source: String = ""
    
    init(filename:String) {
        self.filename = URL(fileURLWithPath:filename)
    }
    
    func openFile() {
        print("File location [\(filename.absoluteString)]")

        //check if file excists
        if (checkIfFileExists(filename.path) == false) {
            print("Supplied path \(filename.path) doesnt exists")
            exit(1)
        }
    }
    
    func readFile() {
        //load the file with adsb data
        do {
            adsb_source = try String(contentsOfFile: filename.path)
            print("Loaded \(adsb_source.count) bytes")
        } catch {
            print("Couldn't load text from a file \(filename.path)")
            exit(1)
        }
        print("If there anything new in file")
    }
    
    func decode() {
        for line in self.adsb_source.components(separatedBy: .newlines) {
            var found=false
            do {
                if let tokenMatch = try matchADSBLong.prefixMatch(in: line) {
                    //print("\(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 {
                                    tracker.addDF17Indentification(d17.AddressAnnounced, indentification.ICAOName)
                                }
                            } else if (d17.TypeCode >= 9 && d17.TypeCode <= 18) {
                                if let airbornposition = d17.messageAirbornPositon {
                                    tracker.addDF17AirBornPosition(
                                        d17.AddressAnnounced,
                                        airbornposition.Latitude,
                                        airbornposition.Longitude,
                                        airbornposition.Altitude,
                                        airbornposition.CPRFormat == 0
                                    )
                                    if let position = tracker.getPosition(d17.AddressAnnounced) {
                                        print("position: \(position)")
                                    }
                                }
                            }
                        }
                    }
                };
            } catch {
                print("Error")
            }
            
            if (found == false) {
                print("Unknown adsb data line \(line)")
            }
        }
        //try to free the string after decoded
        //adsb_source = ""
    }
}