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

62 lines 2.89 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.Authorize]: schemas.AuthorizeRequest, [Action.BootNotification]: schemas.BootNotificationRequest, [Action.CancelReservation]: schemas.CancelReservationRequest, [Action.ChangeAvailability]: schemas.ChangeAvailabilityRequest, [Action.ChangeConfiguration]: schemas.ChangeConfigurationRequest, [Action.ClearCache]: schemas.ClearCacheRequest, [Action.ClearChargingProfile]: schemas.ClearChargingProfileRequest, [Action.DataTransfer]: schemas.DataTransferRequest, [Action.DiagnosticsStatusNotification]: schemas.DiagnosticsStatusNotificationRequest, [Action.FirmwareStatusNotification]: schemas.FirmwareStatusNotificationRequest, [Action.GetCompositeSchedule]: schemas.GetCompositeScheduleRequest, [Action.GetConfiguration]: schemas.GetConfigurationRequest, [Action.GetDiagnostics]: schemas.GetDiagnosticsRequest, [Action.GetLocalListVersion]: schemas.GetLocalListVersionRequest, [Action.Heartbeat]: schemas.HeartbeatRequest, [Action.MeterValues]: schemas.MeterValuesRequest, [Action.RemoteStartTransaction]: schemas.RemoteStartTransactionRequest, [Action.RemoteStopTransaction]: schemas.RemoteStopTransactionRequest, [Action.ReserveNow]: schemas.ReserveNowRequest, [Action.Reset]: schemas.ResetRequest, [Action.SendLocalList]: schemas.SendLocalListRequest, [Action.SetChargingProfile]: schemas.SetChargingProfileRequest, [Action.StartTransaction]: schemas.StartTransactionRequest, [Action.StatusNotification]: schemas.StatusNotificationRequest, [Action.StopTransaction]: schemas.StopTransactionRequest, [Action.TriggerMessage]: schemas.TriggerMessageRequest, [Action.UnlockConnector]: schemas.UnlockConnectorRequest, [Action.UpdateFirmware]: schemas.UpdateFirmwareRequest, }; const basecall_schema = { type: 'array', items: [ { type: 'number', enum: [MessageType.CALL] }, { 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