refactor: connection agent -> hub

This commit is contained in:
2026-04-05 11:21:54 +03:00
parent 1cc23ee3c1
commit acbc100928
+37
View File
@@ -1 +1,38 @@
package grpc
import (
"context"
"fmt"
"time"
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
type HomeOpsAgent struct {
conn pb.HubClient
}
func NewConnectAgent(address string) (*HomeOpsAgent, error) {
conn, err := grpc.NewClient(address)
if err != nil {
return nil, fmt.Errorf("failed connection hub: %v", err)
}
client := pb.NewHubClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
resp, err := client.Ping(ctx, &emptypb.Empty{})
if err != nil {
return nil, fmt.Errorf("failed connection hub: %v", err)
}
if resp.Pong != "Pong" {
return nil, fmt.Errorf("failed connection hub: %v", err)
}
return &HomeOpsAgent{conn: client}, nil
}