1 import { BaseError
, type OCPPError
} from
'../../../exception/index.js'
3 BroadcastChannelProcedureName
,
4 type BroadcastChannelRequestPayload
,
8 type ProtocolRequestHandler
,
14 } from
'../../../types/index.js'
15 import { 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 responsePayload
= await this.requestHandlers
.get(command
)!(messageId
, command
, requestPayload
)
99 logger
.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error
)
101 hashIds
: requestPayload
?.hashIds
,
102 status: ResponseStatus
.FAILURE
,
106 errorMessage
: (error
as OCPPError
).message
,
107 errorStack
: (error
as OCPPError
).stack
,
108 errorDetails
: (error
as OCPPError
).details
111 if (responsePayload
!= null) {
112 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
113 return this.uiServer
.buildProtocolResponse(messageId
!, responsePayload
)
117 // public sendRequest (
118 // messageId: string,
119 // procedureName: ProcedureName,
120 // requestPayload: RequestPayload
122 // this.uiServer.sendRequest(
123 // this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
127 public sendResponse (messageId
: string, responsePayload
: ResponsePayload
): void {
128 if (this.uiServer
.hasResponseHandler(messageId
)) {
129 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
))
133 public logPrefix
= (modName
: string, methodName
: string): string => {
134 return this.uiServer
.logPrefix(modName
, methodName
, this.version
)
137 public deleteBroadcastChannelRequest (uuid
: string): void {
138 this.broadcastChannelRequests
.delete(uuid
)
141 public getBroadcastChannelExpectedResponses (uuid
: string): number {
142 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
143 return this.broadcastChannelRequests
.get(uuid
)!
146 protected handleProtocolRequest (
148 procedureName
: ProcedureName
,
149 payload
: RequestPayload
151 this.sendBroadcastChannelRequest(
153 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
154 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMapping
.get(procedureName
)!,
159 private sendBroadcastChannelRequest (
161 procedureName
: BroadcastChannelProcedureName
,
162 payload
: BroadcastChannelRequestPayload
164 if (isNotEmptyArray(payload
.hashIds
)) {
165 payload
.hashIds
= payload
.hashIds
167 if (this.uiServer
.chargingStations
.has(hashId
)) {
173 'sendBroadcastChannelRequest'
174 )} Charging station with hashId '${hashId}' not found`
178 .filter(hashId
=> hashId
!= null) as string[]
180 delete payload
.hashIds
182 const expectedNumberOfResponses
= Array.isArray(payload
.hashIds
)
183 ? payload
.hashIds
.length
184 : this.uiServer
.chargingStations
.size
185 if (expectedNumberOfResponses
=== 0) {
187 'hashIds array in the request payload does not contain any valid charging station hashId'
190 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
])
191 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
)
194 private handleListChargingStations (): ResponsePayload
{
196 status: ResponseStatus
.SUCCESS
,
197 chargingStations
: [...this.uiServer
.chargingStations
.values()] as JsonType
[]
198 } satisfies ResponsePayload
201 private async handleStartSimulator (): Promise
<ResponsePayload
> {
203 await Bootstrap
.getInstance().start()
204 return { status: ResponseStatus
.SUCCESS
}
206 return { status: ResponseStatus
.FAILURE
}
210 private async handleStopSimulator (): Promise
<ResponsePayload
> {
212 await Bootstrap
.getInstance().stop()
213 return { status: ResponseStatus
.SUCCESS
}
215 return { status: ResponseStatus
.FAILURE
}