|
| 1 | +/* |
| 2 | + * © 2024. TU Dortmund University, |
| 3 | + * Institute of Energy Systems, Energy Efficiency and Energy Economics, |
| 4 | + * Research group Distribution grid planning and operation |
| 5 | + */ |
| 6 | + |
| 7 | +package edu.ie3.simona.api.data.primarydata; |
| 8 | + |
| 9 | +import edu.ie3.datamodel.models.value.Value; |
| 10 | +import edu.ie3.simona.api.data.ExtData; |
| 11 | +import edu.ie3.simona.api.data.ontology.ScheduleDataServiceMessage; |
| 12 | +import edu.ie3.simona.api.data.primarydata.ontology.PrimaryDataMessageFromExt; |
| 13 | +import edu.ie3.simona.api.data.primarydata.ontology.ProvidePrimaryData; |
| 14 | +import edu.ie3.simona.api.exceptions.ConvertionException; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | +import org.apache.pekko.actor.ActorRef; |
| 19 | + |
| 20 | +public class ExtPrimaryData implements ExtData { |
| 21 | + |
| 22 | + /** Actor reference to service that handles ev data within SIMONA */ |
| 23 | + private final ActorRef dataService; |
| 24 | + |
| 25 | + /** Actor reference to adapter that handles scheduler control flow in SIMONA */ |
| 26 | + private final ActorRef extSimAdapter; |
| 27 | + |
| 28 | + /** Factory to convert an external object to PSDM primary data */ |
| 29 | + private final PrimaryDataFactory factory; |
| 30 | + |
| 31 | + public ExtPrimaryData(ActorRef dataService, ActorRef extSimAdapter, PrimaryDataFactory factory) { |
| 32 | + this.dataService = dataService; |
| 33 | + this.extSimAdapter = extSimAdapter; |
| 34 | + this.factory = factory; |
| 35 | + } |
| 36 | + |
| 37 | + /** Provide primary data from an external simulation in one tick. */ |
| 38 | + public void providePrimaryData(Long tick, Map<String, Object> primaryData) { |
| 39 | + Map<UUID, Value> convertedMap = new HashMap<>(); |
| 40 | + primaryData.forEach( |
| 41 | + (k, v) -> { |
| 42 | + try { |
| 43 | + convertedMap.put(UUID.fromString(k), factory.convert(v)); |
| 44 | + } catch (ConvertionException e) { |
| 45 | + throw new RuntimeException(e); |
| 46 | + } |
| 47 | + }); |
| 48 | + sendExtMsg(new ProvidePrimaryData(tick, convertedMap)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Send information from the external simulation to SIMONA's external primary data service. |
| 53 | + * Furthermore, ExtSimAdapter within SIMONA is instructed to activate the ev data service with the |
| 54 | + * current tick. |
| 55 | + * |
| 56 | + * @param msg the data/information that is sent to SIMONA's external primary data service |
| 57 | + */ |
| 58 | + public void sendExtMsg(PrimaryDataMessageFromExt msg) { |
| 59 | + dataService.tell(msg, ActorRef.noSender()); |
| 60 | + // we need to schedule data receiver activation with scheduler |
| 61 | + extSimAdapter.tell(new ScheduleDataServiceMessage(dataService), ActorRef.noSender()); |
| 62 | + } |
| 63 | +} |
0 commit comments