aboutsummaryrefslogtreecommitdiff
path: root/vfd-backend/app/plugins/UavPlugin.scala
blob: 19be049e183c05e5c749bd7dd372330ea341bfca (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
package plugins

import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.Props
import akka.actor.actorRef2Scala
import play.api.Application
import play.api.Plugin
import play.api.libs.concurrent.Akka
import vfd.uav.Connection
import vfd.uav.DummyConnection
import vfd.uav.SerialConnection

class UavPlugin(app: Application) extends Plugin {

  private lazy val config = app.configuration.getConfig("uav")

  lazy val systemId = config.flatMap(_.getInt("system_id")).getOrElse(1)

  private lazy val connection = {
    val conn = config.flatMap(_.getConfig("connection"))
    val tpe = conn.flatMap(_.getString("type")).getOrElse("mock")
    val heartbeat = conn.flatMap(_.getInt("heartbeat")).getOrElse(2000)
    val id = conn.flatMap(_.getInt("component_id")).getOrElse(99).toByte

    val props = tpe match {
      case "mock" =>
        DummyConnection.apply

      case "serial" =>
        val serial = config.flatMap(_.getConfig("serial"))
        SerialConnection(
          id,
          heartbeat,
          serial.flatMap(_.getString("port")).getOrElse("/dev/ttyUSB0"),
          serial.flatMap(_.getInt("baud")).getOrElse(115200),
          serial.flatMap(_.getBoolean("two_stop_bits")).getOrElse(false),
          serial.flatMap(_.getInt("parity")).getOrElse(0))

      case unknown => throw new RuntimeException("Unsupported connection type '" + unknown + "'")
    }

    Akka.system(app).actorOf(props, name = "uav-connection")

  }

  def register(out: ActorRef): Props = Props(classOf[Repeater], out, connection)

}

class Repeater(out: ActorRef, connection: ActorRef) extends Actor {

  override def preStart = {
    connection ! Connection.Register
  }

  def receive = {
    case msg =>
      if (sender == connection)
        out ! msg
      else
        connection ! msg
  }

}