Commit e007d965 by Demid Merzlyakov

Air Quality: Sort of correct limits for pollutants.

parent 98860eea
......@@ -23,8 +23,13 @@ public struct AirQuality: Equatable, Hashable {
return progressValue
}
public var status: HealthStatus {
get {
HealthStatus(value: index)
switch index {
case 0...50: return .good
case 51...100: return .moderate
case 101...150: return .unhealthyForSensitiveGroups
case 151...200: return .unhealthy
case 201...300: return .veryUnhealthy
default: return .hazardous
}
}
}
......@@ -37,23 +42,6 @@ public enum HealthStatus: String {
case veryUnhealthy = "health.airquality.status.veryUnhealthy"
case hazardous = "health.airquality.status.hazardous"
public init(value: Double) {
switch value {
case 0...50:
self = .good
case 51...100:
self = .moderate
case 101...150:
self = .unhealthyForSensitiveGroups
case 151...200:
self = .unhealthy
case 201...300:
self = .veryUnhealthy
default:
self = .hazardous
}
}
public var localized: String {
return self.rawValue.localized()
}
......@@ -192,13 +180,123 @@ public struct Pollutant: Equatable, Hashable {
public let name: String
public let value: Double
public var progress:CGFloat {
var progressValue = max(0, CGFloat(value / 500))
guard let hazardousLevelStart = self.hazardousLevelStart else {
return 0.1 // not great, not terrible
}
var progressValue = max(0, CGFloat(value / hazardousLevelStart))
progressValue = min(1, progressValue)
return progressValue
}
public var status: HealthStatus {
get {
HealthStatus(value: value)
public init(name: String, value: Double) {
self.name = name
self.value = value
var statusValue = HealthStatus.good
let shortName = name.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: " ", with: "").lowercased()
if let statusLimits: [HealthStatus: Double] = Pollutant.upperLimits[shortName] {
let statusesSequence: [HealthStatus] = [.good, .moderate, .unhealthyForSensitiveGroups, .unhealthy, .veryUnhealthy, .hazardous]
for possibleStatus: HealthStatus in statusesSequence {
if let limit: Double = statusLimits[possibleStatus] {
if value < limit {
statusValue = possibleStatus
break
}
}
else {
assertionFailure("No limit for status \(possibleStatus) of pollutant \(name)") // should never happen.
statusValue = possibleStatus
continue
}
}
if let veryUnhealthyUpperLimit = statusLimits[.veryUnhealthy] {
hazardousLevelStart = veryUnhealthyUpperLimit
}
else {
assertionFailure("No limit for status \(HealthStatus.veryUnhealthy) of pollutant \(name)") // should never happen.
hazardousLevelStart = nil
}
}
else {
assertionFailure("No limits for Pollutant \(name)!")
statusValue = .good
hazardousLevelStart = nil
}
self.status = statusValue
}
public private (set) var status: HealthStatus
private let hazardousLevelStart: Double?
/// Preferably these should be configurable from the server.
/// See https://en.wikipedia.org/wiki/Air_quality_index
/// The "AQI Category, Pollutants and Health Breakpoints" table.
private static let upperLimits: [String: [HealthStatus: Double]] = [
"pm10": [
.good: 50.0,
.moderate: 100.0,
.unhealthyForSensitiveGroups: 250.0,
.unhealthy: 350.0,
.veryUnhealthy: 430.0,
.hazardous: Double.infinity
],
"pm2.5": [
.good: 30.0,
.moderate: 60.0,
.unhealthyForSensitiveGroups: 90.0,
.unhealthy: 120.0,
.veryUnhealthy: 250.0,
.hazardous: Double.infinity
],
"no2": [
.good: 40.0,
.moderate: 80.0,
.unhealthyForSensitiveGroups: 180.0,
.unhealthy: 280.0,
.veryUnhealthy: 400.0,
.hazardous: Double.infinity
],
"o3": [
.good: 50.0,
.moderate: 100.0,
.unhealthyForSensitiveGroups: 168.0,
.unhealthy: 208.0,
.veryUnhealthy: 748.0,
.hazardous: Double.infinity
],
"co": [
.good: 1.0,
.moderate: 2.0,
.unhealthyForSensitiveGroups: 10.0,
.unhealthy: 17.0,
.veryUnhealthy: 34.0,
.hazardous: Double.infinity
],
"so2": [
.good: 40.0,
.moderate: 80.0,
.unhealthyForSensitiveGroups: 380.0,
.unhealthy: 800.0,
.veryUnhealthy: 1600.0,
.hazardous: Double.infinity
],
"nh3": [
.good: 200.0,
.moderate: 400.0,
.unhealthyForSensitiveGroups: 800.0,
.unhealthy: 1200.0,
.veryUnhealthy: 1800.0,
.hazardous: Double.infinity
],
"pb": [
.good: 0.5,
.moderate: 1.0,
.unhealthyForSensitiveGroups: 2.0,
.unhealthy: 3.0,
.veryUnhealthy: 3.5,
.hazardous: Double.infinity
]
]
}
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