Skip to content

Can a message be provided for individual processing by oneself #360

@Kinrosslong

Description

@Kinrosslong

Hello, author. This project is wonderful for us. When we were developing OCPP CSMS based on this project, we found that the messages of CSMS could not be recorded separately by ourselves. For example, MongoDB was used to record all the message instructions of CSMS.

So we hope whether a method can be added in the server.go file. For example
type Server struct {
...
MessageLogger MessageLogger
}

type MessageLogger func(direction string, chargePointID string, messageType string, payload []byte)

Then add something similar in methods such as SendRequest(), SendResponse(), SendError(), and ocppMessageHandler()
if s.MessageLogger ! = nil {
s.MessageLogger("in", wsChannel.ID(), "request", data)
}

Then implement this method in files such as v16.go certral_system.go accordingly. For example, add the code in the certral_system.go file
func (cs *centralSystem) SetMessageLogger(logger func(direction, clientId, messageType string, payload []byte)) {
cs.server.SetMessageLogger(logger)
}
In v16.go, type CentralSystem interface {
...
SetMessageLogger(logger func(direction, clientId, messageType string, payload []byte))
}

Finally, we can handle our messages in central_system_sim.go. The storage of messages can be customized, for example, using the Monogodb database.
centralSystem.SetMessageLogger(func(direction, cpid, msgType string, payload []byte) {
fmt.Printf("direction: %s\ ncpID: %s\ nmsgType: %s\ nPayload: %s\n", direction, cpid, msgType, string(payload))
logEntry := OcppMessageLog{
Timestamp: time.Now().UTC(),
Direction: direction,
CPID: cpid,
MessageType: msgType,
RawMessage: string(payload),
}

go func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

if _, err := msgCollection.InsertOne(ctx, logEntry); err ! = nil {
log.Errorf("MongoDB write failed: %v", err)
}
} ()
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions