feat: Adds sensor client dependency
This commit is contained in:
@@ -1,22 +1,18 @@
|
||||
@preconcurrency import Foundation
|
||||
import Foundation
|
||||
import Logging
|
||||
import Models
|
||||
import MQTTNIO
|
||||
import NIO
|
||||
import ServiceLifecycle
|
||||
|
||||
// TODO: This may not need to be an actor.
|
||||
|
||||
/// Manages the MQTT broker connection.
|
||||
public actor MQTTConnectionService: Service {
|
||||
|
||||
private let cleanSession: Bool
|
||||
public let client: MQTTClient
|
||||
private let continuation: AsyncStream<Event>.Continuation
|
||||
public nonisolated let events: AsyncStream<Event>
|
||||
private let client: MQTTClient
|
||||
private let internalEventStream: ConnectionStream
|
||||
nonisolated var logger: Logger { client.logger }
|
||||
// private var shuttingDown = false
|
||||
private let name: String
|
||||
|
||||
public init(
|
||||
cleanSession: Bool = true,
|
||||
@@ -25,22 +21,19 @@ public actor MQTTConnectionService: Service {
|
||||
self.cleanSession = cleanSession
|
||||
self.client = client
|
||||
self.internalEventStream = .init()
|
||||
let (stream, continuation) = AsyncStream.makeStream(of: Event.self)
|
||||
self.events = stream
|
||||
self.continuation = continuation
|
||||
self.name = UUID().uuidString
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.logger.debug("MQTTConnectionService is gone.")
|
||||
self.internalEventStream.stop()
|
||||
continuation.finish()
|
||||
}
|
||||
|
||||
/// The entry-point of the service.
|
||||
///
|
||||
/// This method connects to the MQTT broker and manages the connection.
|
||||
/// It will attempt to gracefully shutdown the connection upon receiving
|
||||
/// `sigterm` signals.
|
||||
/// a shutdown signals.
|
||||
public func run() async throws {
|
||||
await withGracefulShutdownHandler {
|
||||
await withDiscardingTaskGroup { group in
|
||||
@@ -50,11 +43,9 @@ public actor MQTTConnectionService: Service {
|
||||
}
|
||||
for await event in self.internalEventStream.events.cancelOnGracefulShutdown() {
|
||||
if event == .shuttingDown {
|
||||
self.shutdown()
|
||||
break
|
||||
}
|
||||
self.logger.trace("Sending connection event: \(event)")
|
||||
self.continuation.yield(event)
|
||||
}
|
||||
group.cancelAll()
|
||||
}
|
||||
@@ -66,33 +57,26 @@ public actor MQTTConnectionService: Service {
|
||||
|
||||
func connect() async {
|
||||
do {
|
||||
try await withThrowingDiscardingTaskGroup { group in
|
||||
group.addTask {
|
||||
try await self.client.connect(cleanSession: self.cleanSession)
|
||||
try await client.connect(cleanSession: cleanSession)
|
||||
client.addCloseListener(named: name) { _ in
|
||||
Task {
|
||||
self.logger.debug("Connection closed.")
|
||||
self.logger.debug("Reconnecting...")
|
||||
await self.connect()
|
||||
}
|
||||
client.addCloseListener(named: "\(Self.self)") { _ in
|
||||
Task {
|
||||
self.logger.debug("Connection closed.")
|
||||
self.logger.debug("Reconnecting...")
|
||||
await self.connect()
|
||||
}
|
||||
}
|
||||
self.logger.debug("Connection successful.")
|
||||
self.continuation.yield(.connected)
|
||||
}
|
||||
logger.debug("Connection successful.")
|
||||
} catch {
|
||||
logger.trace("Failed to connect: \(error)")
|
||||
continuation.yield(.disconnected)
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func shutdown() {
|
||||
logger.debug("Begin shutting down MQTT broker connection.")
|
||||
client.removeCloseListener(named: "\(Self.self)")
|
||||
client.removeCloseListener(named: name)
|
||||
internalEventStream.stop()
|
||||
_ = client.disconnect()
|
||||
try? client.syncShutdownGracefully()
|
||||
continuation.finish()
|
||||
logger.info("MQTT broker connection closed.")
|
||||
}
|
||||
|
||||
@@ -106,11 +90,8 @@ extension MQTTConnectionService {
|
||||
case shuttingDown
|
||||
}
|
||||
|
||||
// TODO: This functionality can probably move into the connection service.
|
||||
private actor ConnectionStream: Sendable {
|
||||
|
||||
private final class ConnectionStream: Sendable {
|
||||
|
||||
// private var cancellable: AnyCancellable?
|
||||
private let continuation: AsyncStream<MQTTConnectionService.Event>.Continuation
|
||||
let events: AsyncStream<MQTTConnectionService.Event>
|
||||
|
||||
@@ -131,18 +112,9 @@ extension MQTTConnectionService {
|
||||
: .disconnected
|
||||
|
||||
continuation.yield(event)
|
||||
// cancellable = Timer.publish(every: 1.0, on: .main, in: .common)
|
||||
// .autoconnect()
|
||||
// .sink { [weak self] (_: Date) in
|
||||
// let event: MQTTConnectionService.Event = connectionIsActive()
|
||||
// ? .connected
|
||||
// : .disconnected
|
||||
//
|
||||
// self?.continuation.yield(event)
|
||||
// }
|
||||
}
|
||||
|
||||
func stop() {
|
||||
nonisolated func stop() {
|
||||
continuation.yield(.shuttingDown)
|
||||
continuation.finish()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user