Read autoconnect ID out of configuration file, save config file
on any change to autoconnect ID.
This commit is contained in:
parent
45863ab212
commit
a123228e30
29
main.go
29
main.go
|
@ -25,12 +25,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type conf struct {
|
type conf struct {
|
||||||
|
Autoconnect string
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config conf
|
var Config conf
|
||||||
|
var conffile string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conffile := path.Join(confDir, "config.yml")
|
conffile = path.Join(confDir, "config.yml")
|
||||||
if _, err := os.Stat(conffile); os.IsNotExist(err) {
|
if _, err := os.Stat(conffile); os.IsNotExist(err) {
|
||||||
fd, err := os.Create(conffile)
|
fd, err := os.Create(conffile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,6 +52,14 @@ func main() {
|
||||||
app.Main()
|
app.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func saveConfig() {
|
||||||
|
confbytes, err := yaml.Marshal(&Config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Cannot encode configuration: ", err)
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(conffile, confbytes, 0700)
|
||||||
|
}
|
||||||
|
|
||||||
func hrDecode(x []byte) int {
|
func hrDecode(x []byte) int {
|
||||||
flags := x[0]
|
flags := x[0]
|
||||||
if flags&0x80 != 0 { // uint16 format
|
if flags&0x80 != 0 { // uint16 format
|
||||||
|
@ -84,9 +94,6 @@ func eventloop() {
|
||||||
periphs := make([]ble.Peripheral, 0)
|
periphs := make([]ble.Peripheral, 0)
|
||||||
btns := make([]*widget.Button, 0)
|
btns := make([]*widget.Button, 0)
|
||||||
|
|
||||||
var autoconnectID string // should load from config file
|
|
||||||
//autoconnectID = "93D3A64F-1664-497D-8B01-77951DB8E0F3"
|
|
||||||
|
|
||||||
var page, offpage, scanpage, connpage, hrpage func()
|
var page, offpage, scanpage, connpage, hrpage func()
|
||||||
|
|
||||||
f := &layout.Flex{Axis: layout.Vertical}
|
f := &layout.Flex{Axis: layout.Vertical}
|
||||||
|
@ -166,10 +173,10 @@ func eventloop() {
|
||||||
page = offpage
|
page = offpage
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if autoconnectID != "" {
|
if Config.Autoconnect != "" && b.Connect(ble.Peripheral{Identifier: Config.Autoconnect}) {
|
||||||
b.Connect(ble.Peripheral{Identifier: autoconnectID})
|
|
||||||
page = connpage
|
page = connpage
|
||||||
} else {
|
} else {
|
||||||
|
periphs = periphs[:0]
|
||||||
page = scanpage
|
page = scanpage
|
||||||
b.Scan()
|
b.Scan()
|
||||||
}
|
}
|
||||||
|
@ -178,20 +185,24 @@ func eventloop() {
|
||||||
fmt.Printf("found %s (%s)\n", e.Peripheral.Name, e.Peripheral.Identifier)
|
fmt.Printf("found %s (%s)\n", e.Peripheral.Name, e.Peripheral.Identifier)
|
||||||
periphs = append(periphs, e.Peripheral)
|
periphs = append(periphs, e.Peripheral)
|
||||||
btns = append(btns, &widget.Button{})
|
btns = append(btns, &widget.Button{})
|
||||||
if e.Peripheral.Identifier == autoconnectID {
|
if e.Peripheral.Identifier == Config.Autoconnect && b.Connect(e.Peripheral) {
|
||||||
b.Connect(e.Peripheral)
|
page = connpage
|
||||||
}
|
}
|
||||||
case ble.ConnectEvent:
|
case ble.ConnectEvent:
|
||||||
fmt.Printf("Connect event\n")
|
fmt.Printf("Connect event\n")
|
||||||
state = "connected"
|
state = "connected"
|
||||||
periph = e.Peripheral
|
periph = e.Peripheral
|
||||||
|
Config.Autoconnect = periph.Identifier
|
||||||
|
saveConfig()
|
||||||
e.Peripheral.DiscoverServices()
|
e.Peripheral.DiscoverServices()
|
||||||
page = hrpage
|
page = hrpage
|
||||||
case ble.ConnectTimeoutEvent:
|
case ble.ConnectTimeoutEvent:
|
||||||
fmt.Printf("Connect timeout\n")
|
fmt.Printf("Connect timeout\n")
|
||||||
state = "timeout"
|
state = "timeout"
|
||||||
autoconnectID = ""
|
periphs = periphs[:0]
|
||||||
page = scanpage
|
page = scanpage
|
||||||
|
Config.Autoconnect = ""
|
||||||
|
saveConfig()
|
||||||
b.Scan()
|
b.Scan()
|
||||||
case ble.DiscoverServiceEvent:
|
case ble.DiscoverServiceEvent:
|
||||||
fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
|
fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user