kirby-ocpp 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +134 -95
- package/THIRD_PARTY_NOTICES.md +6 -2
- package/bin/kirby-ocpp.js +55 -0
- package/package.json +22 -44
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -72
- package/dist/config.d.ts +0 -2
- package/dist/config.js +0 -53
- package/dist/ocpp/client.d.ts +0 -59
- package/dist/ocpp/client.js +0 -299
- package/dist/ocpp/schema-validator.d.ts +0 -15
- package/dist/ocpp/schema-validator.js +0 -75
- package/dist/ocpp/schemas/json/Authorize.json +0 -16
- package/dist/ocpp/schemas/json/AuthorizeResponse.json +0 -40
- package/dist/ocpp/schemas/json/BootNotification.json +0 -49
- package/dist/ocpp/schemas/json/BootNotificationResponse.json +0 -30
- package/dist/ocpp/schemas/json/CancelReservation.json +0 -15
- package/dist/ocpp/schemas/json/CancelReservationResponse.json +0 -20
- package/dist/ocpp/schemas/json/ChangeAvailability.json +0 -24
- package/dist/ocpp/schemas/json/ChangeAvailabilityResponse.json +0 -21
- package/dist/ocpp/schemas/json/ChangeConfiguration.json +0 -21
- package/dist/ocpp/schemas/json/ChangeConfigurationResponse.json +0 -22
- package/dist/ocpp/schemas/json/ClearCache.json +0 -8
- package/dist/ocpp/schemas/json/ClearCacheResponse.json +0 -20
- package/dist/ocpp/schemas/json/ClearChargingProfile.json +0 -27
- package/dist/ocpp/schemas/json/ClearChargingProfileResponse.json +0 -20
- package/dist/ocpp/schemas/json/DataTransfer.json +0 -23
- package/dist/ocpp/schemas/json/DataTransferResponse.json +0 -25
- package/dist/ocpp/schemas/json/DiagnosticsStatusNotification.json +0 -22
- package/dist/ocpp/schemas/json/DiagnosticsStatusNotificationResponse.json +0 -8
- package/dist/ocpp/schemas/json/FirmwareStatusNotification.json +0 -25
- package/dist/ocpp/schemas/json/FirmwareStatusNotificationResponse.json +0 -8
- package/dist/ocpp/schemas/json/GetCompositeSchedule.json +0 -27
- package/dist/ocpp/schemas/json/GetCompositeScheduleResponse.json +0 -79
- package/dist/ocpp/schemas/json/GetConfiguration.json +0 -16
- package/dist/ocpp/schemas/json/GetConfigurationResponse.json +0 -40
- package/dist/ocpp/schemas/json/GetDiagnostics.json +0 -30
- package/dist/ocpp/schemas/json/GetDiagnosticsResponse.json +0 -13
- package/dist/ocpp/schemas/json/GetLocalListVersion.json +0 -8
- package/dist/ocpp/schemas/json/GetLocalListVersionResponse.json +0 -15
- package/dist/ocpp/schemas/json/Heartbeat.json +0 -8
- package/dist/ocpp/schemas/json/HeartbeatResponse.json +0 -16
- package/dist/ocpp/schemas/json/MeterValues.json +0 -151
- package/dist/ocpp/schemas/json/MeterValuesResponse.json +0 -8
- package/dist/ocpp/schemas/json/RemoteStartTransaction.json +0 -127
- package/dist/ocpp/schemas/json/RemoteStartTransactionResponse.json +0 -20
- package/dist/ocpp/schemas/json/RemoteStopTransaction.json +0 -15
- package/dist/ocpp/schemas/json/RemoteStopTransactionResponse.json +0 -20
- package/dist/ocpp/schemas/json/ReserveNow.json +0 -33
- package/dist/ocpp/schemas/json/ReserveNowResponse.json +0 -23
- package/dist/ocpp/schemas/json/Reset.json +0 -20
- package/dist/ocpp/schemas/json/ResetResponse.json +0 -20
- package/dist/ocpp/schemas/json/SendLocalList.json +0 -68
- package/dist/ocpp/schemas/json/SendLocalListResponse.json +0 -22
- package/dist/ocpp/schemas/json/SetChargingProfile.json +0 -124
- package/dist/ocpp/schemas/json/SetChargingProfileResponse.json +0 -21
- package/dist/ocpp/schemas/json/StartTransaction.json +0 -32
- package/dist/ocpp/schemas/json/StartTransactionResponse.json +0 -44
- package/dist/ocpp/schemas/json/StatusNotification.json +0 -70
- package/dist/ocpp/schemas/json/StatusNotificationResponse.json +0 -8
- package/dist/ocpp/schemas/json/StopTransaction.json +0 -177
- package/dist/ocpp/schemas/json/StopTransactionResponse.json +0 -37
- package/dist/ocpp/schemas/json/TriggerMessage.json +0 -27
- package/dist/ocpp/schemas/json/TriggerMessageResponse.json +0 -21
- package/dist/ocpp/schemas/json/UnlockConnector.json +0 -15
- package/dist/ocpp/schemas/json/UnlockConnectorResponse.json +0 -21
- package/dist/ocpp/schemas/json/UpdateFirmware.json +0 -27
- package/dist/ocpp/schemas/json/UpdateFirmwareResponse.json +0 -8
- package/dist/ocpp/types.d.ts +0 -106
- package/dist/ocpp/types.js +0 -1
- package/dist/station/authorization.d.ts +0 -20
- package/dist/station/authorization.js +0 -78
- package/dist/station/charging-profile-registry.d.ts +0 -18
- package/dist/station/charging-profile-registry.js +0 -125
- package/dist/station/configuration-commands.d.ts +0 -11
- package/dist/station/configuration-commands.js +0 -29
- package/dist/station/configuration-registry.d.ts +0 -13
- package/dist/station/configuration-registry.js +0 -39
- package/dist/station/configuration.d.ts +0 -7
- package/dist/station/configuration.js +0 -88
- package/dist/station/connector-registry.d.ts +0 -25
- package/dist/station/connector-registry.js +0 -89
- package/dist/station/connector-status.d.ts +0 -7
- package/dist/station/connector-status.js +0 -66
- package/dist/station/local-auth-list.d.ts +0 -8
- package/dist/station/local-auth-list.js +0 -31
- package/dist/station/maintenance-lifecycle.d.ts +0 -25
- package/dist/station/maintenance-lifecycle.js +0 -110
- package/dist/station/meter-value-scheduler.d.ts +0 -21
- package/dist/station/meter-value-scheduler.js +0 -78
- package/dist/station/metering.d.ts +0 -15
- package/dist/station/metering.js +0 -138
- package/dist/station/ocpp-message.d.ts +0 -2
- package/dist/station/ocpp-message.js +0 -9
- package/dist/station/offline-transaction-sync.d.ts +0 -29
- package/dist/station/offline-transaction-sync.js +0 -99
- package/dist/station/payload.d.ts +0 -4
- package/dist/station/payload.js +0 -12
- package/dist/station/persistence.d.ts +0 -28
- package/dist/station/persistence.js +0 -206
- package/dist/station/reservation-rules.d.ts +0 -3
- package/dist/station/reservation-rules.js +0 -18
- package/dist/station/reservation-scheduler.d.ts +0 -11
- package/dist/station/reservation-scheduler.js +0 -33
- package/dist/station/smart-charging.d.ts +0 -45
- package/dist/station/smart-charging.js +0 -251
- package/dist/station/status-scheduler.d.ts +0 -16
- package/dist/station/status-scheduler.js +0 -58
- package/dist/station/transaction-message-retry.d.ts +0 -3
- package/dist/station/transaction-message-retry.js +0 -7
- package/dist/station/transaction-rules.d.ts +0 -7
- package/dist/station/transaction-rules.js +0 -34
- package/dist/station/trigger-message.d.ts +0 -2
- package/dist/station/trigger-message.js +0 -11
- package/dist/station-store.d.ts +0 -41
- package/dist/station-store.js +0 -104
- package/dist/station.d.ts +0 -113
- package/dist/station.js +0 -1204
- package/dist/tui.d.ts +0 -96
- package/dist/tui.js +0 -1656
- package/docs/OCPP_1.6J_CONFORMANCE.md +0 -61
package/dist/station.js
DELETED
|
@@ -1,1204 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "node:events";
|
|
2
|
-
import { OcppClient } from "./ocpp/client.js";
|
|
3
|
-
import { StationStore } from "./station-store.js";
|
|
4
|
-
import { authorizeLocally, isAcceptedIdTagInfo, normalizeIdTagInfo, serializeIdTagInfo } from "./station/authorization.js";
|
|
5
|
-
import { faultErrorCode, nextConnectorStatus, statusAfterAvailabilityChange, statusAfterFaultClear, statusAfterReservationClear } from "./station/connector-status.js";
|
|
6
|
-
import { ConnectorRegistry } from "./station/connector-registry.js";
|
|
7
|
-
import { ChargingProfileRegistry } from "./station/charging-profile-registry.js";
|
|
8
|
-
import { changeConfiguration, getConfiguration } from "./station/configuration-commands.js";
|
|
9
|
-
import { ConfigurationRegistry } from "./station/configuration-registry.js";
|
|
10
|
-
import { buildMeterValue, isValidSampledDataConfiguration, readConfiguredMeasurands } from "./station/metering.js";
|
|
11
|
-
import { MeterValueScheduler } from "./station/meter-value-scheduler.js";
|
|
12
|
-
import { ReservationScheduler } from "./station/reservation-scheduler.js";
|
|
13
|
-
import { buildPersistedStationState, restorePersistedStationState } from "./station/persistence.js";
|
|
14
|
-
import { readNumber, readObject, readOptionalString, readString } from "./station/payload.js";
|
|
15
|
-
import { applyLocalAuthListUpdate } from "./station/local-auth-list.js";
|
|
16
|
-
import { MaintenanceLifecycle } from "./station/maintenance-lifecycle.js";
|
|
17
|
-
import { describeOcppMessage } from "./station/ocpp-message.js";
|
|
18
|
-
import { StatusNotificationScheduler } from "./station/status-scheduler.js";
|
|
19
|
-
import { canTriggerMessage } from "./station/trigger-message.js";
|
|
20
|
-
import { reserveNowRejection } from "./station/reservation-rules.js";
|
|
21
|
-
import { canRemoteStart, startTransactionRejection, statusAfterStopTransaction } from "./station/transaction-rules.js";
|
|
22
|
-
import { readChargingProfile } from "./station/smart-charging.js";
|
|
23
|
-
import { isTransactionMessage, retryDelay } from "./station/transaction-message-retry.js";
|
|
24
|
-
import { localTransactionId, OfflineTransactionSync, pendingStopPayload } from "./station/offline-transaction-sync.js";
|
|
25
|
-
export class Station extends EventEmitter {
|
|
26
|
-
config;
|
|
27
|
-
client;
|
|
28
|
-
state = {
|
|
29
|
-
connected: false,
|
|
30
|
-
booted: false,
|
|
31
|
-
registrationStatus: "Unknown",
|
|
32
|
-
connectorStatus: "Available",
|
|
33
|
-
availability: "Operative",
|
|
34
|
-
localListVersion: 0,
|
|
35
|
-
meterWh: 0,
|
|
36
|
-
connectors: [],
|
|
37
|
-
diagnosticsStatus: "Idle",
|
|
38
|
-
firmwareStatus: "Idle"
|
|
39
|
-
};
|
|
40
|
-
heartbeatTimer;
|
|
41
|
-
resetTimer;
|
|
42
|
-
bootRetryTimer;
|
|
43
|
-
reconnectTimer;
|
|
44
|
-
reconnectAttempts = 0;
|
|
45
|
-
manuallyDisconnected = true;
|
|
46
|
-
store;
|
|
47
|
-
configuration;
|
|
48
|
-
localAuthorizationList = new Map();
|
|
49
|
-
authorizationCache = new Map();
|
|
50
|
-
connectors;
|
|
51
|
-
statusScheduler;
|
|
52
|
-
meterValueScheduler;
|
|
53
|
-
reservationScheduler;
|
|
54
|
-
maintenance;
|
|
55
|
-
chargingProfiles = new ChargingProfileRegistry();
|
|
56
|
-
offlineTransactions;
|
|
57
|
-
constructor(config) {
|
|
58
|
-
super();
|
|
59
|
-
this.config = config;
|
|
60
|
-
this.client = new OcppClient(config.centralSystemUrl, config.chargePointId, {
|
|
61
|
-
subprotocol: config.webSocketSubprotocol,
|
|
62
|
-
pingIntervalSeconds: config.webSocketPingIntervalSeconds,
|
|
63
|
-
tlsRejectUnauthorized: config.tlsRejectUnauthorized,
|
|
64
|
-
tlsCaFile: config.tlsCaFile,
|
|
65
|
-
tlsCertFile: config.tlsCertFile,
|
|
66
|
-
tlsKeyFile: config.tlsKeyFile,
|
|
67
|
-
tlsServerName: config.tlsServerName
|
|
68
|
-
});
|
|
69
|
-
this.configuration = new ConfigurationRegistry(config);
|
|
70
|
-
this.connectors = new ConnectorRegistry(config.connectorId, config.connectorCount);
|
|
71
|
-
this.connectors.setOnChange(() => this.refreshConnectorSnapshot());
|
|
72
|
-
this.configuration.setValue("NumberOfConnectors", String(this.connectors.size));
|
|
73
|
-
this.statusScheduler = new StatusNotificationScheduler({
|
|
74
|
-
minimumSeconds: () => this.configuration.integer("MinimumStatusDuration", 0),
|
|
75
|
-
currentStatus: (connectorId) => this.connectors.find(connectorId)?.status,
|
|
76
|
-
send: (connectorId, status) => void this.runSafely(() => this.sendStatusNotification(connectorId, status))
|
|
77
|
-
});
|
|
78
|
-
this.meterValueScheduler = new MeterValueScheduler({
|
|
79
|
-
sampleIntervalSeconds: () => this.configuration.integer("MeterValueSampleInterval", 0),
|
|
80
|
-
clockAlignedIntervalSeconds: () => this.configuration.integer("ClockAlignedDataInterval", 0),
|
|
81
|
-
isConnected: () => this.state.connected,
|
|
82
|
-
activeConnectorIds: () => this.connectors.all()
|
|
83
|
-
.filter((connector) => connector.transactionId && connector.status === "Charging")
|
|
84
|
-
.map((connector) => connector.id),
|
|
85
|
-
isPeriodicConnectorActive: (connectorId) => {
|
|
86
|
-
const connector = this.connectors.find(connectorId);
|
|
87
|
-
return Boolean(connector?.transactionId && connector.status === "Charging");
|
|
88
|
-
},
|
|
89
|
-
sendPeriodic: (connectorId, deltaWh) => this.sendMeterValues(connectorId, "Sample.Periodic", deltaWh, this.configuration.measurands("MeterValuesSampledData")),
|
|
90
|
-
sendClockAligned: (connectorIds, deltaWh) => this.sendClockAlignedMeterValues(connectorIds, deltaWh),
|
|
91
|
-
runSafely: (task) => void this.runSafely(task)
|
|
92
|
-
});
|
|
93
|
-
this.reservationScheduler = new ReservationScheduler({
|
|
94
|
-
expiryDate: (connectorId) => this.connectors.find(connectorId)?.reservationExpiryDate,
|
|
95
|
-
expire: (connectorId) => this.expireReservation(connectorId)
|
|
96
|
-
});
|
|
97
|
-
this.maintenance = new MaintenanceLifecycle({
|
|
98
|
-
run: (task) => void this.runSafely(task),
|
|
99
|
-
diagnosticsStatus: (status) => this.diagnosticsStatusNotification(status),
|
|
100
|
-
firmwareStatus: (status) => this.firmwareStatusNotification(status),
|
|
101
|
-
log: (level, message) => this.log(level, message)
|
|
102
|
-
});
|
|
103
|
-
this.offlineTransactions = new OfflineTransactionSync({
|
|
104
|
-
connectors: this.connectors,
|
|
105
|
-
chargingProfiles: this.chargingProfiles,
|
|
106
|
-
send: (action, payload) => this.send(action, payload),
|
|
107
|
-
readIdTagInfo: (value) => this.readIdTagInfo(value),
|
|
108
|
-
rememberAuthorization: (idTag, idTagInfo) => this.rememberAuthorization(idTag, idTagInfo),
|
|
109
|
-
save: () => this.savePersistedState(),
|
|
110
|
-
log: (level, message) => this.log(level, message),
|
|
111
|
-
statusNotification: (connectorId, status) => this.statusNotification(connectorId, status)
|
|
112
|
-
});
|
|
113
|
-
this.store = config.persistState ? new StationStore(config.chargePointId, config.stateDirectory) : undefined;
|
|
114
|
-
this.loadPersistedState();
|
|
115
|
-
this.refreshConnectorSnapshot();
|
|
116
|
-
this.bindClientEvents();
|
|
117
|
-
}
|
|
118
|
-
connect() {
|
|
119
|
-
this.manuallyDisconnected = false;
|
|
120
|
-
this.clearReconnectTimer();
|
|
121
|
-
this.log("info", `Connecting to ${this.config.centralSystemUrl}`);
|
|
122
|
-
this.client.connect();
|
|
123
|
-
}
|
|
124
|
-
disconnect() {
|
|
125
|
-
this.manuallyDisconnected = true;
|
|
126
|
-
this.clearReconnectTimer();
|
|
127
|
-
this.stopHeartbeatTimer();
|
|
128
|
-
this.clearResetTimer();
|
|
129
|
-
this.clearBootRetryTimer();
|
|
130
|
-
this.meterValueScheduler.stopAllPeriodic();
|
|
131
|
-
this.meterValueScheduler.stopClockAligned();
|
|
132
|
-
this.reservationScheduler.clearAll();
|
|
133
|
-
this.statusScheduler.clearAll();
|
|
134
|
-
this.maintenance.clearAll();
|
|
135
|
-
this.client.close();
|
|
136
|
-
this.patchState({ connected: false });
|
|
137
|
-
}
|
|
138
|
-
reconnect() {
|
|
139
|
-
this.disconnect();
|
|
140
|
-
this.connect();
|
|
141
|
-
}
|
|
142
|
-
async boot(options = {}) {
|
|
143
|
-
const scheduleRetry = options.scheduleRetry ?? true;
|
|
144
|
-
this.clearBootRetryTimer();
|
|
145
|
-
const response = await this.send("BootNotification", {
|
|
146
|
-
chargePointVendor: this.config.vendor,
|
|
147
|
-
chargePointModel: this.config.model
|
|
148
|
-
});
|
|
149
|
-
const status = readString(response.payload.status, "Unknown");
|
|
150
|
-
const interval = readNumber(response.payload.interval, this.config.heartbeatIntervalSeconds);
|
|
151
|
-
this.patchState({
|
|
152
|
-
booted: status === "Accepted",
|
|
153
|
-
registrationStatus: status
|
|
154
|
-
});
|
|
155
|
-
if (status === "Accepted") {
|
|
156
|
-
this.reconnectAttempts = 0;
|
|
157
|
-
this.configuration.setValue("HeartbeatInterval", String(interval));
|
|
158
|
-
this.savePersistedState();
|
|
159
|
-
this.startHeartbeatTimer(interval);
|
|
160
|
-
await this.offlineTransactions.syncPendingTransactions();
|
|
161
|
-
this.meterValueScheduler.reschedulePeriodic(this.connectors.all().map((connector) => connector.id));
|
|
162
|
-
this.meterValueScheduler.startClockAligned();
|
|
163
|
-
await Promise.all(this.connectors.all().map((connector) => this.statusNotification(connector.id, connector.status, true)));
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
this.stopHeartbeatTimer();
|
|
167
|
-
if (scheduleRetry) {
|
|
168
|
-
this.scheduleBootRetry(interval);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
async heartbeat() {
|
|
172
|
-
const response = await this.send("Heartbeat", {});
|
|
173
|
-
this.patchState({ lastHeartbeatAt: new Date() });
|
|
174
|
-
if (response.payload.currentTime) {
|
|
175
|
-
this.log("success", `Heartbeat accepted, Central System time: ${String(response.payload.currentTime)}`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
async statusNotification(connectorId = this.config.connectorId, status = this.connectors.get(connectorId).status, force = false) {
|
|
179
|
-
if (!this.client.isConnected) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
if (!force && this.statusScheduler.schedule(connectorId, status)) {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
this.statusScheduler.clear(connectorId);
|
|
186
|
-
await this.sendStatusNotification(connectorId, status);
|
|
187
|
-
}
|
|
188
|
-
async sendStatusNotification(connectorId, status) {
|
|
189
|
-
const connector = this.connectors.get(connectorId);
|
|
190
|
-
const payload = {
|
|
191
|
-
connectorId,
|
|
192
|
-
errorCode: status === "Faulted" ? faultErrorCode(connector) : "NoError",
|
|
193
|
-
status,
|
|
194
|
-
timestamp: new Date().toISOString()
|
|
195
|
-
};
|
|
196
|
-
if (connector.info) {
|
|
197
|
-
payload.info = connector.info;
|
|
198
|
-
}
|
|
199
|
-
if (connector.vendorId) {
|
|
200
|
-
payload.vendorId = connector.vendorId;
|
|
201
|
-
}
|
|
202
|
-
if (connector.vendorErrorCode) {
|
|
203
|
-
payload.vendorErrorCode = connector.vendorErrorCode;
|
|
204
|
-
}
|
|
205
|
-
await this.send("StatusNotification", payload);
|
|
206
|
-
this.statusScheduler.markSent(connectorId, status);
|
|
207
|
-
}
|
|
208
|
-
async authorize(idTag) {
|
|
209
|
-
if (!idTag) {
|
|
210
|
-
this.log("warn", "Authorize requires idTag");
|
|
211
|
-
return { accepted: false, idTagInfo: { status: "Invalid" }, source: "None" };
|
|
212
|
-
}
|
|
213
|
-
if (this.client.isConnected) {
|
|
214
|
-
const response = await this.send("Authorize", { idTag });
|
|
215
|
-
const idTagInfo = this.readIdTagInfo(response.payload.idTagInfo);
|
|
216
|
-
this.rememberAuthorization(idTag, idTagInfo);
|
|
217
|
-
const accepted = isAcceptedIdTagInfo(idTagInfo);
|
|
218
|
-
this.log(accepted ? "success" : "warn", `Authorization status: ${idTagInfo.status} (${idTag})`);
|
|
219
|
-
if (!accepted) {
|
|
220
|
-
await this.stopTransactionsForInvalidIdTag(idTag);
|
|
221
|
-
}
|
|
222
|
-
return { accepted, idTagInfo, source: "CentralSystem" };
|
|
223
|
-
}
|
|
224
|
-
const decision = this.authorizeLocally(idTag, true);
|
|
225
|
-
this.log(decision.accepted ? "success" : "warn", `Offline authorization status: ${decision.idTagInfo.status} (${idTag}, ${decision.source})`);
|
|
226
|
-
return decision;
|
|
227
|
-
}
|
|
228
|
-
async toggleTransaction(connectorId = this.config.connectorId, idTag) {
|
|
229
|
-
if (!idTag) {
|
|
230
|
-
this.log("warn", "StartTransaction requires idTag");
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
const connector = this.connectors.get(connectorId);
|
|
234
|
-
if (connector.transactionId) {
|
|
235
|
-
await this.stopTransaction(connectorId, "Local", idTag);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
await this.startTransaction(connectorId, idTag);
|
|
239
|
-
}
|
|
240
|
-
async startTransaction(connectorId = this.config.connectorId, idTag) {
|
|
241
|
-
if (!idTag) {
|
|
242
|
-
this.log("warn", "StartTransaction requires idTag");
|
|
243
|
-
return undefined;
|
|
244
|
-
}
|
|
245
|
-
const connector = this.connectors.get(connectorId);
|
|
246
|
-
const rejection = startTransactionRejection(connector, idTag);
|
|
247
|
-
if (rejection) {
|
|
248
|
-
this.log("warn", rejection);
|
|
249
|
-
return undefined;
|
|
250
|
-
}
|
|
251
|
-
if (!connector.evConnected && connector.status === "Available") {
|
|
252
|
-
this.connectors.patch(connectorId, { evConnected: true, status: "Preparing" });
|
|
253
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Preparing"));
|
|
254
|
-
}
|
|
255
|
-
else if (connector.status === "Available") {
|
|
256
|
-
this.connectors.patch(connectorId, { status: "Preparing" });
|
|
257
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Preparing"));
|
|
258
|
-
}
|
|
259
|
-
const payload = {
|
|
260
|
-
connectorId,
|
|
261
|
-
idTag,
|
|
262
|
-
meterStart: Math.round(connector.meterWh),
|
|
263
|
-
timestamp: new Date().toISOString()
|
|
264
|
-
};
|
|
265
|
-
if (connector.reservationId) {
|
|
266
|
-
payload.reservationId = connector.reservationId;
|
|
267
|
-
}
|
|
268
|
-
const localPreAuthorize = this.configuration.boolean("LocalPreAuthorize", false);
|
|
269
|
-
if (localPreAuthorize) {
|
|
270
|
-
const decision = this.authorizeLocally(idTag, false);
|
|
271
|
-
if (!decision.accepted) {
|
|
272
|
-
this.log("warn", `StartTransaction blocked by local authorization: ${decision.idTagInfo.status} (${idTag})`);
|
|
273
|
-
return undefined;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
if (!this.client.isConnected) {
|
|
277
|
-
const decision = this.authorizeLocally(idTag, true);
|
|
278
|
-
if (!decision.accepted) {
|
|
279
|
-
this.log("warn", `Offline StartTransaction blocked: ${decision.idTagInfo.status} (${idTag}, ${decision.source})`);
|
|
280
|
-
return undefined;
|
|
281
|
-
}
|
|
282
|
-
const transactionId = localTransactionId(connectorId);
|
|
283
|
-
this.reservationScheduler.clear(connectorId);
|
|
284
|
-
this.connectors.patch(connectorId, {
|
|
285
|
-
transactionId,
|
|
286
|
-
transactionStartedAt: readString(payload.timestamp, new Date().toISOString()),
|
|
287
|
-
reservationId: undefined,
|
|
288
|
-
reservationIdTag: undefined,
|
|
289
|
-
reservationParentIdTag: undefined,
|
|
290
|
-
reservationExpiryDate: undefined,
|
|
291
|
-
lastIdTag: idTag,
|
|
292
|
-
stopTransactionAtWh: undefined,
|
|
293
|
-
pendingStartTransaction: payload,
|
|
294
|
-
pendingStopTransaction: undefined,
|
|
295
|
-
status: "Charging"
|
|
296
|
-
});
|
|
297
|
-
this.savePersistedState();
|
|
298
|
-
this.meterValueScheduler.startPeriodic(connectorId);
|
|
299
|
-
this.log("warn", `Started local offline transaction ${transactionId} on connector ${connectorId}; will sync after reconnect`);
|
|
300
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Charging"));
|
|
301
|
-
return transactionId;
|
|
302
|
-
}
|
|
303
|
-
const response = await this.send("StartTransaction", payload);
|
|
304
|
-
const idTagInfo = this.readIdTagInfo(response.payload.idTagInfo);
|
|
305
|
-
this.rememberAuthorization(idTag, idTagInfo);
|
|
306
|
-
if (!isAcceptedIdTagInfo(idTagInfo)) {
|
|
307
|
-
this.log("warn", `StartTransaction rejected by Central System: ${idTagInfo.status} (${idTag})`);
|
|
308
|
-
return undefined;
|
|
309
|
-
}
|
|
310
|
-
const transactionId = readNumber(response.payload.transactionId, Date.now());
|
|
311
|
-
this.reservationScheduler.clear(connectorId);
|
|
312
|
-
this.connectors.patch(connectorId, {
|
|
313
|
-
transactionId,
|
|
314
|
-
transactionStartedAt: new Date().toISOString(),
|
|
315
|
-
reservationId: undefined,
|
|
316
|
-
reservationIdTag: undefined,
|
|
317
|
-
reservationParentIdTag: undefined,
|
|
318
|
-
reservationExpiryDate: undefined,
|
|
319
|
-
lastIdTag: idTag,
|
|
320
|
-
stopTransactionAtWh: undefined,
|
|
321
|
-
pendingStartTransaction: undefined,
|
|
322
|
-
pendingStopTransaction: undefined,
|
|
323
|
-
status: "Charging"
|
|
324
|
-
});
|
|
325
|
-
this.savePersistedState();
|
|
326
|
-
this.meterValueScheduler.startPeriodic(connectorId);
|
|
327
|
-
this.meterValueScheduler.startClockAligned();
|
|
328
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Charging"));
|
|
329
|
-
return transactionId;
|
|
330
|
-
}
|
|
331
|
-
async stopTransaction(connectorId = this.config.connectorId, reason = "Local", idTag) {
|
|
332
|
-
const connector = this.connectors.get(connectorId);
|
|
333
|
-
if (!connector.transactionId) {
|
|
334
|
-
this.log("warn", `Connector ${connectorId} has no active transaction`);
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
this.meterValueScheduler.stopPeriodic(connectorId);
|
|
338
|
-
const transactionId = connector.transactionId;
|
|
339
|
-
const pendingStop = {
|
|
340
|
-
meterStop: Math.round(connector.meterWh),
|
|
341
|
-
timestamp: new Date().toISOString(),
|
|
342
|
-
reason,
|
|
343
|
-
transactionData: this.buildStopTransactionData(connectorId)
|
|
344
|
-
};
|
|
345
|
-
if (idTag) {
|
|
346
|
-
pendingStop.idTag = idTag;
|
|
347
|
-
}
|
|
348
|
-
if (!this.client.isConnected || connector.pendingStartTransaction) {
|
|
349
|
-
const { evConnected, status } = statusAfterStopTransaction(reason, connector.evConnected, connector.availability);
|
|
350
|
-
this.connectors.patch(connectorId, {
|
|
351
|
-
pendingStopTransaction: pendingStop,
|
|
352
|
-
evConnected,
|
|
353
|
-
status
|
|
354
|
-
});
|
|
355
|
-
this.savePersistedState();
|
|
356
|
-
this.log("warn", `Queued ${connector.pendingStartTransaction ? "offline" : "disconnected"} StopTransaction for connector ${connectorId}; will sync after reconnect`);
|
|
357
|
-
void this.runSafely(() => this.statusNotification(connectorId, status));
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
const payload = { ...pendingStopPayload(pendingStop), transactionId };
|
|
361
|
-
this.connectors.patch(connectorId, { status: "Finishing" });
|
|
362
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Finishing"));
|
|
363
|
-
try {
|
|
364
|
-
await this.send("StopTransaction", payload);
|
|
365
|
-
}
|
|
366
|
-
catch (error) {
|
|
367
|
-
const restoredStatus = connector.evConnected ? "Charging" : "SuspendedEV";
|
|
368
|
-
this.connectors.patch(connectorId, { status: restoredStatus });
|
|
369
|
-
if (restoredStatus === "Charging") {
|
|
370
|
-
this.meterValueScheduler.startPeriodic(connectorId);
|
|
371
|
-
this.meterValueScheduler.startClockAligned();
|
|
372
|
-
}
|
|
373
|
-
void this.runSafely(() => this.statusNotification(connectorId, restoredStatus));
|
|
374
|
-
throw error;
|
|
375
|
-
}
|
|
376
|
-
const { evConnected, status } = statusAfterStopTransaction(reason, connector.evConnected, connector.availability);
|
|
377
|
-
this.connectors.patch(connectorId, {
|
|
378
|
-
transactionId: undefined,
|
|
379
|
-
transactionStartedAt: undefined,
|
|
380
|
-
stopTransactionAtWh: undefined,
|
|
381
|
-
pendingStartTransaction: undefined,
|
|
382
|
-
pendingStopTransaction: undefined,
|
|
383
|
-
evConnected,
|
|
384
|
-
status
|
|
385
|
-
});
|
|
386
|
-
this.chargingProfiles.clearForTransaction(transactionId);
|
|
387
|
-
this.savePersistedState();
|
|
388
|
-
if (status !== "Finishing") {
|
|
389
|
-
void this.runSafely(() => this.statusNotification(connectorId, status));
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
async plugIn(connectorId = this.config.connectorId) {
|
|
393
|
-
const connector = this.connectors.get(connectorId);
|
|
394
|
-
if (connector.evConnected) {
|
|
395
|
-
this.log("warn", `Connector ${connectorId} already has EV connected`);
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
if (connector.availability === "Inoperative" || connector.status === "Unavailable" || connector.status === "Faulted") {
|
|
399
|
-
this.log("warn", `Connector ${connectorId} cannot accept EV while ${connector.status}`);
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
if (connector.transactionId) {
|
|
403
|
-
this.connectors.patch(connectorId, { evConnected: true, status: "Charging" });
|
|
404
|
-
this.meterValueScheduler.startPeriodic(connectorId);
|
|
405
|
-
this.meterValueScheduler.startClockAligned();
|
|
406
|
-
await this.statusNotification(connectorId, "Charging");
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
const status = connector.reservationId ? "Reserved" : "Preparing";
|
|
410
|
-
this.connectors.patch(connectorId, { evConnected: true, status });
|
|
411
|
-
await this.statusNotification(connectorId, status);
|
|
412
|
-
}
|
|
413
|
-
async unplug(connectorId = this.config.connectorId) {
|
|
414
|
-
const connector = this.connectors.get(connectorId);
|
|
415
|
-
if (connector.transactionId) {
|
|
416
|
-
if (!this.configuration.boolean("StopTransactionOnEVSideDisconnect", true)) {
|
|
417
|
-
this.meterValueScheduler.stopPeriodic(connectorId);
|
|
418
|
-
this.connectors.patch(connectorId, { evConnected: false, status: "SuspendedEV" });
|
|
419
|
-
await this.statusNotification(connectorId, "SuspendedEV");
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
await this.stopTransaction(connectorId, "EVDisconnected", connector.lastIdTag);
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
if (!connector.evConnected && connector.status !== "Finishing") {
|
|
426
|
-
this.log("warn", `Connector ${connectorId} has no EV connected`);
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
|
-
const status = connector.availability === "Inoperative" || connector.status === "Unavailable" ? "Unavailable" : "Available";
|
|
430
|
-
this.connectors.patch(connectorId, { evConnected: false, status });
|
|
431
|
-
await this.statusNotification(connectorId, status);
|
|
432
|
-
}
|
|
433
|
-
async setFault(connectorId = this.config.connectorId, errorCode = "OtherError", info, vendorErrorCode) {
|
|
434
|
-
const connector = this.connectors.get(connectorId);
|
|
435
|
-
if (connector.transactionId) {
|
|
436
|
-
await this.stopTransaction(connectorId, "Other", connector.lastIdTag);
|
|
437
|
-
}
|
|
438
|
-
this.connectors.patch(connectorId, {
|
|
439
|
-
status: "Faulted",
|
|
440
|
-
errorCode: errorCode === "NoError" ? "OtherError" : errorCode,
|
|
441
|
-
info: trimOptional(info, 50),
|
|
442
|
-
vendorId: this.config.vendor,
|
|
443
|
-
vendorErrorCode: trimOptional(vendorErrorCode, 50)
|
|
444
|
-
});
|
|
445
|
-
await this.statusNotification(connectorId, "Faulted");
|
|
446
|
-
}
|
|
447
|
-
async clearFault(connectorId = this.config.connectorId) {
|
|
448
|
-
const connector = this.connectors.get(connectorId);
|
|
449
|
-
const status = statusAfterFaultClear(connector);
|
|
450
|
-
this.connectors.patch(connectorId, {
|
|
451
|
-
status,
|
|
452
|
-
errorCode: "NoError",
|
|
453
|
-
info: undefined,
|
|
454
|
-
vendorId: undefined,
|
|
455
|
-
vendorErrorCode: undefined
|
|
456
|
-
});
|
|
457
|
-
await this.statusNotification(connectorId, status);
|
|
458
|
-
}
|
|
459
|
-
async meterValues(connectorId = this.config.connectorId, deltaWh = 120, sampledData) {
|
|
460
|
-
if (sampledData !== undefined && !isValidSampledDataConfiguration(sampledData)) {
|
|
461
|
-
this.log("warn", `Invalid MeterValues sampled data: ${sampledData}`);
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
const connector = this.connectors.get(connectorId);
|
|
465
|
-
const measurands = sampledData === undefined ? this.configuration.measurands("MeterValuesSampledData") : readConfiguredMeasurands(sampledData);
|
|
466
|
-
if (!connector) {
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
await this.sendMeterValues(connectorId, "Sample.Periodic", deltaWh, measurands);
|
|
470
|
-
}
|
|
471
|
-
async dataTransfer(payload = { vendorId: this.config.vendor }) {
|
|
472
|
-
const response = await this.send("DataTransfer", payload);
|
|
473
|
-
const status = readString(response.payload.status, "Unknown");
|
|
474
|
-
this.log(status === "Accepted" ? "success" : "warn", `DataTransfer status: ${status}`);
|
|
475
|
-
if (response.payload.data) {
|
|
476
|
-
this.log("info", `DataTransfer response data: ${String(response.payload.data)}`);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
async firmwareStatusNotification(status = "Installed") {
|
|
480
|
-
this.patchState({ firmwareStatus: status });
|
|
481
|
-
await this.send("FirmwareStatusNotification", { status });
|
|
482
|
-
}
|
|
483
|
-
async diagnosticsStatusNotification(status = "Uploaded") {
|
|
484
|
-
this.patchState({ diagnosticsStatus: status });
|
|
485
|
-
await this.send("DiagnosticsStatusNotification", { status });
|
|
486
|
-
}
|
|
487
|
-
setDiagnosticsOutcome(outcome) {
|
|
488
|
-
this.maintenance.setDiagnosticsOutcome(outcome);
|
|
489
|
-
this.log("info", `Diagnostics outcome set to ${outcome}`);
|
|
490
|
-
}
|
|
491
|
-
setFirmwareOutcome(outcome) {
|
|
492
|
-
this.maintenance.setFirmwareOutcome(outcome);
|
|
493
|
-
this.log("info", `Firmware outcome set to ${outcome}`);
|
|
494
|
-
}
|
|
495
|
-
async cycleStatus(connectorId = this.config.connectorId) {
|
|
496
|
-
const connector = this.connectors.get(connectorId);
|
|
497
|
-
const next = nextConnectorStatus(connector.status);
|
|
498
|
-
await this.setStatus(connectorId, next);
|
|
499
|
-
}
|
|
500
|
-
async setStatus(connectorId = this.config.connectorId, status) {
|
|
501
|
-
this.connectors.patch(connectorId, {
|
|
502
|
-
status,
|
|
503
|
-
errorCode: status === "Faulted" ? "OtherError" : "NoError",
|
|
504
|
-
info: status === "Faulted" ? "Manual fault" : undefined,
|
|
505
|
-
vendorId: status === "Faulted" ? this.config.vendor : undefined,
|
|
506
|
-
vendorErrorCode: undefined
|
|
507
|
-
});
|
|
508
|
-
await this.statusNotification(connectorId, status);
|
|
509
|
-
}
|
|
510
|
-
addConnector() {
|
|
511
|
-
const { id: nextId } = this.connectors.add();
|
|
512
|
-
this.configuration.setValue("NumberOfConnectors", String(this.connectors.size));
|
|
513
|
-
this.savePersistedState();
|
|
514
|
-
void this.runSafely(() => this.statusNotification(nextId, "Available"));
|
|
515
|
-
return nextId;
|
|
516
|
-
}
|
|
517
|
-
bindClientEvents() {
|
|
518
|
-
this.client.on("connected", () => {
|
|
519
|
-
this.clearReconnectTimer();
|
|
520
|
-
this.patchState({ connected: true });
|
|
521
|
-
this.log("success", "Connected");
|
|
522
|
-
void this.runSafely(() => this.boot());
|
|
523
|
-
});
|
|
524
|
-
this.client.on("disconnected", (code, reason) => {
|
|
525
|
-
this.stopHeartbeatTimer();
|
|
526
|
-
this.clearResetTimer();
|
|
527
|
-
this.clearBootRetryTimer();
|
|
528
|
-
this.patchState({ connected: false, booted: false });
|
|
529
|
-
this.log("warn", `Disconnected${code ? ` (${code})` : ""}${reason ? `: ${reason}` : ""}`);
|
|
530
|
-
this.scheduleReconnect();
|
|
531
|
-
});
|
|
532
|
-
this.client.on("error", (error) => this.log("error", error.message));
|
|
533
|
-
this.client.on("log", (direction, message) => {
|
|
534
|
-
this.log(direction, describeOcppMessage(message), JSON.stringify(message));
|
|
535
|
-
});
|
|
536
|
-
this.client.on("call", (messageId, action, payload) => {
|
|
537
|
-
void this.handleCentralSystemCall(messageId, action, payload);
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
async handleCentralSystemCall(messageId, action, payload) {
|
|
541
|
-
try {
|
|
542
|
-
switch (action) {
|
|
543
|
-
case "CancelReservation":
|
|
544
|
-
this.handleCancelReservation(messageId, payload);
|
|
545
|
-
break;
|
|
546
|
-
case "ChangeAvailability":
|
|
547
|
-
await this.handleChangeAvailability(messageId, payload);
|
|
548
|
-
break;
|
|
549
|
-
case "ChangeConfiguration":
|
|
550
|
-
this.handleChangeConfiguration(messageId, payload);
|
|
551
|
-
break;
|
|
552
|
-
case "ClearCache":
|
|
553
|
-
this.authorizationCache.clear();
|
|
554
|
-
this.savePersistedState();
|
|
555
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
556
|
-
break;
|
|
557
|
-
case "ClearChargingProfile":
|
|
558
|
-
this.handleClearChargingProfile(messageId, payload);
|
|
559
|
-
break;
|
|
560
|
-
case "DataTransfer":
|
|
561
|
-
this.handleDataTransfer(messageId, payload);
|
|
562
|
-
break;
|
|
563
|
-
case "GetCompositeSchedule":
|
|
564
|
-
this.handleGetCompositeSchedule(messageId, payload);
|
|
565
|
-
break;
|
|
566
|
-
case "GetConfiguration":
|
|
567
|
-
this.handleGetConfiguration(messageId, payload);
|
|
568
|
-
break;
|
|
569
|
-
case "GetDiagnostics":
|
|
570
|
-
this.client.reply(messageId, { fileName: this.maintenance.startDiagnostics(payload) });
|
|
571
|
-
break;
|
|
572
|
-
case "GetLocalListVersion":
|
|
573
|
-
this.client.reply(messageId, { listVersion: this.state.localListVersion });
|
|
574
|
-
break;
|
|
575
|
-
case "RemoteStartTransaction":
|
|
576
|
-
await this.handleRemoteStartTransaction(messageId, payload);
|
|
577
|
-
break;
|
|
578
|
-
case "RemoteStopTransaction":
|
|
579
|
-
await this.handleRemoteStopTransaction(messageId, payload);
|
|
580
|
-
break;
|
|
581
|
-
case "ReserveNow":
|
|
582
|
-
await this.handleReserveNow(messageId, payload);
|
|
583
|
-
break;
|
|
584
|
-
case "Reset":
|
|
585
|
-
this.handleReset(messageId, payload);
|
|
586
|
-
break;
|
|
587
|
-
case "SendLocalList":
|
|
588
|
-
this.handleSendLocalList(messageId, payload);
|
|
589
|
-
break;
|
|
590
|
-
case "SetChargingProfile":
|
|
591
|
-
this.handleSetChargingProfile(messageId, payload);
|
|
592
|
-
break;
|
|
593
|
-
case "TriggerMessage":
|
|
594
|
-
await this.handleTriggerMessage(messageId, payload);
|
|
595
|
-
break;
|
|
596
|
-
case "UnlockConnector":
|
|
597
|
-
await this.handleUnlockConnector(messageId, payload);
|
|
598
|
-
break;
|
|
599
|
-
case "UpdateFirmware":
|
|
600
|
-
this.client.reply(messageId, {});
|
|
601
|
-
this.maintenance.startFirmware(payload);
|
|
602
|
-
break;
|
|
603
|
-
default:
|
|
604
|
-
this.client.replyError(messageId, "NotSupported", `${action} is not supported by this emulator`);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
catch (error) {
|
|
608
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
609
|
-
this.client.replyError(messageId, "InternalError", message);
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
handleCancelReservation(messageId, payload) {
|
|
613
|
-
const reservationId = readNumber(payload.reservationId, -1);
|
|
614
|
-
const connector = this.connectors.findByReservation(reservationId);
|
|
615
|
-
if (!connector) {
|
|
616
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
617
|
-
return;
|
|
618
|
-
}
|
|
619
|
-
this.clearReservation(connector.id);
|
|
620
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
621
|
-
void this.runSafely(() => this.statusNotification(connector.id, this.connectors.get(connector.id).status));
|
|
622
|
-
}
|
|
623
|
-
async handleChangeAvailability(messageId, payload) {
|
|
624
|
-
const availability = readString(payload.type, "Operative");
|
|
625
|
-
const connectorId = readNumber(payload.connectorId, 0);
|
|
626
|
-
const requestedConnector = connectorId === 0 ? undefined : this.connectors.find(connectorId);
|
|
627
|
-
if (connectorId !== 0 && !requestedConnector) {
|
|
628
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
629
|
-
return;
|
|
630
|
-
}
|
|
631
|
-
const connectors = connectorId === 0 ? this.connectors.all() : [requestedConnector ?? this.connectors.get(connectorId)];
|
|
632
|
-
const scheduled = connectors.some((connector) => connector.transactionId);
|
|
633
|
-
for (const connector of connectors) {
|
|
634
|
-
if (connector.transactionId) {
|
|
635
|
-
this.connectors.patch(connector.id, { availability });
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
638
|
-
this.connectors.patch(connector.id, { availability, status: statusAfterAvailabilityChange(connector, availability) });
|
|
639
|
-
}
|
|
640
|
-
this.client.reply(messageId, { status: scheduled ? "Scheduled" : "Accepted" });
|
|
641
|
-
for (const connector of connectors.filter((connector) => !connector.transactionId)) {
|
|
642
|
-
void this.runSafely(() => this.statusNotification(connector.id, this.connectors.get(connector.id).status));
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
handleChangeConfiguration(messageId, payload) {
|
|
646
|
-
const result = changeConfiguration(this.configuration, payload);
|
|
647
|
-
this.client.reply(messageId, { status: result.status });
|
|
648
|
-
if (result.status !== "Accepted") {
|
|
649
|
-
return;
|
|
650
|
-
}
|
|
651
|
-
this.savePersistedState();
|
|
652
|
-
if (result.key === "HeartbeatInterval") {
|
|
653
|
-
const interval = Number.parseInt(result.value, 10);
|
|
654
|
-
if (Number.isFinite(interval) && interval > 0) {
|
|
655
|
-
this.startHeartbeatTimer(interval);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
if (result.key === "MeterValueSampleInterval") {
|
|
659
|
-
this.meterValueScheduler.reschedulePeriodic(this.connectors.all().map((connector) => connector.id));
|
|
660
|
-
}
|
|
661
|
-
if (result.key === "ClockAlignedDataInterval") {
|
|
662
|
-
this.meterValueScheduler.startClockAligned();
|
|
663
|
-
}
|
|
664
|
-
if (result.key === "MinimumStatusDuration" && result.value === "0") {
|
|
665
|
-
this.statusScheduler.flush();
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
handleDataTransfer(messageId, payload) {
|
|
669
|
-
const vendorId = readString(payload.vendorId, "");
|
|
670
|
-
if (vendorId !== this.config.vendor) {
|
|
671
|
-
this.client.reply(messageId, { status: "UnknownVendorId" });
|
|
672
|
-
return;
|
|
673
|
-
}
|
|
674
|
-
this.client.reply(messageId, { status: "Accepted", data: readString(payload.data, "") });
|
|
675
|
-
}
|
|
676
|
-
handleGetConfiguration(messageId, payload) {
|
|
677
|
-
this.client.reply(messageId, getConfiguration(this.configuration, payload));
|
|
678
|
-
}
|
|
679
|
-
handleSetChargingProfile(messageId, payload) {
|
|
680
|
-
const connectorId = readNumber(payload.connectorId, -1);
|
|
681
|
-
const status = this.chargingProfiles.set(connectorId, payload.csChargingProfiles, {
|
|
682
|
-
hasConnector: (id) => Boolean(this.connectors.find(id)),
|
|
683
|
-
transactionIdForConnector: (id) => this.connectors.find(id)?.transactionId
|
|
684
|
-
});
|
|
685
|
-
if (status !== "Accepted") {
|
|
686
|
-
this.client.reply(messageId, { status });
|
|
687
|
-
return;
|
|
688
|
-
}
|
|
689
|
-
this.savePersistedState();
|
|
690
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
691
|
-
}
|
|
692
|
-
handleClearChargingProfile(messageId, payload) {
|
|
693
|
-
const status = this.chargingProfiles.clear(payload);
|
|
694
|
-
if (status === "Accepted") {
|
|
695
|
-
this.savePersistedState();
|
|
696
|
-
}
|
|
697
|
-
this.client.reply(messageId, { status });
|
|
698
|
-
}
|
|
699
|
-
handleGetCompositeSchedule(messageId, payload) {
|
|
700
|
-
const connectorId = readNumber(payload.connectorId, -1);
|
|
701
|
-
const connector = connectorId === 0 ? undefined : this.connectors.find(connectorId);
|
|
702
|
-
if (connectorId !== 0 && !connector) {
|
|
703
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
const schedule = this.chargingProfiles.compositeSchedulePayload(payload, connector?.transactionId, connector?.transactionStartedAt);
|
|
707
|
-
if (!schedule) {
|
|
708
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
if (schedule.prunedExpired) {
|
|
712
|
-
this.savePersistedState();
|
|
713
|
-
}
|
|
714
|
-
this.client.reply(messageId, schedule.payload);
|
|
715
|
-
}
|
|
716
|
-
handleSendLocalList(messageId, payload) {
|
|
717
|
-
const result = applyLocalAuthListUpdate(this.localAuthorizationList, this.state.localListVersion, payload, {
|
|
718
|
-
enabled: this.configuration.boolean("LocalAuthListEnabled", true),
|
|
719
|
-
maxLength: this.configuration.integer("SendLocalListMaxLength", 1000)
|
|
720
|
-
});
|
|
721
|
-
if (result.status !== "Accepted") {
|
|
722
|
-
this.client.reply(messageId, { status: result.status });
|
|
723
|
-
return;
|
|
724
|
-
}
|
|
725
|
-
this.patchState({ localListVersion: result.listVersion });
|
|
726
|
-
this.savePersistedState();
|
|
727
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
728
|
-
}
|
|
729
|
-
async handleRemoteStartTransaction(messageId, payload) {
|
|
730
|
-
const requestedConnector = readNumber(payload.connectorId, 0);
|
|
731
|
-
const connector = requestedConnector > 0 ? this.connectors.find(requestedConnector) : this.connectors.findAvailable();
|
|
732
|
-
const idTag = readString(payload.idTag, "");
|
|
733
|
-
if (!idTag || !canRemoteStart(connector) || !this.canAcceptRemoteStartChargingProfile(payload)) {
|
|
734
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
if (this.configuration.boolean("AuthorizeRemoteTxRequests", false)) {
|
|
738
|
-
const decision = await this.authorize(idTag);
|
|
739
|
-
if (!decision.accepted) {
|
|
740
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
741
|
-
return;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
745
|
-
if (connector) {
|
|
746
|
-
const transactionId = await this.startTransaction(connector.id, idTag);
|
|
747
|
-
if (transactionId !== undefined) {
|
|
748
|
-
this.applyRemoteStartChargingProfile(connector.id, payload.chargingProfile, transactionId);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
async handleRemoteStopTransaction(messageId, payload) {
|
|
753
|
-
const transactionId = readNumber(payload.transactionId, -1);
|
|
754
|
-
const connector = this.connectors.findByTransaction(transactionId);
|
|
755
|
-
const accepted = Boolean(connector);
|
|
756
|
-
this.client.reply(messageId, { status: accepted ? "Accepted" : "Rejected" });
|
|
757
|
-
if (accepted && connector) {
|
|
758
|
-
await this.stopTransaction(connector.id, "Remote");
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
async handleReserveNow(messageId, payload) {
|
|
762
|
-
const requestedConnectorId = readNumber(payload.connectorId, this.config.connectorId);
|
|
763
|
-
const connector = requestedConnectorId === 0 ? this.connectors.findAvailable() : this.connectors.find(requestedConnectorId);
|
|
764
|
-
const connectorId = connector?.id ?? requestedConnectorId;
|
|
765
|
-
const expiryDate = readString(payload.expiryDate, "");
|
|
766
|
-
const rejection = reserveNowRejection(connector, expiryDate);
|
|
767
|
-
if (rejection) {
|
|
768
|
-
this.client.reply(messageId, { status: rejection });
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
this.connectors.patch(connectorId, {
|
|
772
|
-
reservationId: readNumber(payload.reservationId, Date.now()),
|
|
773
|
-
reservationIdTag: readString(payload.idTag, ""),
|
|
774
|
-
reservationParentIdTag: readOptionalString(payload.parentIdTag),
|
|
775
|
-
reservationExpiryDate: expiryDate,
|
|
776
|
-
status: "Reserved"
|
|
777
|
-
});
|
|
778
|
-
this.reservationScheduler.schedule(connectorId);
|
|
779
|
-
this.savePersistedState();
|
|
780
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
781
|
-
void this.runSafely(() => this.statusNotification(connectorId, "Reserved"));
|
|
782
|
-
}
|
|
783
|
-
handleReset(messageId, payload) {
|
|
784
|
-
const resetType = readString(payload.type, "Soft");
|
|
785
|
-
const reason = resetType === "Hard" ? "HardReset" : "SoftReset";
|
|
786
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
787
|
-
void this.runSafely(() => this.performReset(reason, resetType));
|
|
788
|
-
}
|
|
789
|
-
async handleTriggerMessage(messageId, payload) {
|
|
790
|
-
const requestedMessage = readString(payload.requestedMessage, "");
|
|
791
|
-
if (!canTriggerMessage(requestedMessage)) {
|
|
792
|
-
this.client.reply(messageId, { status: "NotImplemented" });
|
|
793
|
-
return;
|
|
794
|
-
}
|
|
795
|
-
if (!this.canTriggerMessageForPayload(requestedMessage, payload)) {
|
|
796
|
-
this.client.reply(messageId, { status: "Rejected" });
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
this.client.reply(messageId, { status: "Accepted" });
|
|
800
|
-
await this.triggerMessage(requestedMessage, payload);
|
|
801
|
-
}
|
|
802
|
-
async handleUnlockConnector(messageId, payload = {}) {
|
|
803
|
-
const connectorId = readNumber(payload.connectorId, this.config.connectorId);
|
|
804
|
-
const connector = this.connectors.find(connectorId);
|
|
805
|
-
if (!connector) {
|
|
806
|
-
this.client.reply(messageId, { status: "NotSupported" });
|
|
807
|
-
return;
|
|
808
|
-
}
|
|
809
|
-
if (connector.transactionId) {
|
|
810
|
-
this.client.reply(messageId, { status: "Unlocked" });
|
|
811
|
-
await this.stopTransaction(connectorId, "UnlockCommand");
|
|
812
|
-
return;
|
|
813
|
-
}
|
|
814
|
-
this.client.reply(messageId, { status: "Unlocked" });
|
|
815
|
-
}
|
|
816
|
-
async triggerMessage(requestedMessage, payload = {}) {
|
|
817
|
-
switch (requestedMessage) {
|
|
818
|
-
case "BootNotification":
|
|
819
|
-
await this.boot();
|
|
820
|
-
break;
|
|
821
|
-
case "DiagnosticsStatusNotification":
|
|
822
|
-
await this.diagnosticsStatusNotification("Idle");
|
|
823
|
-
break;
|
|
824
|
-
case "FirmwareStatusNotification":
|
|
825
|
-
await this.firmwareStatusNotification("Idle");
|
|
826
|
-
break;
|
|
827
|
-
case "Heartbeat":
|
|
828
|
-
await this.heartbeat();
|
|
829
|
-
break;
|
|
830
|
-
case "MeterValues":
|
|
831
|
-
await this.meterValues(this.triggerConnectorId(payload), 0);
|
|
832
|
-
break;
|
|
833
|
-
case "StatusNotification":
|
|
834
|
-
await this.statusNotification(this.triggerConnectorId(payload), this.connectors.get(this.triggerConnectorId(payload)).status, true);
|
|
835
|
-
break;
|
|
836
|
-
default:
|
|
837
|
-
this.log("warn", `TriggerMessage requested unsupported message: ${requestedMessage}`);
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
canTriggerMessageForPayload(requestedMessage, payload) {
|
|
841
|
-
if (requestedMessage !== "MeterValues" && requestedMessage !== "StatusNotification") {
|
|
842
|
-
return true;
|
|
843
|
-
}
|
|
844
|
-
return Boolean(this.connectors.find(this.triggerConnectorId(payload)));
|
|
845
|
-
}
|
|
846
|
-
triggerConnectorId(payload) {
|
|
847
|
-
return "connectorId" in payload ? readNumber(payload.connectorId, -1) : this.config.connectorId;
|
|
848
|
-
}
|
|
849
|
-
canAcceptRemoteStartChargingProfile(payload) {
|
|
850
|
-
if (!("chargingProfile" in payload)) {
|
|
851
|
-
return true;
|
|
852
|
-
}
|
|
853
|
-
const profile = readChargingProfile(payload.chargingProfile);
|
|
854
|
-
return Boolean(profile &&
|
|
855
|
-
profile.chargingProfilePurpose === "TxProfile" &&
|
|
856
|
-
profile.transactionId === undefined);
|
|
857
|
-
}
|
|
858
|
-
applyRemoteStartChargingProfile(connectorId, profilePayload, transactionId) {
|
|
859
|
-
const profile = readChargingProfile(profilePayload);
|
|
860
|
-
if (!profile) {
|
|
861
|
-
return;
|
|
862
|
-
}
|
|
863
|
-
const status = this.chargingProfiles.set(connectorId, { ...profile, transactionId }, {
|
|
864
|
-
hasConnector: (id) => Boolean(this.connectors.find(id)),
|
|
865
|
-
transactionIdForConnector: (id) => this.connectors.find(id)?.transactionId
|
|
866
|
-
});
|
|
867
|
-
if (status === "Accepted") {
|
|
868
|
-
this.savePersistedState();
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
async send(action, payload) {
|
|
872
|
-
if (isTransactionMessage(action)) {
|
|
873
|
-
return this.sendTransactionMessage(action, payload);
|
|
874
|
-
}
|
|
875
|
-
try {
|
|
876
|
-
const response = await this.client.call(action, payload, this.config.callTimeoutMs ?? 30_000);
|
|
877
|
-
this.log("success", `${action} accepted`);
|
|
878
|
-
return response;
|
|
879
|
-
}
|
|
880
|
-
catch (error) {
|
|
881
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
882
|
-
this.log("error", message);
|
|
883
|
-
throw error;
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
async sendTransactionMessage(action, payload) {
|
|
887
|
-
const attempts = this.configuration.integer("TransactionMessageAttempts", 1);
|
|
888
|
-
const retryInterval = this.configuration.integer("TransactionMessageRetryInterval", 30);
|
|
889
|
-
let lastError;
|
|
890
|
-
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
891
|
-
try {
|
|
892
|
-
const response = await this.client.call(action, payload, this.config.callTimeoutMs ?? 30_000);
|
|
893
|
-
this.log("success", `${action} accepted`);
|
|
894
|
-
return response;
|
|
895
|
-
}
|
|
896
|
-
catch (error) {
|
|
897
|
-
lastError = error;
|
|
898
|
-
if (attempt >= attempts) {
|
|
899
|
-
break;
|
|
900
|
-
}
|
|
901
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
902
|
-
this.log("warn", `${action} attempt ${attempt}/${attempts} failed: ${message}; retrying in ${retryInterval}s`);
|
|
903
|
-
await retryDelay(retryInterval);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
const message = lastError instanceof Error ? lastError.message : String(lastError);
|
|
907
|
-
this.log("error", message);
|
|
908
|
-
throw lastError instanceof Error ? lastError : new Error(message);
|
|
909
|
-
}
|
|
910
|
-
async sendMeterValues(connectorId, context, deltaWh, measurands) {
|
|
911
|
-
const connector = this.connectors.get(connectorId);
|
|
912
|
-
const nextMeter = connector.meterWh + deltaWh;
|
|
913
|
-
this.connectors.patch(connectorId, { meterWh: nextMeter });
|
|
914
|
-
this.savePersistedState();
|
|
915
|
-
if (!this.client.isConnected || connector.pendingStartTransaction) {
|
|
916
|
-
await this.stopTransactionIfInvalidEnergyLimitReached(connectorId, nextMeter);
|
|
917
|
-
return;
|
|
918
|
-
}
|
|
919
|
-
const payload = {
|
|
920
|
-
connectorId,
|
|
921
|
-
meterValue: [buildMeterValue(this.connectors.get(connectorId), context, measurands)]
|
|
922
|
-
};
|
|
923
|
-
if (connector.transactionId) {
|
|
924
|
-
payload.transactionId = connector.transactionId;
|
|
925
|
-
}
|
|
926
|
-
await this.send("MeterValues", payload);
|
|
927
|
-
await this.stopTransactionIfInvalidEnergyLimitReached(connectorId, nextMeter);
|
|
928
|
-
}
|
|
929
|
-
buildStopTransactionData(connectorId) {
|
|
930
|
-
return [
|
|
931
|
-
buildMeterValue(this.connectors.get(connectorId), "Transaction.End", this.configuration.measurands("StopTxnSampledData")),
|
|
932
|
-
buildMeterValue(this.connectors.get(connectorId), "Sample.Clock", this.configuration.measurands("StopTxnAlignedData"))
|
|
933
|
-
];
|
|
934
|
-
}
|
|
935
|
-
authorizeLocally(idTag, requireOfflinePermission) {
|
|
936
|
-
return authorizeLocally(idTag, requireOfflinePermission, this.localAuthorizationList, this.authorizationCache, {
|
|
937
|
-
localAuthListEnabled: this.configuration.boolean("LocalAuthListEnabled", true),
|
|
938
|
-
localAuthorizeOffline: this.configuration.boolean("LocalAuthorizeOffline", false),
|
|
939
|
-
authorizationCacheEnabled: this.configuration.boolean("AuthorizationCacheEnabled", true),
|
|
940
|
-
allowOfflineTxForUnknownId: this.configuration.boolean("AllowOfflineTxForUnknownId", false)
|
|
941
|
-
});
|
|
942
|
-
}
|
|
943
|
-
rememberAuthorization(idTag, idTagInfo) {
|
|
944
|
-
if (!this.configuration.boolean("AuthorizationCacheEnabled", true)) {
|
|
945
|
-
return;
|
|
946
|
-
}
|
|
947
|
-
this.authorizationCache.set(idTag, serializeIdTagInfo(idTagInfo));
|
|
948
|
-
this.savePersistedState();
|
|
949
|
-
}
|
|
950
|
-
readIdTagInfo(value) {
|
|
951
|
-
return normalizeIdTagInfo(readObject(value));
|
|
952
|
-
}
|
|
953
|
-
async stopTransactionsForInvalidIdTag(idTag) {
|
|
954
|
-
if (!this.configuration.boolean("StopTransactionOnInvalidId", false)) {
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
const maxEnergyOnInvalidId = this.configuration.integer("MaxEnergyOnInvalidId", 0);
|
|
958
|
-
const connectors = this.connectors.all().filter((connector) => connector.transactionId && connector.lastIdTag === idTag);
|
|
959
|
-
for (const connector of connectors) {
|
|
960
|
-
if (maxEnergyOnInvalidId <= 0) {
|
|
961
|
-
await this.stopTransaction(connector.id, "DeAuthorized", idTag);
|
|
962
|
-
continue;
|
|
963
|
-
}
|
|
964
|
-
this.connectors.patch(connector.id, { stopTransactionAtWh: connector.meterWh + maxEnergyOnInvalidId });
|
|
965
|
-
this.log("warn", `Connector ${connector.id} will stop after ${maxEnergyOnInvalidId}Wh because idTag ${idTag} is no longer authorized`);
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
async stopTransactionIfInvalidEnergyLimitReached(connectorId, meterWh) {
|
|
969
|
-
const connector = this.connectors.get(connectorId);
|
|
970
|
-
if (!connector.transactionId || connector.stopTransactionAtWh === undefined || meterWh < connector.stopTransactionAtWh) {
|
|
971
|
-
return;
|
|
972
|
-
}
|
|
973
|
-
await this.stopTransaction(connectorId, "DeAuthorized", connector.lastIdTag);
|
|
974
|
-
}
|
|
975
|
-
refreshConnectorSnapshot() {
|
|
976
|
-
const { connectors, primary } = this.connectors.snapshot();
|
|
977
|
-
this.state.connectors = connectors;
|
|
978
|
-
if (primary) {
|
|
979
|
-
this.state.connectorStatus = primary.status;
|
|
980
|
-
this.state.availability = primary.availability;
|
|
981
|
-
this.state.transactionId = primary.transactionId;
|
|
982
|
-
this.state.reservationId = primary.reservationId;
|
|
983
|
-
this.state.meterWh = primary.meterWh;
|
|
984
|
-
}
|
|
985
|
-
this.emit("state", { ...this.state, connectors });
|
|
986
|
-
}
|
|
987
|
-
clearReservation(connectorId) {
|
|
988
|
-
const connector = this.connectors.get(connectorId);
|
|
989
|
-
this.reservationScheduler.clear(connectorId);
|
|
990
|
-
this.connectors.patch(connectorId, {
|
|
991
|
-
reservationId: undefined,
|
|
992
|
-
reservationIdTag: undefined,
|
|
993
|
-
reservationParentIdTag: undefined,
|
|
994
|
-
reservationExpiryDate: undefined,
|
|
995
|
-
status: statusAfterReservationClear(connector)
|
|
996
|
-
});
|
|
997
|
-
this.savePersistedState();
|
|
998
|
-
}
|
|
999
|
-
async sendClockAlignedMeterValues(connectorIds, deltaWh) {
|
|
1000
|
-
const measurands = this.configuration.measurands("MeterValuesAlignedData");
|
|
1001
|
-
for (const connectorId of connectorIds) {
|
|
1002
|
-
await this.sendMeterValues(connectorId, "Sample.Clock", deltaWh, measurands);
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
expireReservation(connectorId) {
|
|
1006
|
-
const connector = this.connectors.find(connectorId);
|
|
1007
|
-
if (!connector?.reservationId) {
|
|
1008
|
-
return;
|
|
1009
|
-
}
|
|
1010
|
-
this.log("warn", `Reservation ${connector.reservationId} expired on connector ${connectorId}`);
|
|
1011
|
-
this.clearReservation(connectorId);
|
|
1012
|
-
void this.runSafely(() => this.statusNotification(connectorId, this.connectors.get(connectorId).status));
|
|
1013
|
-
}
|
|
1014
|
-
loadPersistedState() {
|
|
1015
|
-
if (!this.store) {
|
|
1016
|
-
return;
|
|
1017
|
-
}
|
|
1018
|
-
let persisted;
|
|
1019
|
-
try {
|
|
1020
|
-
persisted = this.store.load();
|
|
1021
|
-
}
|
|
1022
|
-
catch (error) {
|
|
1023
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1024
|
-
this.log("warn", `Could not load persisted station state: ${message}`);
|
|
1025
|
-
return;
|
|
1026
|
-
}
|
|
1027
|
-
if (!persisted) {
|
|
1028
|
-
return;
|
|
1029
|
-
}
|
|
1030
|
-
const restored = restorePersistedStationState({
|
|
1031
|
-
persisted,
|
|
1032
|
-
connectorId: this.config.connectorId,
|
|
1033
|
-
connectors: this.connectors,
|
|
1034
|
-
configuration: this.configuration,
|
|
1035
|
-
localAuthorizationList: this.localAuthorizationList,
|
|
1036
|
-
authorizationCache: this.authorizationCache,
|
|
1037
|
-
scheduleReservation: (connectorId) => this.reservationScheduler.schedule(connectorId)
|
|
1038
|
-
});
|
|
1039
|
-
this.state.localListVersion = restored.localListVersion;
|
|
1040
|
-
this.chargingProfiles.replace(restored.chargingProfiles);
|
|
1041
|
-
}
|
|
1042
|
-
savePersistedState() {
|
|
1043
|
-
if (!this.store) {
|
|
1044
|
-
return;
|
|
1045
|
-
}
|
|
1046
|
-
try {
|
|
1047
|
-
this.store.save(buildPersistedStationState({
|
|
1048
|
-
chargePointId: this.config.chargePointId,
|
|
1049
|
-
connectorId: this.config.connectorId,
|
|
1050
|
-
connectors: this.connectors.all(),
|
|
1051
|
-
localListVersion: this.state.localListVersion,
|
|
1052
|
-
localAuthorizationList: this.localAuthorizationList,
|
|
1053
|
-
authorizationCache: this.authorizationCache,
|
|
1054
|
-
chargingProfiles: this.chargingProfiles.entries(),
|
|
1055
|
-
configuration: this.configuration.values
|
|
1056
|
-
}));
|
|
1057
|
-
}
|
|
1058
|
-
catch (error) {
|
|
1059
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1060
|
-
this.log("error", `Could not save station state: ${message}`);
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
startHeartbeatTimer(intervalSeconds) {
|
|
1064
|
-
this.stopHeartbeatTimer();
|
|
1065
|
-
this.heartbeatTimer = setInterval(() => {
|
|
1066
|
-
void this.runSafely(() => this.heartbeat());
|
|
1067
|
-
}, intervalSeconds * 1000);
|
|
1068
|
-
}
|
|
1069
|
-
stopHeartbeatTimer() {
|
|
1070
|
-
if (!this.heartbeatTimer) {
|
|
1071
|
-
return;
|
|
1072
|
-
}
|
|
1073
|
-
clearInterval(this.heartbeatTimer);
|
|
1074
|
-
this.heartbeatTimer = undefined;
|
|
1075
|
-
}
|
|
1076
|
-
async performReset(reason, resetType) {
|
|
1077
|
-
this.log("warn", `${resetType} reset accepted; rebooting station`);
|
|
1078
|
-
this.stopHeartbeatTimer();
|
|
1079
|
-
this.clearResetTimer();
|
|
1080
|
-
this.maintenance.clearAll();
|
|
1081
|
-
this.meterValueScheduler.stopClockAligned();
|
|
1082
|
-
for (const connector of this.connectors.all()) {
|
|
1083
|
-
if (connector.transactionId) {
|
|
1084
|
-
await this.stopTransaction(connector.id, reason);
|
|
1085
|
-
const resetStatus = this.connectors.get(connector.id).availability === "Inoperative" ? "Unavailable" : "Available";
|
|
1086
|
-
this.connectors.patch(connector.id, { evConnected: false, status: resetStatus });
|
|
1087
|
-
void this.runSafely(() => this.statusNotification(connector.id, resetStatus));
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
this.meterValueScheduler.stopAllPeriodic();
|
|
1091
|
-
for (const connector of this.connectors.all()) {
|
|
1092
|
-
if (!connector.reservationId) {
|
|
1093
|
-
continue;
|
|
1094
|
-
}
|
|
1095
|
-
this.clearReservation(connector.id);
|
|
1096
|
-
void this.runSafely(() => this.statusNotification(connector.id, this.connectors.get(connector.id).status));
|
|
1097
|
-
}
|
|
1098
|
-
this.patchState({ booted: false, registrationStatus: "Unknown" });
|
|
1099
|
-
this.resetTimer = setTimeout(() => {
|
|
1100
|
-
this.resetTimer = undefined;
|
|
1101
|
-
if (this.state.connected) {
|
|
1102
|
-
void this.runSafely(() => this.bootAfterReset(this.configuration.integer("ResetRetries", 0)));
|
|
1103
|
-
}
|
|
1104
|
-
}, 500);
|
|
1105
|
-
}
|
|
1106
|
-
async bootAfterReset(remainingRetries) {
|
|
1107
|
-
await this.boot({ scheduleRetry: false });
|
|
1108
|
-
if (this.state.registrationStatus === "Accepted" || remainingRetries <= 0 || !this.state.connected) {
|
|
1109
|
-
return;
|
|
1110
|
-
}
|
|
1111
|
-
this.log("warn", `Reset reboot BootNotification was ${this.state.registrationStatus}; retrying (${remainingRetries} left)`);
|
|
1112
|
-
this.resetTimer = setTimeout(() => {
|
|
1113
|
-
this.resetTimer = undefined;
|
|
1114
|
-
if (this.state.connected) {
|
|
1115
|
-
void this.runSafely(() => this.bootAfterReset(remainingRetries - 1));
|
|
1116
|
-
}
|
|
1117
|
-
}, 500);
|
|
1118
|
-
}
|
|
1119
|
-
clearResetTimer() {
|
|
1120
|
-
if (!this.resetTimer) {
|
|
1121
|
-
return;
|
|
1122
|
-
}
|
|
1123
|
-
clearTimeout(this.resetTimer);
|
|
1124
|
-
this.resetTimer = undefined;
|
|
1125
|
-
}
|
|
1126
|
-
scheduleBootRetry(intervalSeconds) {
|
|
1127
|
-
if (this.bootRetryTimer || !this.state.connected) {
|
|
1128
|
-
return;
|
|
1129
|
-
}
|
|
1130
|
-
const delayMs = Math.max(1, intervalSeconds) * 1000;
|
|
1131
|
-
this.log("warn", `BootNotification was ${this.state.registrationStatus}; retrying in ${delayMs}ms`);
|
|
1132
|
-
this.bootRetryTimer = setTimeout(() => {
|
|
1133
|
-
this.bootRetryTimer = undefined;
|
|
1134
|
-
if (this.state.connected) {
|
|
1135
|
-
void this.runSafely(() => this.boot());
|
|
1136
|
-
}
|
|
1137
|
-
}, delayMs);
|
|
1138
|
-
}
|
|
1139
|
-
clearBootRetryTimer() {
|
|
1140
|
-
if (!this.bootRetryTimer) {
|
|
1141
|
-
return;
|
|
1142
|
-
}
|
|
1143
|
-
clearTimeout(this.bootRetryTimer);
|
|
1144
|
-
this.bootRetryTimer = undefined;
|
|
1145
|
-
}
|
|
1146
|
-
scheduleReconnect() {
|
|
1147
|
-
if (this.manuallyDisconnected ||
|
|
1148
|
-
!this.config.webSocketReconnectEnabled ||
|
|
1149
|
-
this.reconnectTimer ||
|
|
1150
|
-
this.state.connected) {
|
|
1151
|
-
return;
|
|
1152
|
-
}
|
|
1153
|
-
const maxAttempts = this.config.webSocketReconnectMaxAttempts;
|
|
1154
|
-
if (maxAttempts > 0 && this.reconnectAttempts >= maxAttempts) {
|
|
1155
|
-
this.log("error", `Reconnect stopped after ${this.reconnectAttempts} failed attempt(s)`);
|
|
1156
|
-
return;
|
|
1157
|
-
}
|
|
1158
|
-
this.reconnectAttempts += 1;
|
|
1159
|
-
const delayMs = this.reconnectDelayMs(this.reconnectAttempts);
|
|
1160
|
-
this.log("warn", `Reconnect attempt ${this.reconnectAttempts}${maxAttempts > 0 ? `/${maxAttempts}` : ""} in ${delayMs}ms`);
|
|
1161
|
-
this.reconnectTimer = setTimeout(() => {
|
|
1162
|
-
this.reconnectTimer = undefined;
|
|
1163
|
-
if (this.manuallyDisconnected || this.state.connected) {
|
|
1164
|
-
return;
|
|
1165
|
-
}
|
|
1166
|
-
this.log("info", `Reconnecting to ${this.config.centralSystemUrl}`);
|
|
1167
|
-
this.client.connect();
|
|
1168
|
-
}, delayMs);
|
|
1169
|
-
}
|
|
1170
|
-
reconnectDelayMs(attempt) {
|
|
1171
|
-
const initial = Math.max(0, this.config.webSocketReconnectInitialDelayMs);
|
|
1172
|
-
const max = Math.max(initial, this.config.webSocketReconnectMaxDelayMs);
|
|
1173
|
-
return Math.min(initial * 2 ** Math.max(0, attempt - 1), max);
|
|
1174
|
-
}
|
|
1175
|
-
clearReconnectTimer() {
|
|
1176
|
-
if (!this.reconnectTimer) {
|
|
1177
|
-
return;
|
|
1178
|
-
}
|
|
1179
|
-
clearTimeout(this.reconnectTimer);
|
|
1180
|
-
this.reconnectTimer = undefined;
|
|
1181
|
-
}
|
|
1182
|
-
patchState(patch) {
|
|
1183
|
-
Object.assign(this.state, patch);
|
|
1184
|
-
this.emit("state", { ...this.state });
|
|
1185
|
-
}
|
|
1186
|
-
log(level, message, details) {
|
|
1187
|
-
this.emit("log", { at: new Date(), level, message, details });
|
|
1188
|
-
}
|
|
1189
|
-
async runSafely(task) {
|
|
1190
|
-
try {
|
|
1191
|
-
await task();
|
|
1192
|
-
}
|
|
1193
|
-
catch {
|
|
1194
|
-
// Errors are logged in send(); keeping async UI handlers quiet avoids unhandled rejections.
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
function trimOptional(value, maxLength) {
|
|
1199
|
-
const trimmed = value?.trim();
|
|
1200
|
-
if (!trimmed) {
|
|
1201
|
-
return undefined;
|
|
1202
|
-
}
|
|
1203
|
-
return trimmed.slice(0, maxLength);
|
|
1204
|
-
}
|