This commit is contained in:
61
Sources/ManualS/Internal/ProposedKWInterpolation.swift
Normal file
61
Sources/ManualS/Internal/ProposedKWInterpolation.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
import CoreFoundation
|
||||
import Models
|
||||
import Validations
|
||||
|
||||
extension ProposedKWInterpolation.Request {
|
||||
|
||||
private static let oversizingLimit = 175
|
||||
|
||||
func respond() async throws -> ProposedKWInterpolation.Response {
|
||||
try await validate()
|
||||
let (requiredKW, optionalHeatPump) = try await requiredKW()
|
||||
let capacityAsPercentOfLoad = normalizePercentage(proposedKW, requiredKW)
|
||||
|
||||
var failures = [String]()
|
||||
if capacityAsPercentOfLoad > Self.oversizingLimit {
|
||||
failures.append("Oversizing failure.")
|
||||
}
|
||||
|
||||
return .init(
|
||||
failures: failures.isEmpty ? nil : failures,
|
||||
supplementalHeatRequired: requiredKW,
|
||||
capacityAsPercentOfLoad: capacityAsPercentOfLoad,
|
||||
oversizingLimit: Self.oversizingLimit,
|
||||
altitudeAdjustmentMultiplier: optionalHeatPump?.altitudeAdjustmentMultiplier,
|
||||
balancePointTemperature: optionalHeatPump?.balancePointTemperature,
|
||||
capacityAtDesign: optionalHeatPump?.capacityAtDesign,
|
||||
finalCapacity: optionalHeatPump?.finalCapacity
|
||||
)
|
||||
}
|
||||
|
||||
private var heatPumpRequest: HeatPumpHeatingInterpolation.Request? {
|
||||
guard let winterDesignTemperature, let climateType, let capacity else {
|
||||
return nil
|
||||
}
|
||||
return .init(
|
||||
winterDesignTemperature: winterDesignTemperature,
|
||||
heatLoss: heatLoss,
|
||||
climateType: climateType,
|
||||
capacity: capacity
|
||||
)
|
||||
}
|
||||
|
||||
private func requiredKW() async throws -> (Int, HeatPumpHeatingInterpolation.Response?) {
|
||||
guard let heatPumpRequest = heatPumpRequest else {
|
||||
let requiredKW = try await RequiredKW.Request(heatLoss: heatLoss).respond()
|
||||
return (Int(requiredKW.requiredKW), nil)
|
||||
}
|
||||
let heatPumpResponse = try await heatPumpRequest.respond()
|
||||
return (heatPumpResponse.supplementalHeatRequired, heatPumpResponse)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension ProposedKWInterpolation.Request: AsyncValidatable {
|
||||
public var body: some AsyncValidation<Self> {
|
||||
AsyncValidator.accumulating {
|
||||
AsyncValidator.greaterThan(\.heatLoss, 0)
|
||||
AsyncValidator.greaterThan(\.proposedKW, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,35 +12,39 @@ public extension DependencyValues {
|
||||
@DependencyClient
|
||||
public struct ManualS: Sendable {
|
||||
public var balancePoint: @Sendable (BalancePoint.Request) async throws -> BalancePoint.Response
|
||||
|
||||
public var derating: @Sendable (Derating.Request) async throws -> Derating.Response
|
||||
public var interpolate: Interpolations
|
||||
public var requiredKW: @Sendable (RequiredKW.Request) async throws -> RequiredKW.Response
|
||||
public var sizingLimits: @Sendable (SizingLimits.Request) async throws -> SizingLimits.Response
|
||||
}
|
||||
|
||||
public var coolingInterpolation:
|
||||
@Sendable (CoolingInterpolation.Request) async throws -> CoolingInterpolation.Response
|
||||
@DependencyClient
|
||||
public struct Interpolations: Sendable {
|
||||
public var cooling: @Sendable (CoolingInterpolation.Request) async throws -> CoolingInterpolation.Response
|
||||
|
||||
public var furnaceInterpolation:
|
||||
@Sendable (FurnaceInterpolation.Request) async throws -> FurnaceInterpolation.Response
|
||||
public var furnace: @Sendable (FurnaceInterpolation.Request) async throws -> FurnaceInterpolation.Response
|
||||
|
||||
public var heatPumpHeatingInterpolation:
|
||||
public var heatPumpHeating:
|
||||
@Sendable (HeatPumpHeatingInterpolation.Request) async throws -> HeatPumpHeatingInterpolation.Response
|
||||
|
||||
public var requiredKW: @Sendable (RequiredKW.Request) async throws -> RequiredKW.Response
|
||||
|
||||
public var sizingLimits: @Sendable (SizingLimits.Request) async throws -> SizingLimits.Response
|
||||
public var proposeKW: @Sendable (ProposedKWInterpolation.Request) async throws -> ProposedKWInterpolation.Response
|
||||
}
|
||||
|
||||
extension ManualS: DependencyKey {
|
||||
public static let liveValue = Self(
|
||||
balancePoint: { try await $0.respond() },
|
||||
derating: { try await $0.respond() },
|
||||
coolingInterpolation: { try await $0.respond() },
|
||||
furnaceInterpolation: { try await $0.respond() },
|
||||
heatPumpHeatingInterpolation: { try await $0.respond() },
|
||||
interpolate: .init(
|
||||
cooling: { try await $0.respond() },
|
||||
furnace: { try await $0.respond() },
|
||||
heatPumpHeating: { try await $0.respond() },
|
||||
proposeKW: { try await $0.respond() }
|
||||
),
|
||||
requiredKW: { try await $0.respond() },
|
||||
sizingLimits: { try await $0.respond() }
|
||||
)
|
||||
}
|
||||
|
||||
extension ManualS: TestDependencyKey {
|
||||
public static let testValue: ManualS = Self()
|
||||
public static let testValue: ManualS = Self(interpolate: .init())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user