Add getJoint function & Share ip/port declaration

This commit is contained in:
AntoineH 2024-09-04 11:35:29 +02:00
parent 01514ad6d2
commit d950ba6d1d
4 changed files with 49 additions and 14 deletions

View file

@ -8,15 +8,26 @@ import (
"fmt"
"github.com/spf13/cobra"
"context"
"log"
"time"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "github.com/AntoineHX/multi-motors-controller/src/proto"
)
//Cobra CLI
// getJointsCmd represents the getJoints command
var getJointsCmd = &cobra.Command{
Use: "getJoints",
Short: "getJoints command descritpion",
Long: `getJoints command descritpion`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("getJoints called")
// fmt.Println("getJoints called")
GetJoints()
},
}
@ -33,3 +44,24 @@ func init() {
// is called directly, e.g.:
// getJointsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
//gRPC Client
func GetJoints(){
// Set up a connection to the server.
var addr = fmt.Sprintf("%s:%d", ip, port) //Defined in controller/serve
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewMotorsControllerClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.GetJoints(ctx, &pb.Empty{})
if err != nil {
log.Fatalf("could not send: %v", err)
}
log.Printf("Angles (°): %v", r.GetAngles()) //TODO: Format with °
}