Implement file transfer progress

This commit is contained in:
2021-12-12 17:08:48 -08:00
parent 56ed3dda88
commit 8c020f792d
8 changed files with 328 additions and 73 deletions

View File

@@ -16,13 +16,15 @@ const DefaultAddr = "/tmp/itd/socket"
// Client is the socket API client
type Client struct {
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
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
}
// New creates a new client and sets it up
@@ -100,6 +102,24 @@ func (c *Client) handleResp(res types.Response) error {
c.motionCh <- res
case types.ReqTypeFwUpgrade:
c.dfuProgressCh <- res
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
}

View File

@@ -66,22 +66,28 @@ func (c *Client) ReadDir(path string) ([]types.FileInfo, error) {
return out, nil
}
func (c *Client) ReadFile(localPath, remotePath string) error {
_, err := c.request(types.Request{
func (c *Client) ReadFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) {
c.readProgressCh = make(chan types.FSTransferProgress, 5)
err := c.requestNoRes(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeRead,
Files: []string{localPath, remotePath},
},
})
if err != nil {
return err
return nil, err
}
return nil
return c.readProgressCh, nil
}
func (c *Client) WriteFile(localPath, remotePath string) error {
_, err := c.request(types.Request{
func (c *Client) WriteFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) {
c.writeProgressCh = make(chan types.FSTransferProgress, 5)
err := c.requestNoRes(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeWrite,
@@ -89,7 +95,8 @@ func (c *Client) WriteFile(localPath, remotePath string) error {
},
})
if err != nil {
return err
return nil, err
}
return nil
return c.writeProgressCh, nil
}