UNPKG

typed-ocpp

Version:

A library for type-aware parsing, serialization and validation of OCPP 1.6, OCPP 2.0 and OCPP 2.1 messages

125 lines 7.26 kB
import { EMPTY_ARR, assign } from '../common/utils.js'; import { Action, MessageType } from './utils.js'; import { validate } from '../common/ajv.js'; import * as schemas from './schemas.js'; import * as types from './types.js'; const schemasByCommand = { [Action.AdjustPeriodicEventStream]: schemas.AdjustPeriodicEventStreamRequest, [Action.AFRRSignal]: schemas.AFRRSignalRequest, [Action.Authorize]: schemas.AuthorizeRequest, [Action.BatterySwap]: schemas.BatterySwapRequest, [Action.BootNotification]: schemas.BootNotificationRequest, [Action.CancelReservation]: schemas.CancelReservationRequest, [Action.CertificateSigned]: schemas.CertificateSignedRequest, [Action.ChangeAvailability]: schemas.ChangeAvailabilityRequest, [Action.ChangeTransactionTariff]: schemas.ChangeTransactionTariffRequest, [Action.ClearCache]: schemas.ClearCacheRequest, [Action.ClearChargingProfile]: schemas.ClearChargingProfileRequest, [Action.ClearDERControl]: schemas.ClearDERControlRequest, [Action.ClearDisplayMessage]: schemas.ClearDisplayMessageRequest, [Action.ClearedChargingLimit]: schemas.ClearedChargingLimitRequest, [Action.ClearTariffs]: schemas.ClearTariffsRequest, [Action.ClearVariableMonitoring]: schemas.ClearVariableMonitoringRequest, [Action.ClosePeriodicEventStream]: schemas.ClosePeriodicEventStreamRequest, [Action.CostUpdated]: schemas.CostUpdatedRequest, [Action.CustomerInformation]: schemas.CustomerInformationRequest, [Action.DataTransfer]: schemas.DataTransferRequest, [Action.DeleteCertificate]: schemas.DeleteCertificateRequest, [Action.FirmwareStatusNotification]: schemas.FirmwareStatusNotificationRequest, [Action.Get15118EVCertificate]: schemas.Get15118EVCertificateRequest, [Action.GetBaseReport]: schemas.GetBaseReportRequest, [Action.GetCertificateChainStatus]: schemas.GetCertificateChainStatusRequest, [Action.GetCertificateStatus]: schemas.GetCertificateStatusRequest, [Action.GetChargingProfiles]: schemas.GetChargingProfilesRequest, [Action.GetCompositeSchedule]: schemas.GetCompositeScheduleRequest, [Action.GetDERControl]: schemas.GetDERControlRequest, [Action.GetDisplayMessages]: schemas.GetDisplayMessagesRequest, [Action.GetInstalledCertificateIds]: schemas.GetInstalledCertificateIdsRequest, [Action.GetLocalListVersion]: schemas.GetLocalListVersionRequest, [Action.GetLog]: schemas.GetLogRequest, [Action.GetMonitoringReport]: schemas.GetMonitoringReportRequest, [Action.GetPeriodicEventStream]: schemas.GetPeriodicEventStreamRequest, [Action.GetReport]: schemas.GetReportRequest, [Action.GetTariffs]: schemas.GetTariffsRequest, [Action.GetTransactionStatus]: schemas.GetTransactionStatusRequest, [Action.GetVariables]: schemas.GetVariablesRequest, [Action.Heartbeat]: schemas.HeartbeatRequest, [Action.InstallCertificate]: schemas.InstallCertificateRequest, [Action.LogStatusNotification]: schemas.LogStatusNotificationRequest, [Action.MeterValues]: schemas.MeterValuesRequest, [Action.NotifyAllowedEnergyTransfer]: schemas.NotifyAllowedEnergyTransferRequest, [Action.NotifyChargingLimit]: schemas.NotifyChargingLimitRequest, [Action.NotifyCustomerInformation]: schemas.NotifyCustomerInformationRequest, [Action.NotifyDERAlarm]: schemas.NotifyDERAlarmRequest, [Action.NotifyDERStartStop]: schemas.NotifyDERStartStopRequest, [Action.NotifyDisplayMessages]: schemas.NotifyDisplayMessagesRequest, [Action.NotifyEVChargingNeeds]: schemas.NotifyEVChargingNeedsRequest, [Action.NotifyEVChargingSchedule]: schemas.NotifyEVChargingScheduleRequest, [Action.NotifyEvent]: schemas.NotifyEventRequest, [Action.NotifyMonitoringReport]: schemas.NotifyMonitoringReportRequest, [Action.NotifyPeriodicEventStream]: schemas.NotifyPeriodicEventStream, [Action.NotifyPriorityCharging]: schemas.NotifyPriorityChargingRequest, [Action.NotifyReport]: schemas.NotifyReportRequest, [Action.NotifySettlement]: schemas.NotifySettlementRequest, [Action.NotifyWebPaymentStarted]: schemas.NotifyWebPaymentStartedRequest, [Action.OpenPeriodicEventStream]: schemas.OpenPeriodicEventStreamRequest, [Action.PublishFirmware]: schemas.PublishFirmwareRequest, [Action.PublishFirmwareStatusNotification]: schemas.PublishFirmwareStatusNotificationRequest, [Action.PullDynamicScheduleUpdate]: schemas.PullDynamicScheduleUpdateRequest, [Action.ReportChargingProfiles]: schemas.ReportChargingProfilesRequest, [Action.ReportDERControl]: schemas.ReportDERControlRequest, [Action.RequestBatterySwap]: schemas.RequestBatterySwapRequest, [Action.RequestStartTransaction]: schemas.RequestStartTransactionRequest, [Action.RequestStopTransaction]: schemas.RequestStopTransactionRequest, [Action.ReservationStatusUpdate]: schemas.ReservationStatusUpdateRequest, [Action.ReserveNow]: schemas.ReserveNowRequest, [Action.Reset]: schemas.ResetRequest, [Action.SecurityEventNotification]: schemas.SecurityEventNotificationRequest, [Action.SendLocalList]: schemas.SendLocalListRequest, [Action.SetChargingProfile]: schemas.SetChargingProfileRequest, [Action.SetDefaultTariff]: schemas.SetDefaultTariffRequest, [Action.SetDERControl]: schemas.SetDERControlRequest, [Action.SetDisplayMessage]: schemas.SetDisplayMessageRequest, [Action.SetMonitoringBase]: schemas.SetMonitoringBaseRequest, [Action.SetMonitoringLevel]: schemas.SetMonitoringLevelRequest, [Action.SetNetworkProfile]: schemas.SetNetworkProfileRequest, [Action.SetVariableMonitoring]: schemas.SetVariableMonitoringRequest, [Action.SetVariables]: schemas.SetVariablesRequest, [Action.SignCertificate]: schemas.SignCertificateRequest, [Action.StatusNotification]: schemas.StatusNotificationRequest, [Action.TransactionEvent]: schemas.TransactionEventRequest, [Action.TriggerMessage]: schemas.TriggerMessageRequest, [Action.UnlockConnector]: schemas.UnlockConnectorRequest, [Action.UnpublishFirmware]: schemas.UnpublishFirmwareRequest, [Action.UpdateDynamicSchedule]: schemas.UpdateDynamicScheduleRequest, [Action.UpdateFirmware]: schemas.UpdateFirmwareRequest, [Action.UsePriorityCharging]: schemas.UsePriorityChargingRequest, [Action.VatNumberValidation]: schemas.VatNumberValidationRequest, }; const basecall_schema = { type: 'array', items: [ { type: 'number', enum: [MessageType.CALL, MessageType.SEND] }, { type: 'string' }, { type: 'string', enum: Object.values(Action) }, { type: 'object', additionalProperties: true }, ], minItems: 4, maxItems: 4 }; export const validateCall = assign((arr) => { if (!validate(arr, basecall_schema, 'Invalid OCPP call')) { validateCall.errors = validate.errors; return false; } const [, , action, payload] = arr; const schema = schemasByCommand[action]; if (!validate(payload, schema, 'Invalid OCPP call')) { validateCall.errors = validate.errors; return false; } validateCall.errors = EMPTY_ARR; return true; }, { errors: EMPTY_ARR }); validateCall.errors = EMPTY_ARR; //# sourceMappingURL=call.js.map