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

105 lines 5.74 kB
import { EMPTY_ARR, assign } from '../common/utils.js'; import { validate } from '../common/ajv.js'; import { Action, MessageType } from './utils.js'; import * as schemas from './schemas.js'; import * as types from './types.js'; const unchecked_call_result_schema = { type: 'array', items: [ { type: 'number', enum: [MessageType.CALLRESULT] }, { type: 'string' }, { type: 'object', additionalProperties: true }, ], minItems: 3, maxItems: 3, }; const schemasByCommand = { [Action.Authorize]: schemas.AuthorizeResponse, [Action.BootNotification]: schemas.BootNotificationResponse, [Action.CancelReservation]: schemas.CancelReservationResponse, [Action.CertificateSigned]: schemas.CertificateSignedResponse, [Action.ChangeAvailability]: schemas.ChangeAvailabilityResponse, [Action.ClearCache]: schemas.ClearCacheResponse, [Action.ClearChargingProfile]: schemas.ClearChargingProfileResponse, [Action.ClearDisplayMessage]: schemas.ClearDisplayMessageResponse, [Action.ClearedChargingLimit]: schemas.ClearedChargingLimitResponse, [Action.ClearVariableMonitoring]: schemas.ClearVariableMonitoringResponse, [Action.CostUpdated]: schemas.CostUpdatedResponse, [Action.CustomerInformation]: schemas.CustomerInformationResponse, [Action.DataTransfer]: schemas.DataTransferResponse, [Action.DeleteCertificate]: schemas.DeleteCertificateResponse, [Action.FirmwareStatusNotification]: schemas.FirmwareStatusNotificationResponse, [Action.Get15118EVCertificate]: schemas.Get15118EVCertificateResponse, [Action.GetBaseReport]: schemas.GetBaseReportResponse, [Action.GetCertificateStatus]: schemas.GetCertificateStatusResponse, [Action.GetChargingProfiles]: schemas.GetChargingProfilesResponse, [Action.GetCompositeSchedule]: schemas.GetCompositeScheduleResponse, [Action.GetDisplayMessages]: schemas.GetDisplayMessagesResponse, [Action.GetInstalledCertificateIds]: schemas.GetInstalledCertificateIdsResponse, [Action.GetLocalListVersion]: schemas.GetLocalListVersionResponse, [Action.GetLog]: schemas.GetLogResponse, [Action.GetMonitoringReport]: schemas.GetMonitoringReportResponse, [Action.GetReport]: schemas.GetReportResponse, [Action.GetTransactionStatus]: schemas.GetTransactionStatusResponse, [Action.GetVariables]: schemas.GetVariablesResponse, [Action.Heartbeat]: schemas.HeartbeatResponse, [Action.InstallCertificate]: schemas.InstallCertificateResponse, [Action.LogStatusNotification]: schemas.LogStatusNotificationResponse, [Action.MeterValues]: schemas.MeterValuesResponse, [Action.NotifyChargingLimit]: schemas.NotifyChargingLimitResponse, [Action.NotifyCustomerInformation]: schemas.NotifyCustomerInformationResponse, [Action.NotifyDisplayMessages]: schemas.NotifyDisplayMessagesResponse, [Action.NotifyEVChargingNeeds]: schemas.NotifyEVChargingNeedsResponse, [Action.NotifyEVChargingSchedule]: schemas.NotifyEVChargingScheduleResponse, [Action.NotifyEvent]: schemas.NotifyEventResponse, [Action.NotifyMonitoringReport]: schemas.NotifyMonitoringReportResponse, [Action.NotifyReport]: schemas.NotifyReportResponse, [Action.PublishFirmware]: schemas.PublishFirmwareResponse, [Action.PublishFirmwareStatusNotification]: schemas.PublishFirmwareStatusNotificationResponse, [Action.ReportChargingProfiles]: schemas.ReportChargingProfilesResponse, [Action.RequestStartTransaction]: schemas.RequestStartTransactionResponse, [Action.RequestStopTransaction]: schemas.RequestStopTransactionResponse, [Action.ReservationStatusUpdate]: schemas.ReservationStatusUpdateResponse, [Action.ReserveNow]: schemas.ReserveNowResponse, [Action.Reset]: schemas.ResetResponse, [Action.SecurityEventNotification]: schemas.SecurityEventNotificationResponse, [Action.SendLocalList]: schemas.SendLocalListResponse, [Action.SetChargingProfile]: schemas.SetChargingProfileResponse, [Action.SetDisplayMessage]: schemas.SetDisplayMessageResponse, [Action.SetMonitoringBase]: schemas.SetMonitoringBaseResponse, [Action.SetMonitoringLevel]: schemas.SetMonitoringLevelResponse, [Action.SetNetworkProfile]: schemas.SetNetworkProfileResponse, [Action.SetVariableMonitoring]: schemas.SetVariableMonitoringResponse, [Action.SetVariables]: schemas.SetVariablesResponse, [Action.SignCertificate]: schemas.SignCertificateResponse, [Action.StatusNotification]: schemas.StatusNotificationResponse, [Action.TransactionEvent]: schemas.TransactionEventResponse, [Action.TriggerMessage]: schemas.TriggerMessageResponse, [Action.UnlockConnector]: schemas.UnlockConnectorResponse, [Action.UnpublishFirmware]: schemas.UnpublishFirmwareResponse, [Action.UpdateFirmware]: schemas.UpdateFirmwareResponse, }; export const validateCallResult = assign((arr) => { if (!validate(arr, unchecked_call_result_schema, 'Invalid OCPP call result')) { validateCallResult.errors = validate.errors; return false; } validateCallResult.errors = EMPTY_ARR; return true; }, { errors: EMPTY_ARR }); ; export const checkCallResult = assign((result, call) => { const [, call_id, payload] = result; if (call_id !== call[1]) { checkCallResult.errors = [`Invalid OCPP call result: id ${call_id} does not equal call id ${call[1]}`]; return false; } const schema = schemasByCommand[call[2]]; if (!validate(payload, schema, 'Invalid OCPP call result')) { checkCallResult.errors = validate.errors; return false; } checkCallResult.errors = EMPTY_ARR; return true; }, { errors: EMPTY_ARR }); //# sourceMappingURL=callresult.js.map