feat: Begins pressure estimation feature tests.
This commit is contained in:
@@ -173,39 +173,102 @@ public struct FlaggedMeasurementsList: Sendable {
|
||||
logger: logger
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
guard let estimationState = ensureHasChanges(
|
||||
formState: form,
|
||||
flaggedEstimations: state.sharedSettings.flaggedEstimations
|
||||
) else {
|
||||
logger.debug("No changes found, not generating a new flagged estimation measurement.")
|
||||
print("No changes found, not generating a new flagged estimation measurement.")
|
||||
return .none
|
||||
}
|
||||
//
|
||||
// let estimationState = form.estimationState
|
||||
// // Check if there's an existing estimation with the same id, and
|
||||
// // check for changes. If no changes, then short circuit and return nothing.
|
||||
// if let id = form.id,
|
||||
// let existingState = state.sharedSettings.flaggedEstimations[id: id]?.estimationState,
|
||||
// existingState.hasChanges(estimationState)
|
||||
// {
|
||||
// return .none
|
||||
// }
|
||||
|
||||
return .receive(action: \.receive) { [sharedSettings = state.sharedSettings] in
|
||||
|
||||
var filterPressureDrop: Positive<Double>? = nil
|
||||
|
||||
if sharedSettings.flaggedEquipmentMeasurement?.filterPressureDrop.wrappedValue != 0 {
|
||||
filterPressureDrop = form.filterPressureDrop != nil
|
||||
? Positive(wrappedValue: form.filterPressureDrop!)
|
||||
: nil
|
||||
}
|
||||
|
||||
let measurement = try await estimatedPressuresClient.estimatedPressure(
|
||||
equipmentMeasurement: equipmentMeasurement,
|
||||
airflow: form.airflow,
|
||||
filterPressureDrop: filterPressureDrop
|
||||
)
|
||||
|
||||
let flaggedMeasurement = EquipmentMeasurement.FlaggedMeasurement(
|
||||
let flaggedMeasurement = try await flaggedMeasurement(
|
||||
airflow: estimationState.cfm.airflow,
|
||||
budgets: budgets,
|
||||
measurement: measurement,
|
||||
ratedPressures: sharedSettings.ratedStaticPressures,
|
||||
equipmentMeasurement: equipmentMeasurement,
|
||||
filterPressureDrop: parseFilterPressureDrop(formState: form, sharedSettings: sharedSettings),
|
||||
ratedStaticPresures: sharedSettings.ratedStaticPressures,
|
||||
tons: form.coolingCapacity
|
||||
)
|
||||
|
||||
|
||||
return .estimatedFlaggedMeasurement(.init(
|
||||
id: form.id ?? uuid(),
|
||||
estimationState: .init(state: form),
|
||||
estimationState: estimationState,
|
||||
flaggedMeasurement: flaggedMeasurement
|
||||
))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func ensureHasChanges(
|
||||
formState: EstimationForm.State,
|
||||
flaggedEstimations: IdentifiedArrayOf<SharedPressureEstimationState.FlaggedEstimationContainer>
|
||||
) -> SharedPressureEstimationState.FlaggedEstimationContainer.EstimationState? {
|
||||
let estimationState = formState.estimationState
|
||||
|
||||
guard let id = formState.id,
|
||||
let existingState = flaggedEstimations[id: id]?.estimationState
|
||||
else { return estimationState }
|
||||
|
||||
if existingState.hasChanges(estimationState) {
|
||||
return estimationState
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func parseFilterPressureDrop(
|
||||
formState: EstimationForm.State,
|
||||
sharedSettings: SharedPressureEstimationState
|
||||
) -> Positive<Double>? {
|
||||
guard sharedSettings.flaggedEquipmentMeasurement?.filterPressureDrop.wrappedValue != 0,
|
||||
let filterPressureDrop = formState.filterPressureDrop
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
return Positive(filterPressureDrop)
|
||||
}
|
||||
|
||||
private func flaggedMeasurement(
|
||||
airflow: Double,
|
||||
budgets: BudgetedPercentEnvelope,
|
||||
equipmentMeasurement: EquipmentMeasurement,
|
||||
filterPressureDrop: Positive<Double>?,
|
||||
ratedStaticPresures: RatedStaticPressures,
|
||||
tons: EquipmentMetadata.CoolingCapacity
|
||||
) async throws -> EquipmentMeasurement.FlaggedMeasurement {
|
||||
|
||||
let measurement = try await estimatedPressuresClient.estimatedPressure(
|
||||
equipmentMeasurement: equipmentMeasurement,
|
||||
airflow: airflow,
|
||||
filterPressureDrop: filterPressureDrop
|
||||
)
|
||||
|
||||
return .init(
|
||||
budgets: budgets,
|
||||
measurement: measurement,
|
||||
ratedPressures: ratedStaticPresures,
|
||||
tons: tons
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate extension EstimationForm.State {
|
||||
|
||||
var estimationState: SharedPressureEstimationState.FlaggedEstimationContainer.EstimationState {
|
||||
.init(state: self)
|
||||
}
|
||||
}
|
||||
|
||||
@ViewAction(for: FlaggedMeasurementsList.self)
|
||||
|
||||
@@ -36,7 +36,6 @@ public struct SharedPressureEstimationState: Equatable, Sendable {
|
||||
set { equipmentMetadata[keyPath: keyPath] = newValue }
|
||||
}
|
||||
|
||||
#warning("Needs to hold onto estimation state, so it can be editable")
|
||||
@dynamicMemberLookup
|
||||
public struct FlaggedEstimationContainer: Equatable, Identifiable, Sendable {
|
||||
public let id: UUID
|
||||
@@ -92,6 +91,11 @@ public struct SharedPressureEstimationState: Equatable, Sendable {
|
||||
return name
|
||||
}
|
||||
|
||||
// Compare relevant values on if two states have changes.
|
||||
func hasChanges(_ other: Self) -> Bool {
|
||||
cfm != other.cfm || filterPressureDrop != other.filterPressureDrop
|
||||
}
|
||||
|
||||
public enum CFMContainer: Equatable, Sendable {
|
||||
case cfm(Int)
|
||||
case cfmPerTon(Int, EquipmentMetadata.CoolingCapacity)
|
||||
|
||||
Reference in New Issue
Block a user