Add cancellation to watchable values

This commit is contained in:
Elara 2021-10-22 22:14:01 -07:00
parent 68bac8859f
commit ff8ce1b2a5
4 changed files with 85 additions and 24 deletions

2
go.mod
View File

@ -24,7 +24,7 @@ require (
github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/yuin/goldmark v1.4.1 // indirect github.com/yuin/goldmark v1.4.1 // indirect
go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b go.arsenm.dev/infinitime v0.0.0-20211023042633-53aa6f8a0c72
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect

4
go.sum
View File

@ -366,8 +366,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.3.8/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.3.8/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM= github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b h1:2VitKPwSYSWXmL5BH88nfTPLSIYPCt4yubpEJHhcQBc= go.arsenm.dev/infinitime v0.0.0-20211023042633-53aa6f8a0c72 h1:e8kOuL6Jj8ZjJzkGwJ3xqpGG9EhUzfvZk9AlSsm3X1U=
go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q= go.arsenm.dev/infinitime v0.0.0-20211023042633-53aa6f8a0c72/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q=
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ= go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

View File

@ -9,11 +9,15 @@ const (
ReqTypeNotify ReqTypeNotify
ReqTypeSetTime ReqTypeSetTime
ReqTypeWatchHeartRate ReqTypeWatchHeartRate
ReqTypeCancelHeartRate
ReqTypeWatchBattLevel ReqTypeWatchBattLevel
ReqTypeCancelBattLevel
ReqTypeMotion ReqTypeMotion
ReqTypeWatchMotion ReqTypeWatchMotion
ReqTypeCancelMotion
ReqTypeStepCount ReqTypeStepCount
ReqTypeWatchStepCount ReqTypeWatchStepCount
ReqTypeCancelStepCount
) )
const ( const (
@ -25,11 +29,15 @@ const (
ResTypeNotify ResTypeNotify
ResTypeSetTime ResTypeSetTime
ResTypeWatchHeartRate ResTypeWatchHeartRate
ResTypeCancelHeartRate
ResTypeWatchBattLevel ResTypeWatchBattLevel
ResTypeCancelBattLevel
ResTypeMotion ResTypeMotion
ResTypeWatchMotion ResTypeWatchMotion
ResTypeCancelMotion
ResTypeStepCount ResTypeStepCount
ResTypeWatchStepCount ResTypeWatchStepCount
ResTypeCancelStepCount
) )
const ( const (
@ -43,7 +51,7 @@ type ReqDataFwUpgrade struct {
} }
type Response struct { type Response struct {
Type int Type int `json:"type"`
Value interface{} `json:"value,omitempty"` Value interface{} `json:"value,omitempty"`
Message string `json:"msg,omitempty"` Message string `json:"msg,omitempty"`
Error bool `json:"error"` Error bool `json:"error"`

View File

@ -81,6 +81,11 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
return return
} }
heartRateDone := make(chan struct{})
battLevelDone := make(chan struct{})
stepCountDone := make(chan struct{})
motionDone := make(chan struct{})
// Create new scanner on connection // Create new scanner on connection
scanner := bufio.NewScanner(conn) scanner := bufio.NewScanner(conn)
for scanner.Scan() { for scanner.Scan() {
@ -106,19 +111,31 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
Value: heartRate, Value: heartRate,
}) })
case types.ReqTypeWatchHeartRate: case types.ReqTypeWatchHeartRate:
heartRateCh, err := dev.WatchHeartRate() heartRateCh, cancel, err := dev.WatchHeartRate()
if err != nil { if err != nil {
connErr(conn, err, "Error getting heart rate channel") connErr(conn, err, "Error getting heart rate channel")
break break
} }
go func() { go func() {
// For every heart rate value
for heartRate := range heartRateCh { for heartRate := range heartRateCh {
json.NewEncoder(conn).Encode(types.Response{ select {
Type: types.ResTypeWatchHeartRate, case <-heartRateDone:
Value: heartRate, // Stop notifications if done signal received
}) cancel()
return
default:
// Encode response to connection if no done signal received
json.NewEncoder(conn).Encode(types.Response{
Type: types.ResTypeWatchHeartRate,
Value: heartRate,
})
}
} }
}() }()
case types.ReqTypeCancelHeartRate:
// Stop heart rate notifications
heartRateDone <- struct{}{}
case types.ReqTypeBattLevel: case types.ReqTypeBattLevel:
// Get battery level from watch // Get battery level from watch
battLevel, err := dev.BatteryLevel() battLevel, err := dev.BatteryLevel()
@ -132,19 +149,31 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
Value: battLevel, Value: battLevel,
}) })
case types.ReqTypeWatchBattLevel: case types.ReqTypeWatchBattLevel:
battLevelCh, err := dev.WatchBatteryLevel() battLevelCh, cancel, err := dev.WatchBatteryLevel()
if err != nil { if err != nil {
connErr(conn, err, "Error getting battery level channel") connErr(conn, err, "Error getting battery level channel")
break break
} }
go func() { go func() {
// For every battery level value
for battLevel := range battLevelCh { for battLevel := range battLevelCh {
json.NewEncoder(conn).Encode(types.Response{ select {
Type: types.ResTypeWatchBattLevel, case <-battLevelDone:
Value: battLevel, // Stop notifications if done signal received
}) cancel()
return
default:
// Encode response to connection if no done signal received
json.NewEncoder(conn).Encode(types.Response{
Type: types.ResTypeWatchBattLevel,
Value: battLevel,
})
}
} }
}() }()
case types.ReqTypeCancelBattLevel:
// Stop battery level notifications
battLevelDone <- struct{}{}
case types.ReqTypeMotion: case types.ReqTypeMotion:
// Get battery level from watch // Get battery level from watch
motionVals, err := dev.Motion() motionVals, err := dev.Motion()
@ -158,19 +187,31 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
Value: motionVals, Value: motionVals,
}) })
case types.ReqTypeWatchMotion: case types.ReqTypeWatchMotion:
motionValCh, _, err := dev.WatchMotion() motionValCh, cancel, err := dev.WatchMotion()
if err != nil { if err != nil {
connErr(conn, err, "Error getting heart rate channel") connErr(conn, err, "Error getting heart rate channel")
break break
} }
go func() { go func() {
// For every motion event
for motionVals := range motionValCh { for motionVals := range motionValCh {
json.NewEncoder(conn).Encode(types.Response{ select {
Type: types.ResTypeWatchMotion, case <-motionDone:
Value: motionVals, // Stop notifications if done signal received
}) cancel()
return
default:
// Encode response to connection if no done signal received
json.NewEncoder(conn).Encode(types.Response{
Type: types.ResTypeWatchMotion,
Value: motionVals,
})
}
} }
}() }()
case types.ReqTypeCancelMotion:
// Stop motion notifications
motionDone <- struct{}{}
case types.ReqTypeStepCount: case types.ReqTypeStepCount:
// Get battery level from watch // Get battery level from watch
stepCount, err := dev.StepCount() stepCount, err := dev.StepCount()
@ -184,19 +225,31 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
Value: stepCount, Value: stepCount,
}) })
case types.ReqTypeWatchStepCount: case types.ReqTypeWatchStepCount:
stepCountCh, _, err := dev.WatchStepCount() stepCountCh, cancel, err := dev.WatchStepCount()
if err != nil { if err != nil {
connErr(conn, err, "Error getting heart rate channel") connErr(conn, err, "Error getting heart rate channel")
break break
} }
go func() { go func() {
// For every step count value
for stepCount := range stepCountCh { for stepCount := range stepCountCh {
json.NewEncoder(conn).Encode(types.Response{ select {
Type: types.ResTypeWatchStepCount, case <-stepCountDone:
Value: stepCount, // Stop notifications if done signal received
}) cancel()
return
default:
// Encode response to connection if no done signal received
json.NewEncoder(conn).Encode(types.Response{
Type: types.ResTypeWatchStepCount,
Value: stepCount,
})
}
} }
}() }()
case types.ReqTypeCancelStepCount:
// Stop step count notifications
stepCountDone <- struct{}{}
case types.ReqTypeFwVersion: case types.ReqTypeFwVersion:
// Get firmware version from watch // Get firmware version from watch
version, err := dev.Version() version, err := dev.Version()