diff --git a/cs/cs.go b/cs/cs.go index b9a2412..4435b8c 100644 --- a/cs/cs.go +++ b/cs/cs.go @@ -3,6 +3,7 @@ package cs import ( "errors" "fmt" + "net" "net/http" "net/http/httputil" "strings" @@ -21,12 +22,13 @@ import ( type ChargePointRequestMetadata struct { ChargePointID string HTTPRequest *http.Request + Host string } // ChargePointMessageHandler handles the OCPP messages coming from the charger type ChargePointMessageHandler func(cprequest cpreq.ChargePointRequest, metadata ChargePointRequestMetadata) (cpresp.ChargePointResponse, error) -type ChargePointConnectionListener func(cpID string) +type ChargePointConnectionListener func(cpID string, host string) type CentralSystem interface { // Run the central system on the given port // and handles each incoming ChargepointRequest @@ -63,8 +65,8 @@ func New() CentralSystem { connChans: make(map[string]chan struct{}, 0), connsCount: make(map[string]int, 0), connsConnected: make(map[string]bool, 0), - connListener: func(cpID string) {}, - disconnListener: func(cpID string) {}, + connListener: func(cpID string, host string) {}, + disconnListener: func(cpID string, host string) {}, } } @@ -94,7 +96,12 @@ func (csys *centralSystem) Run(port string, cphandler ChargePointMessageHandler) func (csys *centralSystem) handleWebsocket(w http.ResponseWriter, r *http.Request, cphandler ChargePointMessageHandler) { log.Debug("Current WS connections map: %v", csys.conns) - cpID := strings.TrimPrefix(r.URL.Path, "/") + host, _, _ := net.SplitHostPort(r.Host) + if host == "" { + host = r.Host + } + cpID := strings.Trim(r.URL.Path, "/") + cpID = host + ":" + cpID rawReq, _ := httputil.DumpRequest(r, true) log.Debug("Raw WS request: %s", string(rawReq)) @@ -118,12 +125,12 @@ func (csys *centralSystem) handleWebsocket(w http.ResponseWriter, r *http.Reques csys.connMux.Unlock() log.Debug("Connected with %s", cpID) - go csys.connListener(cpID) + go csys.connListener(cpID, host) defer func() { conn.Close() log.Debug("Closed connection of: %s", cpID) - go csys.disconnListener(cpID) + go csys.disconnListener(cpID, host) csys.connMux.Lock() csys.connsCount[cpID]-- // if the same CP connected more times before we do the @@ -147,6 +154,7 @@ func (csys *centralSystem) handleWebsocket(w http.ResponseWriter, r *http.Reques cpresponse, err := cphandler(cprequest, ChargePointRequestMetadata{ ChargePointID: cpID, HTTPRequest: r, + Host: host, }) err = conn.SendResponse(req.MessageID, cpresponse, err) if err != nil { diff --git a/messages/req/actions.go b/messages/req/actions.go index d7a5f17..0adffa7 100644 --- a/messages/req/actions.go +++ b/messages/req/actions.go @@ -13,8 +13,8 @@ func FromActionName(action string) messages.Request { case "BootNotification": return &cpreq.BootNotification{} // TODO: DataTransfer comes from both the CS and CP - // case "DataTransfer": - // return &cpreq.DataTransfer{} + case "DataTransfer": + return &cpreq.DataTransfer{} case "DiagnosticsStatusNotification": return &cpreq.DiagnosticsStatusNotification{} case "FirmwareStatusNotification": diff --git a/messages/res/actions.go b/messages/res/actions.go index e511c9c..d9d1b8f 100644 --- a/messages/res/actions.go +++ b/messages/res/actions.go @@ -53,8 +53,8 @@ func FromActionName(action string) messages.Response { case "BootNotification": return &cpresp.BootNotification{} // TODO: DataTransfer comes from both the CS and CP - // case "DataTransfer": - // return &cpresp.DataTransfer{} + case "DataTransfer": + return &cpresp.DataTransfer{} case "DiagnosticsStatusNotification": return &cpresp.DiagnosticsStatusNotification{} case "FirmwareStatusNotification": diff --git a/ocpp_test.go b/ocpp_test.go index cc0f5a3..7ff6376 100644 --- a/ocpp_test.go +++ b/ocpp_test.go @@ -25,11 +25,11 @@ func Test_Connection(t *testing.T) { csysPort := ":5050" csys := cs.New() - csys.SetChargePointConnectionListener(func(cpID string) { + csys.SetChargePointConnectionListener(func(cpID string, host string) { // t.Log("cpoint connected: ", cpID) cpointConnected <- cpID }) - csys.SetChargePointDisconnectionListener(func(cpID string) { + csys.SetChargePointDisconnectionListener(func(cpID string, host string) { // t.Log("cpoint disconnected: ", cpID) cpointDisconnected <- cpID }) diff --git a/ws/conn.go b/ws/conn.go index b85bc00..8804b9c 100644 --- a/ws/conn.go +++ b/ws/conn.go @@ -182,6 +182,7 @@ func (c *Conn) ReadMessage() error { log.Debug("Received a message, raw: %v", string(messageBytes)) msg, err := UnmarshalMessage(messageBytes) if err != nil { + log.Error("unmarshal error=%v", err) return err } @@ -234,6 +235,7 @@ func (c *Conn) callToRequest(call *CallMessage) (messages.Request, ErrorCode) { } err = json.Unmarshal(originalPayload, req) if err != nil { + fmt.Println(err) return nil, FormationViolation } return req, Nil