feat: Removes sensor client in favor of more generic topic listener and publisher
This commit is contained in:
@@ -13,11 +13,10 @@ let package = Package(
|
|||||||
.macOS(.v14)
|
.macOS(.v14)
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
.executable(name: "dewpoint-controller", targets: ["dewPoint-controller"]),
|
.executable(name: "dewpoint-controller", targets: ["dewpoint-controller"]),
|
||||||
.library(name: "Models", targets: ["Models"]),
|
.library(name: "Models", targets: ["Models"]),
|
||||||
.library(name: "MQTTConnectionManagerLive", targets: ["MQTTConnectionManagerLive"]),
|
.library(name: "MQTTConnectionManagerLive", targets: ["MQTTConnectionManagerLive"]),
|
||||||
.library(name: "MQTTConnectionService", targets: ["MQTTConnectionService"]),
|
.library(name: "MQTTConnectionService", targets: ["MQTTConnectionService"]),
|
||||||
.library(name: "SensorsClientLive", targets: ["SensorsClientLive"]),
|
|
||||||
.library(name: "SensorsService", targets: ["SensorsService"]),
|
.library(name: "SensorsService", targets: ["SensorsService"]),
|
||||||
.library(name: "TopicDependencies", targets: ["TopicDependencies"])
|
.library(name: "TopicDependencies", targets: ["TopicDependencies"])
|
||||||
],
|
],
|
||||||
@@ -31,11 +30,12 @@ let package = Package(
|
|||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.executableTarget(
|
.executableTarget(
|
||||||
name: "dewPoint-controller",
|
name: "dewpoint-controller",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
"Models",
|
"Models",
|
||||||
"MQTTConnectionManagerLive",
|
"MQTTConnectionManagerLive",
|
||||||
"SensorsClientLive",
|
"SensorsService",
|
||||||
|
"TopicDependencies",
|
||||||
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
||||||
.product(name: "NIO", package: "swift-nio"),
|
.product(name: "NIO", package: "swift-nio"),
|
||||||
.product(name: "PsychrometricClientLive", package: "swift-psychrometrics")
|
.product(name: "PsychrometricClientLive", package: "swift-psychrometrics")
|
||||||
@@ -74,20 +74,12 @@ let package = Package(
|
|||||||
.product(name: "ServiceLifecycleTestKit", package: "swift-service-lifecycle")
|
.product(name: "ServiceLifecycleTestKit", package: "swift-service-lifecycle")
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.target(
|
|
||||||
name: "SensorsClientLive",
|
|
||||||
dependencies: [
|
|
||||||
"SensorsService",
|
|
||||||
.product(name: "Dependencies", package: "swift-dependencies"),
|
|
||||||
.product(name: "MQTTNIO", package: "mqtt-nio")
|
|
||||||
],
|
|
||||||
swiftSettings: swiftSettings
|
|
||||||
),
|
|
||||||
.target(
|
.target(
|
||||||
name: "SensorsService",
|
name: "SensorsService",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
"Models",
|
"Models",
|
||||||
"MQTTConnectionService",
|
"MQTTConnectionService",
|
||||||
|
"TopicDependencies",
|
||||||
.product(name: "Dependencies", package: "swift-dependencies"),
|
.product(name: "Dependencies", package: "swift-dependencies"),
|
||||||
.product(name: "DependenciesMacros", package: "swift-dependencies"),
|
.product(name: "DependenciesMacros", package: "swift-dependencies"),
|
||||||
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ extension MQTTConnectionManager: TestDependencyKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public extension DependencyValues {
|
public extension DependencyValues {
|
||||||
|
|
||||||
|
/// A dependency that is responsible for managing the connection to
|
||||||
|
/// an MQTT broker.
|
||||||
var mqttConnectionManager: MQTTConnectionManager {
|
var mqttConnectionManager: MQTTConnectionManager {
|
||||||
get { self[MQTTConnectionManager.self] }
|
get { self[MQTTConnectionManager.self] }
|
||||||
set { self[MQTTConnectionManager.self] = newValue }
|
set { self[MQTTConnectionManager.self] = newValue }
|
||||||
|
|||||||
@@ -56,10 +56,11 @@ public struct TemperatureAndHumiditySensor: Identifiable, Sendable {
|
|||||||
public var dewPoint: DewPoint? {
|
public var dewPoint: DewPoint? {
|
||||||
get async {
|
get async {
|
||||||
guard let temperature = temperature,
|
guard let temperature = temperature,
|
||||||
let humidity = humidity
|
let humidity = humidity,
|
||||||
|
!temperature.value.isNaN,
|
||||||
|
!humidity.value.isNaN
|
||||||
else { return nil }
|
else { return nil }
|
||||||
return try? await psychrometrics.dewPoint(.dryBulb(temperature, relativeHumidity: humidity))
|
return try? await psychrometrics.dewPoint(.dryBulb(temperature, relativeHumidity: humidity))
|
||||||
// return .init(dryBulb: temperature, humidity: humidity)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,12 +68,13 @@ public struct TemperatureAndHumiditySensor: Identifiable, Sendable {
|
|||||||
public var enthalpy: EnthalpyOf<MoistAir>? {
|
public var enthalpy: EnthalpyOf<MoistAir>? {
|
||||||
get async {
|
get async {
|
||||||
guard let temperature = temperature,
|
guard let temperature = temperature,
|
||||||
let humidity = humidity
|
let humidity = humidity,
|
||||||
|
!temperature.value.isNaN,
|
||||||
|
!humidity.value.isNaN
|
||||||
else { return nil }
|
else { return nil }
|
||||||
return try? await psychrometrics.enthalpy.moistAir(
|
return try? await psychrometrics.enthalpy.moistAir(
|
||||||
.dryBulb(temperature, relativeHumidity: humidity, altitude: altitude)
|
.dryBulb(temperature, relativeHumidity: humidity, altitude: altitude)
|
||||||
)
|
)
|
||||||
// return .init(dryBulb: temperature, humidity: humidity, altitude: altitude)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,126 +0,0 @@
|
|||||||
import Dependencies
|
|
||||||
import Foundation
|
|
||||||
import MQTTNIO
|
|
||||||
import NIO
|
|
||||||
@_exported import SensorsService
|
|
||||||
|
|
||||||
public extension SensorsClient {
|
|
||||||
|
|
||||||
/// Creates the live implementation of the sensor client.
|
|
||||||
static func live(
|
|
||||||
client: MQTTClient,
|
|
||||||
publishQoS: MQTTQoS = .exactlyOnce,
|
|
||||||
subscribeQoS: MQTTQoS = .atLeastOnce
|
|
||||||
) -> Self {
|
|
||||||
let listener = SensorClientListener(
|
|
||||||
client: client,
|
|
||||||
publishQoS: publishQoS,
|
|
||||||
subscribeQoS: subscribeQoS
|
|
||||||
)
|
|
||||||
|
|
||||||
return .init(
|
|
||||||
listen: { try await listener.listen($0) },
|
|
||||||
publish: { try await listener.publish($0, $1) },
|
|
||||||
shutdown: { listener.shutdown() }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ConnectionTimeoutError: Error {}
|
|
||||||
|
|
||||||
private actor SensorClientListener {
|
|
||||||
|
|
||||||
let client: MQTTClient
|
|
||||||
private let continuation: AsyncStream<SensorsClient.PublishInfo>.Continuation
|
|
||||||
let name: String
|
|
||||||
let publishQoS: MQTTQoS
|
|
||||||
let stream: AsyncStream<SensorsClient.PublishInfo>
|
|
||||||
let subscribeQoS: MQTTQoS
|
|
||||||
|
|
||||||
init(
|
|
||||||
client: MQTTClient,
|
|
||||||
publishQoS: MQTTQoS,
|
|
||||||
subscribeQoS: MQTTQoS
|
|
||||||
) {
|
|
||||||
let (stream, continuation) = AsyncStream<SensorsClient.PublishInfo>.makeStream()
|
|
||||||
self.client = client
|
|
||||||
self.continuation = continuation
|
|
||||||
self.name = UUID().uuidString
|
|
||||||
self.publishQoS = publishQoS
|
|
||||||
self.stream = stream
|
|
||||||
self.subscribeQoS = subscribeQoS
|
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
client.logger.trace("Sensor listener is gone.")
|
|
||||||
self.client.removeCloseListener(named: name)
|
|
||||||
self.client.removePublishListener(named: name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func listen(_ topics: [String]) async throws -> AsyncStream<SensorsClient.PublishInfo> {
|
|
||||||
client.logger.trace("Begin listen...")
|
|
||||||
// Ensure we are subscribed to the topics.
|
|
||||||
var sleepTimes = 0
|
|
||||||
|
|
||||||
while !client.isActive() {
|
|
||||||
guard sleepTimes < 10 else {
|
|
||||||
throw ConnectionTimeoutError()
|
|
||||||
}
|
|
||||||
try await Task.sleep(for: .milliseconds(100))
|
|
||||||
sleepTimes += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
client.logger.trace("Connection is active, begin listening for updates.")
|
|
||||||
client.logger.trace("Topics: \(topics)")
|
|
||||||
|
|
||||||
_ = try await client.subscribe(to: topics.map { topic in
|
|
||||||
MQTTSubscribeInfo(topicFilter: topic, qos: subscribeQoS)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.logger.trace("Done subscribing to topics.")
|
|
||||||
|
|
||||||
client.addPublishListener(named: name) { result in
|
|
||||||
self.client.logger.trace("Received new result...")
|
|
||||||
switch result {
|
|
||||||
case let .failure(error):
|
|
||||||
self.client.logger.error("Received error while listening: \(error)")
|
|
||||||
case let .success(publishInfo):
|
|
||||||
// Only publish values back to caller if they are listening to a
|
|
||||||
// the topic.
|
|
||||||
if topics.contains(publishInfo.topicName) {
|
|
||||||
self.client.logger.trace("Recieved published info for: \(publishInfo.topicName)")
|
|
||||||
self.continuation.yield((buffer: publishInfo.payload, topic: publishInfo.topicName))
|
|
||||||
} else {
|
|
||||||
self.client.logger.trace("Skipping topic: \(publishInfo.topicName)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
client.addShutdownListener(named: name) { _ in
|
|
||||||
self.continuation.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream
|
|
||||||
}
|
|
||||||
|
|
||||||
func publish(_ double: Double, _ topic: String) async throws {
|
|
||||||
// Ensure the client is active before publishing values.
|
|
||||||
guard client.isActive() else { return }
|
|
||||||
|
|
||||||
// Round the double and publish.
|
|
||||||
let rounded = round(double * 100) / 100
|
|
||||||
client.logger.trace("Begin publishing: \(rounded) to: \(topic)")
|
|
||||||
try await client.publish(
|
|
||||||
to: topic,
|
|
||||||
payload: ByteBufferAllocator().buffer(string: "\(rounded)"),
|
|
||||||
qos: publishQoS,
|
|
||||||
retain: true
|
|
||||||
)
|
|
||||||
client.logger.trace("Begin publishing: \(rounded) to: \(topic)")
|
|
||||||
}
|
|
||||||
|
|
||||||
nonisolated func shutdown() {
|
|
||||||
continuation.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -3,56 +3,11 @@ import DependenciesMacros
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Logging
|
import Logging
|
||||||
import Models
|
import Models
|
||||||
|
import MQTTNIO
|
||||||
import NIO
|
import NIO
|
||||||
import PsychrometricClient
|
import PsychrometricClient
|
||||||
import ServiceLifecycle
|
import ServiceLifecycle
|
||||||
|
import TopicDependencies
|
||||||
/// Represents the interface required for the sensor service to operate.
|
|
||||||
///
|
|
||||||
/// This allows the dependency to be controlled for testing purposes and
|
|
||||||
/// not rely on an active MQTT broker connection.
|
|
||||||
///
|
|
||||||
/// For the live implementation see ``SensorsClientLive`` module.
|
|
||||||
///
|
|
||||||
@DependencyClient
|
|
||||||
public struct SensorsClient: Sendable {
|
|
||||||
|
|
||||||
public typealias PublishInfo = (buffer: ByteBuffer, topic: String)
|
|
||||||
|
|
||||||
/// Start listening for changes to sensor values on the MQTT broker.
|
|
||||||
public var listen: @Sendable ([String]) async throws -> AsyncStream<PublishInfo>
|
|
||||||
|
|
||||||
/// Publish dew-point or enthalpy values back to the MQTT broker.
|
|
||||||
public var publish: @Sendable (Double, String) async throws -> Void
|
|
||||||
|
|
||||||
/// Shutdown the service.
|
|
||||||
public var shutdown: @Sendable () -> Void = {}
|
|
||||||
|
|
||||||
/// Start listening for changes to sensor values on the MQTT broker.
|
|
||||||
public func listen(to topics: [String]) async throws -> AsyncStream<PublishInfo> {
|
|
||||||
try await listen(topics)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Publish dew-point or enthalpy values back to the MQTT broker.
|
|
||||||
public func publish(_ value: Double, to topic: String) async throws {
|
|
||||||
try await publish(value, topic)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension SensorsClient: TestDependencyKey {
|
|
||||||
public static var testValue: SensorsClient {
|
|
||||||
Self()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public extension DependencyValues {
|
|
||||||
var sensorsClient: SensorsClient {
|
|
||||||
get { self[SensorsClient.self] }
|
|
||||||
set { self[SensorsClient.self] = newValue }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - SensorsService
|
|
||||||
|
|
||||||
/// Service that is responsible for listening to changes of the temperature and humidity
|
/// Service that is responsible for listening to changes of the temperature and humidity
|
||||||
/// sensors, then publishing back the calculated dew-point temperature and enthalpy for
|
/// sensors, then publishing back the calculated dew-point temperature and enthalpy for
|
||||||
@@ -61,12 +16,24 @@ public extension DependencyValues {
|
|||||||
///
|
///
|
||||||
public actor SensorsService: Service {
|
public actor SensorsService: Service {
|
||||||
|
|
||||||
@Dependency(\.sensorsClient) var client
|
@Dependency(\.topicListener) var topicListener
|
||||||
|
@Dependency(\.topicPublisher) var topicPublisher
|
||||||
private var sensors: [TemperatureAndHumiditySensor]
|
|
||||||
|
|
||||||
|
/// The logger to use for the service.
|
||||||
private let logger: Logger?
|
private let logger: Logger?
|
||||||
|
|
||||||
|
/// The sensors that we are listening for updates to, so
|
||||||
|
/// that we can calculate the dew-point temperature and enthalpy
|
||||||
|
/// values to publish back to the MQTT broker.
|
||||||
|
var sensors: [TemperatureAndHumiditySensor]
|
||||||
|
|
||||||
|
var topics: [String] {
|
||||||
|
sensors.reduce(into: [String]()) { array, sensor in
|
||||||
|
array.append(sensor.topics.temperature)
|
||||||
|
array.append(sensor.topics.humidity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new sensors service that listens to the passed in
|
/// Create a new sensors service that listens to the passed in
|
||||||
/// sensors.
|
/// sensors.
|
||||||
///
|
///
|
||||||
@@ -74,6 +41,7 @@ public actor SensorsService: Service {
|
|||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - sensors: The sensors to listen for changes to.
|
/// - sensors: The sensors to listen for changes to.
|
||||||
|
/// - logger: An optional logger to use.
|
||||||
public init(
|
public init(
|
||||||
sensors: [TemperatureAndHumiditySensor],
|
sensors: [TemperatureAndHumiditySensor],
|
||||||
logger: Logger? = nil
|
logger: Logger? = nil
|
||||||
@@ -85,33 +53,50 @@ public actor SensorsService: Service {
|
|||||||
/// Start the service with graceful shutdown, which will attempt to publish
|
/// Start the service with graceful shutdown, which will attempt to publish
|
||||||
/// any pending changes to the MQTT broker, upon a shutdown signal.
|
/// any pending changes to the MQTT broker, upon a shutdown signal.
|
||||||
public func run() async throws {
|
public func run() async throws {
|
||||||
precondition(sensors.count > 0)
|
precondition(sensors.count > 0, "Sensors should not be empty.")
|
||||||
|
|
||||||
let stream = try await client.listen(to: topics)
|
let stream = try await makeStream()
|
||||||
|
|
||||||
await withGracefulShutdownHandler {
|
await withGracefulShutdownHandler {
|
||||||
await withDiscardingTaskGroup { group in
|
await withDiscardingTaskGroup { group in
|
||||||
for await result in stream {
|
for await result in stream.cancelOnGracefulShutdown() {
|
||||||
|
logger?.trace("Received result for topic: \(result.topic)")
|
||||||
group.addTask { await self.handleResult(result) }
|
group.addTask { await self.handleResult(result) }
|
||||||
}
|
}
|
||||||
|
// group.cancelAll()
|
||||||
}
|
}
|
||||||
} onGracefulShutdown: {
|
} onGracefulShutdown: {
|
||||||
Task {
|
Task {
|
||||||
self.logger?.trace("Received graceful shutdown.")
|
self.logger?.trace("Received graceful shutdown.")
|
||||||
try? await self.publishUpdates()
|
try? await self.publishUpdates()
|
||||||
await self.client.shutdown()
|
await self.topicListener.shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var topics: [String] {
|
private func makeStream() async throws -> AsyncStream<(buffer: ByteBuffer, topic: String)> {
|
||||||
sensors.reduce(into: [String]()) { array, sensor in
|
try await topicListener.listen(to: topics)
|
||||||
array.append(sensor.topics.temperature)
|
// ignore errors, so that we continue to listen, but log them
|
||||||
array.append(sensor.topics.humidity)
|
// for debugging purposes.
|
||||||
}
|
.compactMap { result in
|
||||||
|
switch result {
|
||||||
|
case let .failure(error):
|
||||||
|
self.logger?.trace("Received error listening for sensors: \(error)")
|
||||||
|
return nil
|
||||||
|
case let .success(info):
|
||||||
|
return (info.payload, info.topicName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ignore duplicate values, to prevent publishing dew-point and enthalpy
|
||||||
|
// changes to frequently.
|
||||||
|
.removeDuplicates { lhs, rhs in
|
||||||
|
lhs.buffer == rhs.buffer
|
||||||
|
&& lhs.topic == rhs.topic
|
||||||
|
}
|
||||||
|
.eraseToStream()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleResult(_ result: SensorsClient.PublishInfo) async {
|
private func handleResult(_ result: (buffer: ByteBuffer, topic: String)) async {
|
||||||
do {
|
do {
|
||||||
let topic = result.topic
|
let topic = result.topic
|
||||||
assert(topics.contains(topic))
|
assert(topics.contains(topic))
|
||||||
@@ -150,7 +135,12 @@ public actor SensorsService: Service {
|
|||||||
|
|
||||||
private func publish(_ double: Double?, to topic: String) async throws {
|
private func publish(_ double: Double?, to topic: String) async throws {
|
||||||
guard let double else { return }
|
guard let double else { return }
|
||||||
try await client.publish(double, to: topic)
|
try await topicPublisher.publish(
|
||||||
|
to: topic,
|
||||||
|
payload: ByteBufferAllocator().buffer(string: "\(double)"),
|
||||||
|
qos: .exactlyOnce,
|
||||||
|
retain: true
|
||||||
|
)
|
||||||
logger?.trace("Published update to topic: \(topic)")
|
logger?.trace("Published update to topic: \(topic)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +148,7 @@ public actor SensorsService: Service {
|
|||||||
for sensor in sensors.filter(\.needsProcessed) {
|
for sensor in sensors.filter(\.needsProcessed) {
|
||||||
try await publish(sensor.dewPoint?.value, to: sensor.topics.dewPoint)
|
try await publish(sensor.dewPoint?.value, to: sensor.topics.dewPoint)
|
||||||
try await publish(sensor.enthalpy?.value, to: sensor.topics.enthalpy)
|
try await publish(sensor.enthalpy?.value, to: sensor.topics.enthalpy)
|
||||||
|
try sensors.hasProcessed(sensor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ import MQTTNIO
|
|||||||
@DependencyClient
|
@DependencyClient
|
||||||
public struct TopicListener: Sendable {
|
public struct TopicListener: Sendable {
|
||||||
|
|
||||||
|
public typealias Stream = AsyncStream<Result<MQTTPublishInfo, MQTTListenResultError>>
|
||||||
|
|
||||||
/// Create an async stream that listens for changes to the given topics.
|
/// Create an async stream that listens for changes to the given topics.
|
||||||
private var _listen: @Sendable (_ topics: [String]) async throws -> AsyncThrowingStream<MQTTPublishInfo, any Error>
|
private var _listen: @Sendable ([String], MQTTQoS) async throws -> Stream
|
||||||
|
|
||||||
/// Shutdown the listener stream.
|
/// Shutdown the listener stream.
|
||||||
public var shutdown: @Sendable () -> Void
|
public var shutdown: @Sendable () -> Void
|
||||||
@@ -22,7 +24,7 @@ public struct TopicListener: Sendable {
|
|||||||
/// - listen: Generate an async stream of changes for the given topics.
|
/// - listen: Generate an async stream of changes for the given topics.
|
||||||
/// - shutdown: Shutdown the topic listener stream.
|
/// - shutdown: Shutdown the topic listener stream.
|
||||||
public init(
|
public init(
|
||||||
listen: @Sendable @escaping ([String]) async throws -> AsyncThrowingStream<MQTTPublishInfo, any Error>,
|
listen: @Sendable @escaping ([String], MQTTQoS) async throws -> Stream,
|
||||||
shutdown: @Sendable @escaping () -> Void
|
shutdown: @Sendable @escaping () -> Void
|
||||||
) {
|
) {
|
||||||
self._listen = listen
|
self._listen = listen
|
||||||
@@ -33,16 +35,24 @@ public struct TopicListener: Sendable {
|
|||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - topics: The topics to listen for changes to.
|
/// - topics: The topics to listen for changes to.
|
||||||
public func listen(to topics: [String]) async throws -> AsyncThrowingStream<MQTTPublishInfo, any Error> {
|
/// - qos: The MQTTQoS for the subscription.
|
||||||
try await _listen(topics)
|
public func listen(
|
||||||
|
to topics: [String],
|
||||||
|
qos: MQTTQoS = .atLeastOnce
|
||||||
|
) async throws -> Stream {
|
||||||
|
try await _listen(topics, qos)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an async stream that listens for changes to the given topics.
|
/// Create an async stream that listens for changes to the given topics.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - topics: The topics to listen for changes to.
|
/// - topics: The topics to listen for changes to.
|
||||||
public func listen(_ topics: String...) async throws -> AsyncThrowingStream<MQTTPublishInfo, any Error> {
|
/// - qos: The MQTTQoS for the subscription.
|
||||||
try await listen(to: topics)
|
public func listen(
|
||||||
|
_ topics: String...,
|
||||||
|
qos: MQTTQoS = .atLeastOnce
|
||||||
|
) async throws -> Stream {
|
||||||
|
try await listen(to: topics, qos: qos)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create the live implementation of the topic listener with the given MQTTClient.
|
/// Create the live implementation of the topic listener with the given MQTTClient.
|
||||||
@@ -52,16 +62,14 @@ public struct TopicListener: Sendable {
|
|||||||
public static func live(client: MQTTClient) -> Self {
|
public static func live(client: MQTTClient) -> Self {
|
||||||
let listener = MQTTTopicListener(client: client)
|
let listener = MQTTTopicListener(client: client)
|
||||||
return .init(
|
return .init(
|
||||||
listen: { await listener.listen($0) },
|
listen: { try await listener.listen($0, $1) },
|
||||||
shutdown: { listener.shutdown() }
|
shutdown: { listener.shutdown() }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension TopicListener: TestDependencyKey {
|
extension TopicListener: TestDependencyKey {
|
||||||
public static var testValue: TopicListener {
|
public static var testValue: TopicListener { Self() }
|
||||||
Self()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension DependencyValues {
|
public extension DependencyValues {
|
||||||
@@ -75,15 +83,15 @@ public extension DependencyValues {
|
|||||||
|
|
||||||
private actor MQTTTopicListener {
|
private actor MQTTTopicListener {
|
||||||
private let client: MQTTClient
|
private let client: MQTTClient
|
||||||
private let continuation: AsyncThrowingStream<MQTTPublishInfo, any Error>.Continuation
|
private let continuation: TopicListener.Stream.Continuation
|
||||||
private let name: String
|
private let name: String
|
||||||
let stream: AsyncThrowingStream<MQTTPublishInfo, any Error>
|
let stream: TopicListener.Stream
|
||||||
private var shuttingDown: Bool = false
|
private var shuttingDown: Bool = false
|
||||||
|
|
||||||
init(
|
init(
|
||||||
client: MQTTClient
|
client: MQTTClient
|
||||||
) {
|
) {
|
||||||
let (stream, continuation) = AsyncThrowingStream<MQTTPublishInfo, any Error>.makeStream()
|
let (stream, continuation) = TopicListener.Stream.makeStream()
|
||||||
self.client = client
|
self.client = client
|
||||||
self.continuation = continuation
|
self.continuation = continuation
|
||||||
self.name = UUID().uuidString
|
self.name = UUID().uuidString
|
||||||
@@ -102,22 +110,53 @@ private actor MQTTTopicListener {
|
|||||||
continuation.finish()
|
continuation.finish()
|
||||||
}
|
}
|
||||||
client.removePublishListener(named: name)
|
client.removePublishListener(named: name)
|
||||||
|
client.removeShutdownListener(named: name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listen(_ topics: [String]) async -> AsyncThrowingStream<MQTTPublishInfo, any Error> {
|
func listen(
|
||||||
assert(client.isActive(), "The client is not connected.")
|
_ topics: [String],
|
||||||
|
_ qos: MQTTQoS = .atLeastOnce
|
||||||
|
) async throws(TopicListenerError) -> TopicListener.Stream {
|
||||||
|
var sleepTimes = 0
|
||||||
|
|
||||||
|
while !client.isActive() {
|
||||||
|
guard sleepTimes < 10 else {
|
||||||
|
throw .connectionTimeout
|
||||||
|
}
|
||||||
|
try? await Task.sleep(for: .milliseconds(100))
|
||||||
|
sleepTimes += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
client.logger.trace("Client is active, begin subscribing to topics.")
|
||||||
|
|
||||||
|
let subscription = try? await client.subscribe(to: topics.map {
|
||||||
|
MQTTSubscribeInfo(topicFilter: $0, qos: qos)
|
||||||
|
})
|
||||||
|
|
||||||
|
guard subscription != nil else {
|
||||||
|
client.logger.error("Error subscribing to topics: \(topics)")
|
||||||
|
throw .failedToSubscribe
|
||||||
|
}
|
||||||
|
|
||||||
|
client.logger.trace("Done subscribing, begin listening to topics.")
|
||||||
|
|
||||||
client.addPublishListener(named: name) { result in
|
client.addPublishListener(named: name) { result in
|
||||||
switch result {
|
switch result {
|
||||||
case let .failure(error):
|
case let .failure(error):
|
||||||
self.client.logger.error("Received error while listening: \(error)")
|
self.client.logger.error("Received error while listening: \(error)")
|
||||||
self.continuation.yield(with: .failure(error))
|
self.continuation.yield(.failure(.init(error)))
|
||||||
case let .success(publishInfo):
|
case let .success(publishInfo):
|
||||||
if topics.contains(publishInfo.topicName) {
|
if topics.contains(publishInfo.topicName) {
|
||||||
self.client.logger.trace("Recieved new value for topic: \(publishInfo.topicName)")
|
self.client.logger.trace("Recieved new value for topic: \(publishInfo.topicName)")
|
||||||
self.continuation.yield(publishInfo)
|
self.continuation.yield(.success(publishInfo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.addShutdownListener(named: name) { _ in
|
||||||
|
self.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,3 +170,16 @@ private actor MQTTTopicListener {
|
|||||||
Task { await self.setIsShuttingDown() }
|
Task { await self.setIsShuttingDown() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum TopicListenerError: Error {
|
||||||
|
case connectionTimeout
|
||||||
|
case failedToSubscribe
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct MQTTListenResultError: Error {
|
||||||
|
let underlyingError: any Error
|
||||||
|
|
||||||
|
init(_ underlyingError: any Error) {
|
||||||
|
self.underlyingError = underlyingError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,15 +6,16 @@ import MQTTConnectionManagerLive
|
|||||||
import MQTTNIO
|
import MQTTNIO
|
||||||
import NIO
|
import NIO
|
||||||
import PsychrometricClientLive
|
import PsychrometricClientLive
|
||||||
import SensorsClientLive
|
import SensorsService
|
||||||
import ServiceLifecycle
|
import ServiceLifecycle
|
||||||
|
import TopicDependencies
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct Application {
|
struct Application {
|
||||||
|
|
||||||
/// The main entry point of the application.
|
/// The main entry point of the application.
|
||||||
static func main() async throws {
|
static func main() async throws {
|
||||||
let eventloopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
let eventloopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
|
||||||
var logger = Logger(label: "dewpoint-controller")
|
var logger = Logger(label: "dewpoint-controller")
|
||||||
logger.logLevel = .trace
|
logger.logLevel = .trace
|
||||||
|
|
||||||
@@ -35,7 +36,9 @@ struct Application {
|
|||||||
|
|
||||||
try await withDependencies {
|
try await withDependencies {
|
||||||
$0.psychrometricClient = .liveValue
|
$0.psychrometricClient = .liveValue
|
||||||
$0.sensorsClient = .live(client: mqtt)
|
// $0.sensorsClient = .live(client: mqtt)
|
||||||
|
$0.topicListener = .live(client: mqtt)
|
||||||
|
$0.topicPublisher = .live(client: mqtt)
|
||||||
$0.mqttConnectionManager = .live(client: mqtt, logger: logger)
|
$0.mqttConnectionManager = .live(client: mqtt, logger: logger)
|
||||||
} operation: {
|
} operation: {
|
||||||
let mqttConnection = MQTTConnectionService(cleanSession: false, logger: logger)
|
let mqttConnection = MQTTConnectionService(cleanSession: false, logger: logger)
|
||||||
|
|||||||
Reference in New Issue
Block a user