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

69 lines 3.22 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.ChangeAvailability]: schemas.ChangeAvailabilityResponse, [Action.ChangeConfiguration]: schemas.ChangeConfigurationResponse, [Action.ClearCache]: schemas.ClearCacheResponse, [Action.ClearChargingProfile]: schemas.ClearChargingProfileResponse, [Action.DataTransfer]: schemas.DataTransferResponse, [Action.DiagnosticsStatusNotification]: schemas.DiagnosticsStatusNotificationResponse, [Action.FirmwareStatusNotification]: schemas.FirmwareStatusNotificationResponse, [Action.GetCompositeSchedule]: schemas.GetCompositeScheduleResponse, [Action.GetConfiguration]: schemas.GetConfigurationResponse, [Action.GetDiagnostics]: schemas.GetDiagnosticsResponse, [Action.GetLocalListVersion]: schemas.GetLocalListVersionResponse, [Action.Heartbeat]: schemas.HeartbeatResponse, [Action.MeterValues]: schemas.MeterValuesResponse, [Action.RemoteStartTransaction]: schemas.RemoteStartTransactionResponse, [Action.RemoteStopTransaction]: schemas.RemoteStopTransactionResponse, [Action.ReserveNow]: schemas.ReserveNowResponse, [Action.Reset]: schemas.ResetResponse, [Action.SendLocalList]: schemas.SendLocalListResponse, [Action.SetChargingProfile]: schemas.SetChargingProfileResponse, [Action.StartTransaction]: schemas.StartTransactionResponse, [Action.StatusNotification]: schemas.StatusNotificationResponse, [Action.StopTransaction]: schemas.StopTransactionResponse, [Action.TriggerMessage]: schemas.TriggerMessageResponse, [Action.UnlockConnector]: schemas.UnlockConnectorResponse, [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