diff --git a/.dockerignore b/.dockerignore index bb56973..d04abd1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ .build/* +mqtt_password.txt diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..eb2383b --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,61 @@ +# +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'] + +# 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: ghcr.io + IMAGE_NAME: swift-mqtt-dewpoint + +# 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: ${{ github.actor }} + password: ${{ secrets.GITHUB_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 }} + # 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: . + 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 + + diff --git a/.gitignore b/.gitignore index 0d58ddf..d8cdac7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ DerivedData/ .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .dewPoint-env .topics +mqtt_password.txt +.env diff --git a/Sources/ClientLive/Helpers.swift b/Sources/ClientLive/Helpers.swift index f468dd5..e047ed6 100644 --- a/Sources/ClientLive/Helpers.swift +++ b/Sources/ClientLive/Helpers.swift @@ -12,7 +12,7 @@ protocol BufferInitalizable { } extension Double: BufferInitalizable { - + /// Attempt to create / parse a double from a byte buffer. init?(buffer: inout ByteBuffer) { guard let string = buffer.readString( @@ -61,7 +61,7 @@ extension Result where Success == MQTTPublishInfo { } extension Optional where Wrapped == ByteBuffer { - + func parse(as type: T.Type) -> T? where T: BufferInitalizable { switch self { case var .some(buffer): @@ -73,14 +73,14 @@ extension Optional where Wrapped == ByteBuffer { } fileprivate struct TemperatureAndHumiditySensorKeyPathEnvelope { - + let humidityTopic: KeyPath let temperatureTopic: KeyPath let temperatureState: WritableKeyPath let humidityState: WritableKeyPath - + func addListener(to client: MQTTNIO.MQTTClient, topics: Topics, state: State) { - + let temperatureTopic = topics.sensors[keyPath: temperatureTopic] client.logger.trace("Adding listener for topic: \(temperatureTopic)") client.addPublishListener(named: temperatureTopic) { result in @@ -90,7 +90,7 @@ fileprivate struct TemperatureAndHumiditySensorKeyPathEnvelope { state.sensors[keyPath: temperatureState] = temperature } } - + let humidityTopic = topics.sensors[keyPath: humidityTopic] client.logger.trace("Adding listener for topic: \(humidityTopic)") client.addPublishListener(named: humidityTopic) { result in @@ -159,7 +159,7 @@ extension State { } extension Client.SensorPublishRequest { - + func dewPointData(topics: Topics, units: PsychrometricEnvironment.Units?) -> (DewPoint, String)? { switch self { case let .mixed(sensor): @@ -176,7 +176,7 @@ extension Client.SensorPublishRequest { return (dp, topics.sensors.supplyAirSensor.dewPoint) } } - + func enthalpyData(altitude: Length, topics: Topics, units: PsychrometricEnvironment.Units?) -> (EnthalpyOf, String)? { switch self { case let .mixed(sensor): @@ -193,7 +193,7 @@ extension Client.SensorPublishRequest { return (enthalpy, topics.sensors.supplyAirSensor.enthalpy) } } - + func setHasProcessed(state: State) { switch self { case .mixed: @@ -209,7 +209,7 @@ extension Client.SensorPublishRequest { } extension MQTTNIO.MQTTClient { - + func publishDewPoint( request: Client.SensorPublishRequest, state: State, @@ -225,7 +225,8 @@ extension MQTTNIO.MQTTClient { return publish( to: topic, payload: ByteBufferAllocator().buffer(string: "\(roundedDewPoint)"), - qos: .atLeastOnce + qos: .atLeastOnce, + retain: true ) .map { (self, request, state, topics) } } diff --git a/docker-compose.yaml b/docker-compose.yaml index d5e5ff0..60a308f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,10 @@ # run this with docker-compose -f docker/docker-compose.yml run test services: + server: + image: swift-mqtt-dewpoint:latest + env_file: .env + command: /bin/bash -xc "./dewPoint-controller" + test: image: swift:latest #build: @@ -39,3 +44,4 @@ networks: test: driver: bridge external: false +