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_CHARGING_STATIONS
, this.handleListChargingStations
.bind(this)],
69 [ProcedureName
.START_SIMULATOR
, this.handleStartSimulator
.bind(this)],
70 [ProcedureName
.STOP_SIMULATOR
, this.handleStopSimulator
.bind(this)]
72 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this)
73 this.broadcastChannelRequests
= new Map
<string, number>()
76 public async requestHandler (request
: ProtocolRequest
): Promise
<ProtocolResponse
| undefined> {
77 let messageId
: string | undefined
78 let command
: ProcedureName
| undefined
79 let requestPayload
: RequestPayload
| undefined
80 let responsePayload
: ResponsePayload
| undefined
82 [messageId
, command
, requestPayload
] = request
84 if (!this.requestHandlers
.has(command
)) {
86 `${command} is not implemented to handle message payload ${JSON.stringify(
94 // Call the request handler to build the response payload
95 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
96 const requestHandler
= this.requestHandlers
.get(command
)!
97 if (isAsyncFunction(requestHandler
)) {
98 responsePayload
= await requestHandler(messageId
, command
, requestPayload
)
103 procedureName
?: ProcedureName
,
104 payload
?: RequestPayload
105 ) => undefined | ResponsePayload
106 )(messageId
, command
, requestPayload
)
110 logger
.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error
)
112 hashIds
: requestPayload
?.hashIds
,
113 status: ResponseStatus
.FAILURE
,
117 errorMessage
: (error
as OCPPError
).message
,
118 errorStack
: (error
as OCPPError
).stack
,
119 errorDetails
: (error
as OCPPError
).details
122 if (responsePayload
!= null) {
123 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
124 return this.uiServer
.buildProtocolResponse(messageId
!, responsePayload
)
128 // public sendRequest (
129 // messageId: string,
130 // procedureName: ProcedureName,
131 // requestPayload: RequestPayload
133 // this.uiServer.sendRequest(
134 // this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
138 public sendResponse (messageId
: string, responsePayload
: ResponsePayload
): void {
139 if (this.uiServer
.hasResponseHandler(messageId
)) {
140 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
))
144 public logPrefix
= (modName
: string, methodName
: string): string => {
145 return this.uiServer
.logPrefix(modName
, methodName
, this.version
)
148 public deleteBroadcastChannelRequest (uuid
: string): void {
149 this.broadcastChannelRequests
.delete(uuid
)
152 public getBroadcastChannelExpectedResponses (uuid
: string): number {
153 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
154 return this.broadcastChannelRequests
.get(uuid
)!
157 protected handleProtocolRequest (
159 procedureName
: ProcedureName
,
160 payload
: RequestPayload
162 this.sendBroadcastChannelRequest(
164 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMapping
.get(procedureName
)!,
170 private sendBroadcastChannelRequest (
172 procedureName
: BroadcastChannelProcedureName
,
173 payload
: BroadcastChannelRequestPayload
175 if (isNotEmptyArray(payload
.hashIds
)) {
176 payload
.hashIds
= payload
.hashIds
178 if (this.uiServer
.chargingStations
.has(hashId
)) {
184 'sendBroadcastChannelRequest'
185 )} Charging station with hashId '${hashId}' not found`
189 .filter(hashId
=> hashId
!= null) as string[]
191 delete payload
.hashIds
193 const expectedNumberOfResponses
= Array.isArray(payload
.hashIds
)
194 ? payload
.hashIds
.length
195 : this.uiServer
.chargingStations
.size
196 if (expectedNumberOfResponses
=== 0) {
198 'hashIds array in the request payload does not contain any valid charging station hashId'
201 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
])
202 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
)
205 private handleListChargingStations (): ResponsePayload
{
207 status: ResponseStatus
.SUCCESS
,
208 chargingStations
: [...this.uiServer
.chargingStations
.values()] as JsonType
[]
209 } satisfies ResponsePayload
212 private async handleStartSimulator (): Promise
<ResponsePayload
> {
214 await Bootstrap
.getInstance().start()
215 return { status: ResponseStatus
.SUCCESS
}
217 return { status: ResponseStatus
.FAILURE
}
221 private async handleStopSimulator (): Promise
<ResponsePayload
> {
223 await Bootstrap
.getInstance().stop()
224 return { status: ResponseStatus
.SUCCESS
}
226 return { status: ResponseStatus
.FAILURE
}