1 import { BaseError
, type OCPPError
} from
'../../../exception/index.js'
3 BroadcastChannelProcedureName
,
4 type BroadcastChannelRequestPayload
,
5 type ChargingStationOptions
,
10 type ProtocolRequestHandler
,
11 type ProtocolResponse
,
16 type StorageConfiguration
17 } from
'../../../types/index.js'
18 import { Configuration
, isAsyncFunction
, isNotEmptyArray
, logger
} from
'../../../utils/index.js'
19 import { Bootstrap
} from
'../../Bootstrap.js'
20 import { UIServiceWorkerBroadcastChannel
} from
'../../broadcast-channel/UIServiceWorkerBroadcastChannel.js'
21 import type { AbstractUIServer
} from
'../AbstractUIServer.js'
23 const moduleName
= 'AbstractUIService'
25 export abstract class AbstractUIService
{
26 protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping
= new Map
<
28 BroadcastChannelProcedureName
30 [ProcedureName
.START_CHARGING_STATION
, BroadcastChannelProcedureName
.START_CHARGING_STATION
],
31 [ProcedureName
.STOP_CHARGING_STATION
, BroadcastChannelProcedureName
.STOP_CHARGING_STATION
],
32 [ProcedureName
.CLOSE_CONNECTION
, BroadcastChannelProcedureName
.CLOSE_CONNECTION
],
33 [ProcedureName
.OPEN_CONNECTION
, BroadcastChannelProcedureName
.OPEN_CONNECTION
],
35 ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
36 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
39 ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
40 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
42 [ProcedureName
.SET_SUPERVISION_URL
, BroadcastChannelProcedureName
.SET_SUPERVISION_URL
],
43 [ProcedureName
.START_TRANSACTION
, BroadcastChannelProcedureName
.START_TRANSACTION
],
44 [ProcedureName
.STOP_TRANSACTION
, BroadcastChannelProcedureName
.STOP_TRANSACTION
],
45 [ProcedureName
.AUTHORIZE
, BroadcastChannelProcedureName
.AUTHORIZE
],
46 [ProcedureName
.BOOT_NOTIFICATION
, BroadcastChannelProcedureName
.BOOT_NOTIFICATION
],
47 [ProcedureName
.STATUS_NOTIFICATION
, BroadcastChannelProcedureName
.STATUS_NOTIFICATION
],
48 [ProcedureName
.HEARTBEAT
, BroadcastChannelProcedureName
.HEARTBEAT
],
49 [ProcedureName
.METER_VALUES
, BroadcastChannelProcedureName
.METER_VALUES
],
50 [ProcedureName
.DATA_TRANSFER
, BroadcastChannelProcedureName
.DATA_TRANSFER
],
52 ProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
53 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
56 ProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
57 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
61 protected readonly requestHandlers
: Map
<ProcedureName
, ProtocolRequestHandler
>
62 private readonly version
: ProtocolVersion
63 private readonly uiServer
: AbstractUIServer
64 private readonly uiServiceWorkerBroadcastChannel
: UIServiceWorkerBroadcastChannel
65 private readonly broadcastChannelRequests
: Map
<string, number>
67 constructor (uiServer
: AbstractUIServer
, version
: ProtocolVersion
) {
68 this.uiServer
= uiServer
69 this.version
= version
70 this.requestHandlers
= new Map
<ProcedureName
, ProtocolRequestHandler
>([
71 [ProcedureName
.LIST_TEMPLATES
, this.handleListTemplates
.bind(this)],
72 [ProcedureName
.LIST_CHARGING_STATIONS
, this.handleListChargingStations
.bind(this)],
73 [ProcedureName
.ADD_CHARGING_STATIONS
, this.handleAddChargingStations
.bind(this)],
74 [ProcedureName
.PERFORMANCE_STATISTICS
, this.handlePerformanceStatistics
.bind(this)],
75 [ProcedureName
.START_SIMULATOR
, this.handleStartSimulator
.bind(this)],
76 [ProcedureName
.STOP_SIMULATOR
, this.handleStopSimulator
.bind(this)]
78 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this)
79 this.broadcastChannelRequests
= new Map
<string, number>()
82 public async requestHandler (request
: ProtocolRequest
): Promise
<ProtocolResponse
| undefined> {
83 let messageId
: string | undefined
84 let command
: ProcedureName
| undefined
85 let requestPayload
: RequestPayload
| undefined
86 let responsePayload
: ResponsePayload
| undefined
88 [messageId
, command
, requestPayload
] = request
90 if (!this.requestHandlers
.has(command
)) {
92 `${command} is not implemented to handle message payload ${JSON.stringify(
100 // Call the request handler to build the response payload
101 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
102 const requestHandler
= this.requestHandlers
.get(command
)!
103 if (isAsyncFunction(requestHandler
)) {
104 responsePayload
= await requestHandler(messageId
, command
, requestPayload
)
109 procedureName
?: ProcedureName
,
110 payload
?: RequestPayload
111 ) => undefined | ResponsePayload
112 )(messageId
, command
, requestPayload
)
116 logger
.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error
)
118 hashIds
: requestPayload
?.hashIds
,
119 status: ResponseStatus
.FAILURE
,
123 errorMessage
: (error
as OCPPError
).message
,
124 errorStack
: (error
as OCPPError
).stack
,
125 errorDetails
: (error
as OCPPError
).details
126 } satisfies ResponsePayload
128 if (responsePayload
!= null) {
129 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
130 return this.uiServer
.buildProtocolResponse(messageId
!, responsePayload
)
134 // public sendRequest (
135 // messageId: string,
136 // procedureName: ProcedureName,
137 // requestPayload: RequestPayload
139 // this.uiServer.sendRequest(
140 // this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
144 public sendResponse (messageId
: string, responsePayload
: ResponsePayload
): void {
145 if (this.uiServer
.hasResponseHandler(messageId
)) {
146 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
))
150 public logPrefix
= (modName
: string, methodName
: string): string => {
151 return this.uiServer
.logPrefix(modName
, methodName
, this.version
)
154 public deleteBroadcastChannelRequest (uuid
: string): void {
155 this.broadcastChannelRequests
.delete(uuid
)
158 public getBroadcastChannelExpectedResponses (uuid
: string): number {
159 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
160 return this.broadcastChannelRequests
.get(uuid
)!
163 protected handleProtocolRequest (
165 procedureName
: ProcedureName
,
166 payload
: RequestPayload
168 this.sendBroadcastChannelRequest(
170 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
171 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMapping
.get(procedureName
)!,
176 private sendBroadcastChannelRequest (
178 procedureName
: BroadcastChannelProcedureName
,
179 payload
: BroadcastChannelRequestPayload
181 if (isNotEmptyArray(payload
.hashIds
)) {
182 payload
.hashIds
= payload
.hashIds
184 if (this.uiServer
.chargingStations
.has(hashId
)) {
190 'sendBroadcastChannelRequest'
191 )} Charging station with hashId '${hashId}' not found`
195 .filter(hashId
=> hashId
!= null) as string[]
197 delete payload
.hashIds
199 const expectedNumberOfResponses
= Array.isArray(payload
.hashIds
)
200 ? payload
.hashIds
.length
201 : this.uiServer
.chargingStations
.size
202 if (expectedNumberOfResponses
=== 0) {
204 'hashIds array in the request payload does not contain any valid charging station hashId'
207 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
])
208 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
)
211 private handleListTemplates (): ResponsePayload
{
213 status: ResponseStatus
.SUCCESS
,
214 templates
: [...this.uiServer
.chargingStationTemplates
.values()] as JsonType
[]
215 } satisfies ResponsePayload
218 private handleListChargingStations (): ResponsePayload
{
220 status: ResponseStatus
.SUCCESS
,
221 chargingStations
: [...this.uiServer
.chargingStations
.values()] as JsonType
[]
222 } satisfies ResponsePayload
225 private async handleAddChargingStations (
227 procedureName
?: ProcedureName
,
228 requestPayload
?: RequestPayload
229 ): Promise
<ResponsePayload
> {
230 const { template
, numberOfStations
, options
} = requestPayload
as {
232 numberOfStations
: number
233 options
?: ChargingStationOptions
235 if (!this.uiServer
.chargingStationTemplates
.has(template
)) {
237 status: ResponseStatus
.FAILURE
,
238 errorMessage
: `Template '${template}' not found`
239 } satisfies ResponsePayload
241 for (let i
= 0; i
< numberOfStations
; i
++) {
243 await Bootstrap
.getInstance().addChargingStation(
244 Bootstrap
.getInstance().getLastIndex(template
) + 1,
250 status: ResponseStatus
.FAILURE
,
251 errorMessage
: (error
as Error).message
,
252 errorStack
: (error
as Error).stack
253 } satisfies ResponsePayload
257 status: ResponseStatus
.SUCCESS
261 private handlePerformanceStatistics (): ResponsePayload
{
263 Configuration
.getConfigurationSection
<StorageConfiguration
>(
264 ConfigurationSection
.performanceStorage
268 status: ResponseStatus
.FAILURE
,
269 errorMessage
: 'Performance statistics storage is not enabled'
270 } satisfies ResponsePayload
274 status: ResponseStatus
.SUCCESS
,
275 performanceStatistics
: [
276 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
277 ...Bootstrap
.getInstance().getPerformanceStatistics()!
282 status: ResponseStatus
.FAILURE
,
283 errorMessage
: (error
as Error).message
,
284 errorStack
: (error
as Error).stack
285 } satisfies ResponsePayload
289 private async handleStartSimulator (): Promise
<ResponsePayload
> {
291 await Bootstrap
.getInstance().start()
292 return { status: ResponseStatus
.SUCCESS
}
295 status: ResponseStatus
.FAILURE
,
296 errorMessage
: (error
as Error).message
,
297 errorStack
: (error
as Error).stack
298 } satisfies ResponsePayload
302 private async handleStopSimulator (): Promise
<ResponsePayload
> {
304 await Bootstrap
.getInstance().stop()
305 return { status: ResponseStatus
.SUCCESS
}
308 status: ResponseStatus
.FAILURE
,
309 errorMessage
: (error
as Error).message
,
310 errorStack
: (error
as Error).stack
311 } satisfies ResponsePayload