aboutsummaryrefslogtreecommitdiff
path: root/mavigator-server/src/main/scala/mavigator/Router.scala
blob: 389cb4db267562eebdfc818d56c00979ecb18d9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package mavigator

import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.http.scaladsl._
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.ws._
import akka.http.scaladsl.server._
import uav.Uav
import akka.util._

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.MediaTypes._
import akka.http.scaladsl.model.MediaType
import play.twirl.api.{ Xml, Txt, Html }


object Router {
  import Directives._

  val socketUrl = "ws://localhost:8080/mavlink"

  def route(implicit system: ActorSystem): Route = (
    path("cockpit" / IntNumber) { id =>
      get {
        val html = mavigator.views.html.app(
          "Mavigator",
          "mavigator_cockpit_Main",
          Map(
            "socketUrl" -> socketUrl,
	    "remoteSystemId" -> "0",
            "systemId" -> "0",
	    "componentId" -> "0"
          )
        )
        complete(html)
      }
    } ~
    path("mavlink") {
      get {
        val fromWebSocket = Flow[Message].collect{
          case BinaryMessage.Strict(data) => data
        }

        val toWebSocket = Flow[ByteString].map{bytes =>
          BinaryMessage(bytes)
        }

        val backend = Uav().connect()

        handleWebSocketMessages(fromWebSocket via backend via toWebSocket)
      }
    } ~
    pathPrefix("assets") {
      get {
        encodeResponse {
          getFromResourceDirectory("assets")
        }
      }
    } ~
    pathEndOrSingleSlash {
      get {
        val html = mavigator.views.html.app(
          "Index",
          "mavigator_index_Main",
          Map(
            "socketUrl" -> socketUrl
          )
        )
        complete(html)
      }
    }
  )

  implicit val twirlHtml : ToEntityMarshaller[Html] = Marshaller.StringMarshaller.wrap(`text/html`){(h: Html) =>
    h.toString
  }
  
}