| 1 | import { |
| 2 | ChargingStationData, |
| 3 | ChargingStationWorkerMessage, |
| 4 | ChargingStationWorkerMessageEvents, |
| 5 | } from '../types/ChargingStationWorker'; |
| 6 | import Statistics from '../types/Statistics'; |
| 7 | import type ChargingStation from './ChargingStation'; |
| 8 | |
| 9 | export class MessageChannelUtils { |
| 10 | private constructor() { |
| 11 | // This is intentional |
| 12 | } |
| 13 | |
| 14 | public static buildStartedMessage( |
| 15 | chargingStation: ChargingStation |
| 16 | ): ChargingStationWorkerMessage<ChargingStationData> { |
| 17 | return { |
| 18 | id: ChargingStationWorkerMessageEvents.STARTED, |
| 19 | data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | public static buildStoppedMessage( |
| 24 | chargingStation: ChargingStation |
| 25 | ): ChargingStationWorkerMessage<ChargingStationData> { |
| 26 | return { |
| 27 | id: ChargingStationWorkerMessageEvents.STOPPED, |
| 28 | data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | public static buildUpdatedMessage( |
| 33 | chargingStation: ChargingStation |
| 34 | ): ChargingStationWorkerMessage<ChargingStationData> { |
| 35 | return { |
| 36 | id: ChargingStationWorkerMessageEvents.UPDATED, |
| 37 | data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | public static buildPerformanceStatisticsMessage( |
| 42 | statistics: Statistics |
| 43 | ): ChargingStationWorkerMessage<Statistics> { |
| 44 | return { |
| 45 | id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, |
| 46 | data: statistics, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | private static buildChargingStationDataPayload( |
| 51 | chargingStation: ChargingStation |
| 52 | ): ChargingStationData { |
| 53 | return { |
| 54 | hashId: chargingStation.hashId, |
| 55 | stationInfo: chargingStation.stationInfo, |
| 56 | stopped: chargingStation.stopped, |
| 57 | bootNotificationResponse: chargingStation.bootNotificationResponse, |
| 58 | connectors: Array.from(chargingStation.connectors.values()), |
| 59 | }; |
| 60 | } |
| 61 | } |