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, |
81533a20 | 13 | type OCPP20HeartbeatRequest, |
d270cc87 JB |
14 | OCPP20RequestCommand, |
15 | } from '../../../types/ocpp/2.0/Requests'; | |
953d6b02 | 16 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
d270cc87 | 17 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
953d6b02 | 18 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
953d6b02 JB |
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 { | |
b3fc3ff5 | 28 | protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>; |
953d6b02 JB |
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 | ], | |
81533a20 JB |
48 | [ |
49 | OCPP20RequestCommand.HEARTBEAT, | |
50 | JSON.parse( | |
51 | fs.readFileSync( | |
52 | path.resolve( | |
53 | path.dirname(fileURLToPath(import.meta.url)), | |
54 | '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json' | |
55 | ), | |
56 | 'utf8' | |
57 | ) | |
58 | ) as JSONSchemaType<OCPP20HeartbeatRequest>, | |
59 | ], | |
d270cc87 | 60 | ]); |
953d6b02 | 61 | this.buildRequestPayload.bind(this); |
953d6b02 JB |
62 | } |
63 | ||
64 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( | |
65 | chargingStation: ChargingStation, | |
66 | commandName: OCPP20RequestCommand, | |
67 | commandParams?: JsonType, | |
68 | params?: RequestParams | |
69 | ): Promise<ResponseType> { | |
70 | if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { | |
71 | const requestPayload = this.buildRequestPayload<RequestType>( | |
72 | chargingStation, | |
73 | commandName, | |
74 | commandParams | |
75 | ); | |
953d6b02 JB |
76 | return (await this.sendMessage( |
77 | chargingStation, | |
78 | Utils.generateUUID(), | |
79 | requestPayload, | |
80 | commandName, | |
81 | params | |
82 | )) as unknown as ResponseType; | |
83 | } | |
84 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
85 | throw new OCPPError( | |
86 | ErrorType.NOT_SUPPORTED, | |
87 | `Unsupported OCPP command '${commandName}'`, | |
88 | commandName, | |
89 | commandParams | |
90 | ); | |
91 | } | |
92 | ||
93 | private buildRequestPayload<Request extends JsonType>( | |
94 | chargingStation: ChargingStation, | |
95 | commandName: OCPP20RequestCommand, | |
96 | commandParams?: JsonType | |
97 | ): Request { | |
98 | commandParams = commandParams as JsonObject; | |
99 | switch (commandName) { | |
d270cc87 | 100 | case OCPP20RequestCommand.BOOT_NOTIFICATION: |
98fc1389 JB |
101 | commandParams.chargingStation = commandParams.chargingStation as JsonObject; |
102 | commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject; | |
d270cc87 JB |
103 | return { |
104 | reason: commandParams?.reason, | |
105 | chargingStation: { | |
98fc1389 JB |
106 | model: commandParams?.chargingStation?.model, |
107 | vendorName: commandParams?.chargingStation?.vendorName, | |
108 | ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && { | |
109 | firmwareVersion: commandParams.chargingStation?.firmwareVersion, | |
d270cc87 | 110 | }), |
98fc1389 JB |
111 | ...(!Utils.isUndefined(commandParams?.chargingStation?.serialNumber) && { |
112 | serialNumber: commandParams.chargingStation?.serialNumber, | |
113 | }), | |
114 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem) && { | |
115 | modem: { | |
116 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.iccid) && { | |
117 | iccid: commandParams.chargingStation.modem.iccid, | |
118 | }), | |
119 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.imsi) && { | |
120 | imsi: commandParams.chargingStation.modem.imsi, | |
121 | }), | |
122 | }, | |
d270cc87 | 123 | }), |
d270cc87 JB |
124 | }, |
125 | } as unknown as Request; | |
81533a20 JB |
126 | case OCPP20RequestCommand.HEARTBEAT: |
127 | return {} as unknown as Request; | |
953d6b02 JB |
128 | default: |
129 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
130 | throw new OCPPError( | |
131 | ErrorType.NOT_SUPPORTED, | |
132 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
133 | `Unsupported OCPP command '${commandName}'`, | |
134 | commandName, | |
135 | commandParams | |
136 | ); | |
137 | } | |
138 | } | |
953d6b02 | 139 | } |