summaryrefslogtreecommitdiff
path: root/LearnMapKit/LearnMapKitApp.swift
blob: 8132c92f6a4f28220907f1a2a26202819821a17a (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
106
107
108
109
110
111
112
//
//  LearnMapKitApp.swift
//  LearnMapKit
//
//  Created by Jacky Jack on 05/06/2024.
//

import SwiftUI
import Collections
import ArgumentParser

//https://www.hackingwithswift.com/quick-start/swiftui/how-to-run-code-when-your-app-launches

struct CommandLineArgs: ParsableCommand {
    @Option(name: .shortAndLong) var hostname: String? = nil
    @Option(name: .shortAndLong) var port: Int? = nil
    @Option(name: .shortAndLong) var inputfile: String? = nil
    @Flag(name: .shortAndLong) var debug:Bool = false
    @Flag(name: .shortAndLong) var version:Bool = false
}

@main
struct LearnMapKitApp: App {
    var network_mode = false
    var file_mode = false
    @State var queue: Deque<ADSBLocation> = []
    @State var netconfig: NetworkConfigure = NetworkConfigure()
    
    @StateObject private var flightState:FlightState = FlightState()
    
    var default_hostname = "192.168.4.201"
    var default_port = 30002
    var default_input_file = ""
    
    init() {
        print("Init app")
        //parse arguments
        //let args = CommandLineArgs.parseOrExit()
        //flightState.defaultMode()
        let args = CommandLineArgs.parseOrExit()
        print(args)
        print(args.inputfile)
        //exit(0)
        //if (true)//
        if let args = CommandLineArgs.parseNotExit()
        {
            if args.hostname != nil {
                default_hostname = args.hostname!
                network_mode = true
            }
            if args.port != nil {
                default_port = args.port!
                network_mode = true
            }
            
            if args.inputfile != nil {
                default_input_file = args.inputfile!
                file_mode = true
            }
            print("Set this")
            
        }
        
        //flightState.run()
        
        
        /*let ADSBClient = NetADSBDecoder(host: default_hostname, port: default_port)
        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, net_config: $netconfig).onAppear(perform: {
                if network_mode {
                    print("network mode")
                    flightState.networkMode(hostname: self.default_hostname, port: self.default_port)
                } else if (file_mode){
                    print("RUn file mode")
                    flightState.fileMode(filepath: self.default_input_file)
                } else {
                    print("Run default mode")
                    flightState.defaultMode()
                }
                flightState.run()
            })
        }.environmentObject(flightState)
        
        WindowGroup("Network", id: "net-config") {
            NetConfigView(net_config: $netconfig)
        }
    }
}

extension ParsableArguments {
    static func parseNotExit(
        _ arguments: [String]? = nil
      ) -> Self! {
        do {
          return try parse(arguments)
        } catch {
          print("Ignore error")
          return nil
        }
      }
}