Commit 3ed817e8 by Demid Merzlyakov

Wdt response parsing (working).

parent 4d65cd88
...@@ -80,7 +80,7 @@ public enum MoonPhase: String, Codable { ...@@ -80,7 +80,7 @@ public enum MoonPhase: String, Codable {
case waningGibbousMoon = "Waning Gibbous Moon" case waningGibbousMoon = "Waning Gibbous Moon"
case lastQuarterMoon = "Last Quarter Moon" case lastQuarterMoon = "Last Quarter Moon"
case waningCrescentMoon = "Waning Crescent Moon" case waningCrescentMoon = "Waning Crescent Moon"
case unknown = "unknown" case unknown = ""
} }
public struct Time: CustomStringConvertible, Codable { public struct Time: CustomStringConvertible, Codable {
......
...@@ -13,8 +13,26 @@ public struct Location: CustomStringConvertible, Equatable { ...@@ -13,8 +13,26 @@ public struct Location: CustomStringConvertible, Equatable {
public var coordinates: CLLocationCoordinate2D? public var coordinates: CLLocationCoordinate2D?
public var imageName = "ny_bridge" //we'll possibly need to switch to URL public var imageName = "ny_bridge" //we'll possibly need to switch to URL
public var countryCode: String? public var countryCode: String? {
public var countryName: String? didSet {
if countryCode == "USA" {
countryCode = "US"
}
}
}
public var countryName: String? {
didSet {
if let newCountryName = countryName {
if newCountryName != oldValue {
if newCountryName == "USA" || newCountryName == "US" || newCountryName.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() == "UNITED STATES OF AMERICA" {
if countryCode != "US" {
countryCode = "US"
}
}
}
}
}
}
public var region: String? // also, state public var region: String? // also, state
public var cityName: String? public var cityName: String?
public var nickname: String? // nickname given to this location by the user public var nickname: String? // nickname given to this location by the user
......
...@@ -9,13 +9,14 @@ import Foundation ...@@ -9,13 +9,14 @@ import Foundation
struct WdtDailySummary: Codable { struct WdtDailySummary: Codable {
public var dateLocal: String public var dateLocal: String
public var minTempF: Double? public var weekDay: String
public var maxTempF: Double
public var windSpeedKph: Double? public var minTempF: String?
public var windDirection: WindDirection? public var maxTempF: String?
public var weekDay: WeekDay public var windSpeedKph: String?
public var weatherCode: WdtWeatherCode? public var windDirection: String?
public var precipitationProbability: Int? public var weatherCode: String?
public var precipitationProbability: String?
public var sunriseUtc: String? public var sunriseUtc: String?
public var sunsetUtc: String? public var sunsetUtc: String?
public var sunState: CelestialState? // if nil, then normal public var sunState: CelestialState? // if nil, then normal
...@@ -26,11 +27,12 @@ struct WdtDailySummary: Codable { ...@@ -26,11 +27,12 @@ struct WdtDailySummary: Codable {
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case dateLocal = "summary_date" case dateLocal = "summary_date"
case weekDay = "day_of_week"
case minTempF = "min_temp_F" case minTempF = "min_temp_F"
case maxTempF = "max_temp_F" case maxTempF = "max_temp_F"
case windSpeedKph = "wnd_spd_kph" case windSpeedKph = "wnd_spd_kph"
case windDirection = "wnd_dir" case windDirection = "wnd_dir"
case weekDay = "day_of_week"
case weatherCode = "wx_code" case weatherCode = "wx_code"
case precipitationProbability = "pop" case precipitationProbability = "pop"
case sunriseUtc = "solunar_sunrise_utc" case sunriseUtc = "solunar_sunrise_utc"
......
...@@ -9,25 +9,27 @@ import Foundation ...@@ -9,25 +9,27 @@ import Foundation
struct WdtHourlySummary: Codable { struct WdtHourlySummary: Codable {
public var dateTimeLocal: String public var dateTimeLocal: String
public var tempF: Double? public var weekDay: String
public var apparentTempF: Double? public var dayNight: String
public var tempF: String?
public var apparentTempF: String?
public var windSpeedKph: Double? public var windSpeedKph: Double?
public var windDirection: WindDirection? public var windDirection: String?
public var humidity: Percent? public var humidity: String?
public var weekDay: WeekDay public var precipitationProbability: String?
public var dayNight: WdtDayNight public var weatherCode: String?
public var precipitationProbability: Int?
public var weatherCode: WdtWeatherCode?
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case dateTimeLocal = "time_local" case dateTimeLocal = "time_local"
case weekDay = "day_of_week_local"
case dayNight = "day_night"
case tempF = "temp_F" case tempF = "temp_F"
case apparentTempF = "apparent_temp_F" case apparentTempF = "apparent_temp_F"
case windSpeedKph = "wnd_spd_kph" case windSpeedKph = "wnd_spd_kph"
case windDirection = "wnd_dir" case windDirection = "wnd_dir"
case humidity = "rh_pct" case humidity = "rh_pct"
case weekDay = "day_of_week_local"
case dayNight = "day_night"
case precipitationProbability = "pop" case precipitationProbability = "pop"
case weatherCode = "wx_code" case weatherCode = "wx_code"
} }
......
...@@ -9,21 +9,25 @@ import Foundation ...@@ -9,21 +9,25 @@ import Foundation
struct WdtSurfaceObservation: Codable { struct WdtSurfaceObservation: Codable {
public var dateTimeLocal: String public var dateTimeLocal: String
public var tempF: Double? public var weekDay: String
public var apparentTempF: Double? public var dayNight: String
public var windSpeedKph: Double?
public var windDirection: WindDirection? public var tempF: String? // Currently XMLCoder seems to struggle with decoding optional doubles with an empty value: <temp_F></temp_F> won't be decoded as nil, instead decoding will fail. So, using Strings for now.
public var pressureMb: Double? public var apparentTempF: String?
public var visibilityFt: Double? public var windSpeedKph: String?
public var humidity: Percent? public var windDirection: String?
public var weekDay: WeekDay? public var pressureMb: String?
public var dayNight: WdtDayNight public var visibilityFt: String?
public var weatherCode: WdtWeatherCode? public var humidity: String?
public var weatherCode: String?
public var sunriseLocalTime: String? public var sunriseLocalTime: String?
public var sunsetLocalTime: String? public var sunsetLocalTime: String?
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case dateTimeLocal = "ob_time" case dateTimeLocal = "ob_time"
case weekDay = "day_of_week_local"
case dayNight = "day_night"
case tempF = "temp_F" case tempF = "temp_F"
case apparentTempF = "apparent_temp_F" case apparentTempF = "apparent_temp_F"
case windSpeedKph = "wnd_spd_kph" case windSpeedKph = "wnd_spd_kph"
...@@ -31,8 +35,6 @@ struct WdtSurfaceObservation: Codable { ...@@ -31,8 +35,6 @@ struct WdtSurfaceObservation: Codable {
case pressureMb = "press_mb" case pressureMb = "press_mb"
case visibilityFt = "visibility_ft" case visibilityFt = "visibility_ft"
case humidity = "rh_pct" case humidity = "rh_pct"
case weekDay = "day_of_week_local"
case dayNight = "day_night"
case weatherCode = "wx_code" case weatherCode = "wx_code"
case sunriseLocalTime = "sunrise_local" case sunriseLocalTime = "sunrise_local"
case sunsetLocalTime = "sunset_local" case sunsetLocalTime = "sunset_local"
......
...@@ -13,6 +13,7 @@ public enum WdtWeatherSourceError: Error { ...@@ -13,6 +13,7 @@ public enum WdtWeatherSourceError: Error {
case badUrl case badUrl
case networkError(Error?) case networkError(Error?)
case badServerResponse(Error?) case badServerResponse(Error?)
case dataMergeError
} }
public class WdtWeatherSource: WeatherSource { public class WdtWeatherSource: WeatherSource {
...@@ -48,7 +49,8 @@ public class WdtWeatherSource: WeatherSource { ...@@ -48,7 +49,8 @@ public class WdtWeatherSource: WeatherSource {
} }
let urlSession = URLSession.shared let urlSession = URLSession.shared
let dataTask = urlSession.dataTask(with: url) { (data, reponse, error) in let dataTask = urlSession.dataTask(with: url) { [weak self] (data, reponse, error) in
guard let self = self else { return }
guard let data = data else { guard let data = data else {
completion(nil, WdtWeatherSourceError.networkError(error)) completion(nil, WdtWeatherSourceError.networkError(error))
return return
...@@ -56,8 +58,11 @@ public class WdtWeatherSource: WeatherSource { ...@@ -56,8 +58,11 @@ public class WdtWeatherSource: WeatherSource {
let decoder = XMLDecoder() let decoder = XMLDecoder()
do { do {
let locationResponse = try decoder.decode(WdtLocationResponse.self, from: data) let locationResponse = try decoder.decode(WdtLocationResponse.self, from: data)
guard let newLocation = self.update(location: location, from: locationResponse) else {
//TODO: convert to app model. completion(nil, WdtWeatherSourceError.badServerResponse(error))
return
}
completion(newLocation, nil)
} }
catch { catch {
completion(nil, WdtWeatherSourceError.badServerResponse(error)) completion(nil, WdtWeatherSourceError.badServerResponse(error))
...@@ -65,4 +70,30 @@ public class WdtWeatherSource: WeatherSource { ...@@ -65,4 +70,30 @@ public class WdtWeatherSource: WeatherSource {
} }
dataTask.resume() dataTask.resume()
} }
func update(location: Location, from locationResponse: WdtLocationResponse) -> Location? {
guard let wdtLocation = locationResponse.locations.first else {
return nil
}
var updatedLocation = location
if updatedLocation.coordinates == nil {
if let lat = CLLocationDegrees(wdtLocation.lat ?? ""), let lon = CLLocationDegrees(wdtLocation.lon ?? "") {
location.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
}
if updatedLocation.cityName == nil {
updatedLocation.cityName = wdtLocation.city
}
if updatedLocation.countryName == nil {
updatedLocation.countryName = wdtLocation.country
}
if updatedLocation.region == nil {
updatedLocation.
}
// TOOD: implement
#warning("Not implemented!")
return updatedLocation
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment