Skip to content

Commit

Permalink
fix: server sent message fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
subnova committed Mar 1, 2024
1 parent 4d6ec70 commit 13dd1ba
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 2 deletions.
37 changes: 37 additions & 0 deletions manager/handlers/ocpp201/set_variables_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ocpp201

import (
"context"
"fmt"
"github.com/thoughtworks/maeve-csms/manager/ocpp"
"github.com/thoughtworks/maeve-csms/manager/ocpp/ocpp201"
"github.com/thoughtworks/maeve-csms/manager/store"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

type SetVariablesResultHandler struct {
Store store.Engine
}

func (i SetVariablesResultHandler) HandleCallResult(ctx context.Context, chargeStationId string, request ocpp.Request, response ocpp.Response, state any) error {
span := trace.SpanFromContext(ctx)
if response != nil {
resp := response.(*ocpp201.SetVariablesResponseJson)

for _, variable := range resp.SetVariableResult {
span.SetAttributes(
attribute.String(fmt.Sprintf("set_variables.%s_%s.result", variable.Component.Name, variable.Variable.Name),
string(variable.AttributeStatus)))
}

err := i.Store.DeleteChargeStationSettings(ctx, chargeStationId)
if err != nil {
slog.Error("failed to delete charge station settings", "err", err)
span.AddEvent("failed to delete charge station settings", trace.WithAttributes(attribute.String("err", err.Error())))
}
}

return nil
}
67 changes: 67 additions & 0 deletions manager/handlers/ocpp201/set_variables_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ocpp201_test

import (
"context"
"github.com/stretchr/testify/require"
handlers201 "github.com/thoughtworks/maeve-csms/manager/handlers/ocpp201"
"github.com/thoughtworks/maeve-csms/manager/ocpp/ocpp201"
"github.com/thoughtworks/maeve-csms/manager/store/inmemory"
"k8s.io/utils/clock"
"testing"
)

func TestSetVariablesResultHandler(t *testing.T) {
engine := inmemory.NewStore(clock.RealClock{})
handler := handlers201.SetVariablesResultHandler{
Store: engine,
}

request := ocpp201.SetVariablesRequestJson{
SetVariableData: []ocpp201.SetVariableDataType{
{
AttributeValue: "20",
Component: ocpp201.ComponentType{
Name: "MyCtrlr",
},
Variable: ocpp201.VariableType{
Name: "MyVariable",
},
},
{
AttributeValue: "something",
Component: ocpp201.ComponentType{
Name: "MyCtrlr",
},
Variable: ocpp201.VariableType{
Name: "MyOtherVariable",
},
},
},
}

response := ocpp201.SetVariablesResponseJson{
SetVariableResult: []ocpp201.SetVariableResultType{
{
Component: ocpp201.ComponentType{
Name: "MyCtrlr",
},
Variable: ocpp201.VariableType{
Name: "MyVariable",
},
AttributeStatus: ocpp201.SetVariableStatusEnumTypeAccepted,
},
{
Component: ocpp201.ComponentType{
Name: "MyCtrlr",
},
Variable: ocpp201.VariableType{
Name: "MyOtherVariable",
},
AttributeStatus: ocpp201.SetVariableStatusEnumTypeRejected,
},
},
}

err := handler.HandleCallResult(context.TODO(), "cs001", &request, &response, nil)
require.NoError(t, err)
}
6 changes: 4 additions & 2 deletions manager/mqtt/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@ func (h *Handler) Connect(errCh chan error) {
v201SyncCallMaker := &BasicCallMaker{
E: v201Emitter,
Actions: map[reflect.Type]string{
reflect.TypeOf(&ocpp201.SetVariablesRequestJson{}): "SetVariables",
reflect.TypeOf(&ocpp201.TriggerMessageRequestJson{}): "TriggerMessage",
reflect.TypeOf(&ocpp201.SetVariablesRequestJson{}): "SetVariables",
reflect.TypeOf(&ocpp201.TriggerMessageRequestJson{}): "TriggerMessage",
reflect.TypeOf(&ocpp201.CertificateSignedRequestJson{}): "CertificateSigned",
reflect.TypeOf(&ocpp201.InstallCertificateRequestJson{}): "InstallCertificate",
},
}
go SyncSettings(context.Background(),
Expand Down
18 changes: 18 additions & 0 deletions manager/mqtt/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ func NewV201Router(clk clock.PassiveClock,
Store: engine,
},
},
"TriggerMessage": {
NewRequest: func() ocpp.Request { return new(ocpp201.TriggerMessageRequestJson) },
NewResponse: func() ocpp.Response { return new(ocpp201.TriggerMessageResponseJson) },
RequestSchema: "ocpp201/TriggerMessageRequest.json",
ResponseSchema: "ocpp201/TriggerMessageResponse.json",
Handler: handlers201.TriggerMessageResultHandler{
Store: engine,
},
},
"SetVariables": {
NewRequest: func() ocpp.Request { return new(ocpp201.SetVariablesRequestJson) },
NewResponse: func() ocpp.Response { return new(ocpp201.SetVariablesResponseJson) },
RequestSchema: "ocpp201/SetVariablesRequest.json",
ResponseSchema: "ocpp201/SetVariablesResponse.json",
Handler: handlers201.SetVariablesResultHandler{
Store: engine,
},
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions manager/store/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type ChargeStationSettingsStore interface {
UpdateChargeStationSettings(ctx context.Context, chargeStationId string, settings *ChargeStationSettings) error
LookupChargeStationSettings(ctx context.Context, chargeStationId string) (*ChargeStationSettings, error)
ListChargeStationSettings(ctx context.Context, pageSize int, previousChargeStationId string) ([]*ChargeStationSettings, error)
DeleteChargeStationSettings(ctx context.Context, chargeStationId string) error
}

type ChargeStationRuntimeDetails struct {
Expand Down
9 changes: 9 additions & 0 deletions manager/store/firestore/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func (s *Store) LookupChargeStationSettings(ctx context.Context, chargeStationId
}, nil
}

func (s *Store) DeleteChargeStationSettings(ctx context.Context, chargeStationId string) error {
csRef := s.client.Doc(fmt.Sprintf("ChargeStationSettings/%s", chargeStationId))
_, err := csRef.Delete(ctx)
if err != nil {
return err
}
return nil
}

func mapChargeStationSettings(csData map[string]*chargeStationSetting) map[string]*store.ChargeStationSetting {
var settings = make(map[string]*store.ChargeStationSetting)
for k, v := range csData {
Expand Down
7 changes: 7 additions & 0 deletions manager/store/inmemory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func (s *Store) UpdateChargeStationSettings(_ context.Context, chargeStationId s
return nil
}

func (s *Store) DeleteChargeStationSettings(_ context.Context, chargeStationId string) error {
s.Lock()
defer s.Unlock()
delete(s.chargeStationSettings, chargeStationId)
return nil
}

func (s *Store) LookupChargeStationSettings(_ context.Context, chargeStationId string) (*store.ChargeStationSettings, error) {
s.Lock()
defer s.Unlock()
Expand Down

0 comments on commit 13dd1ba

Please sign in to comment.