Skip to content

Commit

Permalink
pre release
Browse files Browse the repository at this point in the history
  • Loading branch information
m-pavel committed Feb 4, 2020
1 parent 62619b1 commit 923f10c
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ schedule.db
.idea/
tion.log
tion.pid
dst/
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ Turn on/off
"topic": "nn/tion-control"
}
```
## Building
```
make GOARCH=<target arch> IMPL=<backend implementation>
```
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

35 changes: 6 additions & 29 deletions cli/tion-cli.go → cmd/cli/tion-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions influx/main.go → cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 6 additions & 14 deletions mqtt/mqtt.go → cmd/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -24,18 +23,17 @@ 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
}

// 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")

}
Expand All @@ -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 {
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions schedule/main.go → cmd/schedule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions impl/fake/fake.go → impl/fake.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package fake
// +build fake

package impl

import (
"time"
Expand All @@ -11,7 +13,7 @@ type fakeTion struct {
}

// NewFake backend
func NewFake() tion.Tion {
func NewTionImpl(addr string, debug ...bool) tion.Tion {
return &fakeTion{}
}

Expand Down
36 changes: 19 additions & 17 deletions impl/gatt/simple.go → impl/gatt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package gatt
// +build gatt

package impl

import (
"log"
Expand All @@ -8,47 +10,47 @@ 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
mutex *sync.Mutex
}

// 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() {
return errors.New("Tion already connected")
}
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() {
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion impl/muka/sync.go → impl/sync.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package muka
// +build muka

package impl

import (
"fmt"
Expand Down
Loading

0 comments on commit 923f10c

Please sign in to comment.