From cbc597893bd27de33f327621eb412f6744643c26 Mon Sep 17 00:00:00 2001 From: Rustem Yusupov Date: Wed, 8 Nov 2023 15:17:05 +0100 Subject: [PATCH] Add stubs to track reset and set charging profile commands. --- .eslintrc.js | 2 +- .../ChargeStation/configurations/default-ocpp-16.js | 4 ++++ .../ChargeStation/configurations/default-ocpp-20.ts | 4 ++++ src/lib/ChargeStation/eventHandlers/event-types.js | 2 ++ .../eventHandlers/ocpp-16/handle-reset.ts | 13 +++++++++++++ .../ocpp-16/handle-set-charging-profile.ts | 12 ++++++++++++ .../eventHandlers/ocpp-20/handle-reset.ts | 13 +++++++++++++ .../ocpp-20/handle-set-charging-profile.ts | 12 ++++++++++++ src/lib/ChargeStation/index.ts | 11 +++++++++-- src/screens/Dashboard/index.js | 4 ++-- 10 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/lib/ChargeStation/eventHandlers/ocpp-16/handle-reset.ts create mode 100644 src/lib/ChargeStation/eventHandlers/ocpp-16/handle-set-charging-profile.ts create mode 100644 src/lib/ChargeStation/eventHandlers/ocpp-20/handle-reset.ts create mode 100644 src/lib/ChargeStation/eventHandlers/ocpp-20/handle-set-charging-profile.ts diff --git a/.eslintrc.js b/.eslintrc.js index c708c90..5d72cb5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,7 +8,7 @@ module.exports = { settings: { jest: { version: 26 }, }, - parser: '@typescript-eslint/eslint-parser', + parser: '@typescript-eslint/parser', parserOptions: { sourceType: 'module', ecmaFeatures: { diff --git a/src/lib/ChargeStation/configurations/default-ocpp-16.js b/src/lib/ChargeStation/configurations/default-ocpp-16.js index 000b140..1e0ab0f 100644 --- a/src/lib/ChargeStation/configurations/default-ocpp-16.js +++ b/src/lib/ChargeStation/configurations/default-ocpp-16.js @@ -26,6 +26,8 @@ import handleStartTransactionCallResultReceived from '../eventHandlers/ocpp-16/h import handleStopTransactionCallResultReceived from '../eventHandlers/ocpp-16/handle-stop-transaction-call-result-received'; import sendChargingLimitReached from '../eventHandlers/ocpp-16/send-charging-limit-reached'; import sendMeterValues from '../eventHandlers/ocpp-16/send-meter-values'; +import handleReset from '../eventHandlers/ocpp-16/handle-reset'; +import handleSetChargingProfile from '../eventHandlers/ocpp-16/handle-set-charging-profile'; // This is the default configuration for OCPP 1.6 // Each key represents an event, and the value represents an array of handlers that will be called when the event is emitted @@ -69,4 +71,6 @@ export default { [e16.ChangeConfigurationReceived]: [handleChangeConfiguration], [e.ChargingTick]: [sendMeterValues], [e.ChargingLimitReached]: [sendChargingLimitReached], + [e.ResetReceived]: [handleReset], + [e.SetChargingProfileReceived]: [handleSetChargingProfile], }; diff --git a/src/lib/ChargeStation/configurations/default-ocpp-20.ts b/src/lib/ChargeStation/configurations/default-ocpp-20.ts index 5f199c5..f91f9d5 100644 --- a/src/lib/ChargeStation/configurations/default-ocpp-20.ts +++ b/src/lib/ChargeStation/configurations/default-ocpp-20.ts @@ -18,6 +18,8 @@ import sendStartTransaction from '../eventHandlers/ocpp-20/send-start-transactio import handleTransactionEventCallResultReceived from '../eventHandlers/ocpp-20/handle-transaction-event-call-result-received'; import sendTransationEventUpdated from '../eventHandlers/ocpp-20/send-transaction-event-updated'; import sendChargingLimitReached from '../eventHandlers/ocpp-20/send-charging-limit-reached'; +import handleReset from "lib/ChargeStation/eventHandlers/ocpp-20/handle-reset"; +import handleSetChargingProfile from "lib/ChargeStation/eventHandlers/ocpp-20/handle-set-charging-profile"; // This is the default configuration for OCPP 2.0.* // Each key represents an event, and the value represents an array of handlers that will be called when the event is emitted @@ -42,4 +44,6 @@ export default { ], [e.ChargingTick]: [sendTransationEventUpdated], [e.ChargingLimitReached]: [sendChargingLimitReached], + [e.ResetReceived]: [handleReset], + [e.SetChargingProfileReceived]: [handleSetChargingProfile], }; diff --git a/src/lib/ChargeStation/eventHandlers/event-types.js b/src/lib/ChargeStation/eventHandlers/event-types.js index 58c5617..5385f1b 100644 --- a/src/lib/ChargeStation/eventHandlers/event-types.js +++ b/src/lib/ChargeStation/eventHandlers/event-types.js @@ -14,6 +14,8 @@ export const EventTypes = { AuthorizeCallResultReceived: 'authorizeCallResultReceived', ChargingLimitReached: 'charingLimitReached', ChargingTick: 'chargingTick', + ResetReceived: 'resetReceived', + SetChargingProfileReceived: 'setChargingProfileReceived', }; // OCPP 1.6 specific events diff --git a/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-reset.ts b/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-reset.ts new file mode 100644 index 0000000..e1cbb80 --- /dev/null +++ b/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-reset.ts @@ -0,0 +1,13 @@ +import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers'; +import { ResetRequest } from 'schemas/ocpp/1.6/Reset'; +import { ResetResponse } from 'schemas/ocpp/1.6/ResetResponse'; + +const handleReset: ChargeStationEventHandler = + ({ chargepoint, callMessageId }) => { + const response: ResetResponse = {status: 'Accepted',}; + + chargepoint.writeCallResult(callMessageId, response); + chargepoint.reboot(); + }; + +export default handleReset; diff --git a/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-set-charging-profile.ts b/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-set-charging-profile.ts new file mode 100644 index 0000000..33504a2 --- /dev/null +++ b/src/lib/ChargeStation/eventHandlers/ocpp-16/handle-set-charging-profile.ts @@ -0,0 +1,12 @@ +import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers'; +import { SetChargingProfileRequest } from 'schemas/ocpp/1.6/SetChargingProfile'; +import { SetChargingProfileResponse } from 'schemas/ocpp/1.6/SetChargingProfileResponse'; + +const handleSetChargingProfile: ChargeStationEventHandler = + ({ chargepoint, callMessageId }) => { + const response: SetChargingProfileResponse = {status: 'Accepted'}; + + chargepoint.writeCallResult(callMessageId, response); + }; + +export default handleSetChargingProfile; diff --git a/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-reset.ts b/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-reset.ts new file mode 100644 index 0000000..9a58072 --- /dev/null +++ b/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-reset.ts @@ -0,0 +1,13 @@ +import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers'; +import { ResetRequest } from 'schemas/ocpp/2.0/ResetRequest'; +import { ResetResponse } from 'schemas/ocpp/2.0/ResetResponse'; + +const handleReset: ChargeStationEventHandler = + ({ chargepoint, callMessageId }) => { + const response: ResetResponse = {status: 'Accepted',}; + + chargepoint.writeCallResult(callMessageId, response); + chargepoint.reboot(); + }; + +export default handleReset; diff --git a/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-set-charging-profile.ts b/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-set-charging-profile.ts new file mode 100644 index 0000000..c9af283 --- /dev/null +++ b/src/lib/ChargeStation/eventHandlers/ocpp-20/handle-set-charging-profile.ts @@ -0,0 +1,12 @@ +import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers'; +import { SetChargingProfileRequest } from 'schemas/ocpp/2.0/SetChargingProfileRequest'; +import { SetChargingProfileResponse } from 'schemas/ocpp/2.0/SetChargingProfileResponse'; + +const handleSetChargingProfile: ChargeStationEventHandler = + ({ chargepoint, callMessageId }) => { + const response: SetChargingProfileResponse = {status: 'Accepted'}; + + chargepoint.writeCallResult(callMessageId, response); + }; + +export default handleSetChargingProfile; diff --git a/src/lib/ChargeStation/index.ts b/src/lib/ChargeStation/index.ts index a84c79f..3827af5 100644 --- a/src/lib/ChargeStation/index.ts +++ b/src/lib/ChargeStation/index.ts @@ -119,7 +119,7 @@ export default class ChargeStation { this.emitter.emitEvent(EventTypes.StationConnected); }; this.connection.onError = (error: Event) => { - if (!this.connected) return; + if (!this.connected) {return;} this.connected = false; @@ -186,6 +186,13 @@ export default class ChargeStation { } } + reboot() { + this.disconnect(); + setTimeout(() => { + this.connect(); + }, 1000); + } + reconnect() { if (this.numConnectionAttempts > 100) { this.log('error', 'Too many connection attempts, giving up'); @@ -194,7 +201,7 @@ export default class ChargeStation { const numSeconds = this.numConnectionAttempts < 5 ? 5 : 30; this.log('message', `> Reconnecting in ${numSeconds} seconds`); setTimeout(() => { - if (!this.connection) throw new Error('Connection is undefined'); + if (!this.connection) {throw new Error('Connection is undefined');} this.connection.connect(); this.numConnectionAttempts++; }, numSeconds * 1000); diff --git a/src/screens/Dashboard/index.js b/src/screens/Dashboard/index.js index c3e4822..1d7c90d 100644 --- a/src/screens/Dashboard/index.js +++ b/src/screens/Dashboard/index.js @@ -30,7 +30,7 @@ import { formatDateTimeRelative } from 'utils/date'; import StopSessionModal from './StopSessionModal'; import StatusNotificationModal from './StatusNotificationModal'; -const SESSION_STORAGE_KEY = 'chargeStationSettingsCache' +const SESSION_STORAGE_KEY = 'chargeStationSettingsCache'; @screen export default class Home extends React.Component { @@ -255,7 +255,7 @@ export default class Home extends React.Component { configuration.updateVariablesFromKeyValueMap(config); chargeStation.changeConfiguration(configuration); } - sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify({settings: savedSettings, config: config})) + sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify({settings: savedSettings, config: config})); this.setState( { settings: savedSettings,