diff --git a/example/1.6/cs/central_system_sim.go b/example/1.6/cs/central_system_sim.go index 8eeee105..b0826fd6 100644 --- a/example/1.6/cs/central_system_sim.go +++ b/example/1.6/cs/central_system_sim.go @@ -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 diff --git a/example/2.0.1/csms/csms_sim.go b/example/2.0.1/csms/csms_sim.go index 57b88ec9..d3526b43 100644 --- a/example/2.0.1/csms/csms_sim.go +++ b/example/2.0.1/csms/csms_sim.go @@ -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) diff --git a/ocpp1.6/central_system.go b/ocpp1.6/central_system.go index 6dbfac4a..72bb5c3c 100644 --- a/ocpp1.6/central_system.go +++ b/ocpp1.6/central_system.go @@ -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) + } +} diff --git a/ocpp1.6/v16.go b/ocpp1.6/v16.go index 4fada30f..b2f46fca 100644 --- a/ocpp1.6/v16.go +++ b/ocpp1.6/v16.go @@ -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. diff --git a/ocpp2.0.1/csms.go b/ocpp2.0.1/csms.go index 6d7b1684..2924c084 100644 --- a/ocpp2.0.1/csms.go +++ b/ocpp2.0.1/csms.go @@ -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) + } +} diff --git a/ocpp2.0.1/v2.go b/ocpp2.0.1/v2.go index 3bc38ac3..bc55e053 100644 --- a/ocpp2.0.1/v2.go +++ b/ocpp2.0.1/v2.go @@ -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. diff --git a/ocppj/server.go b/ocppj/server.go index 2b0365da..befb4f04 100644 --- a/ocppj/server.go +++ b/ocppj/server.go @@ -24,6 +24,7 @@ type Server struct { invalidMessageHook InvalidMessageHook dispatcher ServerDispatcher RequestState ServerState + MessageHooks MessageHooks } type ClientHandler func(client ws.Channel) @@ -31,6 +32,7 @@ type RequestHandler func(client ws.Channel, request ocpp.Request, requestId stri 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, @@ -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 } @@ -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 @@ -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 @@ -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()) @@ -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 +}