@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.
230 lines • 11.4 kB
JavaScript
// Copyright (c) 2023 S44, LLC
// Copyright Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache 2.0
/**
* 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 || (OCPPVersion = {}));
export var OCPP1_6_CallAction;
(function (OCPP1_6_CallAction) {
OCPP1_6_CallAction["Authorize"] = "Authorize";
OCPP1_6_CallAction["BootNotification"] = "BootNotification";
OCPP1_6_CallAction["CancelReservation"] = "CancelReservation";
OCPP1_6_CallAction["ChangeAvailability"] = "ChangeAvailability";
OCPP1_6_CallAction["ChangeConfiguration"] = "ChangeConfiguration";
OCPP1_6_CallAction["ClearCache"] = "ClearCache";
OCPP1_6_CallAction["ClearChargingProfile"] = "ClearChargingProfile";
OCPP1_6_CallAction["DataTransfer"] = "DataTransfer";
OCPP1_6_CallAction["DiagnosticsStatusNotification"] = "DiagnosticsStatusNotification";
OCPP1_6_CallAction["FirmwareStatusNotification"] = "FirmwareStatusNotification";
OCPP1_6_CallAction["GetCompositeSchedule"] = "GetCompositeSchedule";
OCPP1_6_CallAction["GetConfiguration"] = "GetConfiguration";
OCPP1_6_CallAction["GetDiagnostics"] = "GetDiagnostics";
OCPP1_6_CallAction["GetLocalListVersion"] = "GetLocalListVersion";
OCPP1_6_CallAction["Heartbeat"] = "Heartbeat";
OCPP1_6_CallAction["MeterValues"] = "MeterValues";
OCPP1_6_CallAction["RemoteStartTransaction"] = "RemoteStartTransaction";
OCPP1_6_CallAction["RemoteStopTransaction"] = "RemoteStopTransaction";
OCPP1_6_CallAction["ReserveNow"] = "ReserveNow";
OCPP1_6_CallAction["Reset"] = "Reset";
OCPP1_6_CallAction["SendLocalList"] = "SendLocalList";
OCPP1_6_CallAction["SetChargingProfile"] = "SetChargingProfile";
OCPP1_6_CallAction["StartTransaction"] = "StartTransaction";
OCPP1_6_CallAction["StatusNotification"] = "StatusNotification";
OCPP1_6_CallAction["StopTransaction"] = "StopTransaction";
OCPP1_6_CallAction["TriggerMessage"] = "TriggerMessage";
OCPP1_6_CallAction["UnlockConnector"] = "UnlockConnector";
OCPP1_6_CallAction["UpdateFirmware"] = "UpdateFirmware";
})(OCPP1_6_CallAction || (OCPP1_6_CallAction = {}));
export var OCPP2_0_1_CallAction;
(function (OCPP2_0_1_CallAction) {
OCPP2_0_1_CallAction["Authorize"] = "Authorize";
OCPP2_0_1_CallAction["BootNotification"] = "BootNotification";
OCPP2_0_1_CallAction["CancelReservation"] = "CancelReservation";
OCPP2_0_1_CallAction["CertificateSigned"] = "CertificateSigned";
OCPP2_0_1_CallAction["ChangeAvailability"] = "ChangeAvailability";
OCPP2_0_1_CallAction["ClearCache"] = "ClearCache";
OCPP2_0_1_CallAction["ClearChargingProfile"] = "ClearChargingProfile";
OCPP2_0_1_CallAction["ClearDisplayMessage"] = "ClearDisplayMessage";
OCPP2_0_1_CallAction["ClearedChargingLimit"] = "ClearedChargingLimit";
OCPP2_0_1_CallAction["ClearVariableMonitoring"] = "ClearVariableMonitoring";
OCPP2_0_1_CallAction["CostUpdated"] = "CostUpdated";
OCPP2_0_1_CallAction["CustomerInformation"] = "CustomerInformation";
OCPP2_0_1_CallAction["DataTransfer"] = "DataTransfer";
OCPP2_0_1_CallAction["DeleteCertificate"] = "DeleteCertificate";
OCPP2_0_1_CallAction["FirmwareStatusNotification"] = "FirmwareStatusNotification";
OCPP2_0_1_CallAction["Get15118EVCertificate"] = "Get15118EVCertificate";
OCPP2_0_1_CallAction["GetBaseReport"] = "GetBaseReport";
OCPP2_0_1_CallAction["GetCertificateStatus"] = "GetCertificateStatus";
OCPP2_0_1_CallAction["GetChargingProfiles"] = "GetChargingProfiles";
OCPP2_0_1_CallAction["GetCompositeSchedule"] = "GetCompositeSchedule";
OCPP2_0_1_CallAction["GetDisplayMessages"] = "GetDisplayMessages";
OCPP2_0_1_CallAction["GetInstalledCertificateIds"] = "GetInstalledCertificateIds";
OCPP2_0_1_CallAction["GetLocalListVersion"] = "GetLocalListVersion";
OCPP2_0_1_CallAction["GetLog"] = "GetLog";
OCPP2_0_1_CallAction["GetMonitoringReport"] = "GetMonitoringReport";
OCPP2_0_1_CallAction["GetReport"] = "GetReport";
OCPP2_0_1_CallAction["GetTransactionStatus"] = "GetTransactionStatus";
OCPP2_0_1_CallAction["GetVariables"] = "GetVariables";
OCPP2_0_1_CallAction["Heartbeat"] = "Heartbeat";
OCPP2_0_1_CallAction["InstallCertificate"] = "InstallCertificate";
OCPP2_0_1_CallAction["LogStatusNotification"] = "LogStatusNotification";
OCPP2_0_1_CallAction["MeterValues"] = "MeterValues";
OCPP2_0_1_CallAction["NotifyChargingLimit"] = "NotifyChargingLimit";
OCPP2_0_1_CallAction["NotifyCustomerInformation"] = "NotifyCustomerInformation";
OCPP2_0_1_CallAction["NotifyDisplayMessages"] = "NotifyDisplayMessages";
OCPP2_0_1_CallAction["NotifyEVChargingNeeds"] = "NotifyEVChargingNeeds";
OCPP2_0_1_CallAction["NotifyEVChargingSchedule"] = "NotifyEVChargingSchedule";
OCPP2_0_1_CallAction["NotifyEvent"] = "NotifyEvent";
OCPP2_0_1_CallAction["NotifyMonitoringReport"] = "NotifyMonitoringReport";
OCPP2_0_1_CallAction["NotifyReport"] = "NotifyReport";
OCPP2_0_1_CallAction["PublishFirmware"] = "PublishFirmware";
OCPP2_0_1_CallAction["PublishFirmwareStatusNotification"] = "PublishFirmwareStatusNotification";
OCPP2_0_1_CallAction["ReportChargingProfiles"] = "ReportChargingProfiles";
OCPP2_0_1_CallAction["RequestStartTransaction"] = "RequestStartTransaction";
OCPP2_0_1_CallAction["RequestStopTransaction"] = "RequestStopTransaction";
OCPP2_0_1_CallAction["ReservationStatusUpdate"] = "ReservationStatusUpdate";
OCPP2_0_1_CallAction["ReserveNow"] = "ReserveNow";
OCPP2_0_1_CallAction["Reset"] = "Reset";
OCPP2_0_1_CallAction["SecurityEventNotification"] = "SecurityEventNotification";
OCPP2_0_1_CallAction["SendLocalList"] = "SendLocalList";
OCPP2_0_1_CallAction["SetChargingProfile"] = "SetChargingProfile";
OCPP2_0_1_CallAction["SetDisplayMessage"] = "SetDisplayMessage";
OCPP2_0_1_CallAction["SetMonitoringBase"] = "SetMonitoringBase";
OCPP2_0_1_CallAction["SetMonitoringLevel"] = "SetMonitoringLevel";
OCPP2_0_1_CallAction["SetNetworkProfile"] = "SetNetworkProfile";
OCPP2_0_1_CallAction["SetVariableMonitoring"] = "SetVariableMonitoring";
OCPP2_0_1_CallAction["SetVariables"] = "SetVariables";
OCPP2_0_1_CallAction["SignCertificate"] = "SignCertificate";
OCPP2_0_1_CallAction["StatusNotification"] = "StatusNotification";
OCPP2_0_1_CallAction["TransactionEvent"] = "TransactionEvent";
OCPP2_0_1_CallAction["TriggerMessage"] = "TriggerMessage";
OCPP2_0_1_CallAction["UnlockConnector"] = "UnlockConnector";
OCPP2_0_1_CallAction["UnpublishFirmware"] = "UnpublishFirmware";
OCPP2_0_1_CallAction["UpdateFirmware"] = "UpdateFirmware";
})(OCPP2_0_1_CallAction || (OCPP2_0_1_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 {
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,
];
}
}
/**
* 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 'ocpp1.6':
if (action in OCPP1_6_CallAction) {
return OCPP1_6_CallAction[action];
}
throw new Error(`Invalid OCPP 1.6 action: ${action}`);
case 'ocpp2.0.1':
if (action in OCPP2_0_1_CallAction) {
return OCPP2_0_1_CallAction[action];
}
throw new Error(`Invalid OCPP 2.0.1 action: ${action}`);
default:
throw new Error(`Unsupported OCPP version: ${version}`);
}
}
//# sourceMappingURL=message.js.map