itd/api/watch.go

136 lines
2.0 KiB
Go
Raw Normal View History

2022-04-23 00:12:30 +00:00
package api
import (
2022-05-01 22:22:28 +00:00
"context"
2023-01-03 06:30:17 +00:00
"go.arsenm.dev/itd/internal/rpc"
2022-04-23 00:12:30 +00:00
)
2022-05-01 22:22:28 +00:00
func (c *Client) WatchHeartRate(ctx context.Context) (<-chan uint8, error) {
outCh := make(chan uint8, 2)
2023-01-03 06:30:17 +00:00
wc, err := c.client.WatchHeartRate(ctx, &rpc.Empty{})
2022-04-23 00:12:30 +00:00
if err != nil {
2022-05-01 22:22:28 +00:00
return nil, err
2022-04-23 00:12:30 +00:00
}
2023-01-03 06:30:17 +00:00
go func() {
defer close(outCh)
var err error
var evt *rpc.IntResponse
for {
select {
case <-ctx.Done():
wc.Close()
return
default:
evt, err = wc.Recv()
if err != nil {
return
}
}
outCh <- uint8(evt.Value)
}
}()
2022-05-01 22:22:28 +00:00
return outCh, nil
2022-04-23 00:12:30 +00:00
}
2022-05-01 22:22:28 +00:00
func (c *Client) WatchBatteryLevel(ctx context.Context) (<-chan uint8, error) {
outCh := make(chan uint8, 2)
2023-01-03 06:30:17 +00:00
wc, err := c.client.WatchBatteryLevel(ctx, &rpc.Empty{})
2022-04-23 00:12:30 +00:00
if err != nil {
2022-05-01 22:22:28 +00:00
return nil, err
2022-04-23 00:12:30 +00:00
}
2023-01-03 06:30:17 +00:00
go func() {
defer close(outCh)
var err error
var evt *rpc.IntResponse
for {
select {
case <-ctx.Done():
wc.Close()
return
default:
evt, err = wc.Recv()
if err != nil {
return
}
}
outCh <- uint8(evt.Value)
}
}()
2022-05-01 22:22:28 +00:00
return outCh, nil
2022-04-23 00:12:30 +00:00
}
2022-05-01 22:22:28 +00:00
func (c *Client) WatchStepCount(ctx context.Context) (<-chan uint32, error) {
outCh := make(chan uint32, 2)
2023-01-03 06:30:17 +00:00
wc, err := c.client.WatchStepCount(ctx, &rpc.Empty{})
2022-04-23 00:12:30 +00:00
if err != nil {
2022-05-01 22:22:28 +00:00
return nil, err
2022-04-23 00:12:30 +00:00
}
2023-01-03 06:30:17 +00:00
go func() {
defer close(outCh)
var err error
var evt *rpc.IntResponse
for {
select {
case <-ctx.Done():
wc.Close()
return
default:
evt, err = wc.Recv()
if err != nil {
return
}
}
outCh <- evt.Value
}
}()
2022-05-01 22:22:28 +00:00
return outCh, nil
2022-04-23 00:12:30 +00:00
}
2023-01-03 06:30:17 +00:00
func (c *Client) WatchMotion(ctx context.Context) (<-chan MotionValues, error) {
outCh := make(chan MotionValues, 2)
wc, err := c.client.WatchMotion(ctx, &rpc.Empty{})
2022-04-23 00:12:30 +00:00
if err != nil {
2022-05-01 22:22:28 +00:00
return nil, err
2022-04-23 00:12:30 +00:00
}
2023-01-03 06:30:17 +00:00
go func() {
defer close(outCh)
var err error
var evt *rpc.MotionResponse
for {
select {
case <-ctx.Done():
wc.Close()
return
default:
evt, err = wc.Recv()
if err != nil {
return
}
}
outCh <- MotionValues{int16(evt.X), int16(evt.Y), int16(evt.Z)}
}
}()
2022-05-01 22:22:28 +00:00
return outCh, nil
2022-04-23 00:12:30 +00:00
}