Skip to content

Conversation

Kinrosslong
Copy link

@Kinrosslong Kinrosslong commented May 14, 2025

Proposed changes

Provide a message processing method. Databases or message forwarding, etc. can be used。

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply

  • [] Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of
them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before
merging your code.

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

We want to record the messages of OCPP through a database such as mongodb. Or forward some instruction messages such as MeterValues and StatusNotification to the mobile terminal for display.

Copy link
Owner

@lorenzodonini lorenzodonini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I can accept it after the feedback was implemented as well.

ocppj/server.go Outdated
@@ -41,7 +43,7 @@ type InvalidMessageHook func(client ws.Channel, err *ocpp.Error, rawJson string,
// s := ocppj.NewServer(ws.NewServer(), nil, nil)
//
// The dispatcher's associated ClientState will be set during initialization.
func NewServer(wsServer ws.Server, dispatcher ServerDispatcher, stateHandler ServerState, profiles ...*ocpp.Profile) *Server {
func NewServer(wsServer ws.Server, dispatcher ServerDispatcher, stateHandler ServerState, msgHooks MessageHooks, profiles ...*ocpp.Profile) *Server {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change and several custom implementations may be affected. I believe the added SetMessageHooks setter is good enough, so we don't have to also include it as a parameter in the constructor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,have been modified。

@@ -231,6 +232,10 @@ func main() {
log.WithField("client", chargePoint.ID()).Info("charge point disconnected")
delete(handler.chargePoints, chargePoint.ID())
})
// set message hook
centralSystem.SetMessageHooks(func(direction, chargePointID, msgType string, payload []byte) {
fmt.Printf("direction:%s \n cpid:%s \n msgType: %s \n payload: %s\n", direction, chargePointID, msgType, string(payload))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chore: this should invoke the logger instead of fmt.Printf

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,have been modified。

龙进 added 2 commits June 26, 2025 09:43
2.It was modified to include it as a parameter in unnecessary constructors as well
@@ -66,7 +66,7 @@ func NewServer(wsServer ws.Server, dispatcher ServerDispatcher, stateHandler Ser
for _, profile := range profiles {
s.AddProfile(profile)
}
s.MessageHooks = msgHooks
//s.MessageHooks = msgHooks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed entirely


// SetMessageHooks sets the hooks for logging incoming and outgoing messages.
func (cs *centralSystem) SetMessageHooks(message func(direction, clientId, messageType string, payload []byte)) {
cs.server.SetMessageHooks(message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a nil check would be nice to prevent crashing in case someone decides to pass in a nil function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants