-
Notifications
You must be signed in to change notification settings - Fork 30
/
custom.js
97 lines (97 loc) · 3.82 KB
/
custom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
if(!process.env.WS_CONNECT_URL) {
throw new Error("env variable WS_CONNECT_URL not set!");
}
let cp, heartbeatTimer;
try {
// WebSocket Connect (no OCPP)
cp = await connect(process.env.WS_CONNECT_URL);
// start a web-listener and wait for GET on /stop
const webserver = cp.startListener(8080, '0.0.0.0', {'admin': 'secret'});
webserver.get('/stop', (req, res) => {
res.send('stopped.');
webserver.terminate();
});
// typical startup OCPP
const bootResp = await cp.sendBootnotification(
{chargePointVendor: "vendor", chargePointModel: "1"});
await cp.sendHeartbeat();
heartbeatTimer = setInterval(() => cp.sendHeartbeat(),
bootResp.interval > 0 ? bootResp.interval * 1000 : 60000);
await cp.sendStatusNotification(
{connectorId: 0, errorCode: "NoError", status: "Available"});
await cp.sendStatusNotification(
{connectorId: 1, errorCode: "NoError", status: "Available"});
// register code for GetDiagnostics, UpdateFirmware, Reset
cp.answerGetDiagnostics(async (request) => {
const fileName = "foo." + new Date().toISOString() + ".txt";
cp.sendResponse(request.uniqueId, {fileName});
await cp.sendDiagnosticsStatusNotification({status: "Idle"});
await cp.sleep(5000);
await cp.sendDiagnosticsStatusNotification({status: "Uploading"});
await cp.ftpUploadDummyFile(request.payload.location, fileName);
await cp.sendDiagnosticsStatusNotification({status: "Uploaded"});
});
cp.answerUpdateFirmware( async (request) => {
cp.sendResponse(request.uniqueId, {});
await cp.sendFirmwareStatusNotification({status: "Idle"});
await cp.sleep(5000);
await cp.sendFirmwareStatusNotification({status: "Downloading"});
const file = await cp.ftpDownload(request.payload.location);
cp.log("file downloaded to: " + file);
await cp.sendFirmwareStatusNotification({status: "Downloaded"});
await cp.sleep(5000);
await cp.sendFirmwareStatusNotification({status: "Installing"});
await cp.sleep(5000);
await cp.sendFirmwareStatusNotification({status: "Installed"});
});
cp.answerReset(async (request) => {
cp.sendResponse(request.uniqueId, {status: "Accepted"});
cp.log("RESET ***boing-boing-boing*** " + request.payload.type);
await cp.sendBootnotification({chargePointVendor: "vendor", chargePointModel: "1"});
});
// Typical charging session
let meterCount = 1377;
for(let chargeCount = 0; chargeCount < 1; chargeCount++) {
const authResp = await cp.sendAuthorize({idTag: "ccc"});
if (authResp.idTagInfo.status == 'Accepted') {
await cp.sendStatusNotification(
{connectorId: 1, errorCode: "NoError", status: "Preparing"});
cp.transaction = await cp.startTransaction({
connectorId: 1,
idTag: "ccc",
meterStart: meterCount,
timestamp: new Date().toISOString()
});
meterCount += 10;
await cp.sendStatusNotification(
{connectorId: 1, errorCode: "NoError", status: "Charging"});
for (let chargeMeterCount = 0; chargeMeterCount < 1; chargeMeterCount++) {
await cp.meterValues({
connectorId: 1,
transactionId: cp.transaction.transactionId,
meterValue: [{
timestamp: new Date().toISOString(),
sampledValue: [{value: meterCount}]
}]
});
meterCount += 10;
await cp.sleep(5000);
}
await cp.stopTransaction({
transactionId: cp.transaction.transactionId,
meterStop: meterCount,
timestamp: new Date().toISOString()
});
meterCount += 10;
await cp.sendStatusNotification(
{connectorId: 1, errorCode: "NoError", status: "Finishing"});
await cp.sendStatusNotification(
{connectorId: 1, errorCode: "NoError", status: "Available"});
}
}
} catch (err) {
console.log(err);
} finally {
clearInterval(heartbeatTimer);
cp.close();
}