Skip to content

Commit

Permalink
Develop (#282)
Browse files Browse the repository at this point in the history
* technical debt

* bug fix
  • Loading branch information
e154 authored Jul 31, 2024
1 parent 210d161 commit 097b246
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 188 deletions.
2 changes: 0 additions & 2 deletions cmd/server/container/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ func MigrationList(adaptors *adaptors.Adaptors,
local_migrations.NewMigrationAutocert(adaptors),
local_migrations.NewMigrationPachka(adaptors),
local_migrations.NewMigrationWebhook(adaptors),
local_migrations.NewMigrationBle(adaptors),
local_migrations.NewMigrationTime(adaptors),
local_migrations.NewMigrationRemoveTriggersPlugin(adaptors),
}
}
18 changes: 8 additions & 10 deletions plugins/ble/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
package ble

import (
"github.com/e154/smart-home/common"
"strings"

"github.com/pkg/errors"
"tinygo.org/x/bluetooth"

"github.com/e154/smart-home/common/events"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/system/supervisor"
"github.com/pkg/errors"
)

// Actor ...
type Actor struct {
*supervisor.BaseActor
actionPool chan events.EventCallEntityAction
ble *Ble
ble Bluetooth
}

// NewActor ...
Expand Down Expand Up @@ -86,13 +85,13 @@ func (e *Actor) Destroy() {

func (e *Actor) Spawn() {

var timeout, connectionTimeout int64 = 5, 5
var timeout, connectionTimeout = DefaultTimeout, DefaultConnectionTimeout
if e.Setts[AttrConnectionTimeoutSec] != nil {
connectionTimeout = e.Setts[AttrConnectionTimeoutSec].Int64()
}

if e.Setts[AttrTimeoutSec] != nil {
connectionTimeout = e.Setts[AttrTimeoutSec].Int64()
timeout = e.Setts[AttrTimeoutSec].Int64()
}

var address string
Expand Down Expand Up @@ -127,11 +126,10 @@ func (e *Actor) addAction(event events.EventCallEntityAction) {
func (e *Actor) runAction(msg events.EventCallEntityAction) {

if strings.ToUpper(msg.ActionName) == ActionScan {
address, err := bluetooth.ParseUUID(e.Setts[AttrAddress].String())
if err != nil {
e.ble.Scan(nil)
if address, ok := e.Setts[AttrAddress]; ok {
e.ble.Scan(common.String(address.String()))
} else {
e.ble.Scan(&address)
e.ble.Scan(nil)
}
}

Expand Down
55 changes: 49 additions & 6 deletions plugins/ble/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func NewBle(address string, timeout, connectionTimeout int64, debug bool) *Ble {
return ble
}

func (b *Ble) Connect() error {
_, err := b.connect()
return err
}

func (b *Ble) Disconnect() error {
if !b.connected.Load() || b.device == nil {
return nil
Expand All @@ -70,8 +75,51 @@ func (b *Ble) Disconnect() error {
return nil
}

func (b *Ble) IsConnected() bool {
return b.connected.Load()
}

func (b *Ble) Scan(param *string) {

if param == nil {
b.scan(nil)
return
}

address, err := bluetooth.ParseUUID(*param)
if err != nil {
b.scan(nil)
} else {
b.scan(&address)
}
}

func (b *Ble) Write(c string, request []byte, withResponse bool) ([]byte, error) {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return nil, err
}
return b.write(char, request, withResponse)
}

func (b *Ble) Read(c string) ([]byte, error) {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return nil, err
}
return b.read(char)
}

func (b *Ble) Subscribe(c string, handler func([]byte)) error {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return err
}
return b.subscribe(char, handler)
}

func (b *Ble) GetServices() ([]bluetooth.DeviceService, error) {
device, err := b.Connect()
device, err := b.connect()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,8 +158,3 @@ func (b *Ble) GetCharacteristics(chars []bluetooth.UUID) ([]bluetooth.DeviceChar
}
return characteristic, nil
}

type Cache struct {
sync.Mutex
pull map[bluetooth.UUID]map[bluetooth.UUID]struct{}
}
42 changes: 2 additions & 40 deletions plugins/ble/ble_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@

package ble

import (
"tinygo.org/x/bluetooth"
)

type Response struct {
Response []byte `json:"response"`
Error string `json:"error"`
}

func GetWriteBind(actor *Actor) func(char string, payload []byte, withResponse bool) Response {
return func(c string, payload []byte, withResponse bool) Response {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return Response{Error: err.Error()}
}

return func(char string, payload []byte, withResponse bool) Response {
response, err := actor.ble.Write(char, payload, withResponse)
if err != nil {
return Response{Error: err.Error()}
Expand All @@ -43,40 +34,11 @@ func GetWriteBind(actor *Actor) func(char string, payload []byte, withResponse b
}

func GetReadBind(actor *Actor) func(char string) Response {
return func(c string) Response {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return Response{Error: err.Error()}
}

return func(char string) Response {
response, err := actor.ble.Read(char)
if err != nil {
return Response{Error: err.Error()}
}
return Response{Response: response}
}
}

func GetSubscribeBind(actor *Actor) func(char string, handler func([]byte)) Response {
return func(c string, handler func([]byte)) Response {
char, err := bluetooth.ParseUUID(c)
if err != nil {
return Response{Error: err.Error()}
}

err = actor.ble.Subscribe(char, handler)
if err != nil {
return Response{Error: err.Error()}
}
return Response{}
}
}

func GetDisconnectBind(actor *Actor) func() Response {
return func() Response {
if err := actor.ble.Disconnect(); err != nil {
return Response{Error: err.Error()}
}
return Response{}
}
}
10 changes: 5 additions & 5 deletions plugins/ble/ble_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"tinygo.org/x/bluetooth"
)

func (b *Ble) Scan(address *bluetooth.UUID) {
func (b *Ble) scan(address *bluetooth.UUID) {
if !b.isScan.CompareAndSwap(false, true) {
return
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (b *Ble) onScanConnect(device bluetooth.Device) {

}

func (b *Ble) Connect() (*bluetooth.Device, error) {
func (b *Ble) connect() (*bluetooth.Device, error) {

if b.debug {
log.Debugf("try connecting to %v", b.address)
Expand Down Expand Up @@ -157,7 +157,7 @@ func (b *Ble) Connect() (*bluetooth.Device, error) {
return &device, nil
}

func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {
func (b *Ble) write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]b
return nil, nil
}

func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
func (b *Ble) read(char bluetooth.UUID) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand All @@ -219,7 +219,7 @@ func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
return nil, nil
}

func (b *Ble) Subscribe(char bluetooth.UUID, handler func([]byte)) error {
func (b *Ble) subscribe(char bluetooth.UUID, handler func([]byte)) error {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions plugins/ble/ble_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"tinygo.org/x/bluetooth"
)

func (b *Ble) Scan(address *bluetooth.UUID) {
func (b *Ble) scan(address *bluetooth.UUID) {
if !b.isScan.CompareAndSwap(false, true) {
return
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (b *Ble) onScanConnect(device bluetooth.Device) {

}

func (b *Ble) Connect() (*bluetooth.Device, error) {
func (b *Ble) connect() (*bluetooth.Device, error) {

if b.debug {
log.Debugf("try connecting to %v", b.address)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (b *Ble) Connect() (*bluetooth.Device, error) {
return &device, nil
}

func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {
func (b *Ble) write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand All @@ -186,7 +186,7 @@ func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]b
return nil, nil
}

func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
func (b *Ble) read(char bluetooth.UUID) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand All @@ -212,7 +212,7 @@ func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
return nil, nil
}

func (b *Ble) Subscribe(char bluetooth.UUID, handler func([]byte)) error {
func (b *Ble) subscribe(char bluetooth.UUID, handler func([]byte)) error {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions plugins/ble/ble_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"tinygo.org/x/bluetooth"
)

func (b *Ble) Scan(address *bluetooth.UUID) {
func (b *Ble) scan(address *bluetooth.UUID) {
if !b.isScan.CompareAndSwap(false, true) {
return
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (b *Ble) onScanConnect(device bluetooth.Device) {

}

func (b *Ble) Connect() (*bluetooth.Device, error) {
func (b *Ble) connect() (*bluetooth.Device, error) {

if b.debug {
log.Debugf("try connecting to %v", b.address)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (b *Ble) Connect() (*bluetooth.Device, error) {
return &device, nil
}

func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {
func (b *Ble) write(char bluetooth.UUID, request []byte, withResponse bool) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (b *Ble) Write(char bluetooth.UUID, request []byte, withResponse bool) ([]b
return nil, nil
}

func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
func (b *Ble) read(char bluetooth.UUID) ([]byte, error) {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand All @@ -223,7 +223,7 @@ func (b *Ble) Read(char bluetooth.UUID) ([]byte, error) {
return nil, nil
}

func (b *Ble) Subscribe(char bluetooth.UUID, handler func([]byte)) error {
func (b *Ble) subscribe(char bluetooth.UUID, handler func([]byte)) error {

characteristics, err := b.GetCharacteristics([]bluetooth.UUID{char})
if err != nil {
Expand Down
Loading

0 comments on commit 097b246

Please sign in to comment.