Working on state and topics.

This commit is contained in:
2021-10-29 09:12:10 -04:00
parent 1971b2fd19
commit 6fd2013376
3 changed files with 124 additions and 37 deletions

View File

@@ -3,26 +3,70 @@ import CoreUnitTypes
public struct State: Equatable {
@TrackedChanges
public var temperature: Temperature?
public var sensors: Sensors
@TrackedChanges
public var humidity: RelativeHumidity?
public init(
temperature: Temperature? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false
) {
self._temperature = .init(wrappedValue: temperature, needsProcessed: needsProcessed)
self._humidity = .init(wrappedValue: humidity, needsProcessed: needsProcessed)
public init(sensors: Sensors = .init()) {
self.sensors = sensors
}
public var needsProcessed: Bool {
$temperature.needsProcessed || $humidity.needsProcessed
public struct Sensors: Equatable {
public var mixedSensor: TemperatureHumiditySensor<Mixed>
public var postCoilSensor: TemperatureHumiditySensor<PostCoil>
public var returnSensor: TemperatureHumiditySensor<Return>
public var supplySensor: TemperatureHumiditySensor<Supply>
public init(
mixedSensor: TemperatureHumiditySensor<Mixed> = .init(),
postCoilSensor: TemperatureHumiditySensor<PostCoil> = .init(),
returnSensor: TemperatureHumiditySensor<Return> = .init(),
supplySensor: TemperatureHumiditySensor<Supply> = .init()
) {
self.mixedSensor = mixedSensor
self.postCoilSensor = postCoilSensor
self.returnSensor = returnSensor
self.supplySensor = supplySensor
}
public var needsProcessed: Bool {
mixedSensor.needsProcessed
|| postCoilSensor.needsProcessed
|| returnSensor.needsProcessed
|| supplySensor.needsProcessed
}
}
}
extension State.Sensors {
public struct TemperatureHumiditySensor<Location>: Equatable {
@TrackedChanges
public var temperature: Temperature?
@TrackedChanges
public var humidity: RelativeHumidity?
public var needsProcessed: Bool {
$temperature.needsProcessed || $humidity.needsProcessed
}
public init(
temperature: Temperature? = nil,
humidity: RelativeHumidity? = nil,
needsProcessed: Bool = false
) {
self._temperature = .init(wrappedValue: temperature, needsProcessed: needsProcessed)
self._humidity = .init(wrappedValue: humidity, needsProcessed: needsProcessed)
}
}
// MARK: - Temperature / Humidity Sensor Locations
public enum Mixed { }
public enum PostCoil { }
public enum Return { }
public enum Supply { }
}
// MARK: - Tracked Changes
@propertyWrapper
public struct TrackedChanges<Value> {

View File

@@ -36,29 +36,72 @@ public struct Topics: Codable, Equatable {
/// Represents the sensor topics.
public struct Sensors: Codable, Equatable {
/// The temperature sensor topic.
public var temperature: String
public var mixedAirSensor: TemperatureAndHumiditySensor<State.Sensors.Mixed>
public var postCoilSensor: TemperatureAndHumiditySensor<State.Sensors.PostCoil>
public var returnSensor: TemperatureAndHumiditySensor<State.Sensors.Return>
public var supplySensor: TemperatureAndHumiditySensor<State.Sensors.Supply>
/// The humidity sensor topic.
public var humidity: String
/// The dew point topic (we write / publish this data from the application).
public var dewPoint: String
/// Create a new sensor topic container.
///
/// - Parameters:
/// - temperature: The temperature sensor topic.
/// - humidity: The humidity sensor topic.
/// - dewPoint: The dew point sensor topic.
// TODO: Fix defaults.
public init(
temperature: String = "sensors/temperature",
humidity: String = "sensors/humidity",
dewPoint: String = "sensors/dew_point"
mixedAirSensor: TemperatureAndHumiditySensor<State.Sensors.Mixed> = .init(),
postCoilSensor: TemperatureAndHumiditySensor<State.Sensors.PostCoil> = .init(),
returnSensor: TemperatureAndHumiditySensor<State.Sensors.Return> = .init(),
supplySensor: TemperatureAndHumiditySensor<State.Sensors.Supply> = .init()
) {
self.temperature = temperature
self.humidity = humidity
self.dewPoint = dewPoint
self.mixedAirSensor = mixedAirSensor
self.postCoilSensor = postCoilSensor
self.returnSensor = returnSensor
self.supplySensor = supplySensor
}
// /// The temperature sensor topic.
// public var temperature: String
//
// /// The humidity sensor topic.
// public var humidity: String
//
// /// The dew point topic (we write / publish this data from the application).
// public var dewPoint: String
//
// /// Create a new sensor topic container.
// ///
// /// - Parameters:
// /// - temperature: The temperature sensor topic.
// /// - humidity: The humidity sensor topic.
// /// - dewPoint: The dew point sensor topic.
// public init(
// temperature: String = "sensors/temperature",
// humidity: String = "sensors/humidity",
// dewPoint: String = "sensors/dew_point"
// ) {
// self.temperature = temperature
// self.humidity = humidity
// self.dewPoint = dewPoint
// }
public struct TemperatureAndHumiditySensor<Location>: Codable, Equatable {
public var temperature: String
public var humidity: String
public var dewPoint: String
public var enthalpy: String
/// Create a new sensor topic container.
///
/// - Parameters:
/// - temperature: The temperature sensor topic.
/// - humidity: The humidity sensor topic.
/// - dewPoint: The dew point sensor topic.
public init(
temperature: String = "sensors/temperature",
humidity: String = "sensors/humidity",
dewPoint: String = "sensors/dew_point",
enthalpy: String = "sensors/enthalpy"
) {
self.temperature = temperature
self.humidity = humidity
self.dewPoint = dewPoint
self.enthalpy = enthalpy
}
}
}