- Implemented TurnOn, TurnOff, and Toggle methods in the gRPC client for switch control. - Added corresponding methods in the CommandApp to handle user commands for turning switches on, off, and toggling. - Created unit tests for the new switch control methods in CommandApp and SwitchApp. - Updated the HAGateway interface to include switch control methods. - Enhanced the SwitchGRPC service to handle switch control requests and return appropriate responses. - Added integration tests for the switch service to ensure correct behavior and error handling.
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
hav1 "gitea.nik4nao.com/nik/home-services/gen/ha/v1"
|
|
"gitea.nik4nao.com/nik/home-services/ha-gateway/internal/core/domain"
|
|
"gitea.nik4nao.com/nik/home-services/ha-gateway/internal/core/ports/driving"
|
|
)
|
|
|
|
type SwitchGRPC struct {
|
|
hav1.UnimplementedSwitchServiceServer
|
|
svc driving.SwitchService
|
|
}
|
|
|
|
// NewSwitchGRPC constructs the gRPC adapter for SwitchService.
|
|
func NewSwitchGRPC(svc driving.SwitchService) *SwitchGRPC {
|
|
return &SwitchGRPC{svc: svc}
|
|
}
|
|
|
|
// ListSwitches returns discovery-oriented switch metadata for clients.
|
|
func (h *SwitchGRPC) ListSwitches(ctx context.Context, req *hav1.ListSwitchesRequest) (*hav1.ListSwitchesResponse, error) {
|
|
switches, err := h.svc.ListSwitches(ctx)
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
out := make([]*hav1.SwitchEntity, 0, len(switches))
|
|
for _, s := range switches {
|
|
out = append(out, domainSwitchToProto(s))
|
|
}
|
|
return &hav1.ListSwitchesResponse{Switches: out}, nil
|
|
}
|
|
|
|
// TurnOn maps a protobuf turn-on request into a domain entity ID.
|
|
func (h *SwitchGRPC) TurnOn(ctx context.Context, req *hav1.SwitchRequest) (*hav1.SwitchResponse, error) {
|
|
s, err := h.svc.TurnOn(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.SwitchResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// TurnOff maps a protobuf turn-off request into a domain entity ID.
|
|
func (h *SwitchGRPC) TurnOff(ctx context.Context, req *hav1.SwitchRequest) (*hav1.SwitchResponse, error) {
|
|
s, err := h.svc.TurnOff(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.SwitchResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// Toggle forwards a switch toggle request to the domain service.
|
|
func (h *SwitchGRPC) Toggle(ctx context.Context, req *hav1.SwitchRequest) (*hav1.SwitchResponse, error) {
|
|
s, err := h.svc.Toggle(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.SwitchResponse{State: domainStateToProto(s)}, nil
|
|
}
|