Ensure that done signals for Watch functions are handled properly and restart notifications if they stop unexpectedly
This commit is contained in:
@@ -473,18 +473,20 @@ func (i *Device) WatchHeartRate() (<-chan uint8, func(), error) {
|
||||
cancel, done := cancelFunc()
|
||||
go func() {
|
||||
// For every event
|
||||
for event := range ch {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
close(out)
|
||||
close(done)
|
||||
i.heartRateChar.StopNotify()
|
||||
return
|
||||
default:
|
||||
case event := <-ch:
|
||||
// If value changed
|
||||
if event.Name == "Value" {
|
||||
// Send heart rate to channel
|
||||
out <- uint8(event.Value.([]byte)[1])
|
||||
} else if event.Name == "Notifying" && !event.Value.(bool) {
|
||||
i.heartRateChar.StartNotify()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -515,18 +517,20 @@ func (i *Device) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
||||
cancel, done := cancelFunc()
|
||||
go func() {
|
||||
// For every event
|
||||
for event := range ch {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
close(out)
|
||||
close(done)
|
||||
i.battLevelChar.StopNotify()
|
||||
return
|
||||
default:
|
||||
case event := <-ch:
|
||||
// If value changed
|
||||
if event.Name == "Value" {
|
||||
// Send heart rate to channel
|
||||
out <- uint8(event.Value.([]byte)[0])
|
||||
} else if event.Name == "Notifying" && !event.Value.(bool) {
|
||||
i.battLevelChar.StartNotify()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -557,18 +561,20 @@ func (i *Device) WatchStepCount() (<-chan uint32, func(), error) {
|
||||
cancel, done := cancelFunc()
|
||||
go func() {
|
||||
// For every event
|
||||
for event := range ch {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
close(out)
|
||||
close(done)
|
||||
i.stepCountChar.StopNotify()
|
||||
return
|
||||
default:
|
||||
case event := <-ch:
|
||||
// If value changed
|
||||
if event.Name == "Value" {
|
||||
// Send step count to channel
|
||||
out <- binary.LittleEndian.Uint32(event.Value.([]byte))
|
||||
} else if event.Name == "Notifying" && !event.Value.(bool) {
|
||||
i.stepCountChar.StartNotify()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -599,20 +605,22 @@ func (i *Device) WatchMotion() (<-chan MotionValues, func(), error) {
|
||||
cancel, done := cancelFunc()
|
||||
go func() {
|
||||
// For every event
|
||||
for event := range ch {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
close(out)
|
||||
close(done)
|
||||
i.motionValChar.StopNotify()
|
||||
return
|
||||
default:
|
||||
case event := <-ch:
|
||||
// If value changed
|
||||
if event.Name == "Value" {
|
||||
// Read binary into MotionValues struct
|
||||
binary.Read(bytes.NewReader(event.Value.([]byte)), binary.LittleEndian, &motionVals)
|
||||
// Send step count to channel
|
||||
out <- motionVals
|
||||
} else if event.Name == "Notifying" && !event.Value.(bool) {
|
||||
i.motionValChar.StartNotify()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user