@citrineos/base
Version:
The base module for OCPP v2.0.1 including all interfaces. This module is not intended to be used directly, but rather as a dependency for other modules.
386 lines • 18 kB
JavaScript
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Expose } from 'class-transformer';
/**
* Number identifying the different types of OCPP messages.
*/
export var MessageTypeId;
(function (MessageTypeId) {
// Call identifies a request.
MessageTypeId[MessageTypeId["Call"] = 2] = "Call";
// CallResult identifies a successful response.
MessageTypeId[MessageTypeId["CallResult"] = 3] = "CallResult";
// CallError identifies an erroneous response.
MessageTypeId[MessageTypeId["CallError"] = 4] = "CallError";
})(MessageTypeId || (MessageTypeId = {}));
/**
* Supported OCPP versions
*/
export var OCPPVersion;
(function (OCPPVersion) {
OCPPVersion["OCPP1_6"] = "ocpp1.6";
OCPPVersion["OCPP2_0_1"] = "ocpp2.0.1";
OCPPVersion["OCPP2_1"] = "ocpp2.1";
})(OCPPVersion || (OCPPVersion = {}));
/**
* All OCPP 2.x versions
*/
export const OCPP_2_VER_LIST = [OCPPVersion.OCPP2_0_1, OCPPVersion.OCPP2_1];
// NoAction is used when the action is not known, for example when a message is invalid json or otherwise violates the OCPP protocol to the extent that the action cannot be determined. This allows us to still store and process these messages, while marking them as having an unknown action.
export const NO_ACTION = 'NoAction';
export var OCPP_CallAction;
(function (OCPP_CallAction) {
OCPP_CallAction["AdjustPeriodicEventStream"] = "AdjustPeriodicEventStream";
OCPP_CallAction["AFRRSignal"] = "AFRRSignal";
OCPP_CallAction["Authorize"] = "Authorize";
OCPP_CallAction["BatterySwap"] = "BatterySwap";
OCPP_CallAction["BootNotification"] = "BootNotification";
OCPP_CallAction["CancelReservation"] = "CancelReservation";
OCPP_CallAction["CertificateSigned"] = "CertificateSigned";
OCPP_CallAction["ChangeAvailability"] = "ChangeAvailability";
OCPP_CallAction["ChangeConfiguration"] = "ChangeConfiguration";
OCPP_CallAction["ChangeTransactionTariff"] = "ChangeTransactionTariff";
OCPP_CallAction["ClearCache"] = "ClearCache";
OCPP_CallAction["ClearChargingProfile"] = "ClearChargingProfile";
OCPP_CallAction["ClearDERControl"] = "ClearDERControl";
OCPP_CallAction["ClearDisplayMessage"] = "ClearDisplayMessage";
OCPP_CallAction["ClearTariffs"] = "ClearTariffs";
OCPP_CallAction["ClearVariableMonitoring"] = "ClearVariableMonitoring";
OCPP_CallAction["ClearedChargingLimit"] = "ClearedChargingLimit";
OCPP_CallAction["ClosePeriodicEventStream"] = "ClosePeriodicEventStream";
OCPP_CallAction["CostUpdated"] = "CostUpdated";
OCPP_CallAction["CustomerInformation"] = "CustomerInformation";
OCPP_CallAction["DataTransfer"] = "DataTransfer";
OCPP_CallAction["DeleteCertificate"] = "DeleteCertificate";
OCPP_CallAction["DiagnosticsStatusNotification"] = "DiagnosticsStatusNotification";
OCPP_CallAction["FirmwareStatusNotification"] = "FirmwareStatusNotification";
OCPP_CallAction["Get15118EVCertificate"] = "Get15118EVCertificate";
OCPP_CallAction["GetBaseReport"] = "GetBaseReport";
OCPP_CallAction["GetCertificateChainStatus"] = "GetCertificateChainStatus";
OCPP_CallAction["GetCertificateStatus"] = "GetCertificateStatus";
OCPP_CallAction["GetChargingProfiles"] = "GetChargingProfiles";
OCPP_CallAction["GetCompositeSchedule"] = "GetCompositeSchedule";
OCPP_CallAction["GetConfiguration"] = "GetConfiguration";
OCPP_CallAction["GetDERControl"] = "GetDERControl";
OCPP_CallAction["GetDiagnostics"] = "GetDiagnostics";
OCPP_CallAction["GetDisplayMessages"] = "GetDisplayMessages";
OCPP_CallAction["GetInstalledCertificateIds"] = "GetInstalledCertificateIds";
OCPP_CallAction["GetLocalListVersion"] = "GetLocalListVersion";
OCPP_CallAction["GetLog"] = "GetLog";
OCPP_CallAction["GetMonitoringReport"] = "GetMonitoringReport";
OCPP_CallAction["GetPeriodicEventStream"] = "GetPeriodicEventStream";
OCPP_CallAction["GetReport"] = "GetReport";
OCPP_CallAction["GetTariffs"] = "GetTariffs";
OCPP_CallAction["GetTransactionStatus"] = "GetTransactionStatus";
OCPP_CallAction["GetVariables"] = "GetVariables";
OCPP_CallAction["Heartbeat"] = "Heartbeat";
OCPP_CallAction["InstallCertificate"] = "InstallCertificate";
OCPP_CallAction["LogStatusNotification"] = "LogStatusNotification";
OCPP_CallAction["MeterValues"] = "MeterValues";
OCPP_CallAction["NotifyAllowedEnergyTransfer"] = "NotifyAllowedEnergyTransfer";
OCPP_CallAction["NotifyChargingLimit"] = "NotifyChargingLimit";
OCPP_CallAction["NotifyCustomerInformation"] = "NotifyCustomerInformation";
OCPP_CallAction["NotifyDERAlarm"] = "NotifyDERAlarm";
OCPP_CallAction["NotifyDERStartStop"] = "NotifyDERStartStop";
OCPP_CallAction["NotifyDisplayMessages"] = "NotifyDisplayMessages";
OCPP_CallAction["NotifyEVChargingNeeds"] = "NotifyEVChargingNeeds";
OCPP_CallAction["NotifyEVChargingSchedule"] = "NotifyEVChargingSchedule";
OCPP_CallAction["NotifyEvent"] = "NotifyEvent";
OCPP_CallAction["NotifyMonitoringReport"] = "NotifyMonitoringReport";
OCPP_CallAction["NotifyPeriodicEventStream"] = "NotifyPeriodicEventStream";
OCPP_CallAction["NotifyPriorityCharging"] = "NotifyPriorityCharging";
OCPP_CallAction["NotifyReport"] = "NotifyReport";
OCPP_CallAction["NotifySettlement"] = "NotifySettlement";
OCPP_CallAction["NotifyWebPaymentStarted"] = "NotifyWebPaymentStarted";
OCPP_CallAction["OpenPeriodicEventStream"] = "OpenPeriodicEventStream";
OCPP_CallAction["PublishFirmware"] = "PublishFirmware";
OCPP_CallAction["PublishFirmwareStatusNotification"] = "PublishFirmwareStatusNotification";
OCPP_CallAction["PullDynamicScheduleUpdate"] = "PullDynamicScheduleUpdate";
OCPP_CallAction["RemoteStartTransaction"] = "RemoteStartTransaction";
OCPP_CallAction["RemoteStopTransaction"] = "RemoteStopTransaction";
OCPP_CallAction["ReportChargingProfiles"] = "ReportChargingProfiles";
OCPP_CallAction["ReportDERControl"] = "ReportDERControl";
OCPP_CallAction["RequestBatterySwap"] = "RequestBatterySwap";
OCPP_CallAction["RequestStartTransaction"] = "RequestStartTransaction";
OCPP_CallAction["RequestStopTransaction"] = "RequestStopTransaction";
OCPP_CallAction["ReservationStatusUpdate"] = "ReservationStatusUpdate";
OCPP_CallAction["ReserveNow"] = "ReserveNow";
OCPP_CallAction["Reset"] = "Reset";
OCPP_CallAction["SecurityEventNotification"] = "SecurityEventNotification";
OCPP_CallAction["SendLocalList"] = "SendLocalList";
OCPP_CallAction["SetChargingProfile"] = "SetChargingProfile";
OCPP_CallAction["SetDefaultTariff"] = "SetDefaultTariff";
OCPP_CallAction["SetDERControl"] = "SetDERControl";
OCPP_CallAction["SetDisplayMessage"] = "SetDisplayMessage";
OCPP_CallAction["SetMonitoringBase"] = "SetMonitoringBase";
OCPP_CallAction["SetMonitoringLevel"] = "SetMonitoringLevel";
OCPP_CallAction["SetNetworkProfile"] = "SetNetworkProfile";
OCPP_CallAction["SetVariableMonitoring"] = "SetVariableMonitoring";
OCPP_CallAction["SetVariables"] = "SetVariables";
OCPP_CallAction["SignCertificate"] = "SignCertificate";
OCPP_CallAction["SignedFirmwareStatusNotification"] = "SignedFirmwareStatusNotification";
OCPP_CallAction["SignedUpdateFirmware"] = "SignedUpdateFirmware";
OCPP_CallAction["StartTransaction"] = "StartTransaction";
OCPP_CallAction["StatusNotification"] = "StatusNotification";
OCPP_CallAction["StopTransaction"] = "StopTransaction";
OCPP_CallAction["TransactionEvent"] = "TransactionEvent";
OCPP_CallAction["TriggerMessage"] = "TriggerMessage";
OCPP_CallAction["UnlockConnector"] = "UnlockConnector";
OCPP_CallAction["UnpublishFirmware"] = "UnpublishFirmware";
OCPP_CallAction["UpdateDynamicSchedule"] = "UpdateDynamicSchedule";
OCPP_CallAction["UpdateFirmware"] = "UpdateFirmware";
OCPP_CallAction["UsePriorityCharging"] = "UsePriorityCharging";
OCPP_CallAction["VatNumberValidation"] = "VatNumberValidation";
})(OCPP_CallAction || (OCPP_CallAction = {}));
/**
* Error codes for CallError message (4.3 RPC Framework Error Codes)
*
*/
export var ErrorCode;
(function (ErrorCode) {
/**
* Payload for Action is syntactically incorrect (OCPP 2.0.1 only, see FormationViolation for OCPP 1.6)
*/
ErrorCode["FormatViolation"] = "FormatViolation";
/**
* Payload for Action is syntactically incorrect (OCPP 1.6 only, see FormatViolation for OCPP 2.0.1)
*/
ErrorCode["FormationViolation"] = "FormationViolation";
/**
* Requested Action is not known by receiver
*/
ErrorCode["NotImplemented"] = "NotImplemented";
/**
* Payload for Action is not conform the PDU structure
*/
ErrorCode["ProtocolError"] = "ProtocolError";
/**
* Any other error not covered by the more specific error codes in this table
*/
ErrorCode["GenericError"] = "GenericError";
/**
* An internal error occurred and the receiver was not able to process the requested Action successfully
*/
ErrorCode["InternalError"] = "InternalError";
/**
* A message with a Message Type Number received that is not supported by this implementation.
*/
ErrorCode["MessageTypeNotSupported"] = "MessageTypeNotSupported";
/**
* Requested Action is recognized but not supported by the receiver
*/
ErrorCode["NotSupported"] = "NotSupported";
/**
* Payload for Action is syntactically correct but at least one of the fields violates occurrence constraints
*/
ErrorCode["OccurrenceConstraintViolation"] = "OccurrenceConstraintViolation";
/**
* Payload is syntactically correct but at least one field contains an invalid value
*/
ErrorCode["PropertyConstraintViolation"] = "PropertyConstraintViolation";
/**
* Content of the call is not a valid RPC Request, for example: MessageId could not be read.
*/
ErrorCode["RpcFrameworkError"] = "RpcFrameworkError";
/**
* During the processing of Action a security issue occurred preventing receiver from completing the Action successfully
*/
ErrorCode["SecurityError"] = "SecurityError";
/**
* Payload for Action is syntactically correct but at least one of the fields violates data type constraints (e.g. 'somestring': 12)
*/
ErrorCode["TypeConstraintViolation"] = "TypeConstraintViolation";
})(ErrorCode || (ErrorCode = {}));
/**
* Custom error to handle OCPP errors better.
*/
export class OcppError extends Error {
_messageId;
_errorCode;
_errorDetails;
get message() {
return super.message;
}
constructor(messageId, errorCode, errorDescription, errorDetails = {}) {
super(errorDescription);
this.name = 'OcppError';
this._messageId = messageId;
this._errorCode = errorCode;
this._errorDetails = errorDetails;
}
asCallError() {
return [
MessageTypeId.CallError,
this._messageId,
this._errorCode,
this.message,
this._errorDetails,
];
}
}
__decorate([
Expose(),
__metadata("design:type", String),
__metadata("design:paramtypes", [])
], OcppError.prototype, "message", null);
const OCPP_Base_CallActions = new Set([
OCPP_CallAction.Authorize,
OCPP_CallAction.BootNotification,
OCPP_CallAction.CancelReservation,
OCPP_CallAction.ChangeAvailability,
OCPP_CallAction.ClearCache,
OCPP_CallAction.ClearChargingProfile,
OCPP_CallAction.DataTransfer,
OCPP_CallAction.FirmwareStatusNotification,
OCPP_CallAction.GetCompositeSchedule,
OCPP_CallAction.GetConfiguration,
OCPP_CallAction.GetLocalListVersion,
OCPP_CallAction.Heartbeat,
OCPP_CallAction.MeterValues,
OCPP_CallAction.ReserveNow,
OCPP_CallAction.Reset,
OCPP_CallAction.SendLocalList,
OCPP_CallAction.SetChargingProfile,
OCPP_CallAction.StartTransaction,
OCPP_CallAction.StatusNotification,
OCPP_CallAction.StopTransaction,
OCPP_CallAction.TriggerMessage,
OCPP_CallAction.UnlockConnector,
OCPP_CallAction.UpdateFirmware,
]);
const OCPP_CallActions = new Set([
...OCPP_Base_CallActions,
OCPP_CallAction.ChangeConfiguration,
OCPP_CallAction.DiagnosticsStatusNotification,
OCPP_CallAction.GetConfiguration,
OCPP_CallAction.GetDiagnostics,
OCPP_CallAction.RemoteStartTransaction,
OCPP_CallAction.RemoteStopTransaction,
OCPP_CallAction.StartTransaction,
OCPP_CallAction.StopTransaction,
]);
const OCPP2_0_1_CallActions = new Set([
...OCPP_Base_CallActions,
OCPP_CallAction.CertificateSigned,
OCPP_CallAction.ClearDisplayMessage,
OCPP_CallAction.ClearedChargingLimit,
OCPP_CallAction.ClearVariableMonitoring,
OCPP_CallAction.CostUpdated,
OCPP_CallAction.CustomerInformation,
OCPP_CallAction.DeleteCertificate,
OCPP_CallAction.Get15118EVCertificate,
OCPP_CallAction.GetBaseReport,
OCPP_CallAction.GetCertificateStatus,
OCPP_CallAction.GetChargingProfiles,
OCPP_CallAction.GetDisplayMessages,
OCPP_CallAction.GetInstalledCertificateIds,
OCPP_CallAction.GetLog,
OCPP_CallAction.GetMonitoringReport,
OCPP_CallAction.GetReport,
OCPP_CallAction.GetTransactionStatus,
OCPP_CallAction.GetVariables,
OCPP_CallAction.InstallCertificate,
OCPP_CallAction.LogStatusNotification,
OCPP_CallAction.NotifyChargingLimit,
OCPP_CallAction.NotifyCustomerInformation,
OCPP_CallAction.NotifyDisplayMessages,
OCPP_CallAction.NotifyEVChargingNeeds,
OCPP_CallAction.NotifyEVChargingSchedule,
OCPP_CallAction.NotifyEvent,
OCPP_CallAction.NotifyMonitoringReport,
OCPP_CallAction.NotifyReport,
OCPP_CallAction.PublishFirmware,
OCPP_CallAction.PublishFirmwareStatusNotification,
OCPP_CallAction.ReportChargingProfiles,
OCPP_CallAction.RequestStartTransaction,
OCPP_CallAction.RequestStopTransaction,
OCPP_CallAction.ReservationStatusUpdate,
OCPP_CallAction.SecurityEventNotification,
OCPP_CallAction.SetDisplayMessage,
OCPP_CallAction.SetMonitoringBase,
OCPP_CallAction.SetMonitoringLevel,
OCPP_CallAction.SetNetworkProfile,
OCPP_CallAction.SetVariableMonitoring,
OCPP_CallAction.SetVariables,
OCPP_CallAction.SignCertificate,
OCPP_CallAction.TransactionEvent,
OCPP_CallAction.UnpublishFirmware,
]);
const OCPP2_1_CallActions = new Set([
...OCPP2_0_1_CallActions,
OCPP_CallAction.AdjustPeriodicEventStream,
OCPP_CallAction.AFRRSignal,
OCPP_CallAction.BatterySwap,
OCPP_CallAction.ChangeTransactionTariff,
OCPP_CallAction.ClearDERControl,
OCPP_CallAction.ClearTariffs,
OCPP_CallAction.ClosePeriodicEventStream,
OCPP_CallAction.GetCertificateChainStatus,
OCPP_CallAction.GetDERControl,
OCPP_CallAction.GetPeriodicEventStream,
OCPP_CallAction.GetTariffs,
OCPP_CallAction.NotifyAllowedEnergyTransfer,
OCPP_CallAction.NotifyDERAlarm,
OCPP_CallAction.NotifyDERStartStop,
OCPP_CallAction.NotifyPeriodicEventStream,
OCPP_CallAction.NotifyPriorityCharging,
OCPP_CallAction.NotifySettlement,
OCPP_CallAction.NotifyWebPaymentStarted,
OCPP_CallAction.OpenPeriodicEventStream,
OCPP_CallAction.PullDynamicScheduleUpdate,
OCPP_CallAction.ReportDERControl,
OCPP_CallAction.RequestBatterySwap,
OCPP_CallAction.SetDefaultTariff,
OCPP_CallAction.SetDERControl,
OCPP_CallAction.UpdateDynamicSchedule,
OCPP_CallAction.UsePriorityCharging,
OCPP_CallAction.VatNumberValidation,
]);
const ALLOWED_ACTIONS = {
[OCPPVersion.OCPP1_6]: OCPP_CallActions,
[OCPPVersion.OCPP2_0_1]: OCPP2_0_1_CallActions,
[OCPPVersion.OCPP2_1]: OCPP2_1_CallActions,
};
/**
* Maps a string to the corresponding OCPP CallAction enum value based on protocol version
* @param version OCPP protocol version
* @param action String representation of the action
* @returns The corresponding enum value
* @throws Error if the action is invalid for the specified version
*/
export function mapToCallAction(version, action) {
// Validate the action string is non-empty
if (!action || typeof action !== 'string') {
throw new Error('Action must be a non-empty string');
}
switch (version) {
case OCPPVersion.OCPP1_6:
if (action in OCPP_CallAction && ALLOWED_ACTIONS[OCPPVersion.OCPP1_6].has(action)) {
return OCPP_CallAction[action];
}
throw new Error(`Invalid OCPP 1.6 action: ${action}`);
case OCPPVersion.OCPP2_0_1:
if (action in OCPP_CallAction && ALLOWED_ACTIONS[OCPPVersion.OCPP2_0_1].has(action)) {
return OCPP_CallAction[action];
}
throw new Error(`Invalid OCPP 2.0.1 action: ${action}`);
case OCPPVersion.OCPP2_1:
if (action in OCPP_CallAction && ALLOWED_ACTIONS[OCPPVersion.OCPP2_1].has(action)) {
return OCPP_CallAction[action];
}
throw new Error(`Invalid OCPP 2.1 action: ${action}`);
default:
throw new Error(`Unsupported OCPP version: ${version}`);
}
}
//# sourceMappingURL=message.js.map