Compare commits
11 Commits
756fd0bccf
...
0.2.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
e44c1c24c5
|
|||
|
df05898a65
|
|||
|
916fcb3584
|
|||
|
d9af0b8b30
|
|||
|
aa666d799a
|
|||
|
3825517dae
|
|||
|
c21695a37e
|
|||
|
3743eefa69
|
|||
|
845d566c60
|
|||
|
99f39b91af
|
|||
|
55ea88a29f
|
@@ -2,6 +2,7 @@
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
|
||||
68
.gitea/workflows/release.yml
Executable file
68
.gitea/workflows/release.yml
Executable file
@@ -0,0 +1,68 @@
|
||||
#
|
||||
name: Create and publish a Docker image
|
||||
|
||||
# Configures this workflow to run every time a change is pushed to the branch called `release`.
|
||||
on:
|
||||
push:
|
||||
branches: ['release']
|
||||
tags:
|
||||
- '*'
|
||||
workflow_dispatch:
|
||||
|
||||
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
|
||||
env:
|
||||
REGISTRY: git.housh.dev
|
||||
IMAGE_NAME: ${{ gitea.repository }}
|
||||
|
||||
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
|
||||
jobs:
|
||||
build-and-push-image:
|
||||
runs-on: ubuntu-latest
|
||||
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
attestations: write
|
||||
id-token: write
|
||||
#
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
|
||||
- name: Log in to the Container registry
|
||||
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ gitea.actor }}
|
||||
password: ${{ secrets.CONTAINER_TOKEN }}
|
||||
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
|
||||
- name: Extract metadata (tags, labels) for Docker
|
||||
id: meta
|
||||
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
type=ref,event=branch
|
||||
type=semver,pattern={{version}}
|
||||
type=sha
|
||||
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
|
||||
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
|
||||
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
|
||||
- name: Build and push Docker image
|
||||
id: push
|
||||
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
|
||||
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
|
||||
# - name: Generate artifact attestation
|
||||
# uses: actions/attest-build-provenance@v1
|
||||
# with:
|
||||
# subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
|
||||
# subject-digest: ${{ steps.push.outputs.digest }}
|
||||
# push-to-registry: true
|
||||
# github-token: ${{ secrets.CONTAINER_TOKEN }}
|
||||
@@ -1,168 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1610"
|
||||
version = "1.7">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES"
|
||||
buildArchitectures = "Automatic">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "CliClient"
|
||||
BuildableName = "CliClient"
|
||||
BlueprintName = "CliClient"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "MQTTConnectionService"
|
||||
BuildableName = "MQTTConnectionService"
|
||||
BlueprintName = "MQTTConnectionService"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "MQTTManager"
|
||||
BuildableName = "MQTTManager"
|
||||
BlueprintName = "MQTTManager"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Models"
|
||||
BuildableName = "Models"
|
||||
BlueprintName = "Models"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "SensorsService"
|
||||
BuildableName = "SensorsService"
|
||||
BlueprintName = "SensorsService"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "dewpoint-controller"
|
||||
BuildableName = "dewpoint-controller"
|
||||
BlueprintName = "dewpoint-controller"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
shouldAutocreateTestPlan = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "CliClientTests"
|
||||
BuildableName = "CliClientTests"
|
||||
BlueprintName = "CliClientTests"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "IntegrationTests"
|
||||
BuildableName = "IntegrationTests"
|
||||
BlueprintName = "IntegrationTests"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
</Testables>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "dewpoint-controller"
|
||||
BuildableName = "dewpoint-controller"
|
||||
BlueprintName = "dewpoint-controller"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "dewpoint-controller"
|
||||
BuildableName = "dewpoint-controller"
|
||||
BlueprintName = "dewpoint-controller"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
||||
@@ -34,12 +34,23 @@ public struct CliClient {
|
||||
|
||||
/// Represents the parameters needed to create an `MQTTClient`.
|
||||
///
|
||||
///
|
||||
public struct ClientRequest: Sendable {
|
||||
|
||||
/// The environment variables used to create the client.
|
||||
public let environment: EnvVars
|
||||
|
||||
/// The event loop group for the client.
|
||||
public let eventLoopGroup: MultiThreadedEventLoopGroup
|
||||
|
||||
/// A logger to use with the client.
|
||||
public let logger: Logger?
|
||||
|
||||
/// Create a new client request.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - environment: The environment variables to use.
|
||||
/// - eventLoopGroup: The event loop group to use on the client.
|
||||
/// - logger: An optional logger to use on the client.
|
||||
public init(
|
||||
environment: EnvVars,
|
||||
eventLoopGroup: MultiThreadedEventLoopGroup,
|
||||
@@ -122,11 +133,10 @@ extension EnvVars {
|
||||
@Dependency(\.environment) var environment
|
||||
|
||||
let defaultEnvVars = EnvVars()
|
||||
let encoder = environment.jsonEncoder()
|
||||
let decoder = environment.jsonDecoder()
|
||||
let coders = environment.coders()
|
||||
|
||||
let defaultEnvDict = (try? encoder.encode(defaultEnvVars))
|
||||
.flatMap { try? decoder.decode([String: String].self, from: $0) }
|
||||
let defaultEnvDict = (try? coders.encode(defaultEnvVars))
|
||||
.flatMap { try? coders.decode([String: String].self, from: $0) }
|
||||
?? [:]
|
||||
|
||||
let dotEnvDict = try await environment.dotEnvDict(path: dotEnvFile)
|
||||
@@ -135,7 +145,7 @@ extension EnvVars {
|
||||
.merging(dotEnvDict, uniquingKeysWith: { $1 })
|
||||
|
||||
var envVars = (try? JSONSerialization.data(withJSONObject: envVarsDict))
|
||||
.flatMap { try? decoder.decode(EnvVars.self, from: $0) }
|
||||
.flatMap { try? coders.decode(EnvVars.self, from: $0) }
|
||||
?? defaultEnvVars
|
||||
|
||||
if let version {
|
||||
@@ -162,7 +172,7 @@ public extension MQTTClient {
|
||||
eventLoopGroupProvider: .shared(eventLoopGroup),
|
||||
logger: logger,
|
||||
configuration: .init(
|
||||
version: .parseOrDefualt(string: envVars.version),
|
||||
version: .parseOrDefault(string: envVars.version),
|
||||
disablePing: false,
|
||||
userName: envVars.userName,
|
||||
password: envVars.password
|
||||
@@ -171,10 +181,11 @@ public extension MQTTClient {
|
||||
}
|
||||
}
|
||||
|
||||
extension MQTTClient.Version {
|
||||
@_spi(Internal)
|
||||
public extension MQTTClient.Version {
|
||||
static let `default` = Self.v3_1_1
|
||||
|
||||
static func parseOrDefualt(string: String?) -> Self {
|
||||
static func parseOrDefault(string: String?) -> Self {
|
||||
guard let string, let value = Self(string: string) else {
|
||||
return .default
|
||||
}
|
||||
|
||||
@@ -24,11 +24,7 @@ public extension DependencyValues {
|
||||
@DependencyClient
|
||||
public struct EnvironmentDependency: Sendable {
|
||||
|
||||
/// A json decoder to use to decode files and environment variables.
|
||||
public var jsonDecoder: @Sendable () -> JSONDecoder = { JSONDecoder() }
|
||||
|
||||
/// A json encoder to use to encode files and environment variables.
|
||||
public var jsonEncoder: @Sendable () -> JSONEncoder = { JSONEncoder() }
|
||||
public var coders: @Sendable () -> any Coderable = { JSONCoders() }
|
||||
|
||||
/// Load the variables based on the request.
|
||||
public var load: @Sendable (FileType) async throws -> [String: String] = { _ in [:] }
|
||||
@@ -61,6 +57,8 @@ public struct EnvironmentDependency: Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
struct DecodeError: Error {}
|
||||
|
||||
@_spi(Internal)
|
||||
extension EnvironmentDependency: DependencyKey {
|
||||
|
||||
@@ -71,8 +69,7 @@ extension EnvironmentDependency: DependencyKey {
|
||||
encoder: JSONEncoder = .init()
|
||||
) -> Self {
|
||||
Self(
|
||||
jsonDecoder: { decoder },
|
||||
jsonEncoder: { encoder },
|
||||
coders: { JSONCoders(decoder: decoder, encoder: encoder) },
|
||||
load: { file in
|
||||
switch file {
|
||||
case let .dotEnv(path: path):
|
||||
@@ -95,6 +92,39 @@ extension EnvironmentDependency: DependencyKey {
|
||||
public static let liveValue: EnvironmentDependency = .live()
|
||||
}
|
||||
|
||||
/// A type that encode and decode values.
|
||||
///
|
||||
/// This is really just here to override tests with coders that will throw an error,
|
||||
/// instead of encoding or decoding data.
|
||||
///
|
||||
@_spi(Internal)
|
||||
public protocol Coderable {
|
||||
func encode<T: Encodable>(_ instance: T) throws -> Data
|
||||
func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T
|
||||
}
|
||||
|
||||
struct JSONCoders: Coderable {
|
||||
|
||||
let decoder: JSONDecoder
|
||||
let encoder: JSONEncoder
|
||||
|
||||
init(
|
||||
decoder: JSONDecoder = .init(),
|
||||
encoder: JSONEncoder = .init()
|
||||
) {
|
||||
self.decoder = decoder
|
||||
self.encoder = encoder
|
||||
}
|
||||
|
||||
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
|
||||
try encoder.encode(instance)
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
|
||||
try decoder.decode(T.self, from: data)
|
||||
}
|
||||
}
|
||||
|
||||
private func url(for path: String) -> URL {
|
||||
#if os(Linux)
|
||||
return URL(fileURLWithPath: path)
|
||||
|
||||
@@ -60,7 +60,10 @@ public struct TemperatureAndHumiditySensor: Identifiable, Sendable {
|
||||
!temperature.value.isNaN,
|
||||
!humidity.value.isNaN
|
||||
else { return nil }
|
||||
return try? await psychrometrics.dewPoint(.dryBulb(temperature, relativeHumidity: humidity))
|
||||
return try? await psychrometrics.dewPoint(.dryBulb(
|
||||
.fahrenheit(temperature.fahrenheit),
|
||||
relativeHumidity: humidity
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +76,7 @@ public struct TemperatureAndHumiditySensor: Identifiable, Sendable {
|
||||
!humidity.value.isNaN
|
||||
else { return nil }
|
||||
return try? await psychrometrics.enthalpy.moistAir(
|
||||
.dryBulb(temperature, relativeHumidity: humidity, altitude: altitude)
|
||||
.dryBulb(.fahrenheit(temperature.fahrenheit), relativeHumidity: humidity, altitude: altitude, units: .imperial)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public actor SensorsService: Service {
|
||||
|
||||
private func publishUpdates() async throws {
|
||||
for sensor in sensors.filter(\.needsProcessed) {
|
||||
try await publish(sensor.dewPoint?.value, to: sensor.topics.dewPoint)
|
||||
try await publish(sensor.dewPoint?.fahrenheit, to: sensor.topics.dewPoint)
|
||||
try await publish(sensor.enthalpy?.value, to: sensor.topics.enthalpy)
|
||||
try sensors.hasProcessed(sensor)
|
||||
}
|
||||
@@ -207,6 +207,6 @@ extension Humidity<Relative>: BufferInitalizable {
|
||||
extension Temperature<DryAir>: BufferInitalizable {
|
||||
init?(buffer: ByteBuffer) {
|
||||
guard let value = Double(buffer: buffer) else { return nil }
|
||||
self.init(value)
|
||||
self.init(value, units: .celsius)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ final class CliClientTests: XCTestCase {
|
||||
XCTAssertEqual(cliClient.parseMqttClientVersion(string), version)
|
||||
}
|
||||
}
|
||||
|
||||
XCTAssertEqual(MQTTClient.Version.parseOrDefault(string: nil), .v3_1_1)
|
||||
}
|
||||
|
||||
func testLogLevelFromEnvironment() {
|
||||
@@ -98,6 +100,26 @@ final class CliClientTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testMakeEnvVarsWithFailingDecoder() async throws {
|
||||
try await withDependencies {
|
||||
$0.environment.coders = { ThrowingDecoder() }
|
||||
} operation: {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
let envVars = try await cliClient.makeEnvVars(.init())
|
||||
XCTAssertEqual(envVars, EnvVars())
|
||||
}
|
||||
}
|
||||
|
||||
func testMakeEnvVarsWithFailingEncoder() async throws {
|
||||
try await withDependencies {
|
||||
$0.environment.coders = { ThrowingEncoder() }
|
||||
} operation: {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
let envVars = try await cliClient.makeEnvVars(.init())
|
||||
XCTAssertEqual(envVars, EnvVars())
|
||||
}
|
||||
}
|
||||
|
||||
func testFileType() {
|
||||
let arguments = [
|
||||
(EnvironmentDependency.FileType.dotEnv(path: "test.env"), "test.env"),
|
||||
@@ -139,12 +161,16 @@ final class CliClientTests: XCTestCase {
|
||||
// - MARK: Helper
|
||||
|
||||
private func cleanFilePath(_ path: String) -> String {
|
||||
#if os(Linux)
|
||||
return "Tests/CliClientTests/\(path)"
|
||||
#else
|
||||
let split = path.split(separator: ".")
|
||||
let fileName = split.first!
|
||||
let ext = split.last!
|
||||
let url = Bundle.module.url(forResource: String(fileName), withExtension: String(ext))!.absoluteString
|
||||
let cleaned = url.split(separator: "file://").last!
|
||||
return String(cleaned)
|
||||
#endif
|
||||
}
|
||||
|
||||
extension EnvVars {
|
||||
@@ -159,3 +185,28 @@ extension EnvVars {
|
||||
version: "5.0"
|
||||
)
|
||||
}
|
||||
|
||||
struct ThrowingDecoder: Coderable {
|
||||
|
||||
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
|
||||
try JSONEncoder().encode(instance)
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
|
||||
throw DecodeError()
|
||||
}
|
||||
}
|
||||
|
||||
struct ThrowingEncoder: Coderable {
|
||||
|
||||
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
|
||||
throw EncodeError()
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
|
||||
try JSONDecoder().decode(T.self, from: data)
|
||||
}
|
||||
}
|
||||
|
||||
struct DecodeError: Error {}
|
||||
struct EncodeError: Error {}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
# run this with docker-compose run test
|
||||
name: swift-mqtt-dewpoint-test
|
||||
|
||||
services:
|
||||
test:
|
||||
build:
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
# run this with docker-compose run dewpoint_controller
|
||||
name: swift-mqtt-dewpoint
|
||||
|
||||
services:
|
||||
dewpoint_controller:
|
||||
container_name: dewpoint-controller
|
||||
|
||||
Reference in New Issue
Block a user