aboutsummaryrefslogtreecommitdiff
path: root/vfd-uav/src/main/scala/vfd/uav/MockConnection.scala
blob: 1217291279c9ac4aebb9b3a70390250ae3d02c70 (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
package vfd.uav

import java.util.concurrent.TimeUnit.MILLISECONDS
import scala.concurrent.duration.FiniteDuration
import scala.util.Random
import Connection.Received
import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.Props
import akka.util.ByteString
import org.mavlink.messages._
import org.mavlink.Packet

class MockConnection extends Actor with ActorLogging with Connection {
  import Connection._
  import context._

  val messageInterval = FiniteDuration(250, MILLISECONDS)

  override def preStart() = {
    context.system.scheduler.schedule(messageInterval, messageInterval) {
      val data = MockPackets.random

      this.log.debug("sending mock flight data: " + data.mkString("(", ",", ")"))
      sendAll(Received(ByteString(data)))
    }
  }

  def receive = registration

}

object MockConnection {
  def apply = Props(classOf[MockConnection])
}

object MockPackets {

  private implicit class RichMessage(val message: Message) extends AnyVal {
    def bytes: Array[Byte] = {
      val (id, payload) = Message.pack(message)
      Packet(5, 42, 1, id, payload).toSeq.toArray
    }
  }
  
  def messages = Heartbeat(0) ::
    Motor(Random.nextInt(101).toByte, Random.nextInt(101).toByte, Random.nextInt(101).toByte, Random.nextInt(101).toByte) ::
    Attitude((Random.nextInt(160) - 80).toShort, (Random.nextInt(160) - 80).toShort.toShort, Random.nextInt(360).toShort) ::
    Power(Random.nextInt(12000).toShort) :: Nil
  
  def valid: Array[Byte] = messages.flatMap(_.bytes).toArray
  
  val invalidCrc = Array(254, 1, 123, 13, 13).map(_.toByte)
  val invalidOverflow = {
    val data = Array.fill[Byte](Packet.MaxPayloadLength + 10)(42)
    data(0) = -2
    data(1) = 2
    data(1) = -1
    data
  }
  
  def randomInvalid = Random.nextInt(2) match {
    case 0 => invalidCrc
    case 1 => invalidOverflow
  }

  def random: Array[Byte] = if (Random.nextInt(5) == 0) {
    randomInvalid
  } else {
    valid
  }
}