From 4b95af905d1a5455a8c743bed9e8c1762cb89b8d Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Fri, 22 Oct 2021 13:21:14 -0700 Subject: [PATCH] Implement motion service --- go.mod | 2 +- go.sum | 4 +-- internal/types/types.go | 4 +++ socket.go | 56 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index f441f90..3a49667 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect github.com/yuin/goldmark v1.4.1 // indirect - go.arsenm.dev/infinitime v0.0.0-20211022031744-1dde7f9b07f6 + go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect diff --git a/go.sum b/go.sum index 65525df..e68a60b 100644 --- a/go.sum +++ b/go.sum @@ -366,8 +366,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.3.8/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= 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-20211022031744-1dde7f9b07f6 h1:XXHdx/G0isWjfhDx9xPPTTjpXwywBk5WMECcMWLPCI0= -go.arsenm.dev/infinitime v0.0.0-20211022031744-1dde7f9b07f6/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q= +go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b h1:2VitKPwSYSWXmL5BH88nfTPLSIYPCt4yubpEJHhcQBc= +go.arsenm.dev/infinitime v0.0.0-20211022195951-45baea10486b/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ= diff --git a/internal/types/types.go b/internal/types/types.go index 285896d..3d1bc70 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -10,6 +10,10 @@ const ( ReqTypeSetTime ReqTypeWatchHeartRate ReqTypeWatchBattLevel + ReqTypeMotion + ReqTypeWatchMotion + ReqTypeStepCount + ReqTypeWatchStepCount ) const ( diff --git a/socket.go b/socket.go index 5bbf835..92fef74 100644 --- a/socket.go +++ b/socket.go @@ -117,6 +117,17 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { }) } }() + case types.ReqTypeBattLevel: + // Get battery level from watch + battLevel, err := dev.BatteryLevel() + if err != nil { + connErr(conn, err, "Error getting battery level") + break + } + // Encode battery level to connection + json.NewEncoder(conn).Encode(types.Response{ + Value: battLevel, + }) case types.ReqTypeWatchBattLevel: battLevelCh, err := dev.WatchBatteryLevel() if err != nil { @@ -130,17 +141,54 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { }) } }() - case types.ReqTypeBattLevel: + case types.ReqTypeMotion: // Get battery level from watch - battLevel, err := dev.BatteryLevel() + motionVals, err := dev.Motion() if err != nil { - connErr(conn, err, "Error getting battery level") + connErr(conn, err, "Error getting motion values") break } // Encode battery level to connection json.NewEncoder(conn).Encode(types.Response{ - Value: battLevel, + Value: motionVals, }) + case types.ReqTypeWatchMotion: + motionValCh, _, err := dev.WatchMotion() + if err != nil { + connErr(conn, err, "Error getting heart rate channel") + break + } + go func() { + for motionVals := range motionValCh { + json.NewEncoder(conn).Encode(types.Response{ + Value: motionVals, + }) + } + }() + case types.ReqTypeStepCount: + // Get battery level from watch + stepCount, err := dev.StepCount() + if err != nil { + connErr(conn, err, "Error getting step count") + break + } + // Encode battery level to connection + json.NewEncoder(conn).Encode(types.Response{ + Value: stepCount, + }) + case types.ReqTypeWatchStepCount: + stepCountCh, _, err := dev.WatchStepCount() + if err != nil { + connErr(conn, err, "Error getting heart rate channel") + break + } + go func() { + for stepCount := range stepCountCh { + json.NewEncoder(conn).Encode(types.Response{ + Value: stepCount, + }) + } + }() case types.ReqTypeFwVersion: // Get firmware version from watch version, err := dev.Version()