37 lines
1.2 KiB
Swift
37 lines
1.2 KiB
Swift
import Dependencies
|
|
import Logging
|
|
import PsychrometricClient
|
|
import Routes
|
|
|
|
public extension Routes.Psychrometrics.Request {
|
|
|
|
func respond(logger: Logger) async throws -> Psychrometrics.Response {
|
|
@Dependency(\.psychrometricClient) var psychrometricClient
|
|
try validate()
|
|
let properties = try await psychrometricClient.psychrometricProperties(
|
|
.dryBulb(.fahrenheit(temperature), relativeHumidity: humidity%, altitude: .feet(altitude ?? 0))
|
|
)
|
|
var warnings = [String]()
|
|
if altitude == nil || altitude == 0 {
|
|
warnings.append(
|
|
"Calculations based on altitude of sea level - set project altitude for higher accuracy."
|
|
)
|
|
}
|
|
|
|
return .init(properties: properties, warnings: warnings)
|
|
}
|
|
|
|
private func validate() throws {
|
|
guard temperature > 0 else {
|
|
throw ValidationError(message: "Temperature should be greater than 0.")
|
|
}
|
|
guard humidity > 0, humidity <= 100 else {
|
|
throw ValidationError(message: "Relative humidity should be greater than 0 and less than or equal to 100.")
|
|
}
|
|
if let altitude, altitude < 0 {
|
|
throw ValidationError(message: "Altitude should be greater than 0.")
|
|
}
|
|
}
|
|
|
|
}
|