Skip to content

Commit

Permalink
Unify codebase for both ESP and RPi stations
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Oct 30, 2019
1 parent fd2896f commit 019cdc1
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 41 deletions.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
.PHONY: all

BIN := openair-station-esp
PKG := github.com/openairtech/station-esp
BIN := openair-station
PKG := github.com/openairtech/station
ARCH := amd64 arm

PUB_SERVER := openair.city
PUB_DIR := /var/www/get.openair.city/station-esp
PUB_DIR := /var/www/get.openair.city/station

BINDIR = bin

VERSION_VAR := main.Version
TIMESTAMP_VAR := main.Timestamp

VERSION ?= $(shell git describe --always --dirty --tags)
TIMESTAMP := $(shell date -u '+%Y-%m-%d_%I:%M:%S%p')
TIMESTAMP := $(shell date '+%Y-%m-%d_%T%Z')

GOBUILD_LDFLAGS := -ldflags "-s -w -X $(VERSION_VAR)=$(VERSION) -X $(TIMESTAMP_VAR)=$(TIMESTAMP)"

Expand All @@ -24,10 +24,13 @@ all: build
build:
go build -x $(GOBUILD_LDFLAGS) -v -o $(BINDIR)/$(BIN)

build-static: $(ARCH)
build-static: $(addprefix build-static-, $(ARCH))

$(ARCH):
env CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -a -installsuffix "static" $(GOBUILD_LDFLAGS) -o $(BINDIR)/$(BIN).$@
build-static-amd64:
env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix "static" $(GOBUILD_LDFLAGS) -o $(BINDIR)/$(BIN).amd64

build-static-arm:
env CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc GOOS=linux GOARCH=arm go build -a -installsuffix "static" $(GOBUILD_LDFLAGS) -o $(BINDIR)/$(BIN).arm

shasum:
cd $(BINDIR) && for file in $(ARCH) ; do sha256sum ./$(BIN).$${file} > ./$(BIN).$${file}.sha256.txt; done
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# OpenAir-Station-ESP
# OpenAir-Station

Data relay station for ESP8266-based monitor for OpenAir realtime air quality map project.
Data relay station for OpenAir air quality map project.

## License

OpenAir-Station-ESP is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/openairtech/station-esp/blob/master/LICENSE.txt)
OpenAir-Station is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/openairtech/station/blob/master/LICENSE.txt)
8 changes: 4 additions & 4 deletions contrib/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

set -e

BIN="openair-station-esp"
BIN="openair-station"

BASE_URL="https://get.openair.city/station-esp"
BASE_URL="https://get.openair.city/station"

BIN_DIR="/usr/bin"

Expand Down Expand Up @@ -196,11 +196,11 @@ run ${sudo} chmod +x "${BIN_DIR}/${UPD_BIN}" || fatal "can't set executable bit
progress "Setting up systemd..."
run ${sudo} sh -c "cat > /etc/systemd/system/${BIN}.service" <<EOF
[Unit]
Description=OpenAir ESP Station
Description=OpenAir Station
After=network.target
[Service]
ExecStart=${BIN_PATH}
ExecStart=${BIN_PATH} \$STATION_EXEC_OPTIONS
Restart=always
RestartSec=5
Expand Down
4 changes: 2 additions & 2 deletions contrib/scripts/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

set -e

BIN="openair-station-esp"
BIN="openair-station"

BASE_URL="https://get.openair.city/station-esp"
BASE_URL="https://get.openair.city/station"

BIN_DIR="/usr/bin"

Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module github.com/openairtech/station-esp
module github.com/openairtech/station

replace github.com/openairtech/api v0.0.0 => ../openair-api

