-
Notifications
You must be signed in to change notification settings - Fork 7
Added external primary service worker #1546
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
Open
staudtMarius
wants to merge
14
commits into
dev
Choose a base branch
from
ms/#1545-add-external-primary-service-worker
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c45e652
Added external primary service worker
staudtMarius 00b2fc3
Increase test coverage.
staudtMarius 08998af
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius 29c043a
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius cbc42f3
Including reviewer's comments.
staudtMarius 9ea8540
Merge branch 'refs/heads/dev' into ms/#1545-add-external-primary-serv…
staudtMarius 3b40a73
fmt
staudtMarius 92975d5
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius 5a50773
Fixing issues after merging dev.
staudtMarius 7515078
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius f6db2e5
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius 3dee063
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius 003c321
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius af46fd5
Merge branch 'dev' into ms/#1545-add-external-primary-service-worker
staudtMarius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
src/main/scala/edu/ie3/simona/service/primary/ExtPrimaryServiceWorker.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * © 2024. TU Dortmund University, | ||
| * Institute of Energy Systems, Energy Efficiency and Energy Economics, | ||
| * Research group Distribution grid planning and operation | ||
| */ | ||
|
|
||
| package edu.ie3.simona.service.primary | ||
|
|
||
| import edu.ie3.simona.agent.participant.ParticipantAgent | ||
| import edu.ie3.simona.agent.participant.ParticipantAgent.{ | ||
| DataProvision, | ||
| PrimaryRegistrationSuccessfulMessage, | ||
| } | ||
| import edu.ie3.simona.api.data.connection.ExtPrimaryDataConnection | ||
| import edu.ie3.simona.api.ontology.DataMessageFromExt | ||
| import edu.ie3.simona.api.ontology.primary.{ | ||
| PrimaryDataMessageFromExt, | ||
| ProvidePrimaryData, | ||
| } | ||
| import edu.ie3.simona.exceptions.WeatherServiceException.InvalidRegistrationRequestException | ||
| import edu.ie3.simona.exceptions.{InitializationException, ServiceException} | ||
| import edu.ie3.simona.ontology.messages.ServiceMessage.{ | ||
| PrimaryServiceRegistrationMessage, | ||
| ServiceRegistrationMessage, | ||
| ServiceResponseMessage, | ||
| } | ||
| import edu.ie3.simona.service.Data.PrimaryData | ||
| import edu.ie3.simona.service.Data.PrimaryData.RichValue | ||
| import edu.ie3.simona.service.ServiceStateData.{ | ||
| InitializeServiceStateData, | ||
| ServiceBaseStateData, | ||
| } | ||
| import edu.ie3.simona.service.{ExtDataSupport, ServiceStateData, SimonaService} | ||
| import org.apache.pekko.actor.typed.ActorRef | ||
| import org.apache.pekko.actor.typed.scaladsl.ActorContext | ||
| import org.slf4j.Logger | ||
|
|
||
| import java.util.UUID | ||
| import scala.jdk.CollectionConverters.MapHasAsScala | ||
| import scala.jdk.OptionConverters.RichOptional | ||
| import scala.util.{Failure, Success, Try} | ||
|
|
||
| object ExtPrimaryServiceWorker extends SimonaService with ExtDataSupport { | ||
|
|
||
| override type S = ExtPrimaryDataStateData | ||
|
|
||
| final case class ExtPrimaryDataStateData( | ||
| extPrimaryData: ExtPrimaryDataConnection, | ||
| uuidToActorRef: Map[UUID, ActorRef[ParticipantAgent.Request]] = | ||
| Map.empty, // subscribers in SIMONA | ||
| extPrimaryDataMessage: Option[PrimaryDataMessageFromExt] = None, | ||
| ) extends ServiceBaseStateData | ||
|
|
||
| case class InitExtPrimaryData( | ||
| extPrimaryData: ExtPrimaryDataConnection | ||
| ) extends InitializeServiceStateData | ||
|
|
||
| override def init( | ||
| initServiceData: ServiceStateData.InitializeServiceStateData | ||
| )(using log: Logger): Try[(ExtPrimaryDataStateData, Option[Long])] = | ||
| initServiceData match { | ||
| case InitExtPrimaryData(extPrimaryData) => | ||
| val primaryDataInitializedStateData = ExtPrimaryDataStateData( | ||
| extPrimaryData | ||
| ) | ||
| Success( | ||
| primaryDataInitializedStateData, | ||
| None, | ||
| ) | ||
|
|
||
| case invalidData => | ||
| Failure( | ||
| new InitializationException( | ||
| s"Provided init data '${invalidData.getClass.getSimpleName}' for ExtPrimaryService are invalid!" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| override protected def handleRegistrationRequest( | ||
| registrationMessage: ServiceRegistrationMessage | ||
| )(using | ||
| serviceStateData: ExtPrimaryDataStateData, | ||
| ctx: ActorContext[Message], | ||
| ): Try[ExtPrimaryDataStateData] = registrationMessage match { | ||
| case PrimaryServiceRegistrationMessage( | ||
| requestingActor, | ||
| modelUuid, | ||
| ) => | ||
| Success(handleRegistrationRequest(requestingActor, modelUuid)) | ||
| case invalidMessage => | ||
| Failure( | ||
| InvalidRegistrationRequestException( | ||
| s"A primary service provider is not able to handle registration request '$invalidMessage'." | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private def handleRegistrationRequest( | ||
| agentToBeRegistered: ActorRef[ParticipantAgent.Request], | ||
| agentUUID: UUID, | ||
| )(using | ||
| serviceStateData: ExtPrimaryDataStateData, | ||
| ctx: ActorContext[Message], | ||
| ): ExtPrimaryDataStateData = { | ||
| serviceStateData.uuidToActorRef.get(agentUUID) match { | ||
| case None => | ||
| // checks if a value class was specified for the agent | ||
| val valueClass = serviceStateData.extPrimaryData | ||
| .getValueClass(agentUUID) | ||
| .toScala | ||
| .getOrElse( | ||
| throw InvalidRegistrationRequestException( | ||
| s"A primary service provider is not able to handle registration request, because there was no value class specified for the agent with id: '$agentUUID'." | ||
| ) | ||
| ) | ||
|
|
||
| agentToBeRegistered ! PrimaryRegistrationSuccessfulMessage( | ||
| ctx.self, | ||
| 0L, | ||
| PrimaryData.getPrimaryDataExtra(valueClass), | ||
| ) | ||
| ctx.log.info(s"Successful registration for $agentUUID") | ||
|
|
||
| serviceStateData.copy(uuidToActorRef = | ||
| serviceStateData.uuidToActorRef + (agentUUID -> agentToBeRegistered) | ||
| ) | ||
|
|
||
| case Some(_) => | ||
| // actor is already registered, do nothing | ||
| ctx.log.warn( | ||
| "Sending actor {} is already registered", | ||
| agentToBeRegistered, | ||
| ) | ||
| serviceStateData | ||
| } | ||
| } | ||
|
|
||
| override protected def announceInformation( | ||
| tick: Long | ||
| )(using | ||
| serviceStateData: ExtPrimaryDataStateData, | ||
| ctx: ActorContext[Message], | ||
| ): (ExtPrimaryDataStateData, Option[Long]) = { // We got activated for this tick, so we expect incoming primary data | ||
| serviceStateData.extPrimaryDataMessage.getOrElse( | ||
| throw ServiceException( | ||
| "ExtPrimaryDataService was triggered without ExtPrimaryDataMessage available" | ||
| ) | ||
| ) match { | ||
| case providedPrimaryData: ProvidePrimaryData => | ||
| processDataAndAnnounce(tick, providedPrimaryData) | ||
| } | ||
| } | ||
|
|
||
| private def processDataAndAnnounce( | ||
| tick: Long, | ||
| primaryDataMessage: ProvidePrimaryData, | ||
| )(using | ||
| serviceStateData: ExtPrimaryDataStateData, | ||
| ctx: ActorContext[Message], | ||
| ): ( | ||
| ExtPrimaryDataStateData, | ||
| Option[Long], | ||
| ) = { | ||
| ctx.log.debug( | ||
| s"Got activation to distribute primaryData = $primaryDataMessage" | ||
| ) | ||
|
|
||
| val uuidToAgent = serviceStateData.uuidToActorRef | ||
| val maybeNextTick = primaryDataMessage.maybeNextTick.toScala.map(Long2long) | ||
|
|
||
| primaryDataMessage.primaryData.asScala.foreach { case (agentUuid, data) => | ||
| data.toPrimaryData match { | ||
| case Success(primaryData) => | ||
| uuidToAgent.get(agentUuid) match { | ||
| case Some(agentRef) => | ||
| agentRef ! DataProvision( | ||
| tick, | ||
| ctx.self, | ||
| primaryData, | ||
| maybeNextTick, | ||
| ) | ||
|
|
||
| case None => | ||
| ctx.log.warn( | ||
| "A corresponding actor ref for UUID {} could not be found", | ||
| agentUuid, | ||
| ) | ||
| } | ||
|
|
||
| case Failure(exception) => | ||
| /* Processing of data failed */ | ||
| ctx.log.warn( | ||
| "Unable to convert received value to primary data. Skipped that data." + | ||
| "\nException: {}", | ||
| exception, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ( | ||
| serviceStateData.copy(extPrimaryDataMessage = None), | ||
| None, | ||
| ) | ||
| } | ||
|
|
||
| override protected def handleDataMessage( | ||
| extMsg: DataMessageFromExt | ||
| )(using | ||
| serviceStateData: ExtPrimaryDataStateData | ||
| ): ExtPrimaryDataStateData = { | ||
| extMsg match { | ||
| case extPrimaryDataMessage: PrimaryDataMessageFromExt => | ||
| serviceStateData.copy( | ||
| extPrimaryDataMessage = Some(extPrimaryDataMessage) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // unused by this service | ||
| override protected def handleDataResponseMessage( | ||
| extResponseMsg: ServiceResponseMessage | ||
| )(implicit | ||
| serviceStateData: ExtPrimaryDataStateData | ||
| ): ExtPrimaryDataStateData = serviceStateData | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.