- Added ClimateApp to handle climate entity operations including turning on/off, adjusting temperature, and setting HVAC modes. - Implemented caching mechanism for climate entities to optimize state retrieval. - Created domain model for Climate with attributes such as current temperature, target temperature, and HVAC modes. - Developed unit tests for ClimateApp to ensure functionality and correctness. feat: add RemoteApp for SwitchBot commands - Introduced RemoteApp to facilitate sending commands to SwitchBot devices. - Implemented SendCommand method for executing device commands without state management. chore: update configuration for SwitchBot integration - Added SwitchBotToken and SwitchBotSecret to configuration for enabling SwitchBot Cloud commands. feat: define gRPC services for Climate and Remote operations - Created climate.proto and remote.proto files to define gRPC services for climate management and remote command execution. - Implemented corresponding request and response message structures for gRPC interactions.
78 lines
2.8 KiB
Go
78 lines
2.8 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 ClimateGRPC struct {
|
|
hav1.UnimplementedClimateServiceServer
|
|
svc driving.ClimateService
|
|
}
|
|
|
|
// NewClimateGRPC constructs the gRPC adapter for ClimateService.
|
|
func NewClimateGRPC(svc driving.ClimateService) *ClimateGRPC {
|
|
return &ClimateGRPC{svc: svc}
|
|
}
|
|
|
|
// ListClimates returns discovery-oriented climate metadata for clients.
|
|
func (h *ClimateGRPC) ListClimates(ctx context.Context, req *hav1.ListClimatesRequest) (*hav1.ListClimatesResponse, error) {
|
|
climates, err := h.svc.ListClimates(ctx)
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
out := make([]*hav1.ClimateEntity, 0, len(climates))
|
|
for _, c := range climates {
|
|
out = append(out, domainClimateToProto(c))
|
|
}
|
|
return &hav1.ListClimatesResponse{Climates: out}, nil
|
|
}
|
|
|
|
// TurnOn maps a protobuf turn-on request into a domain entity ID.
|
|
func (h *ClimateGRPC) TurnOn(ctx context.Context, req *hav1.ClimateRequest) (*hav1.ClimateResponse, error) {
|
|
s, err := h.svc.TurnOn(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.ClimateResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// TurnOff maps a protobuf turn-off request into a domain entity ID.
|
|
func (h *ClimateGRPC) TurnOff(ctx context.Context, req *hav1.ClimateRequest) (*hav1.ClimateResponse, error) {
|
|
s, err := h.svc.TurnOff(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.ClimateResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// IncreaseTemperature forwards a temperature step-up request to the domain service.
|
|
func (h *ClimateGRPC) IncreaseTemperature(ctx context.Context, req *hav1.ClimateRequest) (*hav1.ClimateResponse, error) {
|
|
s, err := h.svc.IncreaseTemperature(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.ClimateResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// DecreaseTemperature forwards a temperature step-down request to the domain service.
|
|
func (h *ClimateGRPC) DecreaseTemperature(ctx context.Context, req *hav1.ClimateRequest) (*hav1.ClimateResponse, error) {
|
|
s, err := h.svc.DecreaseTemperature(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.ClimateResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// SetHVACMode forwards an HVAC mode change request to the domain service.
|
|
func (h *ClimateGRPC) SetHVACMode(ctx context.Context, req *hav1.SetHVACModeRequest) (*hav1.ClimateResponse, error) {
|
|
s, err := h.svc.SetHVACMode(ctx, domain.EntityID(req.EntityId), req.HvacMode)
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.ClimateResponse{State: domainStateToProto(s)}, nil
|
|
}
|