Skip to content

Commit

Permalink
update to new api response format
Browse files Browse the repository at this point in the history
  • Loading branch information
ayn committed Nov 3, 2022
1 parent ec00e4b commit c430db5
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 178 deletions.
12 changes: 4 additions & 8 deletions PurpleMenu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
3726C1D4252CEEAB00B0B216 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3726C1D3252CEEAB00B0B216 /* Preview Assets.xcassets */; };
3726C1D7252CEEAB00B0B216 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3726C1D5252CEEAB00B0B216 /* Main.storyboard */; };
3726C1E1252CF33000B0B216 /* Sensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3726C1E0252CF33000B0B216 /* Sensor.swift */; };
3726C1E4252CF44600B0B216 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3726C1E3252CF44600B0B216 /* Result.swift */; };
3726C1E7252CF4CE00B0B216 /* PurpleAirApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3726C1E6252CF4CE00B0B216 /* PurpleAirApi.swift */; };
3748D18D252D505200A758D6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3748D18C252D505200A758D6 /* AppDelegate.swift */; };
3748D191252D505300A758D6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3748D190252D505300A758D6 /* Assets.xcassets */; };
Expand Down Expand Up @@ -48,7 +47,6 @@
3726C1D8252CEEAB00B0B216 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3726C1D9252CEEAB00B0B216 /* PurpleMenu.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PurpleMenu.entitlements; sourceTree = "<group>"; };
3726C1E0252CF33000B0B216 /* Sensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sensor.swift; sourceTree = "<group>"; };
3726C1E3252CF44600B0B216 /* Result.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
3726C1E6252CF4CE00B0B216 /* PurpleAirApi.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = PurpleAirApi.swift; sourceTree = "<group>"; tabWidth = 4; wrapsLines = 0; };
3748D18A252D505200A758D6 /* LauncherApplication.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LauncherApplication.app; sourceTree = BUILT_PRODUCTS_DIR; };
3748D18C252D505200A758D6 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,7 +111,6 @@
3726C1D9252CEEAB00B0B216 /* PurpleMenu.entitlements */,
3726C1D2252CEEAB00B0B216 /* Preview Content */,
3726C1E0252CF33000B0B216 /* Sensor.swift */,
3726C1E3252CF44600B0B216 /* Result.swift */,
3726C1E6252CF4CE00B0B216 /* PurpleAirApi.swift */,
37A3C07E252FB74400C2D12C /* SensorViewModel.swift */,
37A3C082252FCCA700C2D12C /* UserDefaultsExtension.swift */,
Expand Down Expand Up @@ -263,7 +260,6 @@
3726C1CF252CEEA900B0B216 /* ContentView.swift in Sources */,
3726C1E1252CF33000B0B216 /* Sensor.swift in Sources */,
3726C1E7252CF4CE00B0B216 /* PurpleAirApi.swift in Sources */,
3726C1E4252CF44600B0B216 /* Result.swift in Sources */,
37A3C07F252FB74400C2D12C /* SensorViewModel.swift in Sources */,
37A3C083252FCCA700C2D12C /* UserDefaultsExtension.swift in Sources */,
3726C1CD252CEEA900B0B216 /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -426,7 +422,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"PurpleMenu/Preview Content\"";
DEVELOPMENT_TEAM = BVZF7TL777;
DEVELOPMENT_TEAM = SV8J3E48E9;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = PurpleMenu/Info.plist;
Expand All @@ -453,7 +449,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"PurpleMenu/Preview Content\"";
DEVELOPMENT_TEAM = BVZF7TL777;
DEVELOPMENT_TEAM = SV8J3E48E9;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = PurpleMenu/Info.plist;
Expand Down Expand Up @@ -481,7 +477,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"LauncherApplication/Preview Content\"";
DEVELOPMENT_TEAM = BVZF7TL777;
DEVELOPMENT_TEAM = SV8J3E48E9;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = LauncherApplication/Info.plist;
Expand Down Expand Up @@ -510,7 +506,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"LauncherApplication/Preview Content\"";
DEVELOPMENT_TEAM = BVZF7TL777;
DEVELOPMENT_TEAM = SV8J3E48E9;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = LauncherApplication/Info.plist;
Expand Down
24 changes: 11 additions & 13 deletions PurpleMenu/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func refreshAqi() {
PurpleAirApi(sensorId: UserDefaults.standard.sensorId, apiKey: UserDefaults.standard.apiKey).getData { (sensor) in
guard let result = sensor.results?.first,
let resultB = sensor.results?.last,
let pm25Str = result.pM2_5Value,
let pm25 = Float(pm25Str),
let pm25Cf1Str = result.pm2_5_cf_1,
let pm25Cf1 = Float(pm25Cf1Str),
let pm25Cf1StrB = resultB.pm2_5_cf_1,
let pm25Cf1B = Float(pm25Cf1StrB),
let humidityStr = result.humidity,
let humidity = Float(humidityStr)
PurpleAirApi(sensorId: UserDefaults.standard.sensorId, apiKey: UserDefaults.standard.apiKey).getData { (sensors) in
guard let sensor = sensors.sensor,
let pm25D = sensor.pm25,
let pm25Cf1D = sensor.pm25_CF1,
let humidityD = sensor.humidity
else { return }

let pm25 = Float(pm25D)
let pm25Cf1 = Float(pm25Cf1D)
let humidity = Float(humidityD)

debugPrint("id = \(self.sensorViewModel.sensorId), conversion = \(UserDefaults.standard.conversion), pm25 = \(pm25), pm25Cf1 = \(pm25Cf1), RH = \(humidity)")

let aqi: Int
Expand All @@ -102,13 +100,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
case .none:
aqi = self.pmToAQI(pm25)
case .epa:
aqi = self.pmToEPA(paCf1: (pm25Cf1 + pm25Cf1B) * 0.5, humidity: humidity)
aqi = self.pmToEPA(paCf1: pm25Cf1, humidity: humidity)
case .aqandu:
aqi = self.pmToAQandU(pm: pm25)
case .lrapa:
aqi = self.pmToLRAPA(paCf1: pm25Cf1)
case .woodsmoke:
aqi = self.pmToWoodsmoke(pm25Cf1: (pm25Cf1 + pm25Cf1B) * 0.5)
aqi = self.pmToWoodsmoke(pm25Cf1: pm25Cf1)
}

DispatchQueue.main.async {
Expand Down
6 changes: 3 additions & 3 deletions PurpleMenu/PurpleAirApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PurpleAirApi {
self.apiKey = apiKey
}

func getData(completionHandler: @escaping (Sensor) -> ()) {
func getData(completionHandler: @escaping (Sensors) -> ()) {
guard let url = URL(string: urlString) else { return }

var request = URLRequest(url: url)
Expand All @@ -29,8 +29,8 @@ class PurpleAirApi {
let decoder = JSONDecoder()

do {
let sensor = try decoder.decode(Sensor.self, from: data)
completionHandler(sensor)
let sensors = try decoder.decode(Sensors.self, from: data)
completionHandler(sensors)
} catch {
debugPrint("error decoding response")
}
Expand Down
139 changes: 0 additions & 139 deletions PurpleMenu/Result.swift

This file was deleted.

Loading

0 comments on commit c430db5

Please sign in to comment.