| 1 | import { clone } from 'rambda' |
| 2 | |
| 3 | import type { ChargingStation } from '../charging-station/index.js' |
| 4 | import { |
| 5 | type ChargingStationData, |
| 6 | type ChargingStationWorkerMessage, |
| 7 | ChargingStationWorkerMessageEvents, |
| 8 | type Statistics |
| 9 | } from '../types/index.js' |
| 10 | import { |
| 11 | buildChargingStationAutomaticTransactionGeneratorConfiguration, |
| 12 | buildConnectorsStatus, |
| 13 | buildEvsesStatus, |
| 14 | OutputFormat |
| 15 | } from './ChargingStationConfigurationUtils.js' |
| 16 | |
| 17 | export const buildAddedMessage = ( |
| 18 | chargingStation: ChargingStation |
| 19 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
| 20 | return { |
| 21 | event: ChargingStationWorkerMessageEvents.added, |
| 22 | data: buildChargingStationDataPayload(chargingStation) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | export const buildDeletedMessage = ( |
| 27 | chargingStation: ChargingStation |
| 28 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
| 29 | return { |
| 30 | event: ChargingStationWorkerMessageEvents.deleted, |
| 31 | data: buildChargingStationDataPayload(chargingStation) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | export const buildStartedMessage = ( |
| 36 | chargingStation: ChargingStation |
| 37 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
| 38 | return { |
| 39 | event: ChargingStationWorkerMessageEvents.started, |
| 40 | data: buildChargingStationDataPayload(chargingStation) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export const buildStoppedMessage = ( |
| 45 | chargingStation: ChargingStation |
| 46 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
| 47 | return { |
| 48 | event: ChargingStationWorkerMessageEvents.stopped, |
| 49 | data: buildChargingStationDataPayload(chargingStation) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | export const buildUpdatedMessage = ( |
| 54 | chargingStation: ChargingStation |
| 55 | ): ChargingStationWorkerMessage<ChargingStationData> => { |
| 56 | return { |
| 57 | event: ChargingStationWorkerMessageEvents.updated, |
| 58 | data: buildChargingStationDataPayload(chargingStation) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | export const buildPerformanceStatisticsMessage = ( |
| 63 | statistics: Statistics |
| 64 | ): ChargingStationWorkerMessage<Statistics> => { |
| 65 | return { |
| 66 | event: ChargingStationWorkerMessageEvents.performanceStatistics, |
| 67 | // FIXME: CircularBuffer is not structured-cloneable, rambda clone strips the whole statisticsData Map |
| 68 | data: clone(statistics) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const buildChargingStationDataPayload = (chargingStation: ChargingStation): ChargingStationData => { |
| 73 | return { |
| 74 | started: chargingStation.started, |
| 75 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 76 | stationInfo: chargingStation.stationInfo!, |
| 77 | connectors: buildConnectorsStatus(chargingStation), |
| 78 | evses: buildEvsesStatus(chargingStation, OutputFormat.worker), |
| 79 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 80 | ocppConfiguration: chargingStation.ocppConfiguration!, |
| 81 | supervisionUrl: chargingStation.wsConnectionUrl.href, |
| 82 | wsState: chargingStation.wsConnection?.readyState, |
| 83 | bootNotificationResponse: chargingStation.bootNotificationResponse, |
| 84 | ...(chargingStation.automaticTransactionGenerator != null && { |
| 85 | automaticTransactionGenerator: |
| 86 | buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation) |
| 87 | }) |
| 88 | } |
| 89 | } |