require (
github.com/NotifAi/serial v0.2.3
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e
github.com/grandcat/zeroconf v0.0.0-20190424104450-85eadb44205c
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 // indirect
github.com/miekg/dns v1.1.8 // indirect
github.com/openairtech/api v0.0.0
github.com/openairtech/sds011 v0.0.0-20191029135153-f4ccb629bd55
github.com/pkg/errors v0.8.1 // indirect
github.com/sirupsen/logrus v1.4.1
github.com/stretchr/testify v1.2.2
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
github.com/NotifAi/serial v0.2.3 h1:kfD6mn4e04/m/braL9S5LKtDJMRmuqvFsfyjQUtnrP0=
github.com/NotifAi/serial v0.2.3/go.mod h1:xT0Wsw9XDw01/Zx7JM7JVchvDxzetEvQ6PKyqyIp/xg=
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375 h1:vdUOwcZdV+bBfGUUh5oPPWSzw9p+lBnNSuGgQwGpCH4=
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375/go.mod h1:3iz1WHlYJU9b4NJei+Q8G7DN3K05arcCMlOQ+qNCDjo=
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce h1:Dog7PLNz1fPaXqHPOHonpERqsF57Oh4X76pM80T1GDY=
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce/go.mod h1:AwxDPnsgIpy47jbGXZHA9Rv7pDkOJvQbezPuK1Y+nNk=
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e h1:ZG3JBA6rPRl0xxQ+nNSfO7tor8w+CNCTs05DNJQYbLM=
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e/go.mod h1:oA+9PUt8F1aKJ6o4YU1T120i7sgo1T6/1LWEEBy0BSs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/grandcat/zeroconf v0.0.0-20190424104450-85eadb44205c h1:svzQzfVE9t7Y1CGULS5PsMWs4/H4Au/ZTJzU/0CKgqc=
github.com/grandcat/zeroconf v0.0.0-20190424104450-85eadb44205c/go.mod h1:YjKB0WsLXlMkO9p+wGTCoPIDGRJH0mz7E526PxkQVxI=
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 h1:nI4Q7WQDcng8eRmi8bRP1SXlJIYLkgdgR7ZhJuzPbhs=
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02/go.mod h1:iYGcTYIPUvEWhFo6aKUuLchs+AV4ssYdyuBbQJZGcBk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/miekg/dns v1.1.8 h1:1QYRAKU3lN5cRfLCkPU08hwvLJFhvjP6MqNMmQz6ZVI=
github.com/miekg/dns v1.1.8/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/openairtech/sds011 v0.0.0-20191029135153-f4ccb629bd55 h1:A36hCbMr5fqBhsJx5i4AxijEytodI82DL0C4WtmbLek=
github.com/openairtech/sds011 v0.0.0-20191029135153-f4ccb629bd55/go.mod h1:lwkmgZyUp1ReNSy2vSBImedx/2PBVdOd6y2brXXkE8c=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -26,4 +38,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 h1:rOhMmluY6kLMhdnrivzec6lLgaVbMHMn2ISQXJeJ5EM=
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
35 changes: 29 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
log "github.com/sirupsen/logrus"
)

const (
StationModeEsp = "esp"
StationModeRpi = "rpi"
)

var (
Version = "unknown"
Timestamp = "unknown"
Expand All @@ -23,14 +28,19 @@ func main() {

debugFlag := flag.Bool("d", false, "enable debug logging")

mode := flag.String("m", StationModeEsp, "station mode (esp/rpi)")

espHost := flag.String("h", "OpenAir.local", "ESP station address")
espPort := flag.Int("p", 80, "ESP station port")

rpiI2cBusId := flag.Int("i", 1, "RPi station I2C bus ID")
rpiSerialPort := flag.String("s", "/dev/ttyAMA0", "RPi station serial port name")

apiServerUrl := flag.String("a", "https://api.openair.city/v1/feeder", "feeder endpoint address")

updatePeriod := flag.Duration("t", 1*time.Minute, "data update period")

settleTime := flag.Duration("S", 5*time.Minute, "data settle time after ESP station reboot")
settleTime := flag.Duration("S", 5*time.Minute, "data settle time after station restart")

resolverTimeout := flag.Duration("r", 15*time.Second, "name resolver timeout")

Expand All @@ -50,10 +60,6 @@ func main() {
log.SetLevel(log.DebugLevel)
}

version := fmt.Sprintf("esp-%s_%s-%s_%s", Version, Timestamp, runtime.GOARCH, runtime.GOOS)

log.Printf("starting station, version: %s", version)

InitResolvers(*resolverTimeout)

InitHttp(*httpTimeout)
Expand Down Expand Up @@ -83,7 +89,24 @@ func main() {
}
}()

RunStation(ctx, version, *espHost, *espPort, *apiServerUrl, *updatePeriod, *settleTime, *disablePmCorrectionFlag)
var station Station

if *mode == StationModeEsp {
station = NewEspStation(*espHost, *espPort)
} else if *mode == StationModeRpi {
var err error
if station, err = NewRpiStation(*rpiI2cBusId, 0x76, *rpiSerialPort, 3); err != nil {
log.Fatalf("can't initialize RPi station: %v", err)
}
} else {
log.Fatalf("unknown station mode: %s", *mode)
}

version := fmt.Sprintf("%s-%s_%s-%s_%s", *mode, Version, Timestamp, runtime.GOARCH, runtime.GOOS)

log.Printf("starting station, version: %s", version)

RunStation(ctx, station, version, *apiServerUrl, *updatePeriod, *settleTime, *disablePmCorrectionFlag)

log.Printf("exiting...")
}
Loading

0 comments on commit 019cdc1

Please sign in to comment.