Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/1.6/cs/central_system_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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) {
log.Infof("direction:%s \n cpid:%s \n msgType: %s \n payload: %s\n", direction, chargePointID, msgType, string(payload))
})
ocppj.SetLogger(log.WithField("logger", "ocppj"))
ws.SetLogger(log.WithField("logger", "websocket"))
// Run central system
Expand Down
4 changes: 4 additions & 0 deletions example/2.0.1/csms/csms_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ func main() {
log.WithField("client", chargingStation.ID()).Info("charging station disconnected")
delete(handler.chargingStations, chargingStation.ID())
})
// set message hook
csms.SetMessageHooks(func(direction, chargePointID, msgType string, payload []byte) {
log.Infof("direction:%s \n cpid:%s \n msgType: %s \n payload: %s\n", direction, chargePointID, msgType, string(payload))
})
ocppj.SetLogger(log)
// Run CSMS
log.Infof("starting CSMS on port %v", listenPort)
Expand Down
7 changes: 7 additions & 0 deletions ocpp1.6/central_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,10 @@ func (cs *centralSystem) handleCanceledRequest(chargePointID string, request ocp
cs.error(err)
}
}

// SetMessageHooks sets the hooks for logging incoming and outgoing messages.
func (cs *centralSystem) SetMessageHooks(message func(direction, clientId, messageType string, payload []byte)) {
if message != nil {
cs.server.SetMessageHooks(message)
}
}
3 changes: 3 additions & 0 deletions ocpp1.6/v16.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ type CentralSystem interface {
Stop()
// Errors returns a channel for error messages. If it doesn't exist it es created.
Errors() <-chan error

// SetMessageHooks sets a function that will be called whenever a message is received or sent.
SetMessageHooks(logger func(direction, chargePointID, messageType string, payload []byte))
}

// Creates a new OCPP 1.6 central system.
Expand Down
7 changes: 7 additions & 0 deletions ocpp2.0.1/csms.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,10 @@ func (cs *csms) handleCanceledRequest(chargePointID string, request ocpp.Request
cs.error(err)
}
}

// SetMessageHooks sets the hooks for logging incoming and outgoing messages.
func (cs *csms) SetMessageHooks(message func(direction, clientId, messageType string, payload []byte)) {
if message != nil {
cs.server.SetMessageHooks(message)
}
}
3 changes: 3 additions & 0 deletions ocpp2.0.1/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ type CSMS interface {
Stop()
// Errors returns a channel for error messages. If it doesn't exist it es created.
Errors() <-chan error

// SetMessageHooks sets a function that will be called whenever a message is received or sent.
SetMessageHooks(logger func(direction, chargePointID, messageType string, payload []byte))
}

// Creates a new OCPP 2.0 CSMS.
Expand Down
20 changes: 20 additions & 0 deletions ocppj/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ type Server struct {
invalidMessageHook InvalidMessageHook
dispatcher ServerDispatcher
RequestState ServerState
MessageHooks MessageHooks
}

type ClientHandler func(client ws.Channel)
type RequestHandler func(client ws.Channel, request ocpp.Request, requestId string, action string)
type ResponseHandler func(client ws.Channel, response ocpp.Response, requestId string)
type ErrorHandler func(client ws.Channel, err *ocpp.Error, details interface{})
type InvalidMessageHook func(client ws.Channel, err *ocpp.Error, rawJson string, parsedFields []interface{}) *ocpp.Error
type MessageHooks func(direction string, chargePointID string, messageType string, payload []byte)

// Creates a new Server endpoint.
// Requires a a websocket server. Optionally a structure for queueing/dispatching requests,
Expand Down Expand Up @@ -173,6 +175,9 @@ func (s *Server) SendRequest(clientID string, request ocpp.Request) error {
log.Errorf("error dispatching request [%s, %s] to %s: %v", call.UniqueId, call.Action, clientID, err)
return err
}
if s.MessageHooks != nil {
s.MessageHooks("in", clientID, "request", jsonMessage)
}
log.Debugf("enqueued CALL [%s, %s] for %s", call.UniqueId, call.Action, clientID)
return nil
}
Expand Down Expand Up @@ -200,6 +205,10 @@ func (s *Server) SendResponse(clientID string, requestId string, response ocpp.R
log.Errorf("error sending response [%s] to %s: %v", callResult.GetUniqueId(), clientID, err)
return ocpp.NewError(GenericError, err.Error(), requestId)
}

if s.MessageHooks != nil {
s.MessageHooks("out", clientID, "response", jsonMessage)
}
log.Debugf("sent CALL RESULT [%s] for %s", callResult.GetUniqueId(), clientID)
log.Debugf("sent JSON message to %s: %s", clientID, string(jsonMessage))
return nil
Expand All @@ -226,6 +235,9 @@ func (s *Server) SendError(clientID string, requestId string, errorCode ocpp.Err
log.Errorf("error sending response error [%s] to %s: %v", callError.UniqueId, clientID, err)
return ocpp.NewError(GenericError, err.Error(), requestId)
}
if s.MessageHooks != nil {
s.MessageHooks("out", clientID, "error", jsonMessage)
}
log.Debugf("sent CALL ERROR [%s] for %s", callError.UniqueId, clientID)
log.Debugf("sent JSON message to %s: %s", clientID, string(jsonMessage))
return nil
Expand All @@ -237,6 +249,9 @@ func (s *Server) ocppMessageHandler(wsChannel ws.Channel, data []byte) error {
log.Error(err)
return err
}
if s.MessageHooks != nil {
s.MessageHooks("in", wsChannel.ID(), "request", data)
}
log.Debugf("received JSON message from %s: %s", wsChannel.ID(), string(data))
// Get pending requests for client
pending := s.RequestState.GetClientState(wsChannel.ID())
Expand Down Expand Up @@ -337,3 +352,8 @@ func (s *Server) onClientDisconnected(ws ws.Channel) {
s.disconnectedClientHandler(ws)
}
}

// SetMessageHooks sets the message hooks for the server.
func (s *Server) SetMessageHooks(msg MessageHooks) {
s.MessageHooks = msg
}