mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
go fmt the code
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
25c861571c
commit
da9054d197
@ -2,8 +2,8 @@ package ble
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"github.com/currantlabs/gatt"
|
"github.com/currantlabs/gatt"
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -18,13 +18,13 @@ var _ gobot.Adaptor = (*BLEAdaptor)(nil)
|
|||||||
|
|
||||||
// Represents a Connection to a BLE Peripheral
|
// Represents a Connection to a BLE Peripheral
|
||||||
type BLEAdaptor struct {
|
type BLEAdaptor struct {
|
||||||
name string
|
name string
|
||||||
uuid string
|
uuid string
|
||||||
device gatt.Device
|
device gatt.Device
|
||||||
peripheral gatt.Peripheral
|
peripheral gatt.Peripheral
|
||||||
services map[string]*BLEService
|
services map[string]*BLEService
|
||||||
connected bool
|
connected bool
|
||||||
ready chan struct{}
|
ready chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBLEAdaptor returns a new BLEAdaptor given a name and uuid
|
// NewBLEAdaptor returns a new BLEAdaptor given a name and uuid
|
||||||
@ -33,8 +33,8 @@ func NewBLEAdaptor(name string, uuid string) *BLEAdaptor {
|
|||||||
name: name,
|
name: name,
|
||||||
uuid: uuid,
|
uuid: uuid,
|
||||||
connected: false,
|
connected: false,
|
||||||
ready: make(chan struct{}),
|
ready: make(chan struct{}),
|
||||||
services: make(map[string]*BLEService),
|
services: make(map[string]*BLEService),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,10 +99,10 @@ func (b *BLEAdaptor) ReadCharacteristic(sUUID string, cUUID string) (data []byte
|
|||||||
val, err := b.peripheral.ReadCharacteristic(characteristic)
|
val, err := b.peripheral.ReadCharacteristic(characteristic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BLEAdaptor) StateChangeHandler(d gatt.Device, s gatt.State) {
|
func (b *BLEAdaptor) StateChangeHandler(d gatt.Device, s gatt.State) {
|
||||||
@ -131,33 +131,33 @@ func (b *BLEAdaptor) DiscoveryHandler(p gatt.Peripheral, a *gatt.Advertisement,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BLEAdaptor) ConnectHandler(p gatt.Peripheral, err error) {
|
func (b *BLEAdaptor) ConnectHandler(p gatt.Peripheral, err error) {
|
||||||
fmt.Printf("\nConnected Peripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
|
fmt.Printf("\nConnected Peripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
|
||||||
|
|
||||||
b.peripheral = p
|
b.peripheral = p
|
||||||
|
|
||||||
if err := p.SetMTU(500); err != nil {
|
if err := p.SetMTU(500); err != nil {
|
||||||
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ss, err := p.DiscoverServices(nil)
|
ss, err := p.DiscoverServices(nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to discover services, err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range ss {
|
||||||
|
b.services[s.UUID().String()] = NewBLEService(s.UUID().String(), s)
|
||||||
|
|
||||||
|
cs, err := p.DiscoverCharacteristics(nil, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to discover services, err: %s\n", err)
|
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range ss {
|
for _, c := range cs {
|
||||||
b.services[s.UUID().String()] = NewBLEService(s.UUID().String(), s)
|
b.services[s.UUID().String()].characteristics[c.UUID().String()] = c
|
||||||
|
|
||||||
cs, err := p.DiscoverCharacteristics(nil, s)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cs {
|
|
||||||
b.services[s.UUID().String()].characteristics[c.UUID().String()] = c
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
b.connected = true
|
b.connected = true
|
||||||
close(b.ready)
|
close(b.ready)
|
||||||
@ -170,16 +170,16 @@ func (b *BLEAdaptor) DisconnectHandler(p gatt.Peripheral, err error) {
|
|||||||
|
|
||||||
// Represents a BLE Peripheral's Service
|
// Represents a BLE Peripheral's Service
|
||||||
type BLEService struct {
|
type BLEService struct {
|
||||||
uuid string
|
uuid string
|
||||||
service *gatt.Service
|
service *gatt.Service
|
||||||
characteristics map[string]*gatt.Characteristic
|
characteristics map[string]*gatt.Characteristic
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBLEAdaptor returns a new BLEService given a uuid
|
// NewBLEAdaptor returns a new BLEService given a uuid
|
||||||
func NewBLEService(sUuid string, service *gatt.Service) *BLEService {
|
func NewBLEService(sUuid string, service *gatt.Service) *BLEService {
|
||||||
return &BLEService{
|
return &BLEService{
|
||||||
uuid: sUuid,
|
uuid: sUuid,
|
||||||
service: service,
|
service: service,
|
||||||
characteristics: make(map[string]*gatt.Characteristic),
|
characteristics: make(map[string]*gatt.Characteristic),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,10 +42,8 @@ func (b *BLEDeviceInformationDriver) Start() (errs []error) {
|
|||||||
func (b *BLEDeviceInformationDriver) Halt() (errs []error) { return }
|
func (b *BLEDeviceInformationDriver) Halt() (errs []error) { return }
|
||||||
|
|
||||||
func (b *BLEDeviceInformationDriver) GetModelNumber() (model string) {
|
func (b *BLEDeviceInformationDriver) GetModelNumber() (model string) {
|
||||||
var l string
|
|
||||||
c, _ := b.adaptor().ReadCharacteristic("180a", "2a24")
|
c, _ := b.adaptor().ReadCharacteristic("180a", "2a24")
|
||||||
buf := bytes.NewBuffer(c)
|
buf := bytes.NewBuffer(c)
|
||||||
val, _ := buf.ReadByte()
|
val := buf.String()
|
||||||
l = string(val)
|
return val
|
||||||
return l
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user