1 import { BaseError
, type OCPPError
} from
'../../../exception/index.js'
3 BroadcastChannelProcedureName
,
4 type BroadcastChannelRequestPayload
,
8 type ProtocolRequestHandler
,
14 } from
'../../../types/index.js'
15 import { isAsyncFunction
, isNotEmptyArray
, logger
} from
'../../../utils/index.js'
16 import { Bootstrap
} from
'../../Bootstrap.js'
17 import { UIServiceWorkerBroadcastChannel
} from
'../../broadcast-channel/UIServiceWorkerBroadcastChannel.js'
18 import type { AbstractUIServer
} from
'../AbstractUIServer.js'
20 const moduleName
= 'AbstractUIService'
22 export abstract class AbstractUIService
{
23 protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping
= new Map
<
25 BroadcastChannelProcedureName
27 [ProcedureName
.START_CHARGING_STATION
, BroadcastChannelProcedureName
.START_CHARGING_STATION
],
28 [ProcedureName
.STOP_CHARGING_STATION
, BroadcastChannelProcedureName
.STOP_CHARGING_STATION
],
29 [ProcedureName
.CLOSE_CONNECTION
, BroadcastChannelProcedureName
.CLOSE_CONNECTION
],
30 [ProcedureName
.OPEN_CONNECTION
, BroadcastChannelProcedureName
.OPEN_CONNECTION
],
32 ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
33 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
36 ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
37 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
39 [ProcedureName
.SET_SUPERVISION_URL
, BroadcastChannelProcedureName
.SET_SUPERVISION_URL
],
40 [ProcedureName
.START_TRANSACTION
, BroadcastChannelProcedureName
.START_TRANSACTION
],
41 [ProcedureName
.STOP_TRANSACTION
, BroadcastChannelProcedureName
.STOP_TRANSACTION
],
42 [ProcedureName
.AUTHORIZE
, BroadcastChannelProcedureName
.AUTHORIZE
],
43 [ProcedureName
.BOOT_NOTIFICATION
, BroadcastChannelProcedureName
.BOOT_NOTIFICATION
],
44 [ProcedureName
.STATUS_NOTIFICATION
, BroadcastChannelProcedureName
.STATUS_NOTIFICATION
],
45 [ProcedureName
.HEARTBEAT
, BroadcastChannelProcedureName
.HEARTBEAT
],
46 [ProcedureName
.METER_VALUES
, BroadcastChannelProcedureName
.METER_VALUES
],
47 [ProcedureName
.DATA_TRANSFER
, BroadcastChannelProcedureName
.DATA_TRANSFER
],
49 ProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
50 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
53 ProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
54 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
58 protected readonly requestHandlers
: Map
<ProcedureName
, ProtocolRequestHandler
>
59 private readonly version
: ProtocolVersion
60 private readonly uiServer
: AbstractUIServer
61 private readonly uiServiceWorkerBroadcastChannel
: UIServiceWorkerBroadcastChannel
62 private readonly broadcastChannelRequests
: Map
<string, number>
64 constructor (uiServer
: AbstractUIServer
, version
: ProtocolVersion
) {
65 this.uiServer
= uiServer
66 this.version
= version
67 this.requestHandlers
= new Map
<ProcedureName
, ProtocolRequestHandler
>([
68 [ProcedureName
.LIST_TEMPLATES
, this.handleListTemplates
.bind(this)],
69 [ProcedureName
.LIST_CHARGING_STATIONS
, this.handleListChargingStations
.bind(this)],
70 [ProcedureName
.ADD_CHARGING_STATIONS
, this.handleAddChargingStations
.bind(this)],
71 [ProcedureName
.START_SIMULATOR
, this.handleStartSimulator
.bind(this)],
72 [ProcedureName
.STOP_SIMULATOR
, this.handleStopSimulator
.bind(this)]
74 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this)
75 this.broadcastChannelRequests
= new Map
<string, number>()
78 public async requestHandler (request
: ProtocolRequest
): Promise
<ProtocolResponse
| undefined> {
79 let messageId
: string | undefined
80 let command
: ProcedureName
| undefined
81 let requestPayload
: RequestPayload
| undefined
82 let responsePayload
: ResponsePayload
| undefined
84 [messageId
, command
, requestPayload
] = request
86 if (!this.requestHandlers
.has(command
)) {
88 `${command} is not implemented to handle message payload ${JSON.stringify(
96 // Call the request handler to build the response payload
97 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
98 const requestHandler
= this.requestHandlers
.get(command
)!
99 if (isAsyncFunction(requestHandler
)) {
100 responsePayload
= await requestHandler(messageId
, command
, requestPayload
)
105 procedureName
?: ProcedureName
,
106 payload
?: RequestPayload
107 ) => undefined | ResponsePayload
108 )(messageId
, command
, requestPayload
)
112 logger
.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error
)
114 hashIds
: requestPayload
?.hashIds
,
115 status: ResponseStatus
.FAILURE
,
119 errorMessage
: (error
as OCPPError
).message
,
120 errorStack
: (error
as OCPPError
).stack
,
121 errorDetails
: (error
as OCPPError
).details
122 } satisfies ResponsePayload
124 if (responsePayload
!= null) {
125 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
126 return this.uiServer
.buildProtocolResponse(messageId
!, responsePayload
)
130 // public sendRequest (
131 // messageId: string,
132 // procedureName: ProcedureName,
133 // requestPayload: RequestPayload
135 // this.uiServer.sendRequest(
136 // this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
140 public sendResponse (messageId
: string, responsePayload
: ResponsePayload
): void {
141 if (this.uiServer
.hasResponseHandler(messageId
)) {
142 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
))
146 public logPrefix
= (modName
: string, methodName
: string): string => {
147 return this.uiServer
.logPrefix(modName
, methodName
, this.version
)
150 public deleteBroadcastChannelRequest (uuid
: string): void {
151 this.broadcastChannelRequests
.delete(uuid
)
154 public getBroadcastChannelExpectedResponses (uuid
: string): number {
155 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
156 return this.broadcastChannelRequests
.get(uuid
)!
159 protected handleProtocolRequest (
161 procedureName
: ProcedureName
,
162 payload
: RequestPayload
164 this.sendBroadcastChannelRequest(
166 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
167 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMapping
.get(procedureName
)!,
172 private sendBroadcastChannelRequest (
174 procedureName
: BroadcastChannelProcedureName
,
175 payload
: BroadcastChannelRequestPayload
177 if (isNotEmptyArray(payload
.hashIds
)) {
178 payload
.hashIds
= payload
.hashIds
180 if (this.uiServer
.chargingStations
.has(hashId
)) {
186 'sendBroadcastChannelRequest'
187 )} Charging station with hashId '${hashId}' not found`
191 .filter(hashId
=> hashId
!= null) as string[]
193 delete payload
.hashIds
195 const expectedNumberOfResponses
= Array.isArray(payload
.hashIds
)
196 ? payload
.hashIds
.length
197 : this.uiServer
.chargingStations
.size
198 if (expectedNumberOfResponses
=== 0) {
200 'hashIds array in the request payload does not contain any valid charging station hashId'
203 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
])
204 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
)
207 private handleListTemplates (): ResponsePayload
{
209 status: ResponseStatus
.SUCCESS
,
210 templates
: [...this.uiServer
.chargingStationTemplates
.values()] as JsonType
[]
211 } satisfies ResponsePayload
214 private handleListChargingStations (): ResponsePayload
{
216 status: ResponseStatus
.SUCCESS
,
217 chargingStations
: [...this.uiServer
.chargingStations
.values()] as JsonType
[]
218 } satisfies ResponsePayload
221 private async handleAddChargingStations (
223 procedureName
?: ProcedureName
,
224 requestPayload
?: RequestPayload
225 ): Promise
<ResponsePayload
> {
226 const { template
, numberOfStations
} = requestPayload
as {
228 numberOfStations
: number
230 if (!this.uiServer
.chargingStationTemplates
.has(template
)) {
232 status: ResponseStatus
.FAILURE
,
233 errorMessage
: `Template '${template}' not found`
234 } satisfies ResponsePayload
236 for (let i
= 0; i
< numberOfStations
; i
++) {
238 await Bootstrap
.getInstance().addChargingStation(
239 Bootstrap
.getInstance().getLastIndex(template
) + 1,
244 status: ResponseStatus
.FAILURE
,
245 errorMessage
: (error
as Error).message
,
246 errorStack
: (error
as Error).stack
247 } satisfies ResponsePayload
251 status: ResponseStatus
.SUCCESS
255 private async handleStartSimulator (): Promise
<ResponsePayload
> {
257 await Bootstrap
.getInstance().start()
258 return { status: ResponseStatus
.SUCCESS
}
260 return { status: ResponseStatus
.FAILURE
}
264 private async handleStopSimulator (): Promise
<ResponsePayload
> {
266 await Bootstrap
.getInstance().stop()
267 return { status: ResponseStatus
.SUCCESS
}
269 return { status: ResponseStatus
.FAILURE
}