Reputation: 11
import { WebSocketServer } from "ws";
import { v4 as uuidv4 } from "uuid";
import express from "express";
const app = express();
class CSM {
constructor() {}
get_current_time() {
return new Date().toISOString();
}
init() {
this.port = 8090;
this.ws = new WebSocketServer({ port: this.port });
this.ws.on("listening", () => {
console.log(`Listen on port ${this.port}`);
});
this.ws.on("connection", (ws) => {
ws.on("message", (data) => {
console.log("%s", data);
try {
const [MessageTypeId, UniqueId, Action, Payload] = JSON.parse(
data.toString()
);
switch (Action) {
case "BootNotification":
ws.send([
3,
uuidv4(),
Action,
{
status: "Accepted",
currentTime: this.get_current_time(),
interval: 14400,
},
]);
ws.send([
2,
uuidv4(),
"RemoteStartTransaction",
{
connectorId: 1,
idTag: "738961FE",
},
]);
break;
case "Heartbeat":
ws.send([
3,
uuidv4(),
Action,
{ currentTime: this.get_current_time() },
]);
break;
case "StatusNotification":
ws.send([3, uuidv4(), Action, {}]);
break;
default:
console.log("%", data);
}
} catch (error) {
console.error("Error: ", error);
}
});
ws.on("error", (error) => {
console.log(error);
ws.send([
4,
uuidv4(),
"BootNotification",
{
status: "Accepted",
currentTime: this.get_current_time(),
interval: 14400,
},
]);
});
});
}
}
const csm = new CSM();
csm.init();
Error message:
[2,"170870499378","BootNotification",{"chargePointModel":"c8","chargePointSerialNumber":"SN1120620007","chargePointVendor":"crev","firmwareVersion":"V00.49.15"}] RangeError: Invalid WebSocket frame: invalid payload length 1 at Receiver.getInfo (C:\Users\Fabian\Desktop\ocpp\node_modules.pnpm\[email protected]\node_modules\ws\lib\receiver.js:303:28) at Receiver.startLoop (C:\Users\Fabian\Desktop\ocpp\node_modules.pnpm\[email protected]\node_modules\ws\lib\receiver.js:159:16) at Receiver._write (C:\Users\Fabian\Desktop\ocpp\node_modules.pnpm\[email protected]\node_modules\ws\lib\receiver.js:98:10) at writeOrBuffer (node:internal/streams/writable:399:12) at _write (node:internal/streams/writable:340:10) at Writable.write (node:internal/streams/writable:344:10) at Socket.socketOnData (C:\Users\Fabian\Desktop\ocpp\node_modules.pnpm\[email protected]\node_modules\ws\lib\websocket.js:1303:35) at Socket.emit (node:events:514:28) at addChunk (node:internal/streams/readable:343:12) at readableAddChunk (node:internal/streams/readable:316:9) { code: 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH', [Symbol(status-code)]: 1002
I'm trying to establish communication with an EV charger via OCPP.16 but an error occurs when trying to establish the connection. I'm not sure how to reset or regenerate it so that the connection can be established.
Upvotes: 1
Views: 363
Reputation: 21
I see that You begin StartRemoteTransaction inmediately after BootNotification, but, I know that first you need to handle StatusNotification's after BootNotification, and later you can send RemoteStartTrasaction (RST) message.
Now you need to handle Authorize (A) and StartTransaction (ST) too, I don not know if you implemented, because I can see them (A and ST).
This is because the RST use this tow Messages/Events for do its job.
Upvotes: 0