diff --git a/.gitignore b/.gitignore index bcfc393..4a56574 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ schedule.db .idea/ tion.log tion.pid +dst/ diff --git a/Makefile b/Makefile index 65e501b..6dc63f0 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,38 @@ CGO_CFLAGS="-I${PWD}/../go-gattlib/gattlib/include" CGO_LDFLAGS="-L${PWD}/../go-gattlib/gattlib/build/dbus -lm -lutil" -GF=CGO_CFLAGS=${CGO_CFLAGS} CGO_LDFLAGS=${CGO_LDFLAGS} LD_LIBRARY_PATH=${PWD}/../go-gattlib/gattlib/build/dbus +IMPL?=muka +ARCH?=$(go env GOARCH) +TAGS=-tags ${IMPL} + +GF=CGO_CFLAGS=${CGO_CFLAGS} CGO_LDFLAGS=${CGO_LDFLAGS} LD_LIBRARY_PATH=${PWD}/../go-gattlib/gattlib/build/dbus GOARCH=${ARCH} all: test build deps: - ${GF} go get -v -d ./... + ${GF} go get ${TAGS} -v -d ./... test: deps - ${GF} go test -v $$(go list ./... | grep -v /vendor/) + ${GF} go test ${TAGS} -v $$(go list ./... | grep -v /vendor/) lint: deps ${GF} ${GOPATH}/bin/golangci-lint run -v ./... ${GF} ${GOPATH}/bin/golint $$(go list ./... | grep -v /vendor/) build-cli: deps - ${GF} go build -o tion-cli ./cli + ${GF} go build -o tion-cli ${TAGS} ./cmd/cli build-influx: deps - ${GF} go build -o tion-influx ./influx + ${GF} go build -o tion-influx ${TAGS} ./cmd/influx build-mqtt: deps - ${GF} go build -o tion-mqtt ./mqtt + ${GF} go build -o tion-mqtt ${TAGS} ./cmd/mqtt build-schedule: deps - ${GF} go build -o tion-schedule ./schedule + ${GF} go build -o tion-schedule ${TAGS} ./cmd/schedule build: build-cli build-influx build-schedule build-mqtt -clean: gattlib-clean +clean: rm -f ./tion-influx rm -f ./tion-schedule rm -f ./tion-cli diff --git a/README.md b/README.md index 9b75c00..ec25acc 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,18 @@ Turn on/off "topic": "nn/tion-control" } ``` +## Building +``` + make GOARCH= IMPL= +``` +GOARCH - https://golang.org/doc/install/source#environment +IMPL - one of below + - muka https://github.com/muka/go-bluetooth backend implementation, default + - ppal + - gatt + - fake + - mqtt ## Magic When getting 'Unexpected response length' with muka driver, run once with ppal driver \ No newline at end of file diff --git a/cli/tion-cli.go b/cmd/cli/tion-cli.go similarity index 81% rename from cli/tion-cli.go rename to cmd/cli/tion-cli.go index 2f36db5..92c69e7 100644 --- a/cli/tion-cli.go +++ b/cmd/cli/tion-cli.go @@ -5,23 +5,16 @@ import ( "fmt" "log" - "github.com/m-pavel/go-tion/impl/fake" + "github.com/m-pavel/go-tion/impl" "time" - tion_gatt "github.com/m-pavel/go-tion/impl/gatt" - "github.com/m-pavel/go-tion/impl/mqttcli" - tion_muka "github.com/m-pavel/go-tion/impl/muka" - tion_ppal "github.com/m-pavel/go-tion/impl/ppal" "github.com/m-pavel/go-tion/tion" ) -// timpl "github.com/m-pavel/go-tion/tionm" works -// timpl "github.com/m-pavel/go-tion/tionn" -// timpl "github.com/m-pavel/go-tion/gatt" works type cliDevice struct { - device *string - driver *string + device *string + mqttUser *string mqttPass *string mqttCa *string @@ -35,7 +28,6 @@ type cliDevice struct { func main() { device := cliDevice{} device.device = flag.String("device", "", "BT (or MQTT) address") - device.driver = flag.String("driver", "muka", "driver") device.mqttUser = flag.String("mqtt-user", "", "MQTT user") device.mqttPass = flag.String("mqtt-pass", "", "MQTT password") device.mqttCa = flag.String("mqtt-ca", "", "MQTT ca") @@ -154,11 +146,13 @@ func main() { } } + func deviceCallLog(device *cliDevice, cb func(tion.Tion, *tion.Status) error, succ string) { if err := deviceCall(device, cb, succ); err != nil { log.Println(err) } } + func deviceCall(device *cliDevice, cb func(tion.Tion, *tion.Status) error, succ string) error { t := newDevice(device) if err := t.Connect(device.timeout); err != nil { @@ -184,24 +178,7 @@ func deviceCall(device *cliDevice, cb func(tion.Tion, *tion.Status) error, succ } func newDevice(device *cliDevice) tion.Tion { - if *device.device != "" { - switch *device.driver { - case "muka": - return tion_muka.New(*device.device, *device.debug) - case "ppal": - return tion_ppal.New(*device.device, *device.debug) - case "gatt": - return tion_gatt.New(*device.device, *device.debug) - case "mqtt": - return mqttcli.New(*device.device, *device.mqttUser, *device.mqttPass, *device.mqttCa, *device.mqttTopic, *device.mqttAvalTopic, *device.mqttControlTopic, *device.debug) - case "fake": - return fake.NewFake() - } - panic("Unknown driver " + *device.driver) - } - - log.Panic("Unable to create device") - return nil + return impl.NewTionImpl(*device.device, *device.debug) } //func scan() { diff --git a/influx/main.go b/cmd/influx/main.go similarity index 97% rename from influx/main.go rename to cmd/influx/main.go index 6df1d1c..66a9cf6 100644 --- a/influx/main.go +++ b/cmd/influx/main.go @@ -9,12 +9,14 @@ import ( "syscall" "time" + "github.com/m-pavel/go-tion/impl" + "fmt" "net/http" "github.com/influxdata/influxdb1-client/v2" - tionimpl "github.com/m-pavel/go-tion/impl/muka" + "github.com/m-pavel/go-tion/tion" "github.com/sevlyar/go-daemon" ) @@ -84,7 +86,7 @@ func daemonf(iserver, device string, interval int) { } defer cli.Close() - t := tionimpl.New(device) + t := impl.NewTionImpl(device) erinr := 0 for { diff --git a/mqtt/mqtt.go b/cmd/mqtt/mqtt.go similarity index 91% rename from mqtt/mqtt.go rename to cmd/mqtt/mqtt.go index ec01d39..d5f35d1 100644 --- a/mqtt/mqtt.go +++ b/cmd/mqtt/mqtt.go @@ -7,8 +7,7 @@ import ( _ "net/http" _ "net/http/pprof" - "github.com/m-pavel/go-tion/impl/fake" - tionimpl "github.com/m-pavel/go-tion/impl/muka" + "github.com/m-pavel/go-tion/impl" "time" @@ -24,10 +23,10 @@ const timeout = 7 * time.Second // TionService instance type TionService struct { - t tion.Tion - bt *string - debug bool - fake *bool + t tion.Tion + bt *string + debug bool + keepbt *bool ctx *ghm.ServiceContext } @@ -35,7 +34,6 @@ type TionService struct { // PrepareCommandLineParams for TionService func (ts *TionService) PrepareCommandLineParams() { ts.bt = flag.String("device", "xx:yy:zz:aa:bb:cc", "Device BT address") - ts.fake = flag.Bool("fake", false, "Fake device") ts.keepbt = flag.Bool("keepbt", false, "Keep bluetooth connection") } @@ -54,13 +52,7 @@ func (ts *TionService) Init(ctx *ghm.ServiceContext) error { go func() { log.Println(http.ListenAndServe(":7070", nil)) }() - if *ts.fake { - log.Println("Using fake device.") - ts.t = fake.NewFake() - } else { - ts.t = tionimpl.New(*ts.bt, ctx.Debug()) - } - + ts.t = impl.NewTionImpl(*ts.bt, ctx.Debug()) ts.debug = ctx.Debug() ts.ctx = ctx if *ts.keepbt { diff --git a/schedule/dao.go b/cmd/schedule/dao.go similarity index 100% rename from schedule/dao.go rename to cmd/schedule/dao.go diff --git a/schedule/main.go b/cmd/schedule/main.go similarity index 98% rename from schedule/main.go rename to cmd/schedule/main.go index 8412d48..274ece8 100644 --- a/schedule/main.go +++ b/cmd/schedule/main.go @@ -7,13 +7,14 @@ import ( "syscall" "time" + "github.com/m-pavel/go-tion/impl" + "github.com/m-pavel/go-tion/tion" "fmt" "github.com/gorhill/cronexpr" - tionimpl "github.com/m-pavel/go-tion/impl/muka" "github.com/sevlyar/go-daemon" ) @@ -240,7 +241,7 @@ func daemonf(device string, dao *Dao, repeat int) { } func execute(s Schedule, device string, retry int, interval time.Duration) error { - t := tionimpl.New(device) + t := impl.NewTionImpl(device) if err := t.Connect(timeout); err != nil { return err } diff --git a/impl/fake/fake.go b/impl/fake.go similarity index 86% rename from impl/fake/fake.go rename to impl/fake.go index 7f02fe7..aa72aa4 100644 --- a/impl/fake/fake.go +++ b/impl/fake.go @@ -1,4 +1,6 @@ -package fake +// +build fake + +package impl import ( "time" @@ -11,7 +13,7 @@ type fakeTion struct { } // NewFake backend -func NewFake() tion.Tion { +func NewTionImpl(addr string, debug ...bool) tion.Tion { return &fakeTion{} } diff --git a/impl/gatt/simple.go b/impl/gatt.go similarity index 64% rename from impl/gatt/simple.go rename to impl/gatt.go index 7a24e44..a705e6d 100644 --- a/impl/gatt/simple.go +++ b/impl/gatt.go @@ -1,4 +1,6 @@ -package gatt +// +build gatt + +package impl import ( "log" @@ -8,10 +10,10 @@ import ( "github.com/go-errors/errors" "github.com/m-pavel/go-gattlib/pkg" - tion2 "github.com/m-pavel/go-tion/tion" + "github.com/m-pavel/go-tion/tion" ) -type tion struct { +type gattTion struct { g *gattlib.Gatt Addr string debug bool @@ -19,28 +21,28 @@ type tion struct { } // New gattlib backend -func New(addr string, debug ...bool) tion2.Tion { - t := tion{Addr: addr, g: &gattlib.Gatt{}, mutex: &sync.Mutex{}} +func NewTionImpl(addr string, debug ...bool) tion.Tion { + t := gattTion{Addr: addr, g: &gattlib.Gatt{}, mutex: &sync.Mutex{}} if len(debug) == 1 && debug[0] { t.debug = true } return &t } -func (t tion) Info() string { +func (t gattTion) Info() string { return "github.com/m-pavel/go-tion/tion" } type cRes struct { - s *tion2.Status + s *tion.Status e error } -func (t tion) Connected() bool { +func (t gattTion) Connected() bool { return t.g.Connected() } -func (t *tion) Connect(timeout time.Duration) error { +func (t *gattTion) Connect(timeout time.Duration) error { t.mutex.Lock() defer t.mutex.Unlock() if t.g.Connected() { @@ -48,7 +50,7 @@ func (t *tion) Connect(timeout time.Duration) error { } return t.g.Connect(t.Addr) } -func (t *tion) Disconnect(duration time.Duration) error { +func (t *gattTion) Disconnect(duration time.Duration) error { t.mutex.Lock() defer t.mutex.Unlock() if t.g.Connected() { @@ -57,7 +59,7 @@ func (t *tion) Disconnect(duration time.Duration) error { return t.g.Disconnect() } -func (t *tion) ReadState(readtimeout time.Duration) (*tion2.Status, error) { +func (t *gattTion) ReadState(readtimeout time.Duration) (*tion.Status, error) { t.mutex.Lock() defer t.mutex.Unlock() @@ -80,25 +82,25 @@ func (t *tion) ReadState(readtimeout time.Duration) (*tion2.Status, error) { } } -func (t *tion) rw() (*tion2.Status, error) { +func (t *gattTion) rw() (*tion.Status, error) { if !t.g.Connected() { return nil, errors.New("Not connected") } - if err := t.g.Write(tion2.WriteCaract, tion2.StatusRequest); err != nil { + if err := t.g.Write(tion.WriteCaract, tion.StatusRequest); err != nil { return nil, err } time.Sleep(2 * time.Second) - resp, n, err := t.g.Read(tion2.ReadCharact) + resp, n, err := t.g.Read(tion.ReadCharact) if err != nil { return nil, err } if t.debug { log.Printf("RSP [%d]: %v\n", n, resp) } - return tion2.FromBytes(resp) + return tion.FromBytes(resp) } -func (t *tion) Update(s *tion2.Status, timeout time.Duration) error { +func (t *gattTion) Update(s *tion.Status, timeout time.Duration) error { t.mutex.Lock() defer t.mutex.Unlock() @@ -109,7 +111,7 @@ func (t *tion) Update(s *tion2.Status, timeout time.Duration) error { c1 := make(chan error, 1) go func() { - c1 <- t.g.Write(tion2.WriteCaract, tion2.FromStatus(s)) + c1 <- t.g.Write(tion.WriteCaract, tion.FromStatus(s)) }() select { diff --git a/impl/muka/sync.go b/impl/sync.go similarity index 96% rename from impl/muka/sync.go rename to impl/sync.go index 5a1039d..2700b4c 100644 --- a/impl/muka/sync.go +++ b/impl/sync.go @@ -1,4 +1,6 @@ -package muka +// +build muka + +package impl import ( "fmt" diff --git a/impl/muka/tionm.go b/impl/tionm.go similarity index 97% rename from impl/muka/tionm.go rename to impl/tionm.go index d008a45..1471c4d 100644 --- a/impl/muka/tionm.go +++ b/impl/tionm.go @@ -1,4 +1,6 @@ -package muka +// +build muka + +package impl import ( "errors" @@ -28,7 +30,7 @@ func (n mTion) Info() string { } // New go ble backend -func New(addr string, debug ...bool) tion.Tion { +func NewTionImpl(addr string, debug ...bool) tion.Tion { nt := mTion{addr: addr} nt.st = NewSt() if len(debug) > 0 { diff --git a/impl/mqttcli/tionmqtt.go b/impl/tionmqtt.go similarity index 95% rename from impl/mqttcli/tionmqtt.go rename to impl/tionmqtt.go index 6fa1f3b..9696d98 100644 --- a/impl/mqttcli/tionmqtt.go +++ b/impl/tionmqtt.go @@ -1,4 +1,6 @@ -package mqttcli +// +build mqttcli + +package impl import ( "errors" @@ -36,7 +38,7 @@ type mqttTion struct { } // New MQTT Tion backend -func New(url, user, pass string, ca string, topic, topica, topicc string, dbg bool) tion.Tion { +func NewTionImpl(url, user, pass string, ca string, topic, topica, topicc string, dbg bool) tion.Tion { mqt := mqttTion{url: url, user: user, pass: pass, ca: ca, topic: topic, topica: topica, topicc: topicc, debug: dbg} mqt.update = make(chan *tion.RestStatus) return &mqt diff --git a/impl/ppal/tionn.go b/impl/tionn.go similarity index 97% rename from impl/ppal/tionn.go rename to impl/tionn.go index 8408425..d7b92c3 100644 --- a/impl/ppal/tionn.go +++ b/impl/tionn.go @@ -1,4 +1,6 @@ -package ppal +// +build ppal + +package impl import ( "log" @@ -20,7 +22,7 @@ type nativeTion struct { } // New go gatt backend -func New(addr string, debug ...bool) tion.Tion { +func NewTionImpl(addr string, debug ...bool) tion.Tion { nt := nativeTion{addr: addr} nt.cnct = make(chan error) if len(debug) > 0 { diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..d85bf8a --- /dev/null +++ b/release.sh @@ -0,0 +1,28 @@ +#!/bin/bash +IMPL=muka + +make clean +rm -rf dst/* + +mkdir -p ./dst/386 +make IMPL=$IMPL ARCH=386 +mv ./tion-* ./dst/386 +tar -czvf ./dst/386.tgz -C ./dst/386/ . + +mkdir -p ./dst/amd64 +make IMPL=$IMPL ARCH=amd64 +mv ./tion-* ./dst/amd64 +tar -czvf ./dst/amd64.tgz -C ./dst/amd64/ . + +mkdir -p ./dst/arm +make IMPL=$IMPL ARCH=arm +mv ./tion-* ./dst/arm +tar -czvf ./dst/arm.tgz -C ./dst/arm/ . + + +mkdir -p ./dst/arm64 +make IMPL=$IMPL ARCH=arm64 +mv ./tion-* ./dst/arm64 +tar -czvf ./dst/arm64.tgz -C ./dst/arm64/ . + +