84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package robotclient
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"hyapp-admin-server/internal/appctx"
|
|
robotv1 "hyapp.local/api/proto/robot/v1"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type Client interface {
|
|
ListRoomRobots(ctx context.Context, req ListRoomRobotsRequest) (ListRoomRobotsResult, error)
|
|
}
|
|
|
|
type Robot struct {
|
|
UserID int64
|
|
Status string
|
|
RoomScene string
|
|
CreatedAtMS int64
|
|
UpdatedAtMS int64
|
|
LastUsedAtMS int64
|
|
UsedCount int64
|
|
}
|
|
|
|
type ListRoomRobotsRequest struct {
|
|
RoomScene string
|
|
Status string
|
|
PageSize int32
|
|
Cursor string
|
|
}
|
|
|
|
type ListRoomRobotsResult struct {
|
|
Robots []Robot
|
|
NextCursor string
|
|
ServerTimeMS int64
|
|
}
|
|
|
|
type GRPCClient struct {
|
|
client robotv1.RoomRobotServiceClient
|
|
}
|
|
|
|
func NewGRPC(conn grpc.ClientConnInterface) *GRPCClient {
|
|
return &GRPCClient{client: robotv1.NewRoomRobotServiceClient(conn)}
|
|
}
|
|
|
|
func (c *GRPCClient) ListRoomRobots(ctx context.Context, req ListRoomRobotsRequest) (ListRoomRobotsResult, error) {
|
|
if c == nil || c.client == nil {
|
|
return ListRoomRobotsResult{}, fmt.Errorf("robot service client is not configured")
|
|
}
|
|
resp, err := c.client.ListRoomRobots(ctx, &robotv1.ListRoomRobotsRequest{
|
|
Meta: &robotv1.RequestMeta{
|
|
RequestId: fmt.Sprintf("admin-room-robots-%d", time.Now().UnixMilli()),
|
|
Caller: "admin-server",
|
|
AppCode: appctx.FromContext(ctx),
|
|
SentAtMs: time.Now().UnixMilli(),
|
|
ActorUserId: 0,
|
|
},
|
|
RoomScene: strings.TrimSpace(req.RoomScene),
|
|
Status: strings.TrimSpace(req.Status),
|
|
PageSize: req.PageSize,
|
|
Cursor: strings.TrimSpace(req.Cursor),
|
|
})
|
|
if err != nil {
|
|
return ListRoomRobotsResult{}, err
|
|
}
|
|
items := make([]Robot, 0, len(resp.GetRobots()))
|
|
for _, item := range resp.GetRobots() {
|
|
items = append(items, Robot{
|
|
UserID: item.GetUserId(),
|
|
Status: item.GetStatus(),
|
|
RoomScene: item.GetRoomScene(),
|
|
CreatedAtMS: item.GetCreatedAtMs(),
|
|
UpdatedAtMS: item.GetUpdatedAtMs(),
|
|
LastUsedAtMS: item.GetLastUsedAtMs(),
|
|
UsedCount: item.GetUsedCount(),
|
|
})
|
|
}
|
|
return ListRoomRobotsResult{Robots: items, NextCursor: resp.GetNextCursor(), ServerTimeMS: resp.GetServerTimeMs()}, nil
|
|
}
|