Skip to content

Commit

Permalink
Change serial library, upgrade go and deps and add OpenBSD build in CI.
Browse files Browse the repository at this point in the history
  • Loading branch information
syberalexis committed Nov 1, 2022
1 parent 6f3129e commit 1aa619b
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 61 deletions.
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sudo: false

_go_build: &go_build
language: go
go: 1.14.x
go: 1.19.x
go_import_path: github.com/syberalexis/linky-exporter
script: BUILD_GO111MODULE=on GOOS=${BUILD_GOOS} GOARCH=${BUILD_GOARCH} GOARM=${BUILD_GOARM} make clean build
deploy:
Expand All @@ -20,6 +20,7 @@ _go_build: &go_build
matrix:
include:
# GO builds
### Linux
- <<: *go_build
env: BUILD_GOOS=linux BUILD_GOARCH=amd64
- <<: *go_build
Expand All @@ -30,6 +31,18 @@ matrix:
env: BUILD_GOOS=linux BUILD_GOARCH=arm BUILD_GOARM=7
- <<: *go_build
env: BUILD_GOOS=linux BUILD_GOARCH=arm64
### OpenBSD
- <<: *go_build
env: BUILD_GOOS=openbsd BUILD_GOARCH=amd64
- <<: *go_build
env: BUILD_GOOS=openbsd BUILD_GOARCH=arm BUILD_GOARM=5
- <<: *go_build
env: BUILD_GOOS=openbsd BUILD_GOARCH=arm BUILD_GOARM=6
- <<: *go_build
env: BUILD_GOOS=openbsd BUILD_GOARCH=arm BUILD_GOARM=7
- <<: *go_build
env: BUILD_GOOS=openbsd BUILD_GOARCH=arm64
### Windows
- <<: *go_build
env: BUILD_GOOS=windows BUILD_GOARCH=amd64
- <<: *go_build
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14 as builder
FROM golang:1.19 as builder
RUN mkdir /build
ADD . /build/
WORKDIR /build
Expand Down
2 changes: 1 addition & 1 deletion cmd/linky-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"

"github.com/pkg/errors"
"github.com/prometheus/common/log"
log "github.com/sirupsen/logrus"
"github.com/syberalexis/linky-exporter/pkg/core"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand Down
Binary file added dist/linky-exporter--linux-armv6
Binary file not shown.
27 changes: 20 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
module github.com/syberalexis/linky-exporter

go 1.14
go 1.19

require (
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/common v0.9.1
github.com/sirupsen/logrus v1.5.0
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
google.golang.org/api v0.24.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.0
github.com/sirupsen/logrus v1.9.0
go.bug.st/serial v1.4.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
205 changes: 169 additions & 36 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pkg/collectors/linky-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/tarm/serial"
"go.bug.st/serial"
)

// LinkyCollector object to describe and collect metrics
type LinkyCollector struct {
device string
baudRate int
frameSize byte
frameSize int
parity serial.Parity
stopBits serial.StopBits
index *prometheus.Desc
Expand Down Expand Up @@ -51,7 +51,7 @@ type linkyValues struct {
}

// NewLinkyCollector method to construct LinkyCollector
func NewLinkyCollector(device string, baudRate int, frameSize byte, parity serial.Parity, stopBits serial.StopBits) *LinkyCollector {
func NewLinkyCollector(device string, baudRate int, frameSize int, parity serial.Parity, stopBits serial.StopBits) *LinkyCollector {
return &LinkyCollector{
device: device,
baudRate: baudRate,
Expand Down Expand Up @@ -178,8 +178,8 @@ func (collector *LinkyCollector) Collect(ch chan<- prometheus.Metric) {

// Read information from serial port
func (collector *LinkyCollector) readSerial(linkyValues *linkyValues) error {
c := &serial.Config{Name: collector.device, Baud: collector.baudRate, Size: collector.frameSize, Parity: collector.parity, StopBits: collector.stopBits}
stream, err := serial.OpenPort(c)
mode := &serial.Mode{BaudRate: collector.baudRate, DataBits: collector.frameSize, Parity: collector.parity, StopBits: collector.stopBits}
stream, err := serial.Open(collector.device, mode)
if err != nil {
log.Fatal(err)
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/core/linky-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/syberalexis/linky-exporter/pkg/collectors"
"github.com/tarm/serial"
"go.bug.st/serial"
)

// LinkyExporter object to run exporter server and expose metrics
Expand All @@ -28,7 +28,7 @@ func (exporter *LinkyExporter) Run() {
log.Info(fmt.Sprintf("Beginning to serve on port :%d", exporter.Port))

prometheus.MustRegister(collectors.NewLinkyCollector(exporter.Device, exporter.BaudRate,
byte(exporter.FrameSize), parseParity(exporter.Parity), parseStopBits(exporter.StopBits)))
exporter.FrameSize, parseParity(exporter.Parity), parseStopBits(exporter.StopBits)))
http.Handle("/metrics", promhttp.Handler())

log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", exporter.Address, exporter.Port), nil))
Expand All @@ -37,19 +37,19 @@ func (exporter *LinkyExporter) Run() {
func parseParity(value string) (parity serial.Parity) {
switch value {
case "ParityNone", "N":
parity = serial.ParityNone
parity = serial.NoParity
break
case "ParityOdd", "O":
parity = serial.ParityOdd
parity = serial.OddParity
break
case "ParityEven", "E":
parity = serial.ParityEven
parity = serial.EvenParity
break
case "ParityMark", "M":
parity = serial.ParityMark
parity = serial.MarkParity
break
case "ParitySpace", "S":
parity = serial.ParitySpace
parity = serial.SpaceParity
break
default:
_, err := fmt.Fprintln(os.Stderr, "Impossible to parse Parity named", value)
Expand All @@ -64,13 +64,13 @@ func parseParity(value string) (parity serial.Parity) {
func parseStopBits(value string) (stopBits serial.StopBits) {
switch value {
case "Stop1", "1":
stopBits = serial.Stop1
stopBits = serial.OneStopBit
break
case "Stop1Half", "15":
stopBits = serial.Stop1Half
stopBits = serial.OnePointFiveStopBits
break
case "Stop2", "2":
stopBits = serial.Stop2
stopBits = serial.TwoStopBits
break
default:
_, err := fmt.Fprintln(os.Stderr, "Impossible to parse StopBits named", value)
Expand Down

0 comments on commit 1aa619b

Please sign in to comment.