private def recordTemperature()

in pekko-sample-sharding-scala/killrweather-fog/src/main/scala/sample/killrweather/fog/WeatherStation.scala [87:110]


  private def recordTemperature(eventTime: Long, temperature: Double): Unit = {
    implicit val ec = context.executionContext
    implicit val materializer = SystemMaterializer(context.system).materializer

    // we could also use a class and a Json formatter like in the server
    // but since this is the only json we send this is a bit more concise
    import spray.json._
    import org.apache.pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
    val json = JsObject(
      "eventTime" -> JsNumber(eventTime),
      "dataType" -> JsString("temperature"),
      "value" -> JsNumber(temperature))

    val futureResponseBody: Future[String] = http.singleRequest(Post(stationUrl, json))
      .flatMap(res =>
        Unmarshal(res).to[String].map(body =>
          if (res.status.isSuccess()) body
          else throw new RuntimeException(s"Failed to register data: $body")))
    context.pipeToSelf(futureResponseBody) {
      case Success(s) => ProcessSuccess(s)
      case Failure(e) => ProcessFailure(e)
    }

  }