mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 16:45:15 +03:00
refactor: connection agent -> hub
This commit is contained in:
@@ -1 +1,38 @@
|
|||||||
package grpc
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user