Add context support and update lrpc

This commit is contained in:
Elara 2022-05-01 15:22:28 -07:00
parent b0c4574481
commit 78b5ca1de8
21 changed files with 155 additions and 144 deletions

View File

@ -1,12 +1,15 @@
package api
import (
"context"
"go.arsenm.dev/infinitime"
)
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (chan infinitime.DFUProgress, error) {
func (c *Client) FirmwareUpgrade(ctx context.Context, upgType UpgradeType, files ...string) (chan infinitime.DFUProgress, error) {
progressCh := make(chan infinitime.DFUProgress, 5)
err := c.client.Call(
ctx,
"ITD",
"FirmwareUpgrade",
FwUpgradeData{

View File

@ -1,7 +1,10 @@
package api
func (c *Client) Remove(paths ...string) error {
import "context"
func (c *Client) Remove(ctx context.Context, paths ...string) error {
return c.client.Call(
ctx,
"FS",
"Remove",
paths,
@ -9,8 +12,9 @@ func (c *Client) Remove(paths ...string) error {
)
}
func (c *Client) Rename(old, new string) error {
func (c *Client) Rename(ctx context.Context, old, new string) error {
return c.client.Call(
ctx,
"FS",
"Rename",
[2]string{old, new},
@ -18,8 +22,9 @@ func (c *Client) Rename(old, new string) error {
)
}
func (c *Client) Mkdir(paths ...string) error {
func (c *Client) Mkdir(ctx context.Context, paths ...string) error {
return c.client.Call(
ctx,
"FS",
"Mkdir",
paths,
@ -27,8 +32,9 @@ func (c *Client) Mkdir(paths ...string) error {
)
}
func (c *Client) ReadDir(dir string) (out []FileInfo, err error) {
func (c *Client) ReadDir(ctx context.Context, dir string) (out []FileInfo, err error) {
err = c.client.Call(
ctx,
"FS",
"ReadDir",
dir,
@ -37,9 +43,10 @@ func (c *Client) ReadDir(dir string) (out []FileInfo, err error) {
return
}
func (c *Client) Upload(dst, src string) (chan FSTransferProgress, error) {
func (c *Client) Upload(ctx context.Context, dst, src string) (chan FSTransferProgress, error) {
progressCh := make(chan FSTransferProgress, 5)
err := c.client.Call(
ctx,
"FS",
"Upload",
[2]string{dst, src},
@ -52,9 +59,10 @@ func (c *Client) Upload(dst, src string) (chan FSTransferProgress, error) {
return progressCh, nil
}
func (c *Client) Download(dst, src string) (chan FSTransferProgress, error) {
func (c *Client) Download(ctx context.Context, dst, src string) (chan FSTransferProgress, error) {
progressCh := make(chan FSTransferProgress, 5)
err := c.client.Call(
ctx,
"FS",
"Download",
[2]string{dst, src},

View File

@ -1,11 +1,14 @@
package api
import (
"context"
"go.arsenm.dev/infinitime"
)
func (c *Client) HeartRate() (out uint8, err error) {
func (c *Client) HeartRate(ctx context.Context) (out uint8, err error) {
err = c.client.Call(
ctx,
"ITD",
"HeartRate",
nil,
@ -14,8 +17,9 @@ func (c *Client) HeartRate() (out uint8, err error) {
return
}
func (c *Client) BatteryLevel() (out uint8, err error) {
func (c *Client) BatteryLevel(ctx context.Context) (out uint8, err error) {
err = c.client.Call(
ctx,
"ITD",
"BatteryLevel",
nil,
@ -24,8 +28,9 @@ func (c *Client) BatteryLevel() (out uint8, err error) {
return
}
func (c *Client) Motion() (out infinitime.MotionValues, err error) {
func (c *Client) Motion(ctx context.Context) (out infinitime.MotionValues, err error) {
err = c.client.Call(
ctx,
"ITD",
"Motion",
nil,
@ -34,8 +39,9 @@ func (c *Client) Motion() (out infinitime.MotionValues, err error) {
return
}
func (c *Client) StepCount() (out uint32, err error) {
func (c *Client) StepCount(ctx context.Context) (out uint32, err error) {
err = c.client.Call(
ctx,
"ITD",
"StepCount",
nil,
@ -44,8 +50,9 @@ func (c *Client) StepCount() (out uint32, err error) {
return
}
func (c *Client) Version() (out string, err error) {
func (c *Client) Version(ctx context.Context) (out string, err error) {
err = c.client.Call(
ctx,
"ITD",
"Version",
nil,
@ -54,8 +61,9 @@ func (c *Client) Version() (out string, err error) {
return
}
func (c *Client) Address() (out string, err error) {
func (c *Client) Address(ctx context.Context) (out string, err error) {
err = c.client.Call(
ctx,
"ITD",
"Address",
nil,

View File

@ -1,7 +1,10 @@
package api
func (c *Client) Notify(title, body string) error {
import "context"
func (c *Client) Notify(ctx context.Context, title, body string) error {
return c.client.Call(
ctx,
"ITD",
"Notify",
NotifyData{

View File

@ -1,11 +1,13 @@
package api
import (
"context"
"time"
)
func (c *Client) SetTime(t time.Time) error {
func (c *Client) SetTime(ctx context.Context, t time.Time) error {
return c.client.Call(
ctx,
"ITD",
"SetTime",
t,

View File

@ -1,7 +1,10 @@
package api
func (c *Client) WeatherUpdate() error {
import "context"
func (c *Client) WeatherUpdate(ctx context.Context) error {
return c.client.Call(
ctx,
"ITD",
"WeatherUpdate",
nil,

View File

@ -1,81 +1,71 @@
package api
import (
"context"
"go.arsenm.dev/infinitime"
)
func (c *Client) WatchHeartRate() (<-chan uint8, func(), error) {
func (c *Client) WatchHeartRate(ctx context.Context) (<-chan uint8, error) {
outCh := make(chan uint8, 2)
err := c.client.Call(
ctx,
"ITD",
"WatchHeartRate",
nil,
outCh,
)
if err != nil {
return nil, nil, err
return nil, err
}
doneFn := func() {
close(outCh)
}
return outCh, doneFn, nil
return outCh, nil
}
func (c *Client) WatchBatteryLevel() (<-chan uint8, func(), error) {
func (c *Client) WatchBatteryLevel(ctx context.Context) (<-chan uint8, error) {
outCh := make(chan uint8, 2)
err := c.client.Call(
ctx,
"ITD",
"WatchBatteryLevel",
nil,
outCh,
)
if err != nil {
return nil, nil, err
return nil, err
}
doneFn := func() {
close(outCh)
}
return outCh, doneFn, nil
return outCh, nil
}
func (c *Client) WatchStepCount() (<-chan uint32, func(), error) {
func (c *Client) WatchStepCount(ctx context.Context) (<-chan uint32, error) {
outCh := make(chan uint32, 2)
err := c.client.Call(
ctx,
"ITD",
"WatchStepCount",
nil,
outCh,
)
if err != nil {
return nil, nil, err
return nil, err
}
doneFn := func() {
close(outCh)
}
return outCh, doneFn, nil
return outCh, nil
}
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, func(), error) {
func (c *Client) WatchMotion(ctx context.Context) (<-chan infinitime.MotionValues, error) {
outCh := make(chan infinitime.MotionValues, 2)
err := c.client.Call(
ctx,
"ITD",
"WatchMotion",
nil,
outCh,
)
if err != nil {
return nil, nil, err
return nil, err
}
doneFn := func() {
close(outCh)
}
return outCh, doneFn, nil
return outCh, nil
}

View File

@ -28,7 +28,7 @@ func fwUpgrade(c *cli.Context) error {
return cli.Exit("Upgrade command requires either archive or init packet and firmware.", 1)
}
progress, err := client.FirmwareUpgrade(upgType, abs(files)...)
progress, err := client.FirmwareUpgrade(c.Context, upgType, abs(files)...)
if err != nil {
return err
}
@ -58,7 +58,7 @@ func fwUpgrade(c *cli.Context) error {
}
func fwVersion(c *cli.Context) error {
version, err := client.Version()
version, err := client.Version(c.Context)
if err != nil {
return err
}

View File

@ -17,7 +17,7 @@ func fsList(c *cli.Context) error {
dirPath = c.Args().Get(0)
}
listing, err := client.ReadDir(dirPath)
listing, err := client.ReadDir(c.Context, dirPath)
if err != nil {
return err
}
@ -34,7 +34,7 @@ func fsMkdir(c *cli.Context) error {
return cli.Exit("Command mkdir requires one or more arguments", 1)
}
err := client.Mkdir(c.Args().Slice()...)
err := client.Mkdir(c.Context, c.Args().Slice()...)
if err != nil {
return err
}
@ -47,7 +47,7 @@ func fsMove(c *cli.Context) error {
return cli.Exit("Command move requires two arguments", 1)
}
err := client.Rename(c.Args().Get(0), c.Args().Get(1))
err := client.Rename(c.Context, c.Args().Get(0), c.Args().Get(1))
if err != nil {
return err
}
@ -76,7 +76,7 @@ func fsRead(c *cli.Context) error {
}
}
progress, err := client.Download(path, c.Args().Get(0))
progress, err := client.Download(c.Context, path, c.Args().Get(0))
if err != nil {
return err
}
@ -109,7 +109,7 @@ func fsRemove(c *cli.Context) error {
return cli.Exit("Command remove requires one or more arguments", 1)
}
err := client.Remove(c.Args().Slice()...)
err := client.Remove(c.Context, c.Args().Slice()...)
if err != nil {
return err
}
@ -144,7 +144,7 @@ func fsWrite(c *cli.Context) error {
defer os.Remove(path)
}
progress, err := client.Upload(c.Args().Get(1), path)
progress, err := client.Upload(c.Context, c.Args().Get(1), path)
if err != nil {
return err
}

View File

@ -9,7 +9,7 @@ import (
)
func getAddress(c *cli.Context) error {
address, err := client.Address()
address, err := client.Address(c.Context)
if err != nil {
return err
}
@ -19,7 +19,7 @@ func getAddress(c *cli.Context) error {
}
func getBattery(c *cli.Context) error {
battLevel, err := client.BatteryLevel()
battLevel, err := client.BatteryLevel(c.Context)
if err != nil {
return err
}
@ -30,7 +30,7 @@ func getBattery(c *cli.Context) error {
}
func getHeart(c *cli.Context) error {
heartRate, err := client.HeartRate()
heartRate, err := client.HeartRate(c.Context)
if err != nil {
return err
}
@ -41,7 +41,7 @@ func getHeart(c *cli.Context) error {
}
func getMotion(c *cli.Context) error {
motionVals, err := client.Motion()
motionVals, err := client.Motion(c.Context)
if err != nil {
return err
}
@ -60,7 +60,7 @@ func getMotion(c *cli.Context) error {
}
func getSteps(c *cli.Context) error {
stepCount, err := client.StepCount()
stepCount, err := client.StepCount(c.Context)
if err != nil {
return err
}

View File

@ -8,7 +8,7 @@ func notify(c *cli.Context) error {
return cli.Exit("Command notify requires two arguments", 1)
}
err := client.Notify(c.Args().Get(0), c.Args().Get(1))
err := client.Notify(c.Context, c.Args().Get(0), c.Args().Get(1))
if err != nil {
return err
}

View File

@ -13,12 +13,12 @@ func setTime(c *cli.Context) error {
}
if c.Args().Get(0) == "now" {
return client.SetTime(time.Now())
return client.SetTime(c.Context, time.Now())
} else {
parsed, err := time.Parse(time.RFC3339, c.Args().Get(0))
if err != nil {
return err
}
return client.SetTime(parsed)
return client.SetTime(c.Context, parsed)
}
}

View File

@ -3,5 +3,5 @@ package main
import "github.com/urfave/cli/v2"
func updateWeather(c *cli.Context) error {
return client.WeatherUpdate()
return client.WeatherUpdate(c.Context)
}

View File

@ -9,104 +9,88 @@ import (
)
func watchHeart(c *cli.Context) error {
heartCh, cancel, err := client.WatchHeartRate()
heartCh, err := client.WatchHeartRate(c.Context)
if err != nil {
return err
}
for {
select {
case heartRate := <-heartCh:
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint8{"heartRate": heartRate},
)
} else if c.Bool("shell") {
fmt.Printf("HEART_RATE=%d\n", heartRate)
} else {
fmt.Println(heartRate, "BPM")
}
case <-c.Done():
cancel()
return nil
for heartRate := range heartCh {
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint8{"heartRate": heartRate},
)
} else if c.Bool("shell") {
fmt.Printf("HEART_RATE=%d\n", heartRate)
} else {
fmt.Println(heartRate, "BPM")
}
}
return nil
}
func watchBattLevel(c *cli.Context) error {
battLevelCh, cancel, err := client.WatchBatteryLevel()
battLevelCh, err := client.WatchBatteryLevel(c.Context)
if err != nil {
return err
}
for {
select {
case battLevel := <-battLevelCh:
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint8{"battLevel": battLevel},
)
} else if c.Bool("shell") {
fmt.Printf("BATTERY_LEVEL=%d\n", battLevel)
} else {
fmt.Printf("%d%%\n", battLevel)
}
case <-c.Done():
cancel()
return nil
for battLevel := range battLevelCh {
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint8{"battLevel": battLevel},
)
} else if c.Bool("shell") {
fmt.Printf("BATTERY_LEVEL=%d\n", battLevel)
} else {
fmt.Printf("%d%%\n", battLevel)
}
}
return nil
}
func watchStepCount(c *cli.Context) error {
stepCountCh, cancel, err := client.WatchStepCount()
stepCountCh, err := client.WatchStepCount(c.Context)
if err != nil {
return err
}
for {
select {
case stepCount := <-stepCountCh:
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint32{"stepCount": stepCount},
)
} else if c.Bool("shell") {
fmt.Printf("STEP_COUNT=%d\n", stepCount)
} else {
fmt.Println(stepCount, "Steps")
}
case <-c.Done():
cancel()
return nil
for stepCount := range stepCountCh {
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(
map[string]uint32{"stepCount": stepCount},
)
} else if c.Bool("shell") {
fmt.Printf("STEP_COUNT=%d\n", stepCount)
} else {
fmt.Println(stepCount, "Steps")
}
}
return nil
}
func watchMotion(c *cli.Context) error {
motionCh, cancel, err := client.WatchMotion()
motionCh, err := client.WatchMotion(c.Context)
if err != nil {
return err
}
for {
select {
case motionVals := <-motionCh:
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(motionVals)
} else if c.Bool("shell") {
fmt.Printf(
"X=%d\nY=%d\nZ=%d\n",
motionVals.X,
motionVals.Y,
motionVals.Z,
)
} else {
fmt.Println(motionVals)
}
case <-c.Done():
cancel()
return nil
for motionVals := range motionCh {
if c.Bool("json") {
json.NewEncoder(os.Stdout).Encode(motionVals)
} else if c.Bool("shell") {
fmt.Printf(
"X=%d\nY=%d\nZ=%d\n",
motionVals.X,
motionVals.Y,
motionVals.Z,
)
} else {
fmt.Println(motionVals)
}
}
return nil
}

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"image/color"
@ -27,11 +28,13 @@ func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
)
infoLayout.Add(heartRateSect)
heartRateCh, cancel, err := client.WatchHeartRate()
ctx, cancel := context.WithCancel(context.Background())
onClose = append(onClose, cancel)
heartRateCh, err := client.WatchHeartRate(ctx)
if err != nil {
guiErr(err, "Error getting heart rate channel", true, parent)
}
onClose = append(onClose, cancel)
go func() {
for heartRate := range heartRateCh {
// Change text of heart rate label
@ -51,11 +54,10 @@ func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
)
infoLayout.Add(stepCountSect)
stepCountCh, cancel, err := client.WatchStepCount()
stepCountCh, err := client.WatchStepCount(ctx)
if err != nil {
guiErr(err, "Error getting step count channel", true, parent)
}
onClose = append(onClose, cancel)
go func() {
for stepCount := range stepCountCh {
// Change text of heart rate label
@ -75,11 +77,10 @@ func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
)
infoLayout.Add(battLevel)
battLevelCh, cancel, err := client.WatchBatteryLevel()
battLevelCh, err := client.WatchBatteryLevel(ctx)
if err != nil {
guiErr(err, "Error getting battery level channel", true, parent)
}
onClose = append(onClose, cancel)
go func() {
for battLevel := range battLevelCh {
// Change text of battery level label
@ -89,7 +90,7 @@ func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
}
}()
fwVerString, err := client.Version()
fwVerString, err := client.Version(context.Background())
if err != nil {
guiErr(err, "Error getting firmware string", true, parent)
}
@ -101,7 +102,7 @@ func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
)
infoLayout.Add(fwVer)
btAddrString, err := client.Address()
btAddrString, err := client.Address(context.Background())
if err != nil {
panic(err)
}

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"image/color"
"strconv"
@ -44,6 +45,10 @@ func motionTab(parent fyne.Window, client *api.Client) *fyne.Container {
// Create button to stop motion
stopBtn := widget.NewButton("Stop", nil)
ctx, cancel := context.WithCancel(context.Background())
onClose = append(onClose, cancel)
// Create button to start motion
startBtn := widget.NewButton("Start", func() {
// if motion is started
@ -54,7 +59,7 @@ func motionTab(parent fyne.Window, client *api.Client) *fyne.Container {
// Set motion started
started = true
// Watch motion values
motionCh, cancel, err := client.WatchMotion()
motionCh, err := client.WatchMotion(ctx)
if err != nil {
guiErr(err, "Error getting heart rate channel", true, parent)
}

View File

@ -1,6 +1,8 @@
package main
import (
"context"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
@ -19,7 +21,7 @@ func notifyTab(parent fyne.Window, client *api.Client) *fyne.Container {
// Create new button to send notification
sendBtn := widget.NewButton("Send", func() {
err := client.Notify(titleEntry.Text, bodyEntry.Text)
err := client.Notify(context.Background(), titleEntry.Text, bodyEntry.Text)
if err != nil {
guiErr(err, "Error sending notification", false, parent)
return

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"time"
"fyne.io/fyne/v2"
@ -49,9 +50,9 @@ func timeTab(parent fyne.Window, client *api.Client) *fyne.Container {
func setTime(client *api.Client, current bool, t ...time.Time) error {
var err error
if current {
err = client.SetTime(time.Now())
err = client.SetTime(context.Background(), time.Now())
} else {
err = client.SetTime(t[0])
err = client.SetTime(context.Background(), t[0])
}
if err != nil {
return err

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"path/filepath"
@ -125,7 +126,7 @@ func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
files = append(files, initPktPath, firmwarePath)
}
progress, err := client.FirmwareUpgrade(fwUpgType, files...)
progress, err := client.FirmwareUpgrade(context.Background(), fwUpgType, files...)
if err != nil {
guiErr(err, "Error initiating DFU", false, parent)
return

4
go.mod
View File

@ -2,6 +2,8 @@ module go.arsenm.dev/itd
go 1.17
replace go.arsenm.dev/lrpc => /home/arsen/Code/lrpc
require (
fyne.io/fyne/v2 v2.1.2
github.com/cheggaaa/pb/v3 v3.0.8
@ -13,7 +15,7 @@ require (
github.com/rs/zerolog v1.26.1
github.com/urfave/cli/v2 v2.3.0
go.arsenm.dev/infinitime v0.0.0-20220424030849-6c3f1b14c948
go.arsenm.dev/lrpc v0.0.0-20220501205436-6df8cf53c6e6
go.arsenm.dev/lrpc v0.0.0-20220501221746-f1aa0f5c4f8f
golang.org/x/text v0.3.7
)

2
go.sum
View File

@ -200,8 +200,6 @@ github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.arsenm.dev/infinitime v0.0.0-20220424030849-6c3f1b14c948 h1:OX1SyEIFz4ae2z468lBQvRTNRvqLEwjfJ8lcssUH5+w=
go.arsenm.dev/infinitime v0.0.0-20220424030849-6c3f1b14c948/go.mod h1:1cBQ3fp6QlRbSqu9kEBAHsVThINj31FtqHIYVsQ7wgg=
go.arsenm.dev/lrpc v0.0.0-20220501205436-6df8cf53c6e6 h1:5Sd7fqK3ng/xSI3XNgIVEKfFl+6p2jv+zVo2Ag07V/M=
go.arsenm.dev/lrpc v0.0.0-20220501205436-6df8cf53c6e6/go.mod h1:KtIqSuK4mMzHm9OIO2oRtMxSZTooPCnMgtP3Q88N7hw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=