aboutsummaryrefslogtreecommitdiff
path: root/vfd-index/src/main/scala/vfd/index/Main.scala
blob: f2f9f0db8a6fde1fb0f6f4fd405d73717c2f196e (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
package vfd.index

import org.mavlink.Parser
import org.mavlink.messages.Message
import org.mavlink.messages.Heartbeat

import org.scalajs.dom
import org.scalajs.dom.html

import rx._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport

import scalatags.JsDom.all._

@JSExport("Main")
object Main {
  import Util._

  case class ActiveVehicle(id: Int)

  val active = Var(Set.empty[ActiveVehicle])

  val parser = new Parser(
    packet => Message.unpack(packet.messageId, packet.payload) match {
      case hb: Heartbeat =>
        active() += ActiveVehicle(packet.systemId)
      case _ => ()
    })

  @JSExport
  def main(root: html.Element, baseAssets: String, args: js.Dictionary[String]): Unit = {

    val connection = new dom.WebSocket(args("socketUrl"))

    connection.binaryType = "arraybuffer";
    connection.onmessage = (e: dom.MessageEvent) => {
      val buffer = e.data.asInstanceOf[js.typedarray.ArrayBuffer]
      val view = new js.typedarray.DataView(buffer)

      for (i <- 0 until view.byteLength) {
        parser.push(view.getInt8(i))
      }
    }

    root.appendChild(table(`class` := "table table-hover")(
      thead(
        tr(
          th("System ID"),
          th(""))),
      Rx {
        tbody(
          for (vehicle <- active().toSeq) yield {
            tr(
              td(vehicle.id),
              td(a(href := "/dashboard/" + vehicle.id, `class` := "btn btn-default")("Pilot")))
          })
      }).render)
  }

}