diff --git a/Package.swift b/Package.swift index 300eac6..72bc0b0 100755 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,8 @@ let package = Package( .library(name: "EnvVars", targets: ["EnvVars"]), .library(name: "Models", targets: ["Models"]), .library(name: "Client", targets: ["Client"]), - .library(name: "ClientLive", targets: ["ClientLive"]) + .library(name: "ClientLive", targets: ["ClientLive"]), + .library(name: "SensorsService", targets: ["SensorsService"]) ], dependencies: [ .package(url: "https://github.com/swift-server-community/mqtt-nio.git", from: "2.0.0"), @@ -97,6 +98,22 @@ let package = Package( dependencies: [ "Models" ] + ), + .target( + name: "SensorsService", + dependencies: [ + "Models", + .product(name: "MQTTNIO", package: "mqtt-nio"), + .product(name: "ServiceLifecycle", package: "swift-service-lifecycle") + ] + ), + .testTarget( + name: "SensorsServiceTests", + dependencies: [ + "SensorsService", + // TODO: Remove. + "ClientLive" + ] ) ] ) diff --git a/Sources/ClientLive/SensorsClient.swift b/Sources/ClientLive/SensorsClient.swift index 030f0df..f95be3e 100644 --- a/Sources/ClientLive/SensorsClient.swift +++ b/Sources/ClientLive/SensorsClient.swift @@ -6,6 +6,7 @@ import NIO import Psychrometrics import ServiceLifecycle +// TODO: Remove. // TODO: Pass in eventLoopGroup and MQTTClient. public actor SensorsClient { diff --git a/Sources/SensorsService/Helpers.swift b/Sources/SensorsService/Helpers.swift new file mode 100755 index 0000000..1817e34 --- /dev/null +++ b/Sources/SensorsService/Helpers.swift @@ -0,0 +1,41 @@ +import CoreUnitTypes +import Logging +import Models +import MQTTNIO +import NIO +import NIOFoundationCompat +import Psychrometrics + +/// Represents a type that can be initialized by a ``ByteBuffer``. +protocol BufferInitalizable { + init?(buffer: inout ByteBuffer) +} + +extension Double: BufferInitalizable { + + /// Attempt to create / parse a double from a byte buffer. + init?(buffer: inout ByteBuffer) { + guard let string = buffer.readString( + length: buffer.readableBytes, + encoding: String.Encoding.utf8 + ) + else { return nil } + self.init(string) + } +} + +extension Temperature: BufferInitalizable { + /// Attempt to create / parse a temperature from a byte buffer. + init?(buffer: inout ByteBuffer) { + guard let value = Double(buffer: &buffer) else { return nil } + self.init(value, units: .celsius) + } +} + +extension RelativeHumidity: BufferInitalizable { + /// Attempt to create / parse a relative humidity from a byte buffer. + init?(buffer: inout ByteBuffer) { + guard let value = Double(buffer: &buffer) else { return nil } + self.init(value) + } +} diff --git a/Sources/ClientLive/SensorsService.swift b/Sources/SensorsService/SensorsService.swift similarity index 97% rename from Sources/ClientLive/SensorsService.swift rename to Sources/SensorsService/SensorsService.swift index fa74b38..7aecbfc 100644 --- a/Sources/ClientLive/SensorsService.swift +++ b/Sources/SensorsService/SensorsService.swift @@ -99,6 +99,12 @@ public actor SensorsService: Service { } +// MARK: - Errors + +struct DecodingError: Error {} +struct NotFoundError: Error {} +struct SensorExists: Error {} + // MARK: - Helpers struct MQTTClientNotConnected: Error {} diff --git a/Tests/ClientTests/SensorsClientTests.swift b/Tests/SensorsServiceTests/SensorsClientTests.swift similarity index 99% rename from Tests/ClientTests/SensorsClientTests.swift rename to Tests/SensorsServiceTests/SensorsClientTests.swift index 1c12cc3..263ee63 100755 --- a/Tests/ClientTests/SensorsClientTests.swift +++ b/Tests/SensorsServiceTests/SensorsClientTests.swift @@ -5,6 +5,7 @@ import Models import MQTTNIO import NIO import Psychrometrics +@testable import SensorsService import XCTest final class SensorsClientTests: XCTestCase {