forked from Elara6331/itd
Add doc comments to api package
This commit is contained in:
parent
80a5867d6b
commit
d7057e3f9c
@ -11,8 +11,10 @@ import (
|
|||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Default socket address
|
||||||
const DefaultAddr = "/tmp/itd/socket"
|
const DefaultAddr = "/tmp/itd/socket"
|
||||||
|
|
||||||
|
// Client is the socket API client
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
respCh chan types.Response
|
respCh chan types.Response
|
||||||
@ -23,6 +25,7 @@ type Client struct {
|
|||||||
dfuProgressCh chan DFUProgress
|
dfuProgressCh chan DFUProgress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new client and sets it up
|
||||||
func New(addr string) (*Client, error) {
|
func New(addr string) (*Client, error) {
|
||||||
conn, err := net.Dial("unix", addr)
|
conn, err := net.Dial("unix", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -48,6 +51,16 @@ func New(addr string) (*Client, error) {
|
|||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
err := c.conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
close(c.respCh)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// request sends a request to itd and waits for and returns the response
|
||||||
func (c *Client) request(req types.Request) (types.Response, error) {
|
func (c *Client) request(req types.Request) (types.Response, error) {
|
||||||
// Encode request into connection
|
// Encode request into connection
|
||||||
err := json.NewEncoder(c.conn).Encode(req)
|
err := json.NewEncoder(c.conn).Encode(req)
|
||||||
@ -64,6 +77,7 @@ func (c *Client) request(req types.Request) (types.Response, error) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// requestNoRes sends a request to itd and does not wait for the response
|
||||||
func (c *Client) requestNoRes(req types.Request) error {
|
func (c *Client) requestNoRes(req types.Request) error {
|
||||||
// Encode request into connection
|
// Encode request into connection
|
||||||
err := json.NewEncoder(c.conn).Encode(req)
|
err := json.NewEncoder(c.conn).Encode(req)
|
||||||
@ -73,6 +87,7 @@ func (c *Client) requestNoRes(req types.Request) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleResp handles the received response as needed
|
||||||
func (c *Client) handleResp(res types.Response) error {
|
func (c *Client) handleResp(res types.Response) error {
|
||||||
switch res.Type {
|
switch res.Type {
|
||||||
case types.ResTypeWatchHeartRate:
|
case types.ResTypeWatchHeartRate:
|
||||||
|
14
api/info.go
14
api/info.go
@ -6,6 +6,7 @@ import (
|
|||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Address gets the bluetooth address of the connected device
|
||||||
func (c *Client) Address() (string, error) {
|
func (c *Client) Address() (string, error) {
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeBtAddress,
|
Type: types.ReqTypeBtAddress,
|
||||||
@ -17,6 +18,7 @@ func (c *Client) Address() (string, error) {
|
|||||||
return res.Value.(string), nil
|
return res.Value.(string), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version gets the firmware version of the connected device
|
||||||
func (c *Client) Version() (string, error) {
|
func (c *Client) Version() (string, error) {
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeFwVersion,
|
Type: types.ReqTypeFwVersion,
|
||||||
@ -28,6 +30,7 @@ func (c *Client) Version() (string, error) {
|
|||||||
return res.Value.(string), nil
|
return res.Value.(string), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BatteryLevel gets the battery level of the connected device
|
||||||
func (c *Client) BatteryLevel() (uint8, error) {
|
func (c *Client) BatteryLevel() (uint8, error) {
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeBattLevel,
|
Type: types.ReqTypeBattLevel,
|
||||||
@ -39,6 +42,8 @@ func (c *Client) BatteryLevel() (uint8, error) {
|
|||||||
return uint8(res.Value.(float64)), nil
|
return uint8(res.Value.(float64)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WatchBatteryLevel returns a channel which will contain
|
||||||
|
// new battery level values as they update
|
||||||
func (c *Client) WatchBatteryLevel() (<-chan uint8, error) {
|
func (c *Client) WatchBatteryLevel() (<-chan uint8, error) {
|
||||||
c.battLevelCh = make(chan uint8, 2)
|
c.battLevelCh = make(chan uint8, 2)
|
||||||
err := c.requestNoRes(types.Request{
|
err := c.requestNoRes(types.Request{
|
||||||
@ -50,6 +55,7 @@ func (c *Client) WatchBatteryLevel() (<-chan uint8, error) {
|
|||||||
return c.battLevelCh, nil
|
return c.battLevelCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HeartRate gets the heart rate from the connected device
|
||||||
func (c *Client) HeartRate() (uint8, error) {
|
func (c *Client) HeartRate() (uint8, error) {
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeHeartRate,
|
Type: types.ReqTypeHeartRate,
|
||||||
@ -61,6 +67,8 @@ func (c *Client) HeartRate() (uint8, error) {
|
|||||||
return uint8(res.Value.(float64)), nil
|
return uint8(res.Value.(float64)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WatchHeartRate returns a channel which will contain
|
||||||
|
// new heart rate values as they update
|
||||||
func (c *Client) WatchHeartRate() (<-chan uint8, error) {
|
func (c *Client) WatchHeartRate() (<-chan uint8, error) {
|
||||||
c.heartRateCh = make(chan uint8, 2)
|
c.heartRateCh = make(chan uint8, 2)
|
||||||
err := c.requestNoRes(types.Request{
|
err := c.requestNoRes(types.Request{
|
||||||
@ -72,6 +80,7 @@ func (c *Client) WatchHeartRate() (<-chan uint8, error) {
|
|||||||
return c.heartRateCh, nil
|
return c.heartRateCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepCount gets the step count from the connected device
|
||||||
func (c *Client) StepCount() (uint32, error) {
|
func (c *Client) StepCount() (uint32, error) {
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeStepCount,
|
Type: types.ReqTypeStepCount,
|
||||||
@ -83,6 +92,8 @@ func (c *Client) StepCount() (uint32, error) {
|
|||||||
return uint32(res.Value.(float64)), nil
|
return uint32(res.Value.(float64)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WatchStepCount returns a channel which will contain
|
||||||
|
// new step count values as they update
|
||||||
func (c *Client) WatchStepCount() (<-chan uint32, error) {
|
func (c *Client) WatchStepCount() (<-chan uint32, error) {
|
||||||
c.stepCountCh = make(chan uint32, 2)
|
c.stepCountCh = make(chan uint32, 2)
|
||||||
err := c.requestNoRes(types.Request{
|
err := c.requestNoRes(types.Request{
|
||||||
@ -94,6 +105,7 @@ func (c *Client) WatchStepCount() (<-chan uint32, error) {
|
|||||||
return c.stepCountCh, nil
|
return c.stepCountCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Motion gets the motion values from the connected device
|
||||||
func (c *Client) Motion() (infinitime.MotionValues, error) {
|
func (c *Client) Motion() (infinitime.MotionValues, error) {
|
||||||
out := infinitime.MotionValues{}
|
out := infinitime.MotionValues{}
|
||||||
res, err := c.request(types.Request{
|
res, err := c.request(types.Request{
|
||||||
@ -109,6 +121,8 @@ func (c *Client) Motion() (infinitime.MotionValues, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WatchMotion returns a channel which will contain
|
||||||
|
// new motion values as they update
|
||||||
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, error) {
|
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, error) {
|
||||||
c.motionCh = make(chan infinitime.MotionValues, 2)
|
c.motionCh = make(chan infinitime.MotionValues, 2)
|
||||||
err := c.requestNoRes(types.Request{
|
err := c.requestNoRes(types.Request{
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetTime sets the given time on the connected device
|
||||||
func (c *Client) SetTime(t time.Time) error {
|
func (c *Client) SetTime(t time.Time) error {
|
||||||
_, err := c.request(types.Request{
|
_, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeSetTime,
|
Type: types.ReqTypeSetTime,
|
||||||
@ -17,6 +18,9 @@ func (c *Client) SetTime(t time.Time) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTimeNow sets the time on the connected device to
|
||||||
|
// the current time. This is more accurate than
|
||||||
|
// SetTime(time.Now()) due to RFC3339 formatting
|
||||||
func (c *Client) SetTimeNow() error {
|
func (c *Client) SetTimeNow() error {
|
||||||
_, err := c.request(types.Request{
|
_, err := c.request(types.Request{
|
||||||
Type: types.ReqTypeSetTime,
|
Type: types.ReqTypeSetTime,
|
||||||
|
@ -6,15 +6,19 @@ import (
|
|||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DFUProgress stores the progress of a DFU upfate
|
||||||
type DFUProgress types.DFUProgress
|
type DFUProgress types.DFUProgress
|
||||||
|
|
||||||
|
// UpgradeType indicates the type of upgrade to be performed
|
||||||
type UpgradeType uint8
|
type UpgradeType uint8
|
||||||
|
|
||||||
|
// Type of DFU upgrade
|
||||||
const (
|
const (
|
||||||
UpgradeTypeArchive UpgradeType = iota
|
UpgradeTypeArchive UpgradeType = iota
|
||||||
UpgradeTypeFiles
|
UpgradeTypeFiles
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FirmwareUpgrade initiates a DFU update and returns the progress channel
|
||||||
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (<-chan DFUProgress, error) {
|
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (<-chan DFUProgress, error) {
|
||||||
err := json.NewEncoder(c.conn).Encode(types.Request{
|
err := json.NewEncoder(c.conn).Encode(types.Request{
|
||||||
Type: types.ReqTypeFwUpgrade,
|
Type: types.ReqTypeFwUpgrade,
|
||||||
|
Loading…
Reference in New Issue
Block a user