feat: Adds SensorsService
This commit is contained in:
@@ -7,7 +7,7 @@ import NIO
|
||||
import Psychrometrics
|
||||
import XCTest
|
||||
|
||||
final class AsyncClientTests: XCTestCase {
|
||||
final class SensorsClientTests: XCTestCase {
|
||||
|
||||
static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"
|
||||
|
||||
@@ -35,6 +35,66 @@ final class AsyncClientTests: XCTestCase {
|
||||
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 {
|
||||
let client = createClient(identifier: "testSensorCapturesPublishedState")
|
||||
let mqtt = await client.client
|
||||
|
||||
Reference in New Issue
Block a user