ocpp-go icon indicating copy to clipboard operation
ocpp-go copied to clipboard

Can a message be provided for individual processing by oneself

Open Kinrosslong opened this issue 8 months ago • 3 comments

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) } } () })

Kinrosslong avatar Apr 29 '25 01:04 Kinrosslong

If you want to contribute to the project, you can do so by opening a Pull Request.

Im pretty sure you can add Hooks to the OCPPJ server to process incoming and outgoing requests to achieve your desired functionality.

xBlaz3kx avatar Apr 29 '25 07:04 xBlaz3kx

I have implemented the function. My implementation method is like this. Please correct me.

Image

Image

Image

Image

Image

Image

Image

Image

In this way, we can customize the processing of the message. For example, store it in the database or forward the message to other places.

Kinrosslong avatar Apr 29 '25 09:04 Kinrosslong

@xBlaz3kx Thank you for answering every question carefully!

AndrewYEEE avatar May 18 '25 01:05 AndrewYEEE