chore: sort imports
[e-mobility-charging-stations-simulator.git] / src / utils / MessageChannelUtils.ts
... / ...
CommitLineData
1import { CircularBuffer } from 'mnemonist'
2
3import type { ChargingStation } from '../charging-station/index.js'
4
5import {
6 type ChargingStationData,
7 type ChargingStationWorkerMessage,
8 ChargingStationWorkerMessageEvents,
9 type Statistics,
10 type TimestampedData,
11} from '../types/index.js'
12import {
13 buildChargingStationAutomaticTransactionGeneratorConfiguration,
14 buildConnectorsStatus,
15 buildEvsesStatus,
16 OutputFormat,
17} from './ChargingStationConfigurationUtils.js'
18
19export const buildAddedMessage = (
20 chargingStation: ChargingStation
21): ChargingStationWorkerMessage<ChargingStationData> => {
22 return {
23 data: buildChargingStationDataPayload(chargingStation),
24 event: ChargingStationWorkerMessageEvents.added,
25 }
26}
27
28export const buildDeletedMessage = (
29 chargingStation: ChargingStation
30): ChargingStationWorkerMessage<ChargingStationData> => {
31 return {
32 data: buildChargingStationDataPayload(chargingStation),
33 event: ChargingStationWorkerMessageEvents.deleted,
34 }
35}
36
37export const buildStartedMessage = (
38 chargingStation: ChargingStation
39): ChargingStationWorkerMessage<ChargingStationData> => {
40 return {
41 data: buildChargingStationDataPayload(chargingStation),
42 event: ChargingStationWorkerMessageEvents.started,
43 }
44}
45
46export const buildStoppedMessage = (
47 chargingStation: ChargingStation
48): ChargingStationWorkerMessage<ChargingStationData> => {
49 return {
50 data: buildChargingStationDataPayload(chargingStation),
51 event: ChargingStationWorkerMessageEvents.stopped,
52 }
53}
54
55export const buildUpdatedMessage = (
56 chargingStation: ChargingStation
57): ChargingStationWorkerMessage<ChargingStationData> => {
58 return {
59 data: buildChargingStationDataPayload(chargingStation),
60 event: ChargingStationWorkerMessageEvents.updated,
61 }
62}
63
64export const buildPerformanceStatisticsMessage = (
65 statistics: Statistics
66): ChargingStationWorkerMessage<Statistics> => {
67 const statisticsData = [...statistics.statisticsData].map(([key, value]) => {
68 if (value.measurementTimeSeries instanceof CircularBuffer) {
69 value.measurementTimeSeries = value.measurementTimeSeries.toArray() as TimestampedData[]
70 }
71 return [key, value]
72 })
73 return {
74 data: {
75 createdAt: statistics.createdAt,
76 id: statistics.id,
77 name: statistics.name,
78 statisticsData,
79 updatedAt: statistics.updatedAt,
80 uri: statistics.uri,
81 },
82 event: ChargingStationWorkerMessageEvents.performanceStatistics,
83 }
84}
85
86const buildChargingStationDataPayload = (chargingStation: ChargingStation): ChargingStationData => {
87 return {
88 bootNotificationResponse: chargingStation.bootNotificationResponse,
89 connectors: buildConnectorsStatus(chargingStation),
90 evses: buildEvsesStatus(chargingStation, OutputFormat.worker),
91 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
92 ocppConfiguration: chargingStation.ocppConfiguration!,
93 started: chargingStation.started,
94 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
95 stationInfo: chargingStation.stationInfo!,
96 supervisionUrl: chargingStation.wsConnectionUrl.href,
97 wsState: chargingStation.wsConnection?.readyState,
98 ...(chargingStation.automaticTransactionGenerator != null && {
99 automaticTransactionGenerator:
100 buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation),
101 }),
102 }
103}