-
Notifications
You must be signed in to change notification settings - Fork 23
/
call_maker.go
43 lines (36 loc) · 1.29 KB
/
call_maker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: Apache-2.0
package handlers
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/thoughtworks/maeve-csms/manager/ocpp"
"github.com/thoughtworks/maeve-csms/manager/transport"
"golang.org/x/exp/slog"
"reflect"
)
// OcppCallMaker is an implementation of the CallMaker interface for a specific set of OCPP messages.
type OcppCallMaker struct {
Emitter transport.Emitter // used to send the message to the charge station
OcppVersion transport.OcppVersion // identifies the OCPP version that the messages are for
Actions map[reflect.Type]string // the OCPP Action associated with a specific ocpp.Request object
}
func (b OcppCallMaker) Send(ctx context.Context, chargeStationId string, request ocpp.Request) error {
action, ok := b.Actions[reflect.TypeOf(request)]
if !ok {
return fmt.Errorf("unknown request type: %T", request)
}
requestBytes, err := json.Marshal(request)
if err != nil {
return err
}
msg := &transport.Message{
MessageType: transport.MessageTypeCall,
MessageId: uuid.New().String(),
Action: action,
RequestPayload: requestBytes,
}
slog.Info("sending message", "action", msg.Action, "chargeStationId", chargeStationId)
return b.Emitter.Emit(ctx, b.OcppVersion, chargeStationId, msg)
}