- Added ListSwitches method to SwitchService in switch_grpc.pb.go. - Implemented SwitchGRPC adapter for ListSwitches in switch.go. - Created SwitchApp for managing switch states and added ListSwitches logic. - Updated core domain with Switch struct and associated methods. - Enhanced LightApp to include ListLights functionality. - Updated protobuf definitions for Switch and Light services to include new request and response messages. - Introduced error handling for unimplemented methods in the gRPC server.
55 lines
1.6 KiB
Go
55 lines
1.6 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 LightGRPC struct {
|
|
hav1.UnimplementedLightServiceServer
|
|
svc driving.LightService
|
|
}
|
|
|
|
func NewLightGRPC(svc driving.LightService) *LightGRPC {
|
|
return &LightGRPC{svc: svc}
|
|
}
|
|
|
|
func (h *LightGRPC) TurnOn(ctx context.Context, req *hav1.TurnOnRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.TurnOn(ctx, protoTurnOnToParams(req))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
func (h *LightGRPC) TurnOff(ctx context.Context, req *hav1.TurnOffRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.TurnOff(ctx, protoTurnOffToParams(req))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
func (h *LightGRPC) Toggle(ctx context.Context, req *hav1.ToggleRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.Toggle(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
func (h *LightGRPC) ListLights(ctx context.Context, req *hav1.ListLightsRequest) (*hav1.ListLightsResponse, error) {
|
|
lights, err := h.svc.ListLights(ctx)
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
out := make([]*hav1.LightEntity, 0, len(lights))
|
|
for _, l := range lights {
|
|
out = append(out, domainLightToProto(l))
|
|
}
|
|
return &hav1.ListLightsResponse{Lights: out}, nil
|
|
}
|