Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
953d6b02 | 2 | |
d270cc87 JB |
3 | import fs from 'fs'; |
4 | import path from 'path'; | |
5 | import { fileURLToPath } from 'url'; | |
6 | ||
953d6b02 JB |
7 | import type { JSONSchemaType } from 'ajv'; |
8 | ||
9 | import OCPPError from '../../../exception/OCPPError'; | |
10 | import type { JsonObject, JsonType } from '../../../types/JsonType'; | |
d270cc87 | 11 | import { |
96a52d08 | 12 | type OCPP20BootNotificationRequest, |
d270cc87 JB |
13 | OCPP20RequestCommand, |
14 | } from '../../../types/ocpp/2.0/Requests'; | |
953d6b02 | 15 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
d270cc87 | 16 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
953d6b02 JB |
17 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
18 | import logger from '../../../utils/Logger'; | |
19 | import Utils from '../../../utils/Utils'; | |
20 | import type ChargingStation from '../../ChargingStation'; | |
21 | import OCPPRequestService from '../OCPPRequestService'; | |
22 | import type OCPPResponseService from '../OCPPResponseService'; | |
23 | import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; | |
24 | ||
25 | const moduleName = 'OCPP20RequestService'; | |
26 | ||
27 | export default class OCPP20RequestService extends OCPPRequestService { | |
28 | private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>; | |
29 | ||
30 | public constructor(ocppResponseService: OCPPResponseService) { | |
31 | if (new.target?.name === moduleName) { | |
32 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); | |
33 | } | |
d270cc87 JB |
34 | super(OCPPVersion.VERSION_20, ocppResponseService); |
35 | this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([ | |
36 | [ | |
37 | OCPP20RequestCommand.BOOT_NOTIFICATION, | |
38 | JSON.parse( | |
39 | fs.readFileSync( | |
40 | path.resolve( | |
41 | path.dirname(fileURLToPath(import.meta.url)), | |
42 | '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json' | |
43 | ), | |
44 | 'utf8' | |
45 | ) | |
46 | ) as JSONSchemaType<OCPP20BootNotificationRequest>, | |
47 | ], | |
48 | ]); | |
953d6b02 | 49 | this.buildRequestPayload.bind(this); |
953d6b02 JB |
50 | } |
51 | ||
52 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( | |
53 | chargingStation: ChargingStation, | |
54 | commandName: OCPP20RequestCommand, | |
55 | commandParams?: JsonType, | |
56 | params?: RequestParams | |
57 | ): Promise<ResponseType> { | |
58 | if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { | |
59 | const requestPayload = this.buildRequestPayload<RequestType>( | |
60 | chargingStation, | |
61 | commandName, | |
62 | commandParams | |
63 | ); | |
953d6b02 JB |
64 | return (await this.sendMessage( |
65 | chargingStation, | |
66 | Utils.generateUUID(), | |
67 | requestPayload, | |
68 | commandName, | |
69 | params | |
70 | )) as unknown as ResponseType; | |
71 | } | |
72 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
73 | throw new OCPPError( | |
74 | ErrorType.NOT_SUPPORTED, | |
75 | `Unsupported OCPP command '${commandName}'`, | |
76 | commandName, | |
77 | commandParams | |
78 | ); | |
79 | } | |
80 | ||
45988780 JB |
81 | protected getRequestPayloadValidationSchema( |
82 | chargingStation: ChargingStation, | |
83 | commandName: OCPP20RequestCommand | |
84 | ): JSONSchemaType<JsonObject> | false { | |
85 | if (this.jsonSchemas.has(commandName) === true) { | |
86 | return this.jsonSchemas.get(commandName); | |
87 | } | |
88 | logger.warn( | |
89 | `${chargingStation.logPrefix()} ${moduleName}.getPayloadValidationSchema: No JSON schema found for command ${commandName} PDU validation` | |
90 | ); | |
91 | return false; | |
92 | } | |
93 | ||
953d6b02 JB |
94 | private buildRequestPayload<Request extends JsonType>( |
95 | chargingStation: ChargingStation, | |
96 | commandName: OCPP20RequestCommand, | |
97 | commandParams?: JsonType | |
98 | ): Request { | |
99 | commandParams = commandParams as JsonObject; | |
100 | switch (commandName) { | |
d270cc87 JB |
101 | case OCPP20RequestCommand.BOOT_NOTIFICATION: |
102 | commandParams.modem = commandParams.modem as JsonObject; | |
103 | return { | |
104 | reason: commandParams?.reason, | |
105 | chargingStation: { | |
106 | model: commandParams?.model, | |
107 | vendorName: commandParams?.vendorName, | |
108 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { | |
109 | firmwareVersion: commandParams.firmwareVersion, | |
110 | }), | |
111 | ...(!Utils.isUndefined(commandParams?.serialNumber) && { | |
112 | serialNumber: commandParams.serialNumber, | |
113 | }), | |
114 | modem: { | |
115 | ...(!Utils.isUndefined(commandParams?.modem?.iccid) && { | |
116 | iccid: commandParams.modem.iccid, | |
117 | }), | |
118 | ...(!Utils.isUndefined(commandParams?.modem?.imsi) && { | |
119 | imsi: commandParams.modem.imsi, | |
120 | }), | |
121 | }, | |
122 | }, | |
123 | } as unknown as Request; | |
953d6b02 JB |
124 | default: |
125 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
126 | throw new OCPPError( | |
127 | ErrorType.NOT_SUPPORTED, | |
128 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
129 | `Unsupported OCPP command '${commandName}'`, | |
130 | commandName, | |
131 | commandParams | |
132 | ); | |
133 | } | |
134 | } | |
953d6b02 | 135 | } |