aboutsummaryrefslogtreecommitdiff
path: root/mavigator-server/src/main/scala/mavigator
diff options
context:
space:
mode:
Diffstat (limited to 'mavigator-server/src/main/scala/mavigator')
-rw-r--r--mavigator-server/src/main/scala/mavigator/Main.scala44
-rw-r--r--mavigator-server/src/main/scala/mavigator/Router.scala80
2 files changed, 124 insertions, 0 deletions
diff --git a/mavigator-server/src/main/scala/mavigator/Main.scala b/mavigator-server/src/main/scala/mavigator/Main.scala
new file mode 100644
index 0000000..6ea894e
--- /dev/null
+++ b/mavigator-server/src/main/scala/mavigator/Main.scala
@@ -0,0 +1,44 @@
+package mavigator
+
+import akka.actor._
+import akka.http.scaladsl._
+import akka.http.scaladsl.server._
+import akka.stream._
+import scala.concurrent.Await
+import scala.concurrent.duration.Duration
+
+object Main {
+
+ implicit lazy val system = ActorSystem("mavigator")
+ implicit lazy val materializer = ActorMaterializer()
+
+ def main(args: Array[String]): Unit = {
+ import system.dispatcher
+
+ system.log.info("System started.")
+
+ val router = Router.route
+
+ system.log.info(s"Starting server")
+ val binding = Http(system).bindAndHandle(router, "0.0.0.0", 8080)
+
+ for (b <- binding) {
+ val addr = b.localAddress.getHostString()
+ val port = b.localAddress.getPort()
+ system.log.info(s"Server is listening on $addr:$port")
+ }
+
+ scala.io.StdIn.readLine()
+
+ binding.flatMap{b =>
+ system.log.info("Shutting down server...")
+ b.unbind()
+ }.onComplete{ _ =>
+ system.log.info("Server shut down")
+ system.terminate()
+ }
+
+ Await.result(system.whenTerminated, Duration.Inf)
+
+ }
+}
diff --git a/mavigator-server/src/main/scala/mavigator/Router.scala b/mavigator-server/src/main/scala/mavigator/Router.scala
new file mode 100644
index 0000000..07e40b0
--- /dev/null
+++ b/mavigator-server/src/main/scala/mavigator/Router.scala
@@ -0,0 +1,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("dashboard" / IntNumber) { id =>
+ get {
+ val html = mavigator.views.html.app(
+ "Mavigator",
+ "mavigator_dashboard_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
+ }
+
+}