|
| 1 | +package grpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "time" |
| 8 | + |
| 9 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 10 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1" |
| 11 | + "github.com/bcmi-labs/iot-cloud-cli/arduino" |
| 12 | + "google.golang.org/grpc" |
| 13 | +) |
| 14 | + |
| 15 | +type service struct { |
| 16 | + serviceClient rpc.ArduinoCoreServiceClient |
| 17 | + settingsClient settings.SettingsServiceClient |
| 18 | + instance *rpc.Instance |
| 19 | +} |
| 20 | + |
| 21 | +type client struct { |
| 22 | + boardHandler |
| 23 | + compileHandler |
| 24 | +} |
| 25 | + |
| 26 | +// NewClient instantiates and returns a new grpc client that allows to |
| 27 | +// programmatically call arduino-cli commands. |
| 28 | +// It exploits the grpc interface of the arduino-cli. |
| 29 | +// It returns: the client instance, a callback to close the client and an error |
| 30 | +func NewClient() (arduino.Commander, func() error, error) { |
| 31 | + // Establish a connection with the gRPC server, started with the command: |
| 32 | + // arduino-cli daemon |
| 33 | + conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second)) |
| 34 | + if err != nil { |
| 35 | + err = fmt.Errorf("%s: %w", "cannot connect to arduino-cli rpc server, you can start it by running `arduino-cli daemon`", err) |
| 36 | + return nil, func() error { return nil }, err |
| 37 | + } |
| 38 | + |
| 39 | + serv := &service{} |
| 40 | + // Create an instance of the gRPC clients. |
| 41 | + serv.serviceClient = rpc.NewArduinoCoreServiceClient(conn) |
| 42 | + serv.settingsClient = settings.NewSettingsServiceClient(conn) |
| 43 | + serv.instance, err = initInstance(serv.serviceClient) |
| 44 | + if err != nil { |
| 45 | + conn.Close() |
| 46 | + err = fmt.Errorf("%s: %w", "creating arduino-cli instance", err) |
| 47 | + return nil, func() error { return nil }, err |
| 48 | + } |
| 49 | + |
| 50 | + cl := &client{} |
| 51 | + cl.boardHandler = boardHandler{serv} |
| 52 | + cl.compileHandler = compileHandler{serv} |
| 53 | + |
| 54 | + return cl, conn.Close, nil |
| 55 | +} |
| 56 | + |
| 57 | +func initInstance(client rpc.ArduinoCoreServiceClient) (*rpc.Instance, error) { |
| 58 | + initRespStream, err := client.Init(context.Background(), &rpc.InitRequest{}) |
| 59 | + if err != nil { |
| 60 | + err = fmt.Errorf("%s: %w", "Error creating server instance", err) |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + var instance *rpc.Instance |
| 65 | + // Loop and consume the server stream until all the setup procedures are done. |
| 66 | + for { |
| 67 | + initResp, err := initRespStream.Recv() |
| 68 | + // The server is done. |
| 69 | + if err == io.EOF { |
| 70 | + break |
| 71 | + } |
| 72 | + |
| 73 | + // There was an error. |
| 74 | + if err != nil { |
| 75 | + err = fmt.Errorf("%s: %w", "init error", err) |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + // The server sent us a valid instance, let's print its ID. |
| 80 | + if initResp.GetInstance() != nil { |
| 81 | + instance = initResp.GetInstance() |
| 82 | + //fmt.Printf("Got a new instance with ID: %v", instance.GetId()) |
| 83 | + } |
| 84 | + |
| 85 | + // When a download is ongoing, log the progress |
| 86 | + if initResp.GetDownloadProgress() != nil { |
| 87 | + fmt.Printf("DOWNLOAD: %s", initResp.GetDownloadProgress()) |
| 88 | + } |
| 89 | + |
| 90 | + // When an overall task is ongoing, log the progress |
| 91 | + if initResp.GetTaskProgress() != nil { |
| 92 | + fmt.Printf("TASK: %s", initResp.GetTaskProgress()) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return instance, nil |
| 97 | +} |
0 commit comments