Commit | Line | Data |
---|---|---|
312d3254 | 1 | import { CircularBuffer } from 'mnemonist' |
840ca85d | 2 | |
66a7748d | 3 | import type { ChargingStation } from '../charging-station/index.js' |
32de5a57 | 4 | import { |
e0b0ee21 JB |
5 | type ChargingStationData, |
6 | type ChargingStationWorkerMessage, | |
32de5a57 | 7 | ChargingStationWorkerMessageEvents, |
1c818bd3 JB |
8 | type Statistics, |
9 | type TimestampedData | |
66a7748d | 10 | } from '../types/index.js' |
4c3f6c20 JB |
11 | import { |
12 | buildChargingStationAutomaticTransactionGeneratorConfiguration, | |
13 | buildConnectorsStatus, | |
14 | buildEvsesStatus, | |
15 | OutputFormat | |
16 | } from './ChargingStationConfigurationUtils.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 | 65 | ): ChargingStationWorkerMessage<Statistics> => { |
1c818bd3 | 66 | const statisticsData = [...statistics.statisticsData].map(([key, value]) => { |
312d3254 JB |
67 | if (value.measurementTimeSeries instanceof CircularBuffer) { |
68 | value.measurementTimeSeries = value.measurementTimeSeries.toArray() as TimestampedData[] | |
69 | } | |
1c818bd3 JB |
70 | return [key, value] |
71 | }) | |
c8faabc8 | 72 | return { |
8cc482a9 | 73 | event: ChargingStationWorkerMessageEvents.performanceStatistics, |
1c818bd3 JB |
74 | data: { |
75 | id: statistics.id, | |
76 | name: statistics.name, | |
77 | uri: statistics.uri, | |
78 | createdAt: statistics.createdAt, | |
79 | updatedAt: statistics.updatedAt, | |
80 | statisticsData | |
81 | } | |
66a7748d JB |
82 | } |
83 | } | |
32de5a57 | 84 | |
90dc299a | 85 | const buildChargingStationDataPayload = (chargingStation: ChargingStation): ChargingStationData => { |
c8faabc8 JB |
86 | return { |
87 | started: chargingStation.started, | |
5199f9fd JB |
88 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
89 | stationInfo: chargingStation.stationInfo!, | |
c8faabc8 JB |
90 | connectors: buildConnectorsStatus(chargingStation), |
91 | evses: buildEvsesStatus(chargingStation, OutputFormat.worker), | |
66a7748d | 92 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 93 | ocppConfiguration: chargingStation.ocppConfiguration!, |
1d41bc6b | 94 | supervisionUrl: chargingStation.wsConnectionUrl.href, |
5199f9fd | 95 | wsState: chargingStation.wsConnection?.readyState, |
c8faabc8 | 96 | bootNotificationResponse: chargingStation.bootNotificationResponse, |
66a7748d | 97 | ...(chargingStation.automaticTransactionGenerator != null && { |
c8faabc8 | 98 | automaticTransactionGenerator: |
66a7748d JB |
99 | buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation) |
100 | }) | |
101 | } | |
102 | } |