|
7 | 7 |
|
8 | 8 | import Foundation
|
9 | 9 |
|
10 |
| -// MARK: - This struct is a real mess. It is a bit of outrageous code but the starting point, ie the JSON returned by the API, |
11 |
| - |
12 |
| -// is bad, undocumented and frankly not even nicely modelled. |
13 |
| - |
14 |
| -struct Metar: Decodable { |
15 |
| - var siteId: String |
| 10 | +struct RedhillAtis: Decodable { |
| 11 | + var site: String |
16 | 12 | var metar: String
|
17 | 13 | var designator: String
|
18 | 14 | var runway: String
|
19 | 15 | var updatedOn: String
|
20 |
| - var isAutomatic: Bool |
21 |
| - var isCavok: Bool |
| 16 | +// var isAutomatic: Bool |
| 17 | +// var isCavok: Bool |
22 | 18 | var qfe: Int
|
23 | 19 | var qnh: Int
|
24 |
| - var temperature: Int |
25 |
| - var dewPoint: Int |
26 |
| - var visibility: Int |
27 |
| - var clouds: [CloudCoverage] |
28 |
| - var isWindVariable: Bool |
29 |
| - var windSpeed: Int |
30 |
| - var windBetweenFrom: Int |
31 |
| - var windDirection: Int |
32 |
| - var windBetweenTo: Int |
33 |
| - var windSpeedGust: Int |
34 |
| - var weather: String |
35 |
| - |
36 |
| - struct CloudCoverage: Decodable { |
37 |
| - var type: Int = 0 |
38 |
| - var height: Int = 0 |
39 |
| - var cover: Int = 0 |
40 |
| - } |
41 |
| - |
42 |
| - enum OuterKeys: String, CodingKey { |
43 |
| - case siteId |
44 |
| - case reports |
45 |
| - } |
46 |
| - |
47 |
| - enum ReportsKeys: String, CodingKey { |
48 |
| - case metarReport |
49 |
| - } |
50 |
| - |
51 |
| - // "Reports"."metarReport" |
52 |
| - enum MetarReportKeys: String, CodingKey { |
53 |
| - case auto, arrivalAtis, time, temperature, cloud, cavok, qnh, airfieldQfe, visibility, weather |
54 |
| - } |
55 |
| - |
56 |
| - enum VisibilityKeys: String, CodingKey { |
57 |
| - case visibility |
58 |
| - } |
59 |
| - |
60 |
| - enum TemperatureKeys: String, CodingKey { |
61 |
| - case temperature, dewPoint |
62 |
| - } |
63 |
| - |
64 |
| - enum WeatherKeys: String, CodingKey { |
65 |
| - case fullReportString |
66 |
| - } |
67 |
| - |
68 |
| - enum CloudKeys: String, CodingKey { |
69 |
| - case cloudLayer1, cloudLayer2, cloudLayer3 |
70 |
| - } |
71 |
| - |
72 |
| - // "Reports"."metarReport"."temperature" |
73 |
| - enum MetarKeys: String, CodingKey { |
74 |
| - case cloud |
75 |
| - } |
76 |
| - |
77 |
| - enum ArrivalAtisKeys: String, CodingKey { |
78 |
| - case codeLetter, runway, metReportString, wind |
79 |
| - } |
80 |
| - |
81 |
| - enum WindKeys: String, CodingKey { |
82 |
| - case wind2Min |
83 |
| - } |
84 |
| - |
85 |
| - enum RecentWindContainer: String, CodingKey { |
86 |
| - case isVrb, averageWindSpeed, averageWindDirection, minimumWindDirection, maximumWindDirection, maximumWindSpeed |
87 |
| - } |
88 |
| - |
89 |
| - init(from decoder: Decoder) throws { |
90 |
| - let outerContainer = try decoder.container(keyedBy: OuterKeys.self) |
91 |
| - let reportsContainer = try outerContainer.nestedContainer(keyedBy: ReportsKeys.self, forKey: .reports) |
92 |
| - self.siteId = try outerContainer.decode(String.self, forKey: .siteId) |
93 |
| - |
94 |
| - // METAR information |
95 |
| - let metarContainer = try reportsContainer.nestedContainer(keyedBy: MetarReportKeys.self, forKey: .metarReport) |
96 |
| - self.updatedOn = try metarContainer.decode(String.self, forKey: .time) |
97 |
| - self.isCavok = try metarContainer.decode(Bool.self, forKey: .cavok) |
98 |
| - self.isAutomatic = try metarContainer.decode(Bool.self, forKey: .auto) |
99 |
| - self.qnh = try metarContainer.decode(Int.self, forKey: .qnh) |
100 |
| - self.qfe = try metarContainer.decode(Int.self, forKey: .airfieldQfe) |
101 |
| - |
102 |
| - // Arrival ATIS |
103 |
| - let arrivalAtisContainer = try metarContainer.nestedContainer(keyedBy: ArrivalAtisKeys.self, forKey: .arrivalAtis) |
104 |
| - self.metar = try arrivalAtisContainer.decode(String.self, forKey: .metReportString) |
105 |
| - self.designator = try arrivalAtisContainer.decode(String.self, forKey: .codeLetter) |
106 |
| - self.runway = try arrivalAtisContainer.decode(String.self, forKey: .runway) |
107 |
| - |
108 |
| - // Temperature |
109 |
| - let temperatureContainer = try metarContainer.nestedContainer(keyedBy: TemperatureKeys.self, forKey: .temperature) |
110 |
| - self.temperature = try temperatureContainer.decode(Int.self, forKey: .temperature) |
111 |
| - self.dewPoint = try temperatureContainer.decode(Int.self, forKey: .dewPoint) |
112 |
| - |
113 |
| - // Visibility - This is returned nil when CAVOK |
114 |
| - if self.isCavok == false { |
115 |
| - let visibilityContainer = try metarContainer.nestedContainer(keyedBy: VisibilityKeys.self, forKey: .visibility) |
116 |
| - self.visibility = try visibilityContainer.decode(Int.self, forKey: .visibility) |
117 |
| - } else { self.visibility = 9999 } |
| 20 | +// var temperature: Int |
| 21 | +// var dewPoint: Int |
| 22 | +// var visibility: Int |
| 23 | +// var clouds: [CloudCoverage] |
| 24 | +// var isWindVariable: Bool |
| 25 | +// var windSpeed: Int |
| 26 | +// var windBetweenFrom: Int |
| 27 | +// var windDirection: Int |
| 28 | +// var windBetweenTo: Int |
| 29 | +// var windSpeedGust: Int |
| 30 | +// var weather: String |
| 31 | +// |
| 32 | +// struct CloudCoverage: Decodable { |
| 33 | +// var type: Int = 0 |
| 34 | +// var height: Int = 0 |
| 35 | +// var cover: Int = 0 |
| 36 | +// } |
118 | 37 |
|
119 |
| - // Weather |
120 |
| - let weatherContainer = try metarContainer.nestedContainer(keyedBy: WeatherKeys.self, forKey: .weather) |
121 |
| - self.weather = try weatherContainer.decode(String.self, forKey: .fullReportString) |
122 |
| - |
123 |
| - // Clouds |
124 |
| - let cloudContainer = try metarContainer.nestedContainer(keyedBy: CloudKeys.self, forKey: .cloud) |
125 |
| - self.clouds = [CloudCoverage]() |
126 |
| - |
127 |
| - if let cloud1 = try? cloudContainer.decode(CloudCoverage.self, forKey: .cloudLayer1) { |
128 |
| - self.clouds.append(cloud1) |
129 |
| - } |
130 |
| - |
131 |
| - if let cloud2 = try? cloudContainer.decode(CloudCoverage.self, forKey: .cloudLayer2) { |
132 |
| - self.clouds.append(cloud2) |
133 |
| - } |
134 |
| - |
135 |
| - if let cloud3 = try? cloudContainer.decode(CloudCoverage.self, forKey: .cloudLayer3) { |
136 |
| - self.clouds.append(cloud3) |
137 |
| - } |
138 |
| - |
139 |
| - // Wind |
140 |
| - let windContainer = try arrivalAtisContainer.nestedContainer(keyedBy: WindKeys.self, forKey: .wind) |
141 |
| - let recentWindContainer = try windContainer.nestedContainer(keyedBy: RecentWindContainer.self, forKey: .wind2Min) |
142 |
| - self.windSpeed = try recentWindContainer.decode(Int.self, forKey: .averageWindSpeed) |
143 |
| - self.isWindVariable = try recentWindContainer.decode(Bool.self, forKey: .isVrb) |
144 |
| - |
145 |
| - if let windFrom = try? recentWindContainer.decode(Int.self, forKey: .minimumWindDirection) { |
146 |
| - self.windBetweenFrom = windFrom |
147 |
| - } else |
148 |
| - { self.windBetweenFrom = 0 } |
149 |
| - |
150 |
| - if let windTo = try? recentWindContainer.decode(Int.self, forKey: .minimumWindDirection) { |
151 |
| - self.windBetweenTo = windTo |
152 |
| - } else |
153 |
| - { self.windBetweenTo = 0 } |
154 |
| - |
155 |
| - self.windSpeedGust = try recentWindContainer.decode(Int.self, forKey: .maximumWindSpeed) |
156 |
| - if let windDirection = try? recentWindContainer.decode(Int.self, forKey: .averageWindDirection) { |
157 |
| - self.windDirection = windDirection |
158 |
| - } else |
159 |
| - { self.windDirection = 0 } |
160 |
| - } |
161 |
| - |
162 | 38 | init() {
|
163 |
| - self.siteId = "" |
| 39 | + self.site = "" |
164 | 40 | self.metar = ""
|
165 | 41 | self.designator = ""
|
166 | 42 | self.runway = ""
|
167 | 43 | self.updatedOn = ""
|
168 |
| - self.isAutomatic = true |
169 |
| - self.isCavok = true |
| 44 | +// self.isAutomatic = true |
| 45 | +// self.isCavok = true |
170 | 46 | self.qfe = 0
|
171 | 47 | self.qnh = 0
|
172 |
| - self.temperature = 0 |
173 |
| - self.dewPoint = 0 |
174 |
| - self.visibility = 0 |
175 |
| - self.clouds = [] |
176 |
| - self.isWindVariable = false |
177 |
| - self.windSpeed = 0 |
178 |
| - self.windDirection = 0 |
179 |
| - self.windBetweenFrom = 0 |
180 |
| - self.windBetweenTo = 0 |
181 |
| - self.windSpeedGust = 0 |
182 |
| - self.weather = "" |
| 48 | +// self.temperature = 0 |
| 49 | +// self.dewPoint = 0 |
| 50 | +// self.visibility = 0 |
| 51 | +// self.clouds = [] |
| 52 | +// self.isWindVariable = false |
| 53 | +// self.windSpeed = 0 |
| 54 | +// self.windDirection = 0 |
| 55 | +// self.windBetweenFrom = 0 |
| 56 | +// self.windBetweenTo = 0 |
| 57 | +// self.windSpeedGust = 0 |
| 58 | +// self.weather = "" |
183 | 59 | }
|
184 | 60 | }
|
0 commit comments