2021-10-23 03:47:57 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-10-23 05:30:58 +00:00
|
|
|
"reflect"
|
|
|
|
|
2021-10-23 03:47:57 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"go.arsenm.dev/infinitime"
|
|
|
|
"go.arsenm.dev/itd/internal/types"
|
|
|
|
)
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// Address gets the bluetooth address of the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) Address() (string, error) {
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeBtAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Value.(string), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// Version gets the firmware version of the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) Version() (string, error) {
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeFwVersion,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Value.(string), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// BatteryLevel gets the battery level of the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) BatteryLevel() (uint8, error) {
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeBattLevel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint8(res.Value.(float64)), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// WatchBatteryLevel returns a channel which will contain
|
2021-10-23 05:30:58 +00:00
|
|
|
// new battery level values as they update. Do not use after
|
|
|
|
// calling cancellation function
|
|
|
|
func (c *Client) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
2021-10-23 03:47:57 +00:00
|
|
|
c.battLevelCh = make(chan uint8, 2)
|
|
|
|
err := c.requestNoRes(types.Request{
|
|
|
|
Type: types.ReqTypeBattLevel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-23 05:30:58 +00:00
|
|
|
return nil, nil, err
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
2021-10-23 05:30:58 +00:00
|
|
|
cancel := c.cancelFn(types.ReqTypeCancelBattLevel, c.battLevelCh)
|
|
|
|
return c.battLevelCh, cancel, nil
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// HeartRate gets the heart rate from the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) HeartRate() (uint8, error) {
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeHeartRate,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint8(res.Value.(float64)), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// WatchHeartRate returns a channel which will contain
|
2021-10-23 05:30:58 +00:00
|
|
|
// new heart rate values as they update. Do not use after
|
|
|
|
// calling cancellation function
|
|
|
|
func (c *Client) WatchHeartRate() (<-chan uint8, func(), error) {
|
2021-10-23 03:47:57 +00:00
|
|
|
c.heartRateCh = make(chan uint8, 2)
|
|
|
|
err := c.requestNoRes(types.Request{
|
|
|
|
Type: types.ReqTypeWatchHeartRate,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-23 05:30:58 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cancel := c.cancelFn(types.ReqTypeCancelHeartRate, c.heartRateCh)
|
|
|
|
return c.heartRateCh, cancel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelFn generates a cancellation function for the given
|
|
|
|
// request type and channel
|
|
|
|
func (c *Client) cancelFn(reqType int, ch interface{}) func() {
|
|
|
|
return func() {
|
|
|
|
reflectCh := reflect.ValueOf(ch)
|
|
|
|
reflectCh.Close()
|
|
|
|
reflectCh.Set(reflect.Zero(reflectCh.Type()))
|
|
|
|
c.requestNoRes(types.Request{
|
|
|
|
Type: reqType,
|
|
|
|
})
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// StepCount gets the step count from the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) StepCount() (uint32, error) {
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeStepCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(res.Value.(float64)), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// WatchStepCount returns a channel which will contain
|
2021-10-23 05:30:58 +00:00
|
|
|
// new step count values as they update. Do not use after
|
|
|
|
// calling cancellation function
|
|
|
|
func (c *Client) WatchStepCount() (<-chan uint32, func(), error) {
|
2021-10-23 03:47:57 +00:00
|
|
|
c.stepCountCh = make(chan uint32, 2)
|
|
|
|
err := c.requestNoRes(types.Request{
|
|
|
|
Type: types.ReqTypeWatchStepCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-23 05:30:58 +00:00
|
|
|
return nil, nil, err
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
2021-10-23 05:30:58 +00:00
|
|
|
cancel := c.cancelFn(types.ReqTypeCancelStepCount, c.stepCountCh)
|
|
|
|
return c.stepCountCh, cancel, nil
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// Motion gets the motion values from the connected device
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) Motion() (infinitime.MotionValues, error) {
|
|
|
|
out := infinitime.MotionValues{}
|
|
|
|
res, err := c.request(types.Request{
|
|
|
|
Type: types.ReqTypeMotion,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
err = mapstructure.Decode(res.Value, &out)
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// WatchMotion returns a channel which will contain
|
2021-10-23 05:30:58 +00:00
|
|
|
// new motion values as they update. Do not use after
|
|
|
|
// calling cancellation function
|
|
|
|
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, func(), error) {
|
2021-10-23 03:47:57 +00:00
|
|
|
c.motionCh = make(chan infinitime.MotionValues, 2)
|
|
|
|
err := c.requestNoRes(types.Request{
|
|
|
|
Type: types.ReqTypeWatchMotion,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-23 05:30:58 +00:00
|
|
|
return nil, nil, err
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|
2021-10-23 05:30:58 +00:00
|
|
|
cancel := c.cancelFn(types.ReqTypeCancelMotion, c.motionCh)
|
|
|
|
return c.motionCh, cancel, nil
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|