aboutsummaryrefslogtreecommitdiff
path: root/mavigator-uav/src/main/scala/mavigator/uav/Uav.scala
blob: 138798343bfac1cbaf81094d8648f35ce42c5f17 (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
package mavigator
package uav

import java.lang.IllegalArgumentException

import akka._
import akka.actor._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
import akka.util._

import mock._
import serial._

//TODO: the whole backend system feels hacky, it probably needs a major redesign
class Uav(system: ExtendedActorSystem) extends Extension {

  private val materializer = ActorMaterializer()(system)

  private lazy val config = system.settings.config.getConfig("mavigator.uav")
  private lazy val tpe = config.getString("type")

  private lazy val core = new Core()(system, materializer)

  lazy val backend: Backend = tpe match {
    case "mock" => MockBackend
    case "serial" => SerialBackend
    case _ => throw new IllegalArgumentException(s"Unsupported connection type: $tpe")
  }

  def init(): Unit = {
    backend.init(core)
  }

  def connect(): Flow[ByteString, ByteString, NotUsed] = core.connect()

}

object Uav extends ExtensionId[Uav] with ExtensionIdProvider {

  override def lookup = Uav

  override def createExtension(system: ExtendedActorSystem) = new Uav(system)

  def apply()(implicit system: ActorSystem) = super.apply(system)

}