Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separating free lots request from evcs power calculation #26

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/scala/edu/ie3/simona/agent/ValueStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ final case class ValueStore[+D](
.filter(entry => entry._1 <= requestedTick)
.maxByOption(entry => entry._1)

/** Get the last known entry (with the highest tick)
*
* @return
* An Option to the last entry
*/
def last(): Option[(Long, D)] =
store
.maxByOption(entry => entry._1)
sebastian-peter marked this conversation as resolved.
Show resolved Hide resolved

/** Acquires the stored information within the specified tick window
*
* @param requestStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import edu.ie3.simona.agent.participant.data.Data.PrimaryData.{
}
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService
import edu.ie3.simona.agent.participant.data.secondary.SecondaryDataService.ActorEvMovementsService
import edu.ie3.simona.agent.participant.statedata.BaseStateData.ParticipantModelBaseStateData
import edu.ie3.simona.agent.participant.statedata.ParticipantStateData
import edu.ie3.simona.agent.state.AgentState.Idle
import edu.ie3.simona.config.SimonaConfig.EvcsRuntimeConfig
import edu.ie3.simona.model.participant.EvcsModel
import edu.ie3.simona.model.participant.EvcsModel.EvcsRelevantData
import edu.ie3.simona.ontology.messages.services.EvMessage.EvFreeLotsRequest
import tech.units.indriya.ComparableQuantity

import javax.measure.quantity.Power
Expand Down Expand Up @@ -57,6 +60,19 @@ class EvcsAgent(
with EvcsAgentFundamentals {
override val alternativeResult: ApparentPower = ZERO_POWER

when(Idle) {
case Event(
EvFreeLotsRequest(tick),
modelBaseStateData: ParticipantModelBaseStateData[
ApparentPower,
EvcsRelevantData,
EvcsModel
]
) =>
handleFreeLotsRequest(tick, modelBaseStateData)
stay()
}

/** Determine the average result within the given tick window
*
* @param tickToResults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import edu.ie3.simona.model.participant.EvcsModel
import edu.ie3.simona.model.participant.EvcsModel.EvcsRelevantData
import edu.ie3.simona.ontology.messages.services.EvMessage.{
DepartedEvsResponse,
EvFreeLotsRequest,
EvMovementData,
FreeLotsResponse
}
Expand Down Expand Up @@ -264,12 +263,6 @@ protected trait EvcsAgentFundamentals
modelBaseStateData,
evcsData
)
case (_, Some(EvFreeLotsRequest)) =>
handleFreeLotsRequestAndGoIdle(
currentTick,
scheduler,
modelBaseStateData
)
}
.getOrElse(
throw new InconsistentStateException(
Expand All @@ -285,44 +278,33 @@ protected trait EvcsAgentFundamentals
}

/** Returns the number of free parking lots based on the last available state
* data. Sends completion message to scheduler without scheduling new
* activations.
* @param currentTick
* The current tick that has been triggered
* @param scheduler
* The scheduler ref
* data.
* @param tick
* The tick that free lots have been requested for
* @param modelBaseStateData
* The state data
* @return
* [[Idle]] state
*/
private def handleFreeLotsRequestAndGoIdle(
currentTick: Long,
scheduler: ActorRef,
protected def handleFreeLotsRequest(
tick: Long,
modelBaseStateData: ParticipantModelBaseStateData[
_ <: ApparentPower,
_,
_
]
): FSM.State[AgentState, ParticipantStateData[ApparentPower]] = {
): Unit = {
val evServiceRef = getService[ActorEvMovementsService](
modelBaseStateData.services
)

val (_, lastEvs) =
getTickIntervalAndLastEvs(currentTick, modelBaseStateData)
getTickIntervalAndLastEvs(tick, modelBaseStateData)

val evcsModel = getEvcsModel(modelBaseStateData)

evServiceRef ! FreeLotsResponse(
evcsModel.uuid,
evcsModel.chargingPoints - lastEvs.size
)

goToIdleReplyCompletionAndScheduleTriggerForNextAction(
modelBaseStateData,
scheduler
)
}

/** Handles a evcs movements message that contains information on arriving and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ object EvMessage {
) extends EvMessage
with ProvisionMessage[EvData]

final case object EvFreeLotsRequest extends EvData
/** Requests number of free lots from evcs
* @param tick
* The latest tick that the data is requested for
*/
final case class EvFreeLotsRequest(
tick: Long
)

/** Hold EV movements for one Evcs
*
Expand Down
18 changes: 4 additions & 14 deletions src/main/scala/edu/ie3/simona/service/ev/ExtEvDataService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,9 @@ class ExtEvDataService(override val scheduler: ActorRef)
private def requestFreeLots(tick: Long)(implicit
serviceStateData: ExtEvStateData
): (ExtEvStateData, Option[Seq[ScheduleTriggerMessage]]) = {
val scheduleTriggerMsgs =
serviceStateData.uuidToActorRef.map { case (_, evcsActor) =>
evcsActor ! ProvideEvDataMessage(
tick,
EvFreeLotsRequest
)

// schedule activation of participant
ScheduleTriggerMessage(
ActivityStartTrigger(tick),
evcsActor
)
}
serviceStateData.uuidToActorRef.foreach { case (_, evcsActor) =>
evcsActor ! EvFreeLotsRequest(tick)
}

val freeLots: Map[UUID, Option[Int]] =
serviceStateData.uuidToActorRef.map { case (evcs, _) =>
Expand All @@ -228,7 +218,7 @@ class ExtEvDataService(override val scheduler: ActorRef)
extEvMessage = None,
freeLots = freeLots
),
Option.when(scheduleTriggerMsgs.nonEmpty)(scheduleTriggerMsgs.toSeq)
None
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class EvcsAgentModelCalculationSpec
}
}

"do correct transitions faced to new data in Idle" in {
"do correct transitions faced with new data in Idle" in {
val evcsAgent = TestFSMRef(
new EvcsAgent(
scheduler = scheduler.ref,
Expand Down Expand Up @@ -837,18 +837,7 @@ class EvcsAgentModelCalculationSpec
/* Send out public evcs request */
evService.send(
evcsAgent,
ProvideEvDataMessage(
0L,
EvFreeLotsRequest
)
)
scheduler.send(
evcsAgent,
TriggerWithIdMessage(
ActivityStartTrigger(0L),
3L,
evcsAgent
)
EvFreeLotsRequest(0L)
)

evService.expectMsg(
Expand All @@ -857,7 +846,6 @@ class EvcsAgentModelCalculationSpec
2
)
)
scheduler.expectMsg(CompletionMessage(3L))

/* Send ev for this tick */
evService.send(
Expand All @@ -880,21 +868,10 @@ class EvcsAgentModelCalculationSpec
)
scheduler.expectMsg(CompletionMessage(4L))

/* Ask for public evcs count again */
/* Ask for public evcs lot count again with a later tick */
evService.send(
evcsAgent,
ProvideEvDataMessage(
3600L,
EvFreeLotsRequest
)
)
scheduler.send(
evcsAgent,
TriggerWithIdMessage(
ActivityStartTrigger(3600L),
5L,
evcsAgent
)
EvFreeLotsRequest(3600L)
)

// this time, only one is still free
Expand All @@ -904,7 +881,7 @@ class EvcsAgentModelCalculationSpec
1
)
)
scheduler.expectMsg(CompletionMessage(5L))
scheduler.expectNoMessage()
sebastian-peter marked this conversation as resolved.
Show resolved Hide resolved
}

val evcsAgent = TestFSMRef(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ class ExtEvDataServiceSpec
new RequestEvcsFreeLots()
)

// ev service should receive movements msg at this moment
// ev service should receive request at this moment
// scheduler receives schedule msg
extSimAdapter.expectMsg(new ScheduleDataServiceMessage(evService))

Expand All @@ -654,34 +654,17 @@ class ExtEvDataServiceSpec
)

evcs1.expectMsg(
ProvideEvDataMessage(
tick,
EvFreeLotsRequest
)
EvFreeLotsRequest(tick)
)

evcs2.expectMsg(
ProvideEvDataMessage(
tick,
EvFreeLotsRequest
)
EvFreeLotsRequest(tick)
)

scheduler.expectMsg(
CompletionMessage(
triggerId,
Some(
Seq(
ScheduleTriggerMessage(
ActivityStartTrigger(tick),
evcs1.ref
),
ScheduleTriggerMessage(
ActivityStartTrigger(tick),
evcs2.ref
)
)
)
None
)
)

Expand Down