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

Commit

Permalink
Merge pull request #1 from trizz/develop
Browse files Browse the repository at this point in the history
Release v1.0.0
  • Loading branch information
trizz authored Oct 9, 2019
2 parents 145b6c7 + 2f581bd commit fa3a938
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
/log_mount
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Changelog

### Unreleased / develop

### v1.0.0 - 2019-10-09
- Replace console output with a (rotating) log handler.
- Move to `python:3-alpine` base image for greater compatibility with different platforms.
- Extract the dependencies to `requirements.txt`.
- Using `buildx` to create images for the following platforms:
- `linux/amd64`
- `linux/arm64`
- `linux/386`
- `linux/arm/v7`
- `linux/arm/v6`

### v0.1.0
- Initial release.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM resin/raspberry-pi-alpine-python:3.7-slim
FROM python:3-alpine

COPY dsmr_datalogger_api_client.py ./
COPY requirements.txt ./

RUN pip install --no-cache-dir pyserial==3.2.1 requests==2.12.4
RUN mkdir -p /etc/dsmr_logs
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "./dsmr_datalogger_api_client.py"]
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ You can pass the host and the API key as environment variables and optionally al
the USB device (default: `/dev/ttyUSB0`). Don't forget to add the `--device` parameter to make your DSMR cable available
in the docker container, or run the container with the `--privileged` flag (not recommended).


```
docker run -d \
--device=/dev/ttyUSB0 \
Expand All @@ -15,3 +14,24 @@ docker run -d \
-e DSMR_API_KEY=<YOUR_API_KEY> \
trizz/dsmr-remote-datalogger:latest
```

Log files are being written to `/etc/dsmr_logs` in the container. Override this
location if you want to keep logfiles between containers or have easy access to them:
`-v /path/on/host:/etc/dsmr_logs`

## Supported architectures:
- `linux/amd64`
- `linux/arm64`
- `linux/386`
- `linux/arm/v7`
- `linux/arm/v6`

It is not necessary to define the architecture you want to use when creating a container, Docker will try
to find and match the correct image for your host.

## Versioning
This project uses [Semver](https://semver.org) for versioning. For each release three Docker images will
be provided:
- `latest` - Using this image will ensure you always have the latest version.
- `v1` - This image will be created for every release in the v1.x.x branch. Use this image if you want the latest version, but also want to be sure updating your container does not break stuff.
- `v1.0.0` - For each version an image will be created for those who want to use a specific version.
11 changes: 11 additions & 0 deletions build-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
read -p 'Tag name (default: develop): ' tagname
tagname=${tagname:-develop}

# Setup buildx:
# docker buildx create --name dsmrbuilder
# docker buildx use dsmrbuilder
# docker buildx inspect --bootstrap

docker buildx build --platform linux/amd64,linux/arm64,linux/386,linux/arm/v7,linux/arm/v6 -t trizz/dsmr-remote-datalogger:${tagname} --push . && \
docker buildx imagetools inspect docker.io/trizz/dsmr-remote-datalogger:${tagname}
11 changes: 11 additions & 0 deletions build-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
# DO NOT USE THIS FILE TO RUN THE DATA LOGGER!

docker build . -t trizz/dsmr-remote-datalogger:develop && \
docker run -d \
-e DSMR_USB_PORT=/dev/ttyUSB0 \
-e DSMR_API_URL=https://127.0.0.1/api/v1/datalogger/dsmrreading \
-e DSMR_API_KEY=fake_key \
-e LOG_LEVEL=debug \
-v $(pwd)/log_mount:/etc/dsmr_logs \
trizz/dsmr-remote-datalogger:develop
33 changes: 26 additions & 7 deletions dsmr_datalogger_api_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from time import sleep

from logging.handlers import RotatingFileHandler
from serial.serialutil import SerialException
import logging
import requests
import serial
import os
Expand All @@ -9,9 +10,22 @@
(os.getenv('DSMR_API_URL', "127.0.0.1"), os.getenv('DSMR_API_KEY', "API-KEY")),
)

# Set up the logger instance. Create a maximum of 10 log files of 1MB each.
log_level = getattr(logging, os.getenv('LOG_LEVEL', 'WARNING').upper(), None)
if not isinstance(log_level, int):
raise ValueError('Invalid log level: %s' % os.getenv('log'))

# Configure the logging instance.
handler = RotatingFileHandler('/etc/dsmr_logs/dsmr-datalogger.log', maxBytes=1e6, backupCount=10)
handler.setFormatter(logging.Formatter('[%(asctime)s - %(levelname)s] %(message)s'))

logger = logging.getLogger('dsmr-datalogger')
logger.addHandler(handler)
logger.setLevel(log_level)


def main():
print ('Starting...')
logger.info('Starting...')

for telegram in read_telegram():
for current_server in API_SERVERS:
Expand All @@ -34,8 +48,11 @@ def read_telegram():
serial_handle.rtscts = 0
serial_handle.timeout = 20

# This might fail, but nothing we can do so just let it crash.
serial_handle.open()
try:
# This might fail, but nothing we can do so just let it crash.
serial_handle.open()
except SerialException as error:
logger.error('Serial connection failed: {}'.format(str(error)))

telegram_start_seen = False
buffer = ''
Expand All @@ -46,7 +63,7 @@ def read_telegram():
data = serial_handle.readline()
except SerialException as error:
# Something else and unexpected failed.
print('Serial connection failed:', error)
logger.error('Serial connection failed: {}'.format(str(error)))
return # Break out of yield.

try:
Expand Down Expand Up @@ -81,12 +98,14 @@ def send_telegram(telegram, api_url, api_key):
api_url,
headers={'X-AUTHKEY': api_key},
data={'telegram': telegram},
timeout=60,
)

# Old versions of DSMR-reader return 200, new ones 201.
if response.status_code not in (200, 201):
# Or you will find the error (hint) in the reponse body on failure.
print('API error: {}'.format(response.text))
# Or you will find the error (hint) in the response body on failure.
logger.error('API error: {}'.format(response.text))


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
serial
pyserial==3.2.1
requests==2.12.4

0 comments on commit fa3a938

Please sign in to comment.