Compare commits

2 Commits

4 changed files with 39 additions and 8 deletions

View File

@@ -85,8 +85,12 @@ app.get(
'/ocpp/:chargePointId', '/ocpp/:chargePointId',
upgradeWebSocket((c) => { upgradeWebSocket((c) => {
const chargePointId = c.req.param('chargePointId') const chargePointId = c.req.param('chargePointId')
if (!chargePointId) {
throw new Error('Missing chargePointId route param')
}
const connInfo = getConnInfo(c) const connInfo = getConnInfo(c)
return createOcppHandler(chargePointId, connInfo.remote.address) const requestedProtocol = c.req.header('sec-websocket-protocol')
return createOcppHandler(chargePointId, connInfo.remote.address, requestedProtocol)
}), }),
) )

View File

@@ -92,6 +92,26 @@ function sendCallError(
) )
} }
function pickOcppSubprotocol(
negotiatedProtocol?: string | null,
requestedHeader?: string | null,
) {
if (negotiatedProtocol && isSupportedOCPP(negotiatedProtocol)) {
return negotiatedProtocol
}
if (!requestedHeader) {
return null
}
const requestedProtocols = requestedHeader
.split(',')
.map((value) => value.trim())
.filter(Boolean)
return requestedProtocols.find((protocol) => isSupportedOCPP(protocol)) ?? null
}
/** /**
* Factory that produces a hono-ws event handler object for a single * Factory that produces a hono-ws event handler object for a single
* OCPP WebSocket connection. * OCPP WebSocket connection.
@@ -99,7 +119,11 @@ function sendCallError(
* Usage in route: * Usage in route:
* upgradeWebSocket((c) => createOcppHandler(c.req.param('chargePointId'), remoteAddr)) * upgradeWebSocket((c) => createOcppHandler(c.req.param('chargePointId'), remoteAddr))
*/ */
export function createOcppHandler(chargePointIdentifier: string, remoteAddr?: string) { export function createOcppHandler(
chargePointIdentifier: string,
remoteAddr?: string,
requestedProtocolHeader?: string,
) {
const ctx: OcppConnectionContext = { const ctx: OcppConnectionContext = {
chargePointIdentifier, chargePointIdentifier,
isRegistered: false, isRegistered: false,
@@ -107,14 +131,14 @@ export function createOcppHandler(chargePointIdentifier: string, remoteAddr?: st
return { return {
onOpen(_evt: Event, ws: WSContext) { onOpen(_evt: Event, ws: WSContext) {
const subProtocol = ws.protocol ?? 'unknown' const subProtocol = pickOcppSubprotocol(ws.protocol, requestedProtocolHeader)
if (!isSupportedOCPP(subProtocol)) { if (!subProtocol) {
ws.close(1002, 'Unsupported subprotocol') ws.close(1002, 'Unsupported subprotocol')
return return
} }
ocppConnections.set(chargePointIdentifier, ws) ocppConnections.set(chargePointIdentifier, ws)
console.log( console.log(
`[OCPP] ${chargePointIdentifier} connected` + `[OCPP] ${chargePointIdentifier} connected (${subProtocol})` +
(remoteAddr ? ` from ${remoteAddr}` : ''), (remoteAddr ? ` from ${remoteAddr}` : ''),
) )
}, },

View File

@@ -1 +1,2 @@
#define MG_ARCH MG_ARCH_ESP32 #define MG_ARCH MG_ARCH_ESP32
#define MG_TLS MG_TLS_MBED // Use ESP-IDF built-in mbedTLS for WSS support

View File

@@ -2,10 +2,12 @@
// OCPP 1.6-J: MOcppMongooseClient will append "/<CFG_CP_IDENTIFIER>" to this URL. // OCPP 1.6-J: MOcppMongooseClient will append "/<CFG_CP_IDENTIFIER>" to this URL.
// For local dev: ws://<host>:3001/ocpp // For local dev: ws://<host>:3001/ocpp
// For production: ws://csms.helios.bh8.ga:8180/steve/websocket/CentralSystemService // For production: ws://csms.helios.bh8.ga:8180/steve/websocket/CentralSystemService
#define CFG_OCPP_BACKEND "ws://csms.helios.bh8.ga:8180/steve/websocket/CentralSystemService" #define CFG_OCPP_BACKEND "wss://csms.uniiem.com/ocpp"
#define CFG_CP_IDENTIFIER "CQWU_HHB_0001" // #define CFG_CP_IDENTIFIER "CQWU_HHB_0001"
#define CFG_CP_IDENTIFIER "ICP_906A28"
#define CFG_CB_SERIAL "REDAone_prototype00" #define CFG_CB_SERIAL "REDAone_prototype00"
#define CFG_CP_FW_VERSION "1.0.0" #define CFG_CP_FW_VERSION "1.0.0"
#define CFG_CP_MODAL "Helios DA One" #define CFG_CP_MODAL "Helios DA One"
#define CFG_CP_VENDOR "RayineElec" #define CFG_CP_VENDOR "RayineElec"
#define CFG_AUTHORIZATIONKEY "my_secret_key" // #define CFG_AUTHORIZATIONKEY "my_secret_key"
#define CFG_AUTHORIZATIONKEY nullptr