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
20 changes: 14 additions & 6 deletions cs/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cs
import (
"errors"
"fmt"
"net"
"net/http"
"net/http/httputil"
"strings"
Expand All @@ -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
Expand Down Expand Up @@ -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) {},
}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions messages/req/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions messages/res/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions ocpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 2 additions & 0 deletions ws/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down