Can a message be provided for individual processing by oneself
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) } } () })
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.
I have implemented the function. My implementation method is like this. Please correct me.
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.
@xBlaz3kx Thank you for answering every question carefully!