Skip to content

Commit

Permalink
Merge pull request #2 from heubeck/feature/MySqlSupport
Browse files Browse the repository at this point in the history
feat(backend): Add MySql support
  • Loading branch information
heubeck authored Dec 7, 2022
2 parents c0aeb05 + d15b8aa commit cc1d673
Show file tree
Hide file tree
Showing 12 changed files with 1,600 additions and 67 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build
on:
push:
branches:
- '*'
- '**'
paths-ignore:
- '.github/**'
- 'static/**'
Expand Down Expand Up @@ -35,6 +35,11 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: |
sudo apt-get install -y uuid-dev uuid-runtime
make test
- name: Build AMD64 binary
uses: docker/build-push-action@v3
with:
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

all: samler

test: libsml
go test

libsml:
make -C libsml/sml
make -C libsml/examples

samler: libsml
go build -ldflags "-X main.Version=$$VERSION"
go test

.PHONY: clean libsml
clean:
Expand Down
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SaMLer

The SaMLer is reading SML messages produced by a smart meter from a serial device and publishes it to InfluxDB.
The SaMLer is reading SML messages produced by a smart meter from a serial device and publishes it to a backend like InfluxDB or MySQL.

## Dependencies

Expand All @@ -17,28 +17,45 @@ SaMLer is configured using environment variables, just run it, to let it print i

```shell
> ./samler.amd64
SaMLer; configure via ENV:
SAMLER_INFLUX_TOKEN (default: )
SAMLER_INFLUX_ORG (default: )
SAMLER_INFLUX_MEASUREMENT (default: power)
SAMLER_CACHE_PATH (default: /home/heubeck/.samler)
SaMLer v0.3.0 Copyright (C) 2022 Florian Heubeck
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under certain conditions.

# Configuration options, set them as ENV:
SAMLER_MYSQL_DSN (default: -)
SAMLER_DEVICE_BAUD_RATE (default: 9600)
SAMLER_DEVICE_MODE (default: 8-N-1)
SAMLER_DEBUG (default: false)
SAMLER_INFLUX_URL (default: )
SAMLER_INFLUX_URL (default: -)
SAMLER_INFLUX_TOKEN (default: -)
SAMLER_INFLUX_BUCKET (default: home)
SAMLER_INFLUX_MEASUREMENT (default: power)
SAMLER_MYSQL_TABLE (default: home_power)
SAMLER_DEVICE (default: /dev/ttyUSB0)
SAMLER_DEBUG (default: false)
SAMLER_CACHE_PATH (default: /home/heubeck/.samler)
SAMLER_BACKEND (options: influx, mysql)
SAMLER_INFLUX_ORG (default: -)

Please set all values without default depending on the chosen backend
```

A minimalistic run script using [Influx Cloud](https://cloud2.influxdata.com/) may look like:

```shell
#!/bin/bash
SAMLER_BACKEND=influx \
SAMLER_INFLUX_URL=https://region.provider.cloud2.influxdata.com \
SAMLER_INFLUX_TOKEN=thisIsVerySecret== \
[email protected] \
/opt/samler.arm-v7
```

export SAMLER_INFLUX_URL=https://region.provider.cloud2.influxdata.com
export SAMLER_INFLUX_TOKEN=thisIsVerySecret==
export [email protected]
With MySQL the script can look like, the target table is created automatically if it doesn't exist:

```shell
#!/bin/bash
SAMLER_BACKEND=mysql \
SAMLER_MYSQL_DSN=user:password@tcp(your-database-host:3306)/samler \
/opt/samler.arm-v7
```

Expand All @@ -65,11 +82,11 @@ WantedBy=multi-user.target
* By now, there's only a single serial mode supported what's reflected in the configuration defaults:
_Baud rate `9600` and mode `8-N-1` (8 data bits, 1 stop bit, none parity)_
That needs to be generalized.
* Smart Meter data is properitary to electric power meter, as that's the only one I have running SaMLer right now.
* Smart Meter data may be properitary to electric power meter, as that's the only one I have tested SaMLer with right now.
Please file [issues](https://github.com/heubeck/samler/issues) with devices you'd like to read.
* Timing values are hard coded and should made configurable on demand.
* My C and Go skills are only rudimentary, don't hesitate to point out improvements.
* The only supported backend is InfluxDB by now, but it's prepared to support more in the future, just file an [issues](https://github.com/heubeck/samler/issues).
* The only supported backends are InfluxDB and MySQL by now, but it's prepared to support more, just file an [issues](https://github.com/heubeck/samler/issues).

## Contribution

Expand Down
33 changes: 32 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,44 @@ module samler
go 1.19

require (
github.com/go-sql-driver/mysql v1.7.0
github.com/influxdata/influxdb-client-go/v2 v2.12.1
github.com/nsqio/go-diskqueue v1.1.1-0.20211017194114-cc41549f81d5
github.com/testcontainers/testcontainers-go v0.15.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit cc1d673

Please sign in to comment.