769064b99fc727096b8abc61cedc9129c6a698bd
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
2
3 import type { JSONSchemaType } from 'ajv'
4
5 import { OCPP20Constants } from './OCPP20Constants.js'
6 import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js'
7 import type { ChargingStation } from '../../../charging-station/index.js'
8 import { OCPPError } from '../../../exception/index.js'
9 import {
10 ErrorType,
11 type JsonObject,
12 type JsonType,
13 type OCPP20BootNotificationRequest,
14 type OCPP20HeartbeatRequest,
15 OCPP20RequestCommand,
16 type OCPP20StatusNotificationRequest,
17 OCPPVersion,
18 type RequestParams
19 } from '../../../types/index.js'
20 import { generateUUID } from '../../../utils/index.js'
21 import { OCPPRequestService } from '../OCPPRequestService.js'
22 import type { OCPPResponseService } from '../OCPPResponseService.js'
23
24 const moduleName = 'OCPP20RequestService'
25
26 export class OCPP20RequestService extends OCPPRequestService {
27 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>
28
29 public constructor (ocppResponseService: OCPPResponseService) {
30 // if (new.target.name === moduleName) {
31 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
32 // }
33 super(OCPPVersion.VERSION_20, ocppResponseService)
34 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>([
35 [
36 OCPP20RequestCommand.BOOT_NOTIFICATION,
37 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
38 'assets/json-schemas/ocpp/2.0/BootNotificationRequest.json',
39 moduleName,
40 'constructor'
41 )
42 ],
43 [
44 OCPP20RequestCommand.HEARTBEAT,
45 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
46 'assets/json-schemas/ocpp/2.0/HeartbeatRequest.json',
47 moduleName,
48 'constructor'
49 )
50 ],
51 [
52 OCPP20RequestCommand.STATUS_NOTIFICATION,
53 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
54 'assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
55 moduleName,
56 'constructor'
57 )
58 ]
59 ])
60 this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
61 chargingStation: ChargingStation,
62 commandName: OCPP20RequestCommand,
63 commandParams?: JsonType
64 ) => Request
65 }
66
67 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
68 chargingStation: ChargingStation,
69 commandName: OCPP20RequestCommand,
70 commandParams?: JsonType,
71 params?: RequestParams
72 ): Promise<ResponseType> {
73 // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc.
74 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) {
75 return (await this.sendMessage(
76 chargingStation,
77 generateUUID(),
78 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
79 commandName,
80 params
81 )) as ResponseType
82 }
83 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
84 throw new OCPPError(
85 ErrorType.NOT_SUPPORTED,
86 `Unsupported OCPP command '${commandName}'`,
87 commandName,
88 commandParams
89 )
90 }
91
92 private buildRequestPayload<Request extends JsonType>(
93 chargingStation: ChargingStation,
94 commandName: OCPP20RequestCommand,
95 commandParams?: JsonType
96 ): Request {
97 commandParams = commandParams as JsonObject
98 switch (commandName) {
99 case OCPP20RequestCommand.BOOT_NOTIFICATION:
100 return commandParams as unknown as Request
101 case OCPP20RequestCommand.HEARTBEAT:
102 return OCPP20Constants.OCPP_RESPONSE_EMPTY as unknown as Request
103 case OCPP20RequestCommand.STATUS_NOTIFICATION:
104 return {
105 timestamp: new Date(),
106 ...commandParams
107 } as unknown as Request
108 default:
109 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
110 throw new OCPPError(
111 ErrorType.NOT_SUPPORTED,
112 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
113 `Unsupported OCPP command '${commandName}'`,
114 commandName,
115 commandParams
116 )
117 }
118 }
119 }