feat: Working on async integrations.

This commit is contained in:
2024-11-08 11:05:07 -05:00
parent 408e0484cd
commit e6d1d4578d
8 changed files with 245 additions and 116 deletions

View File

@@ -1,65 +1,117 @@
import Psychrometrics
/// Represents a temperature and humidity sensor that can be used to derive
/// the dew-point temperature and enthalpy values.
///
public struct TemperatureAndHumiditySensor: Equatable, Identifiable {
/// The identifier of the sensor, same as the location.
public var id: Location { location }
public let altitude: Length
/// The identifier of the sensor, same as the location.
public var id: Location { location }
/// The location identifier of the sensor
public let location: Location
/// The altitude of the sensor.
public let altitude: Length
/// The current temperature value of the sensor.
@TrackedChanges
public var temperature: Temperature?
/// The current humidity value of the sensor.
@TrackedChanges
public var humidity: RelativeHumidity?
/// The current humidity value of the sensor.
@TrackedChanges
public var humidity: RelativeHumidity?
/// The location identifier of the sensor
public let location: Location
/// The psychrometric units of the sensor.
public let units: PsychrometricEnvironment.Units
/// The current temperature value of the sensor.
@TrackedChanges
public var temperature: Temperature?
/// The topics to listen for updated sensor values.
public let topics: Topics
/// The psychrometric units of the sensor.
public let units: PsychrometricEnvironment.Units
/// Create a new temperature and humidity sensor.
///
/// - Parameters:
/// - location: The location of the sensor.
/// - altitude: The altitude of the sensor.
/// - temperature: The current temperature value of the sensor.
/// - humidity: The current relative humidity value of the sensor.
/// - needsProcessed: If the sensor needs to be processed.
/// - units: The unit of measure for the sensor.
public init(
location: Location,
altitude: Length = .feet(800.0),
temperature: Temperature? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false,
units: PsychrometricEnvironment.Units = .imperial,
topics: Topics? = nil
) {
self.altitude = altitude
self.location = location
self._temperature = TrackedChanges(wrappedValue: temperature, needsProcessed: needsProcessed)
self._humidity = TrackedChanges(wrappedValue: humidity, needsProcessed: needsProcessed)
self.units = units
self.topics = topics ?? .init(location: location)
}
/// 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, units: units)
}
/// 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, units: units)
}
/// Check whether any of the sensor values have changed and need processed.
public var needsProcessed: Bool {
get { $temperature.needsProcessed || $humidity.needsProcessed }
set {
$temperature.needsProcessed = newValue
$humidity.needsProcessed = newValue
}
}
/// 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, Equatable, Hashable {
case mixedAir = "mixed-air"
case postCoil = "post-coil"
case `return`
case supply
}
/// Represents the MQTT topics to listen for updated sensor values on.
public struct Topics: Equatable {
/// The temperature topic of the sensor.
public let temperature: String
/// The humidity topic of the sensor.
public let humidity: String
public init(
location: Location,
altitude: Length = .feet(800.0),
temperature: Temperature? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false,
units: PsychrometricEnvironment.Units = .imperial
temperature: String,
humidity: String
) {
self.altitude = altitude
self.location = location
self._temperature = .init(wrappedValue: temperature, needsProcessed: needsProcessed)
self._humidity = .init(wrappedValue: humidity, needsProcessed: needsProcessed)
self.units = units
self.temperature = temperature
self.humidity = humidity
}
/// 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, units: units)
}
/// Check whether any of the sensor values have changed and need processed.
public var needsProcessed: Bool {
get { $temperature.needsProcessed || $humidity.needsProcessed }
set {
$temperature.needsProcessed = newValue
$humidity.needsProcessed = newValue
}
}
/// 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, Equatable, Hashable {
case mixedAir = "mixed-air"
case postCoil = "post-coil"
case `return`
case supply
init(location: TemperatureAndHumiditySensor.Location) {
self.temperature = "sensors/\(location.rawValue)/temperature"
self.humidity = "sensors/\(location.rawValue)/temperature"
}
}
}

View File

@@ -1,7 +1,5 @@
/// A container for all the different MQTT topics that are needed by the application.
public struct Topics: Codable, Equatable {
/// The command topics the application can publish to.
public var commands: Commands
@@ -35,7 +33,6 @@ public struct Topics: Codable, Equatable {
/// Represents the sensor topics.
public struct Sensors: Codable, Equatable {
public var mixedAirSensor: TemperatureAndHumiditySensor<State.Sensors.MixedAir>
public var postCoilSensor: TemperatureAndHumiditySensor<State.Sensors.PostCoil>
public var returnAirSensor: TemperatureAndHumiditySensor<State.Sensors.Return>
@@ -81,7 +78,6 @@ public struct Topics: Codable, Equatable {
/// A container for set point related topics used by the application.
public struct SetPoints: Codable, Equatable {
/// The topic for the humidify set point.
public var humidify: Humidify
@@ -103,7 +99,6 @@ public struct Topics: Codable, Equatable {
/// A container for the humidification set point topics used by the application.
public struct Humidify: Codable, Equatable {
/// The topic for dew point control mode set point.
public var dewPoint: String
@@ -126,7 +121,6 @@ public struct Topics: Codable, Equatable {
/// A container for dehumidifcation set point topics.
public struct Dehumidify: Codable, Equatable {
/// A low setting for dew point control modes.
public var lowDewPoint: String
@@ -162,7 +156,6 @@ public struct Topics: Codable, Equatable {
/// A container for control state topics used by the application.
public struct States: Codable, Equatable {
/// The topic for the control mode.
public var mode: String
@@ -183,7 +176,6 @@ public struct Topics: Codable, Equatable {
/// A container for reading the current state of a relay.
public struct Relays: Codable, Equatable {
/// The dehumidification stage-1 relay topic.
public var dehumdification1: String
@@ -213,7 +205,6 @@ public struct Topics: Codable, Equatable {
/// A container for commands topics that the application can publish to.
public struct Commands: Codable, Equatable {
/// The relay command topics.
public var relays: Relays
@@ -227,7 +218,6 @@ public struct Topics: Codable, Equatable {
/// A container for relay command topics used by the application.
public struct Relays: Codable, Equatable {
/// The dehumidification stage-1 relay topic.
public var dehumidification1: String
@@ -257,8 +247,9 @@ public struct Topics: Codable, Equatable {
}
// MARK: Helpers
extension Topics.Sensors.TemperatureAndHumiditySensor {
public static func `default`(location: String) -> Self {
public extension Topics.Sensors.TemperatureAndHumiditySensor {
static func `default`(location: String) -> Self {
.init(
temperature: "sensors/\(location)/temperature",
humidity: "sensors/\(location)/humidity",