Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #45 from steersbob/feature/service-setup
Browse files Browse the repository at this point in the history
feature/service setup
  • Loading branch information
steersbob authored Aug 1, 2023
2 parents 2fd25da + cd44d65 commit 4969e50
Show file tree
Hide file tree
Showing 15 changed files with 650 additions and 517 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv/
.vscode/
12 changes: 8 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches: ["**"]
pull_request:
branches: [develop, edge]
workflow_dispatch: {}

env:
DOCKER_IMAGE: ghcr.io/brewblox/brewblox-plaato

jobs:
build:
Expand All @@ -23,7 +27,7 @@ jobs:
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/brewblox/brewblox-plaato
images: ${{ env.DOCKER_IMAGE }}

- name: ghcr.io login
uses: docker/login-action@v2
Expand All @@ -44,9 +48,9 @@ jobs:
poetry run pytest
poetry run flake8
- name: Run setup script
- name: Build
run: |
bash docker/before_build.sh
poetry run invoke build
- name: Build Docker image
uses: docker/build-push-action@v4
Expand All @@ -55,4 +59,4 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
context: docker
context: .
12 changes: 4 additions & 8 deletions docker/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
FROM python:3.9 as base
FROM python:3.9-bullseye as base

COPY ./dist /app/dist
COPY ./requirements.txt /app/requirements.txt

ENV PIP_EXTRA_INDEX_URL=https://www.piwheels.org/simple
ENV PIP_FIND_LINKS=/wheeley

RUN set -ex \
&& mkdir /wheeley \
&& pip3 install --upgrade pip wheel setuptools \
&& pip3 wheel --wheel-dir=/wheeley -r /app/requirements.txt \
&& pip3 wheel --wheel-dir=/wheeley /app/dist/*
&& pip3 wheel --wheel-dir=/wheeley -r /app/dist/requirements.txt \
&& pip3 wheel --wheel-dir=/wheeley /app/dist/*.tar.gz

FROM python:3.9-slim
FROM python:3.9-slim-bullseye
EXPOSE 5000
WORKDIR /app

ARG service_info=UNKNOWN
ENV SERVICE_INFO=${service_info}

COPY --from=base /wheeley /wheeley

RUN set -ex \
Expand Down
21 changes: 12 additions & 9 deletions brewblox_plaato/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from brewblox_service import brewblox_logger, http, mqtt, scheduler, service

from brewblox_plaato import broadcaster
from brewblox_plaato.models import ServiceConfig

LOGGER = brewblox_logger(__name__)


def create_parser(default_name='plaato'):
parser = service.create_parser(default_name=default_name)
def create_parser():
parser = service.create_parser('plaato')

# Service network options
group = parser.add_argument_group('Service communication')
Expand All @@ -23,15 +24,17 @@ def create_parser(default_name='plaato'):


def main():
app = service.create_app(parser=create_parser())
parser = create_parser()
config = service.create_config(parser, model=ServiceConfig)
app = service.create_app(config)

scheduler.setup(app)
mqtt.setup(app)
http.setup(app)
broadcaster.setup(app)
async def setup():
scheduler.setup(app)
mqtt.setup(app)
http.setup(app)
broadcaster.setup(app)

service.furnish(app)
service.run(app)
service.run_app(app, setup())


if __name__ == '__main__':
Expand Down
20 changes: 12 additions & 8 deletions brewblox_plaato/broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from brewblox_service import (brewblox_logger, features, http, mqtt, repeater,
strex)

from brewblox_plaato.models import ServiceConfig

LOGGER = brewblox_logger(__name__)
AUTH_ENV_KEY = 'PLAATO_AUTH'
PINS = [
Expand Down Expand Up @@ -77,9 +79,11 @@ async def _fetch(self, url):
return val

async def prepare(self):
self.name = self.app['config']['name']
self.interval = self.app['config']['broadcast_interval']
self.topic = self.app['config']['history_topic'] + '/plaato'
config: ServiceConfig = self.app['config']

self.name = config.name
self.interval = config.broadcast_interval
self.topic = f'{config.history_topic}/plaato'

if self.interval <= 0:
raise repeater.RepeaterCancelled()
Expand All @@ -98,13 +102,13 @@ async def run(self):
LOGGER.debug(data)

await mqtt.publish(self.app,
self.topic,
json.dumps({'key': self.name,
'data': data.serialize()}))
topic=self.topic,
payload=json.dumps({'key': self.name,
'data': data.serialize()}))


def setup(app: web.Application):
features.add(app, Broadcaster(app))
def setup(app: web.Application, **kwargs):
features.add(app, Broadcaster(app, **kwargs))


def fget(app: web.Application) -> Broadcaster:
Expand Down
5 changes: 5 additions & 0 deletions brewblox_plaato/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from brewblox_service.models import BaseServiceConfig


class ServiceConfig(BaseServiceConfig):
broadcast_interval: float
2 changes: 0 additions & 2 deletions docker/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions docker/before_build.sh

This file was deleted.

Loading

0 comments on commit 4969e50

Please sign in to comment.