Last active
January 20, 2016 19:11
-
-
Save Jacoby6000/c5422868f71c5c27dd14 to your computer and use it in GitHub Desktop.
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
| import play.api.libs.json.{Json, Writes} | |
| trait RabbitMessage[T] { | |
| def commandFor(t: T): String | |
| def serializer: Writes[T] | |
| } | |
| object RabbitMessage { | |
| def apply[T](writes: Writes[T], getCommand: T => String) = new RabbitMessage[T] { | |
| def commandFor(t: T) = getCommand(t) | |
| val serializer = writes | |
| } | |
| } | |
| object ActivateMessage { | |
| implicit val toRabbitMessage: RabbitMessage[ActivateMessage] = RabbitMessage[ActivateMessage](Json.writes, _.command) | |
| } | |
| case class ActivateMessage(Id:String, command:String="activate") | |
| def publishToRMQ[T](deviceId: String, message: T)(implicit toRabbitMessage: RabbitMessage[T]) = { | |
| val json = toRabbitMessage.serializer.writes(message) | |
| val command = toRabbitMessage.commandFor(message) | |
| publishString(json, getToRMQRoutingKey(deviceId, command)) | |
| } | |
| sealed case class SendMessage[T](Id: String, message: T, toRabbitMessage: RabbitMessage[T]) | |
| object SendMessage { | |
| def apply[T: RabbitMessage](id: String, message: T): SendMessage = SendMessage.apply(id, message, implicitly) | |
| } | |
| when(Idle){ | |
| import RabbitMessage.writes | |
| case Event(SendMessageWithWrites(id, message, toRabbitMessage),_) => { | |
| log.info(s"sending RMQ message: $message") | |
| publishToRMQ(id, message)(toRabbitMessage) | |
| goto(AwaitRMQResponse$) using AwaitRMQResponseData(sender) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment