Skip to content

Commit d19a001

Browse files
committed
refactor: improve ui service logging
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent 7507c71 commit d19a001

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

src/charging-station/ui-server/ui-services/AbstractUIService.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ export abstract class AbstractUIService {
109109
public getBroadcastChannelExpectedResponses (
110110
uuid: `${string}-${string}-${string}-${string}-${string}`
111111
): number {
112-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
113-
return this.broadcastChannelRequests.get(uuid)!
112+
return this.broadcastChannelRequests.get(uuid) ?? 0
114113
}
115114

116115
public logPrefix = (modName: string, methodName: string): string => {
@@ -164,17 +163,38 @@ export abstract class AbstractUIService {
164163
} satisfies ResponsePayload
165164
}
166165
if (responsePayload != null) {
167-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
168-
return this.uiServer.buildProtocolResponse(uuid!, responsePayload)
166+
if (uuid != null) {
167+
return this.uiServer.buildProtocolResponse(uuid, responsePayload)
168+
}
169+
logger.warn(
170+
`${this.logPrefix(moduleName, 'requestHandler')} UUID is not defined in the request:`,
171+
request
172+
)
173+
return undefined
169174
}
170175
}
171176

177+
// public sendRequest (
178+
// uuid: `${string}-${string}-${string}-${string}-${string}`,
179+
// procedureName: ProcedureName,
180+
// requestPayload: RequestPayload
181+
// ): void {
182+
// this.uiServer.sendRequest(
183+
// this.uiServer.buildProtocolRequest(uuid, procedureName, requestPayload)
184+
// )
185+
// }
186+
172187
public sendResponse (
173188
uuid: `${string}-${string}-${string}-${string}-${string}`,
174189
responsePayload: ResponsePayload
175190
): void {
176191
if (this.uiServer.hasResponseHandler(uuid)) {
177192
this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(uuid, responsePayload))
193+
} else {
194+
logger.warn(`${this.logPrefix(moduleName, 'sendResponse')} Response handler not found:`, {
195+
responsePayload,
196+
uuid,
197+
})
178198
}
179199
}
180200

@@ -188,12 +208,12 @@ export abstract class AbstractUIService {
188208
procedureName: ProcedureName,
189209
payload: RequestPayload
190210
): void {
191-
this.sendBroadcastChannelRequest(
192-
uuid,
193-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
194-
AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping.get(procedureName)!,
195-
payload
196-
)
211+
const broadCastChannelProcedureName =
212+
AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping.get(procedureName)
213+
if (broadCastChannelProcedureName == null) {
214+
throw new BaseError(`No broadcast channel mapping for procedure '${procedureName}'`)
215+
}
216+
this.sendBroadcastChannelRequest(uuid, broadCastChannelProcedureName, payload)
197217
}
198218

199219
private async handleAddChargingStations (
@@ -210,7 +230,12 @@ export abstract class AbstractUIService {
210230
status: ResponseStatus.FAILURE,
211231
} satisfies ResponsePayload
212232
}
213-
if (typeof template !== 'string' || typeof numberOfStations !== 'number') {
233+
if (
234+
typeof template !== 'string' ||
235+
typeof numberOfStations !== 'number' ||
236+
!Number.isInteger(numberOfStations) ||
237+
numberOfStations <= 0
238+
) {
214239
return {
215240
errorMessage: 'Invalid request payload',
216241
status: ResponseStatus.FAILURE,
@@ -283,8 +308,7 @@ export abstract class AbstractUIService {
283308
try {
284309
return {
285310
performanceStatistics: [
286-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
287-
...Bootstrap.getInstance().getPerformanceStatistics()!,
311+
...(Bootstrap.getInstance().getPerformanceStatistics() ?? []),
288312
] as JsonType[],
289313
status: ResponseStatus.SUCCESS,
290314
} satisfies ResponsePayload
@@ -325,16 +349,6 @@ export abstract class AbstractUIService {
325349
}
326350
}
327351

328-
// public sendRequest (
329-
// uuid: `${string}-${string}-${string}-${string}-${string}`,
330-
// procedureName: ProcedureName,
331-
// requestPayload: RequestPayload
332-
// ): void {
333-
// this.uiServer.sendRequest(
334-
// this.uiServer.buildProtocolRequest(uuid, procedureName, requestPayload)
335-
// )
336-
// }
337-
338352
private async handleStopSimulator (): Promise<ResponsePayload> {
339353
try {
340354
await Bootstrap.getInstance().stop()
@@ -367,7 +381,7 @@ export abstract class AbstractUIService {
367381
)
368382
return undefined
369383
})
370-
.filter(hashId => hashId != null)
384+
.filter((hashId): hashId is string => hashId != null)
371385
} else {
372386
delete payload.hashIds
373387
}

0 commit comments

Comments
 (0)