feat: Adds SensorsService
This commit is contained in:
108
Package.resolved
108
Package.resolved
@@ -1,61 +1,87 @@
|
|||||||
{
|
{
|
||||||
"object": {
|
"originHash" : "869c04e4de5c59d3c7c00851dffa2bfde39aa29dc5be3cbbb759ad950fffd706",
|
||||||
"pins": [
|
"pins" : [
|
||||||
{
|
{
|
||||||
"package": "mqtt-nio",
|
"identity" : "mqtt-nio",
|
||||||
"repositoryURL": "https://github.com/swift-server-community/mqtt-nio.git",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/swift-server-community/mqtt-nio.git",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "ca8af7a30c4690456ce7de276cd0f037489ba707",
|
"revision" : "ca8af7a30c4690456ce7de276cd0f037489ba707",
|
||||||
"version": "2.5.3"
|
"version" : "2.5.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"package": "swift-log",
|
"identity" : "swift-async-algorithms",
|
||||||
"repositoryURL": "https://github.com/apple/swift-log.git",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-async-algorithms.git",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "5d66f7ba25daf4f94100e7022febf3c75e37a6c7",
|
"revision" : "5c8bd186f48c16af0775972700626f0b74588278",
|
||||||
"version": "1.4.2"
|
"version" : "1.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"package": "swift-nio",
|
"identity" : "swift-collections",
|
||||||
"repositoryURL": "https://github.com/apple/swift-nio",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-collections.git",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "6aa9347d9bc5bbfe6a84983aec955c17ffea96ef",
|
"revision" : "671108c96644956dddcd89dd59c203dcdb36cec7",
|
||||||
"version": "2.33.0"
|
"version" : "1.1.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"package": "swift-nio-ssl",
|
"identity" : "swift-log",
|
||||||
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-log.git",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "b5260a31c2a72a89fa684f5efb3054d8725a2316",
|
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
|
||||||
"version": "2.18.0"
|
"version" : "1.6.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"package": "swift-nio-transport-services",
|
"identity" : "swift-nio",
|
||||||
"repositoryURL": "https://github.com/apple/swift-nio-transport-services.git",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-nio",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "8ab824b140d0ebcd87e9149266ddc353e3705a3e",
|
"revision" : "6aa9347d9bc5bbfe6a84983aec955c17ffea96ef",
|
||||||
"version": "1.11.4"
|
"version" : "2.33.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"package": "swift-psychrometrics",
|
"identity" : "swift-nio-ssl",
|
||||||
"repositoryURL": "https://github.com/swift-psychrometrics/swift-psychrometrics",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-nio-ssl.git",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "03573545c3750b406921eb22a9575c8062beef88",
|
"revision" : "b5260a31c2a72a89fa684f5efb3054d8725a2316",
|
||||||
"version": "0.1.2"
|
"version" : "2.18.0"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"version": 1
|
{
|
||||||
|
"identity" : "swift-nio-transport-services",
|
||||||
|
"kind" : "remoteSourceControl",
|
||||||
|
"location" : "https://github.com/apple/swift-nio-transport-services.git",
|
||||||
|
"state" : {
|
||||||
|
"revision" : "8ab824b140d0ebcd87e9149266ddc353e3705a3e",
|
||||||
|
"version" : "1.11.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"identity" : "swift-psychrometrics",
|
||||||
|
"kind" : "remoteSourceControl",
|
||||||
|
"location" : "https://github.com/swift-psychrometrics/swift-psychrometrics",
|
||||||
|
"state" : {
|
||||||
|
"revision" : "03573545c3750b406921eb22a9575c8062beef88",
|
||||||
|
"version" : "0.1.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"identity" : "swift-service-lifecycle",
|
||||||
|
"kind" : "remoteSourceControl",
|
||||||
|
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
|
||||||
|
"state" : {
|
||||||
|
"revision" : "f70b838872863396a25694d8b19fe58bcd0b7903",
|
||||||
|
"version" : "2.6.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version" : 3
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "dewPoint-controller",
|
name: "dewPoint-controller",
|
||||||
platforms: [
|
platforms: [
|
||||||
.macOS(.v12)
|
.macOS(.v14)
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
.executable(name: "dewPoint-controller", targets: ["dewPoint-controller"]),
|
.executable(name: "dewPoint-controller", targets: ["dewPoint-controller"]),
|
||||||
@@ -14,12 +14,13 @@ let package = Package(
|
|||||||
.library(name: "EnvVars", targets: ["EnvVars"]),
|
.library(name: "EnvVars", targets: ["EnvVars"]),
|
||||||
.library(name: "Models", targets: ["Models"]),
|
.library(name: "Models", targets: ["Models"]),
|
||||||
.library(name: "Client", targets: ["Client"]),
|
.library(name: "Client", targets: ["Client"]),
|
||||||
.library(name: "ClientLive", targets: ["ClientLive"]),
|
.library(name: "ClientLive", targets: ["ClientLive"])
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/swift-server-community/mqtt-nio.git", from: "2.0.0"),
|
.package(url: "https://github.com/swift-server-community/mqtt-nio.git", from: "2.0.0"),
|
||||||
.package(url: "https://github.com/apple/swift-nio", from: "2.0.0"),
|
.package(url: "https://github.com/apple/swift-nio", from: "2.0.0"),
|
||||||
.package(url: "https://github.com/swift-psychrometrics/swift-psychrometrics", from: "0.1.0")
|
.package(url: "https://github.com/swift-psychrometrics/swift-psychrometrics", from: "0.1.0"),
|
||||||
|
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.3.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.executableTarget(
|
.executableTarget(
|
||||||
@@ -53,7 +54,7 @@ let package = Package(
|
|||||||
"EnvVars",
|
"EnvVars",
|
||||||
"Client",
|
"Client",
|
||||||
"Models",
|
"Models",
|
||||||
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
.product(name: "MQTTNIO", package: "mqtt-nio")
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@@ -63,7 +64,7 @@ let package = Package(
|
|||||||
.target(
|
.target(
|
||||||
name: "Models",
|
name: "Models",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "Psychrometrics", package: "swift-psychrometrics"),
|
.product(name: "Psychrometrics", package: "swift-psychrometrics")
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@@ -80,7 +81,8 @@ let package = Package(
|
|||||||
dependencies: [
|
dependencies: [
|
||||||
"Client",
|
"Client",
|
||||||
"EnvVars",
|
"EnvVars",
|
||||||
.product(name: "MQTTNIO", package: "mqtt-nio")
|
.product(name: "MQTTNIO", package: "mqtt-nio"),
|
||||||
|
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
@@ -95,6 +97,6 @@ let package = Package(
|
|||||||
dependencies: [
|
dependencies: [
|
||||||
"Models"
|
"Models"
|
||||||
]
|
]
|
||||||
),
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import Models
|
|||||||
import MQTTNIO
|
import MQTTNIO
|
||||||
import NIO
|
import NIO
|
||||||
import Psychrometrics
|
import Psychrometrics
|
||||||
|
import ServiceLifecycle
|
||||||
|
|
||||||
// TODO: Pass in eventLoopGroup and MQTTClient.
|
// TODO: Pass in eventLoopGroup and MQTTClient.
|
||||||
public actor SensorsClient {
|
public actor SensorsClient {
|
||||||
@@ -65,14 +66,22 @@ public actor SensorsClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func start() async throws {
|
public func start() async throws {
|
||||||
do {
|
await withGracefulShutdownHandler {
|
||||||
try await subscribeToSensors()
|
await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
try await addSensorListeners()
|
group.addTask { try await self.subscribeToSensors() }
|
||||||
logger.debug("Begin listening to sensors...")
|
group.addTask { try await self.addSensorListeners() }
|
||||||
} catch {
|
|
||||||
logger.trace("Error:\n\(error)")
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
|
} onGracefulShutdown: {
|
||||||
|
Task { await self.shutdown() }
|
||||||
|
}
|
||||||
|
// do {
|
||||||
|
// try await subscribeToSensors()
|
||||||
|
// try await addSensorListeners()
|
||||||
|
// logger.debug("Begin listening to sensors...")
|
||||||
|
// } catch {
|
||||||
|
// logger.trace("Error:\n(error)")
|
||||||
|
// throw error
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public func shutdown() async {
|
public func shutdown() async {
|
||||||
@@ -88,6 +97,48 @@ public actor SensorsClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func _addSensorListeners(qos _: MQTTQoS = .exactlyOnce) async throws {
|
||||||
|
// try await withThrowingDiscardingTaskGroup { group in
|
||||||
|
// group.addTask { try await self.subscribeToSensors(qos: qos) }
|
||||||
|
|
||||||
|
for await result in client.createPublishListener() {
|
||||||
|
switch result {
|
||||||
|
case let .failure(error):
|
||||||
|
logger.trace("Error:\n\(error)")
|
||||||
|
case let .success(value):
|
||||||
|
let topic = value.topicName
|
||||||
|
logger.trace("Received new value for topic: \(topic)")
|
||||||
|
if topic.contains("temperature") {
|
||||||
|
// do something.
|
||||||
|
var buffer = value.payload
|
||||||
|
guard let temperature = Temperature(buffer: &buffer) else {
|
||||||
|
logger.trace("Decoding error for topic: \(topic)")
|
||||||
|
throw DecodingError()
|
||||||
|
}
|
||||||
|
try sensors.update(topic: topic, keyPath: \.temperature, with: temperature)
|
||||||
|
// group.addTask {
|
||||||
|
Task {
|
||||||
|
try await self.publishUpdates()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if topic.contains("humidity") {
|
||||||
|
var buffer = value.payload
|
||||||
|
// Decode and update the temperature value
|
||||||
|
guard let humidity = RelativeHumidity(buffer: &buffer) else {
|
||||||
|
logger.debug("Failed to decode humidity from buffer: \(buffer)")
|
||||||
|
throw DecodingError()
|
||||||
|
}
|
||||||
|
try sensors.update(topic: topic, keyPath: \.humidity, with: humidity)
|
||||||
|
// group.addTask {
|
||||||
|
Task {
|
||||||
|
try await self.publishUpdates()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func addSensorListeners(qos: MQTTQoS = .exactlyOnce) async throws {
|
func addSensorListeners(qos: MQTTQoS = .exactlyOnce) async throws {
|
||||||
try await subscribeToSensors(qos: qos)
|
try await subscribeToSensors(qos: qos)
|
||||||
client.addPublishListener(named: "SensorsClient") { result in
|
client.addPublishListener(named: "SensorsClient") { result in
|
||||||
@@ -138,7 +189,7 @@ public actor SensorsClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func publishUpdates() async throws {
|
private func publishUpdates() async throws {
|
||||||
for sensor in sensors.filter(\.needsProcessed) {
|
for sensor in sensors.filter(\.needsProcessed) {
|
||||||
try await publish(double: sensor.dewPoint?.rawValue, to: sensor.topics.dewPoint)
|
try await publish(double: sensor.dewPoint?.rawValue, to: sensor.topics.dewPoint)
|
||||||
try await publish(double: sensor.enthalpy?.rawValue, to: sensor.topics.enthalpy)
|
try await publish(double: sensor.enthalpy?.rawValue, to: sensor.topics.enthalpy)
|
||||||
|
|||||||
131
Sources/ClientLive/SensorsService.swift
Normal file
131
Sources/ClientLive/SensorsService.swift
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
import Foundation
|
||||||
|
import Logging
|
||||||
|
@preconcurrency import Models
|
||||||
|
@preconcurrency import MQTTNIO
|
||||||
|
import NIO
|
||||||
|
import Psychrometrics
|
||||||
|
import ServiceLifecycle
|
||||||
|
|
||||||
|
public actor SensorsService: Service {
|
||||||
|
private var sensors: [TemperatureAndHumiditySensor]
|
||||||
|
private let client: MQTTClient
|
||||||
|
var logger: Logger { client.logger }
|
||||||
|
|
||||||
|
public init(
|
||||||
|
client: MQTTClient,
|
||||||
|
sensors: [TemperatureAndHumiditySensor]
|
||||||
|
) {
|
||||||
|
self.client = client
|
||||||
|
self.sensors = sensors
|
||||||
|
}
|
||||||
|
|
||||||
|
public func run() async throws {
|
||||||
|
guard client.isActive() else {
|
||||||
|
throw MQTTClientNotConnected()
|
||||||
|
}
|
||||||
|
try await withThrowingDiscardingTaskGroup { group in
|
||||||
|
group.addTask { try await self.subscribeToSensors() }
|
||||||
|
for await result in client.createPublishListener().cancelOnGracefulShutdown() {
|
||||||
|
group.addTask {
|
||||||
|
try await self.handleResult(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleResult(
|
||||||
|
_ result: Result<MQTTPublishInfo, any Error>
|
||||||
|
) async throws {
|
||||||
|
switch result {
|
||||||
|
case let .failure(error):
|
||||||
|
logger.debug("Failed receiving sensor: \(error)")
|
||||||
|
throw error
|
||||||
|
case let .success(value):
|
||||||
|
// do something.
|
||||||
|
let topic = value.topicName
|
||||||
|
logger.trace("Received new value for topic: \(topic)")
|
||||||
|
if topic.contains("temperature") {
|
||||||
|
// do something.
|
||||||
|
var buffer = value.payload
|
||||||
|
guard let temperature = Temperature(buffer: &buffer) else {
|
||||||
|
logger.trace("Decoding error for topic: \(topic)")
|
||||||
|
throw DecodingError()
|
||||||
|
}
|
||||||
|
try sensors.update(topic: topic, keyPath: \.temperature, with: temperature)
|
||||||
|
try await publishUpdates()
|
||||||
|
|
||||||
|
} else if topic.contains("humidity") {
|
||||||
|
var buffer = value.payload
|
||||||
|
// Decode and update the temperature value
|
||||||
|
guard let humidity = RelativeHumidity(buffer: &buffer) else {
|
||||||
|
logger.debug("Failed to decode humidity from buffer: \(buffer)")
|
||||||
|
throw DecodingError()
|
||||||
|
}
|
||||||
|
try sensors.update(topic: topic, keyPath: \.humidity, with: humidity)
|
||||||
|
try await publishUpdates()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func subscribeToSensors() async throws {
|
||||||
|
for sensor in sensors {
|
||||||
|
_ = try await client.subscribe(to: [
|
||||||
|
MQTTSubscribeInfo(topicFilter: sensor.topics.temperature, qos: .atLeastOnce),
|
||||||
|
MQTTSubscribeInfo(topicFilter: sensor.topics.humidity, qos: .atLeastOnce)
|
||||||
|
])
|
||||||
|
logger.debug("Subscribed to sensor: \(sensor.location)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func publish(double: Double?, to topic: String) async throws {
|
||||||
|
guard let double else { return }
|
||||||
|
let rounded = round(double * 100) / 100
|
||||||
|
logger.debug("Publishing \(rounded), to: \(topic)")
|
||||||
|
try await client.publish(
|
||||||
|
to: topic,
|
||||||
|
payload: ByteBufferAllocator().buffer(string: "\(rounded)"),
|
||||||
|
qos: .exactlyOnce,
|
||||||
|
retain: true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func publishUpdates() async throws {
|
||||||
|
for sensor in sensors.filter(\.needsProcessed) {
|
||||||
|
try await publish(double: sensor.dewPoint?.rawValue, to: sensor.topics.dewPoint)
|
||||||
|
try await publish(double: sensor.enthalpy?.rawValue, to: sensor.topics.enthalpy)
|
||||||
|
try sensors.hasProcessed(sensor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Helpers
|
||||||
|
|
||||||
|
struct MQTTClientNotConnected: Error {}
|
||||||
|
|
||||||
|
private extension TemperatureAndHumiditySensor.Topics {
|
||||||
|
func contains(_ topic: String) -> Bool {
|
||||||
|
temperature == topic || humidity == topic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension Array where Element == TemperatureAndHumiditySensor {
|
||||||
|
|
||||||
|
mutating func update<V>(
|
||||||
|
topic: String,
|
||||||
|
keyPath: WritableKeyPath<TemperatureAndHumiditySensor, V>,
|
||||||
|
with value: V
|
||||||
|
) throws {
|
||||||
|
guard let index = firstIndex(where: { $0.topics.contains(topic) }) else {
|
||||||
|
throw NotFoundError()
|
||||||
|
}
|
||||||
|
self[index][keyPath: keyPath] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func hasProcessed(_ sensor: TemperatureAndHumiditySensor) throws {
|
||||||
|
guard let index = firstIndex(where: { $0.id == sensor.id }) else {
|
||||||
|
throw NotFoundError()
|
||||||
|
}
|
||||||
|
self[index].needsProcessed = false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ import EnvVars
|
|||||||
import Models
|
import Models
|
||||||
import MQTTNIO
|
import MQTTNIO
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
|
||||||
public struct DewPointEnvironment {
|
public struct DewPointEnvironment {
|
||||||
|
|
||||||
public var envVars: EnvVars
|
public var envVars: EnvVars
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
// TODO: Move to Models
|
||||||
|
|
||||||
/// Holds common settings for connecting to your MQTT broker. The default values can be used,
|
/// Holds common settings for connecting to your MQTT broker. The default values can be used,
|
||||||
/// they can be loaded from the shell environment, or from a file located in the root directory.
|
/// they can be loaded from the shell environment, or from a file located in the root directory.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import NIO
|
|||||||
import Psychrometrics
|
import Psychrometrics
|
||||||
import XCTest
|
import XCTest
|
||||||
|
|
||||||
final class AsyncClientTests: XCTestCase {
|
final class SensorsClientTests: XCTestCase {
|
||||||
|
|
||||||
static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"
|
static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"
|
||||||
|
|
||||||
@@ -35,6 +35,66 @@ final class AsyncClientTests: XCTestCase {
|
|||||||
await client.shutdown()
|
await client.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSensorService() async throws {
|
||||||
|
let client = createClient(identifier: "testSensorService")
|
||||||
|
let mqtt = await client.client
|
||||||
|
let sensor = TemperatureAndHumiditySensor(location: .mixedAir, units: .metric)
|
||||||
|
let publishInfo = PublishInfoContainer(topicFilters: [
|
||||||
|
sensor.topics.dewPoint,
|
||||||
|
sensor.topics.enthalpy
|
||||||
|
])
|
||||||
|
let service = SensorsService(client: mqtt, sensors: [sensor])
|
||||||
|
|
||||||
|
// fix to connect the mqtt client.
|
||||||
|
await client.connect()
|
||||||
|
let task = Task { try await service.run() }
|
||||||
|
|
||||||
|
_ = try await mqtt.subscribe(to: [
|
||||||
|
.init(topicFilter: sensor.topics.dewPoint, qos: .exactlyOnce),
|
||||||
|
.init(topicFilter: sensor.topics.enthalpy, qos: .exactlyOnce)
|
||||||
|
])
|
||||||
|
|
||||||
|
let listener = mqtt.createPublishListener()
|
||||||
|
Task {
|
||||||
|
for await result in listener {
|
||||||
|
switch result {
|
||||||
|
case let .failure(error):
|
||||||
|
XCTFail("\(error)")
|
||||||
|
case let .success(value):
|
||||||
|
await publishInfo.addPublishInfo(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try await mqtt.publish(
|
||||||
|
to: sensor.topics.temperature,
|
||||||
|
payload: ByteBufferAllocator().buffer(string: "75.123"),
|
||||||
|
qos: .exactlyOnce,
|
||||||
|
retain: true
|
||||||
|
)
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(1))
|
||||||
|
|
||||||
|
// XCTAssert(client.sensors.first!.needsProcessed)
|
||||||
|
// let firstSensor = await client.sensors.first!
|
||||||
|
// XCTAssertEqual(firstSensor.temperature, .init(75.123, units: .celsius))
|
||||||
|
|
||||||
|
try await mqtt.publish(
|
||||||
|
to: sensor.topics.humidity,
|
||||||
|
payload: ByteBufferAllocator().buffer(string: "50"),
|
||||||
|
qos: .exactlyOnce,
|
||||||
|
retain: true
|
||||||
|
)
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(1))
|
||||||
|
|
||||||
|
XCTAssertEqual(publishInfo.info.count, 2)
|
||||||
|
|
||||||
|
// fix to shutdown the mqtt client.
|
||||||
|
task.cancel()
|
||||||
|
await client.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
func testSensorCapturesPublishedState() async throws {
|
func testSensorCapturesPublishedState() async throws {
|
||||||
let client = createClient(identifier: "testSensorCapturesPublishedState")
|
let client = createClient(identifier: "testSensorCapturesPublishedState")
|
||||||
let mqtt = await client.client
|
let mqtt = await client.client
|
||||||
|
|||||||
Reference in New Issue
Block a user