Refine TS and linter configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPServiceUtils.ts
1 import type { DefinedError, ErrorObject } from 'ajv';
2
3 import { ErrorType } from '../../types/ocpp/ErrorType';
4
5 export class OCPPServiceUtils {
6 protected constructor() {
7 // This is intentional
8 }
9
10 public static ajvErrorsToErrorType(errors: ErrorObject[]): ErrorType {
11 for (const error of errors as DefinedError[]) {
12 switch (error.keyword) {
13 case 'type':
14 return ErrorType.TYPE_CONSTRAINT_VIOLATION;
15 case 'dependencies':
16 case 'required':
17 return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION;
18 case 'pattern':
19 case 'format':
20 return ErrorType.PROPERTY_CONSTRAINT_VIOLATION;
21 }
22 }
23 return ErrorType.FORMAT_VIOLATION;
24 }
25
26 protected static getLimitFromSampledValueTemplateCustomValue(
27 value: string,
28 limit: number,
29 options: { limitationEnabled?: boolean; unitMultiplier?: number } = {
30 limitationEnabled: true,
31 unitMultiplier: 1,
32 }
33 ): number {
34 options.limitationEnabled = options?.limitationEnabled ?? true;
35 options.unitMultiplier = options?.unitMultiplier ?? 1;
36 const numberValue = isNaN(parseInt(value)) ? Infinity : parseInt(value);
37 return options?.limitationEnabled
38 ? Math.min(numberValue * options.unitMultiplier, limit)
39 : numberValue * options.unitMultiplier;
40 }
41 }