diff --git a/src/main/scala/edu/ie3/simona/agent/participant/em/EmAgent.scala b/src/main/scala/edu/ie3/simona/agent/participant/em/EmAgent.scala index af4af6991f..a35cd80808 100644 --- a/src/main/scala/edu/ie3/simona/agent/participant/em/EmAgent.scala +++ b/src/main/scala/edu/ie3/simona/agent/participant/em/EmAgent.scala @@ -631,7 +631,7 @@ class EmAgent( baseStateData.schedulerStateData.flexTrigger ) { case (flexTrigger, (uuid, issueCtrlMsg, _)) => // send out flex control messages - scheduleFlexTrigger(flexTrigger, issueCtrlMsg, uuid) + scheduleFlexTriggerOnce(flexTrigger, issueCtrlMsg, uuid) } val updatedScheduledStateData = sendEligibleTrigger( diff --git a/src/main/scala/edu/ie3/simona/agent/participant/em/EmSchedulerHelper.scala b/src/main/scala/edu/ie3/simona/agent/participant/em/EmSchedulerHelper.scala index a4bbb562d6..30f74f0b36 100644 --- a/src/main/scala/edu/ie3/simona/agent/participant/em/EmSchedulerHelper.scala +++ b/src/main/scala/edu/ie3/simona/agent/participant/em/EmSchedulerHelper.scala @@ -113,7 +113,7 @@ trait EmSchedulerHelper { // schedule new flex requests, if applicable completionMessage.requestAtTick .foreach(newTick => - scheduleFlexTrigger( + scheduleFlexTriggerOnce( triggerData, RequestFlexOptions(newTick), completionMessage.modelUuid @@ -222,47 +222,34 @@ trait EmSchedulerHelper { flexTrigger: FlexTriggerData, participantUuids: Set[UUID], tick: Long - ): FlexTriggerData = { - val alreadyScheduled = - flexTrigger.triggerQueue - .get(tick) - .getOrElse(Seq.empty) - .map(_.modelUuid) - - // participants that have to be activated at any next tick - val filteredUuids = - participantUuids - .filterNot { - // filter out duplicates here: we might have been scheduled - // for this tick anyways - alreadyScheduled.contains - } - - // add missing activation triggers - val updatedFlexTrigger = - filteredUuids.foldLeft(flexTrigger) { case (schedulerData, modelUuid) => - scheduleFlexTrigger( - schedulerData, - RequestFlexOptions(tick), - modelUuid - ) - } - - updatedFlexTrigger - } + ): FlexTriggerData = + participantUuids.foldLeft(flexTrigger) { case (schedulerData, modelUuid) => + scheduleFlexTriggerOnce( + schedulerData, + RequestFlexOptions(tick), + modelUuid + ) + } - protected def scheduleFlexTrigger( + protected def scheduleFlexTriggerOnce( flexTrigger: FlexTriggerData, trigger: Trigger with FlexibilityMessage, modelUuid: UUID ): FlexTriggerData = { - flexTrigger.triggerQueue.add( - trigger.tick, - ScheduledFlexTrigger( - trigger, - modelUuid + val scheduledTrigger = ScheduledFlexTrigger(trigger, modelUuid) + + val alreadyScheduled = + flexTrigger.triggerQueue + .get(trigger.tick) + .getOrElse(Seq.empty) + .contains(scheduledTrigger) + + if (!alreadyScheduled) + flexTrigger.triggerQueue.add( + trigger.tick, + scheduledTrigger ) - ) + flexTrigger } @@ -325,7 +312,7 @@ trait EmSchedulerHelper { triggerMessage.actorToBeScheduled ) stateData.copy( - flexTrigger = scheduleFlexTrigger( + flexTrigger = scheduleFlexTriggerOnce( stateData.flexTrigger, flexMessage, uuid diff --git a/src/main/scala/edu/ie3/simona/agent/participant/evcs/EvcsAgentFundamentals.scala b/src/main/scala/edu/ie3/simona/agent/participant/evcs/EvcsAgentFundamentals.scala index 5edef687ae..96de92019d 100644 --- a/src/main/scala/edu/ie3/simona/agent/participant/evcs/EvcsAgentFundamentals.scala +++ b/src/main/scala/edu/ie3/simona/agent/participant/evcs/EvcsAgentFundamentals.scala @@ -44,7 +44,9 @@ import edu.ie3.simona.model.participant.evcs.EvcsModel.{ EvcsRelevantData, EvcsState } +import edu.ie3.simona.ontology.messages.FlexibilityMessage.RequestFlexOptions import edu.ie3.simona.ontology.messages.PowerMessage.AssetPowerChangedMessage +import edu.ie3.simona.ontology.messages.SchedulerMessage.ScheduleTriggerMessage import edu.ie3.simona.ontology.messages.services.EvMessage._ import edu.ie3.simona.util.SimonaConstants import edu.ie3.simona.util.TickUtil.{RichZonedDateTime, TickLong} @@ -460,6 +462,10 @@ protected trait EvcsAgentFundamentals val newState = EvcsState(stayingEvs, stayingSchedules, tick) + baseStateData.flexStateData.foreach( + _.emAgent ! ScheduleTriggerMessage(RequestFlexOptions(tick), self) + ) + baseStateData.copy( stateDataStore = ValueStore.updateValueStore( baseStateData.stateDataStore, diff --git a/src/test/scala/edu/ie3/simona/agent/participant/EvcsAgentModelCalculationSpec.scala b/src/test/scala/edu/ie3/simona/agent/participant/EvcsAgentModelCalculationSpec.scala index d1a97b5d27..1a4c43af50 100644 --- a/src/test/scala/edu/ie3/simona/agent/participant/EvcsAgentModelCalculationSpec.scala +++ b/src/test/scala/edu/ie3/simona/agent/participant/EvcsAgentModelCalculationSpec.scala @@ -1535,6 +1535,10 @@ class EvcsAgentModelCalculationSpec DepartingEvsRequest(4500L, Seq(ev900.getUuid)) ) + emAgent.expectMsg( + ScheduleTriggerMessage(RequestFlexOptions(4500L), evcsAgent) + ) + evService.expectMsgPF() { case DepartingEvsResponse(uuid, evs) => evs.size shouldBe 1 uuid shouldBe evcsInputModel.getUuid @@ -2151,6 +2155,10 @@ class EvcsAgentModelCalculationSpec DepartingEvsRequest(36000L, Seq(ev900.getUuid)) ) + emAgent.expectMsg( + ScheduleTriggerMessage(RequestFlexOptions(36000L), evcsAgent) + ) + evService.expectMsgPF() { case DepartingEvsResponse(uuid, evs) => evs.size shouldBe 1 uuid shouldBe evcsInputModel.getUuid