feat: Updates to newer psychrometrics package. Not yet a working example.

This commit is contained in:
2024-11-09 11:35:30 -05:00
parent e2683d3f06
commit a87addaf0b
13 changed files with 821 additions and 737 deletions

View File

@@ -1,4 +1,4 @@
import CoreUnitTypes
import PsychrometricClient
// TODO: Remove
@@ -21,7 +21,7 @@ public enum Mode: Equatable {
public enum HumidifyMode: Equatable {
/// Control humidifying based off dew-point.
case dewPoint(Temperature)
case dewPoint(DewPoint)
/// Control humidifying based off relative humidity.
case relativeHumidity(RelativeHumidity)
@@ -31,7 +31,7 @@ public enum Mode: Equatable {
public enum DehumidifyMode: Equatable {
/// Control dehumidifying based off dew-point.
case dewPoint(high: Temperature, low: Temperature)
case dewPoint(high: DewPoint, low: DewPoint)
/// Control humidifying based off relative humidity.
case relativeHumidity(high: RelativeHumidity, low: RelativeHumidity)

View File

@@ -1,5 +1,5 @@
import Foundation
@preconcurrency import Psychrometrics
import PsychrometricClient
// TODO: Remove
// TODO: Make this a struct, then create a Store class that holds the state??
@@ -7,16 +7,12 @@ public final class State {
public var altitude: Length
public var sensors: Sensors
public var units: PsychrometricEnvironment.Units {
didSet {
PsychrometricEnvironment.shared.units = units
}
}
public var units: PsychrometricUnits
public init(
altitude: Length = .seaLevel,
sensors: Sensors = .init(),
units: PsychrometricEnvironment.Units = .imperial
units: PsychrometricUnits = .imperial
) {
self.altitude = altitude
self.sensors = sensors
@@ -56,7 +52,7 @@ public extension State.Sensors {
struct TemperatureHumiditySensor<Location>: Equatable {
@TrackedChanges
public var temperature: Temperature?
public var temperature: DryBulb?
@TrackedChanges
public var humidity: RelativeHumidity?
@@ -69,26 +65,26 @@ public extension State.Sensors {
}
}
public func dewPoint(units: PsychrometricEnvironment.Units? = nil) -> DewPoint? {
// WARN: Fix me.
public func dewPoint(units _: PsychrometricUnits? = nil) -> DewPoint? {
guard let temperature = temperature,
let humidity = humidity,
!temperature.rawValue.isNaN,
!humidity.rawValue.isNaN
let humidity = humidity
else { return nil }
return .init(dryBulb: temperature, humidity: humidity, units: units)
return nil
// return .init(dryBulb: temperature, humidity: humidity, units: units)
}
public func enthalpy(altitude: Length, units: PsychrometricEnvironment.Units? = nil) -> EnthalpyOf<MoistAir>? {
// WARN: Fix me.
public func enthalpy(altitude _: Length, units _: PsychrometricUnits? = nil) -> EnthalpyOf<MoistAir>? {
guard let temperature = temperature,
let humidity = humidity,
!temperature.rawValue.isNaN,
!humidity.rawValue.isNaN
let humidity = humidity
else { return nil }
return .init(dryBulb: temperature, humidity: humidity, altitude: altitude, units: units)
return nil
// return .init(dryBulb: temperature, humidity: humidity, altitude: altitude, units: units)
}
public init(
temperature: Temperature? = nil,
temperature: DryBulb? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false
) {

View File

@@ -1,10 +1,13 @@
@preconcurrency import Psychrometrics
import Dependencies
import PsychrometricClient
/// Represents a temperature and humidity sensor that can be used to derive
/// the dew-point temperature and enthalpy values.
///
/// > Note: Temperature values are received in `celsius`.
public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @unchecked Sendable {
public struct TemperatureAndHumiditySensor: Identifiable, Sendable {
@Dependency(\.psychrometricClient) private var psychrometrics
/// The identifier of the sensor, same as the location.
public var id: Location { location }
@@ -21,7 +24,7 @@ public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @
/// The current temperature value of the sensor.
@TrackedChanges
public var temperature: Temperature?
public var temperature: DryBulb?
/// The topics to listen for updated sensor values.
public let topics: Topics
@@ -37,7 +40,7 @@ public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @
public init(
location: Location,
altitude: Length = .feet(800.0),
temperature: Temperature? = nil,
temperature: DryBulb? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false,
topics: Topics? = nil
@@ -51,22 +54,26 @@ public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @
/// The calculated dew-point temperature of the sensor.
public var dewPoint: DewPoint? {
guard let temperature = temperature,
let humidity = humidity,
!temperature.rawValue.isNaN,
!humidity.rawValue.isNaN
else { return nil }
return .init(dryBulb: temperature, humidity: humidity)
get async {
guard let temperature = temperature,
let humidity = humidity
else { return nil }
return try? await psychrometrics.dewPoint(.dryBulb(temperature, relativeHumidity: humidity))
// return .init(dryBulb: temperature, humidity: humidity)
}
}
/// The calculated enthalpy of the sensor.
public var enthalpy: EnthalpyOf<MoistAir>? {
guard let temperature = temperature,
let humidity = humidity,
!temperature.rawValue.isNaN,
!humidity.rawValue.isNaN
else { return nil }
return .init(dryBulb: temperature, humidity: humidity, altitude: altitude)
get async {
guard let temperature = temperature,
let humidity = humidity
else { return nil }
return try? await psychrometrics.enthalpy.moistAir(
.dryBulb(temperature, relativeHumidity: humidity, altitude: altitude)
)
// return .init(dryBulb: temperature, humidity: humidity, altitude: altitude)
}
}
/// Check whether any of the sensor values have changed and need processed.
@@ -82,7 +89,7 @@ public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @
/// Represents the different locations of a temperature and humidity sensor, which can
/// be used to derive the topic to both listen and publish new values to.
public enum Location: String, CaseIterable, Equatable, Hashable {
public enum Location: String, CaseIterable, Equatable, Hashable, Sendable {
case mixedAir = "mixed_air"
case postCoil = "post_coil"
case `return`
@@ -90,7 +97,7 @@ public struct TemperatureAndHumiditySensor: Equatable, Hashable, Identifiable, @
}
/// Represents the MQTT topics to listen for updated sensor values on.
public struct Topics: Equatable, Hashable {
public struct Topics: Equatable, Hashable, Sendable {
/// The dew-point temperature topic for the sensor.
public let dewPoint: String

View File

@@ -96,3 +96,5 @@ extension TrackedChanges: Hashable where Value: Hashable {
hasher.combine(needsProcessed)
}
}
extension TrackedChanges: Sendable where Value: Sendable {}