Commit | Line | Data |
---|---|---|
66a7748d | 1 | import type { ChargingStation } from '../charging-station/index.js' |
32de5a57 | 2 | import { |
e0b0ee21 JB |
3 | type ChargingStationData, |
4 | type ChargingStationWorkerMessage, | |
32de5a57 | 5 | ChargingStationWorkerMessageEvents, |
e8237645 JB |
6 | type InternalTemplateStatistics, |
7 | type Statistics, | |
8 | type TemplateStatistics | |
66a7748d | 9 | } from '../types/index.js' |
4c3f6c20 JB |
10 | import { |
11 | buildChargingStationAutomaticTransactionGeneratorConfiguration, | |
12 | buildConnectorsStatus, | |
13 | buildEvsesStatus, | |
14 | OutputFormat | |
15 | } from './ChargingStationConfigurationUtils.js' | |
16 | import { clone } from './Utils.js' | |
32de5a57 | 17 | |
244c1396 JB |
18 | export const buildAddedMessage = ( |
19 | chargingStation: ChargingStation | |
20 | ): ChargingStationWorkerMessage<ChargingStationData> => { | |
21 | return { | |
22 | event: ChargingStationWorkerMessageEvents.added, | |
23 | data: buildChargingStationDataPayload(chargingStation) | |
24 | } | |
25 | } | |
26 | ||
09e5a7a8 JB |
27 | export const buildDeletedMessage = ( |
28 | chargingStation: ChargingStation | |
29 | ): ChargingStationWorkerMessage<ChargingStationData> => { | |
30 | return { | |
31 | event: ChargingStationWorkerMessageEvents.deleted, | |
32 | data: buildChargingStationDataPayload(chargingStation) | |
33 | } | |
34 | } | |
35 | ||
c8faabc8 | 36 | export const buildStartedMessage = ( |
66a7748d | 37 | chargingStation: ChargingStation |
c8faabc8 JB |
38 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
39 | return { | |
8cc482a9 | 40 | event: ChargingStationWorkerMessageEvents.started, |
66a7748d JB |
41 | data: buildChargingStationDataPayload(chargingStation) |
42 | } | |
43 | } | |
32de5a57 | 44 | |
c8faabc8 | 45 | export const buildStoppedMessage = ( |
66a7748d | 46 | chargingStation: ChargingStation |
c8faabc8 JB |
47 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
48 | return { | |
8cc482a9 | 49 | event: ChargingStationWorkerMessageEvents.stopped, |
66a7748d JB |
50 | data: buildChargingStationDataPayload(chargingStation) |
51 | } | |
52 | } | |
32de5a57 | 53 | |
c8faabc8 | 54 | export const buildUpdatedMessage = ( |
66a7748d | 55 | chargingStation: ChargingStation |
c8faabc8 JB |
56 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
57 | return { | |
8cc482a9 | 58 | event: ChargingStationWorkerMessageEvents.updated, |
66a7748d JB |
59 | data: buildChargingStationDataPayload(chargingStation) |
60 | } | |
61 | } | |
32de5a57 | 62 | |
c8faabc8 | 63 | export const buildPerformanceStatisticsMessage = ( |
66a7748d | 64 | statistics: Statistics |
c8faabc8 JB |
65 | ): ChargingStationWorkerMessage<Statistics> => { |
66 | return { | |
8cc482a9 | 67 | event: ChargingStationWorkerMessageEvents.performanceStatistics, |
66a7748d JB |
68 | data: statistics |
69 | } | |
70 | } | |
32de5a57 | 71 | |
244c1396 JB |
72 | export const buildChargingStationDataPayload = ( |
73 | chargingStation: ChargingStation | |
74 | ): ChargingStationData => { | |
c8faabc8 JB |
75 | return { |
76 | started: chargingStation.started, | |
5199f9fd JB |
77 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
78 | stationInfo: chargingStation.stationInfo!, | |
c8faabc8 JB |
79 | connectors: buildConnectorsStatus(chargingStation), |
80 | evses: buildEvsesStatus(chargingStation, OutputFormat.worker), | |
66a7748d | 81 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 82 | ocppConfiguration: chargingStation.ocppConfiguration!, |
1d41bc6b | 83 | supervisionUrl: chargingStation.wsConnectionUrl.href, |
5199f9fd | 84 | wsState: chargingStation.wsConnection?.readyState, |
c8faabc8 | 85 | bootNotificationResponse: chargingStation.bootNotificationResponse, |
66a7748d | 86 | ...(chargingStation.automaticTransactionGenerator != null && { |
c8faabc8 | 87 | automaticTransactionGenerator: |
66a7748d JB |
88 | buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation) |
89 | }) | |
90 | } | |
91 | } | |
e8237645 JB |
92 | |
93 | export const buildTemplateStatisticsPayload = ( | |
94 | map: Map<string, InternalTemplateStatistics> | |
95 | ): Record<string, TemplateStatistics> => { | |
96 | map = clone(map) | |
97 | for (const value of map.values()) { | |
98 | (value as unknown as TemplateStatistics).indexes = [...value.indexes] | |
99 | } | |
100 | return Object.fromEntries(map.entries() as unknown as Array<[string, TemplateStatistics]>) | |
101 | } |