forked from Elara6331/infinitime
Add cancel functions to battery level and heart rate
This commit is contained in:
parent
4fa0ce1801
commit
651270051e
@ -383,70 +383,88 @@ func (i *Device) HeartRate() (uint8, error) {
|
|||||||
return uint8(heartRate[1]), nil
|
return uint8(heartRate[1]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Device) WatchHeartRate() (<-chan uint8, error) {
|
func (i *Device) WatchHeartRate() (<-chan uint8, func(), error) {
|
||||||
if !i.device.Properties.Connected {
|
if !i.device.Properties.Connected {
|
||||||
return make(<-chan uint8), nil
|
return make(<-chan uint8), nil, nil
|
||||||
}
|
}
|
||||||
// Start notifications on heart rate characteristic
|
// Start notifications on heart rate characteristic
|
||||||
err := i.heartRateChar.StartNotify()
|
err := i.heartRateChar.StartNotify()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
// Watch characteristics of heart rate characteristic
|
// Watch characteristics of heart rate characteristic
|
||||||
ch, err := i.heartRateChar.WatchProperties()
|
ch, err := i.heartRateChar.WatchProperties()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
out := make(chan uint8, 2)
|
out := make(chan uint8, 2)
|
||||||
currentHeartRate, err := i.HeartRate()
|
currentHeartRate, err := i.HeartRate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
out <- currentHeartRate
|
out <- currentHeartRate
|
||||||
|
cancel, done := cancelFunc()
|
||||||
go func() {
|
go func() {
|
||||||
// For every event
|
// For every event
|
||||||
for event := range ch {
|
for event := range ch {
|
||||||
// If value changed
|
select {
|
||||||
if event.Name == "Value" {
|
case <-done:
|
||||||
// Send heart rate to channel
|
close(out)
|
||||||
out <- uint8(event.Value.([]byte)[1])
|
close(done)
|
||||||
|
i.heartRateChar.StopNotify()
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// If value changed
|
||||||
|
if event.Name == "Value" {
|
||||||
|
// Send heart rate to channel
|
||||||
|
out <- uint8(event.Value.([]byte)[1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return out, nil
|
return out, cancel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Device) WatchBatteryLevel() (<-chan uint8, error) {
|
func (i *Device) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
||||||
if !i.device.Properties.Connected {
|
if !i.device.Properties.Connected {
|
||||||
return make(<-chan uint8), nil
|
return make(<-chan uint8), nil, nil
|
||||||
}
|
}
|
||||||
// Start notifications on heart rate characteristic
|
// Start notifications on heart rate characteristic
|
||||||
err := i.battLevelChar.StartNotify()
|
err := i.battLevelChar.StartNotify()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
// Watch characteristics of heart rate characteristic
|
// Watch characteristics of heart rate characteristic
|
||||||
ch, err := i.battLevelChar.WatchProperties()
|
ch, err := i.battLevelChar.WatchProperties()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
out := make(chan uint8, 2)
|
out := make(chan uint8, 2)
|
||||||
currentBattLevel, err := i.BatteryLevel()
|
currentBattLevel, err := i.BatteryLevel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
out <- currentBattLevel
|
out <- currentBattLevel
|
||||||
|
cancel, done := cancelFunc()
|
||||||
go func() {
|
go func() {
|
||||||
// For every event
|
// For every event
|
||||||
for event := range ch {
|
for event := range ch {
|
||||||
// If value changed
|
select {
|
||||||
if event.Name == "Value" {
|
case <-done:
|
||||||
// Send heart rate to channel
|
close(out)
|
||||||
out <- uint8(event.Value.([]byte)[0])
|
close(done)
|
||||||
|
i.battLevelChar.StopNotify()
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// If value changed
|
||||||
|
if event.Name == "Value" {
|
||||||
|
// Send heart rate to channel
|
||||||
|
out <- uint8(event.Value.([]byte)[0])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return out, nil
|
return out, cancel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Device) WatchStepCount() (<-chan uint32, func(), error) {
|
func (i *Device) WatchStepCount() (<-chan uint32, func(), error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user