diff options
Diffstat (limited to 'vfd-frontend/src/main/scala/vfd/frontend/MavlinkSocket.scala')
-rw-r--r-- | vfd-frontend/src/main/scala/vfd/frontend/MavlinkSocket.scala | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/vfd-frontend/src/main/scala/vfd/frontend/MavlinkSocket.scala b/vfd-frontend/src/main/scala/vfd/frontend/MavlinkSocket.scala new file mode 100644 index 0000000..81e6667 --- /dev/null +++ b/vfd-frontend/src/main/scala/vfd/frontend/MavlinkSocket.scala @@ -0,0 +1,52 @@ +package vfd.frontend + +import scala.scalajs.js +import scala.scalajs.js.Any.fromFunction1 + +import org.mavlink.Packet +import org.mavlink.Parser +import org.scalajs.dom + +import rx.core.Rx +import rx.core.Var + +class MavlinkSocket(url: String, remoteSystemId: Int) { + + val packet: Var[Packet] = Var(Packet.Empty) + + object stats { + val crcErrors = Var(0) + val overflows = Var(0) + val wrongIds = Var(0) + val packets = Var(0) + } + + private val parser = new Parser( + pckt => { + pckt match { + case Packet(seq, `remoteSystemId`, compId, msgId, payload) => + packet() = pckt + stats.packets() += 1 + case _ => stats.wrongIds() += 1 + } + }, + err => { + err match { + case Parser.ParseErrors.CrcError => stats.crcErrors() += 1 + case Parser.ParseErrors.OverflowError => stats.overflows() += 1 + } + }) + + private val connection = new dom.WebSocket(url) + + connection.binaryType = "arraybuffer"; + connection.onmessage = (e: dom.MessageEvent) => { + val buffer = e.data.asInstanceOf[js.typedarray.ArrayBuffer] + val dv = new js.typedarray.DataView(buffer) + + for (i <- 0 until dv.byteLength) { + parser.push(dv.getInt8(i)) + } + } + +}
\ No newline at end of file |