itd/api/client.go

154 lines
3.2 KiB
Go
Raw Permalink Normal View History

package api
import (
"bufio"
"encoding/json"
"errors"
"net"
"github.com/mitchellh/mapstructure"
"go.arsenm.dev/infinitime"
"go.arsenm.dev/itd/internal/types"
)
2021-10-23 04:01:18 +00:00
// Default socket address
const DefaultAddr = "/tmp/itd/socket"
2021-10-23 04:01:18 +00:00
// Client is the socket API client
type Client struct {
2021-12-13 01:08:48 +00:00
conn net.Conn
respCh chan types.Response
heartRateCh chan types.Response
battLevelCh chan types.Response
stepCountCh chan types.Response
motionCh chan types.Response
dfuProgressCh chan types.Response
readProgressCh chan types.FSTransferProgress
writeProgressCh chan types.FSTransferProgress
}
2021-10-23 04:01:18 +00:00
// New creates a new client and sets it up
func New(addr string) (*Client, error) {
conn, err := net.Dial("unix", addr)
if err != nil {
return nil, err
}
out := &Client{
conn: conn,
respCh: make(chan types.Response, 5),
}
go func() {
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
var res types.Response
err = json.Unmarshal(scanner.Bytes(), &res)
if err != nil {
continue
}
out.handleResp(res)
}
}()
return out, err
}
2021-10-23 04:01:18 +00:00
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) {
// Encode request into connection
err := json.NewEncoder(c.conn).Encode(req)
if err != nil {
return types.Response{}, err
}
res := <-c.respCh
if res.Error {
return res, errors.New(res.Message)
}
return res, nil
}
2021-10-23 04:01:18 +00:00
// requestNoRes sends a request to itd and does not wait for the response
func (c *Client) requestNoRes(req types.Request) error {
// Encode request into connection
err := json.NewEncoder(c.conn).Encode(req)
if err != nil {
return err
}
return nil
}
2021-10-23 04:01:18 +00:00
// handleResp handles the received response as needed
func (c *Client) handleResp(res types.Response) error {
switch res.Type {
2021-10-24 07:49:48 +00:00
case types.ReqTypeWatchHeartRate:
c.heartRateCh <- res
2021-10-24 07:49:48 +00:00
case types.ReqTypeWatchBattLevel:
c.battLevelCh <- res
2021-10-24 07:49:48 +00:00
case types.ReqTypeWatchStepCount:
c.stepCountCh <- res
2021-10-24 07:49:48 +00:00
case types.ReqTypeWatchMotion:
c.motionCh <- res
2021-10-24 07:49:48 +00:00
case types.ReqTypeFwUpgrade:
c.dfuProgressCh <- res
2021-12-13 01:08:48 +00:00
case types.ReqTypeFS:
if res.Value == nil {
c.respCh <- res
break
}
var progress types.FSTransferProgress
if err := mapstructure.Decode(res.Value, &progress); err != nil {
c.respCh <- res
break
}
switch progress.Type {
case types.FSTypeRead:
c.readProgressCh <- progress
case types.FSTypeWrite:
c.writeProgressCh <- progress
default:
c.respCh <- res
}
default:
c.respCh <- res
}
return nil
}
func decodeUint8(val interface{}) uint8 {
return uint8(val.(float64))
}
func decodeUint32(val interface{}) uint32 {
return uint32(val.(float64))
}
func decodeMotion(val interface{}) (infinitime.MotionValues, error) {
out := infinitime.MotionValues{}
err := mapstructure.Decode(val, &out)
if err != nil {
return out, err
}
return out, nil
}
func decodeDFUProgress(val interface{}) (DFUProgress, error) {
out := DFUProgress{}
err := mapstructure.Decode(val, &out)
if err != nil {
return out, err
}
return out, nil
}