node-mavlink-heartbeat
Version:
MavLink heartbeat services
71 lines (70 loc) • 2.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Heartbeat = void 0;
const stream_1 = require("stream");
const node_mavlink_1 = require("node-mavlink");
const node_mavlink_utils_1 = require("node-mavlink-utils");
/**
* Heartbeat manager for node-mavlink
*/
class Heartbeat extends stream_1.Transform {
port;
protocol;
timer = null;
type = node_mavlink_1.minimal.MavType.GCS;
autopilot = node_mavlink_1.minimal.MavAutopilot.GENERIC;
baseMode = node_mavlink_1.minimal.MavModeFlag.CUSTOM_MODE_ENABLED | node_mavlink_1.minimal.MavModeFlag.TEST_ENABLED;
customMode = 0;
systemStatus = node_mavlink_1.minimal.MavState.ACTIVE;
mavlinkVersion = 2;
constructor(port, options = {}) {
super({ objectMode: true, ...options });
this.port = port;
this.protocol = options.protocol || new node_mavlink_1.MavLinkProtocolV2();
this.send = this.send.bind(this);
}
_transform(packet, encoding, callback) {
if (packet.header.msgid === node_mavlink_1.minimal.Heartbeat.MSG_ID) {
const heartbeat = packet.protocol.data(packet.payload, node_mavlink_1.minimal.Heartbeat);
this.emit('heartbeat', { packet, heartbeat });
}
callback(null, packet);
}
/**
* Wait for one heartbeat
*/
async waitForOne(timeout = 1000) {
return await (0, node_mavlink_utils_1.waitForEvent)(this, 'heartbeat', timeout);
}
/**
* Send heartbeat packet to the other side
*/
async send() {
const packet = new node_mavlink_1.minimal.Heartbeat();
packet.type = this.type;
packet.autopilot = this.autopilot;
packet.baseMode = this.baseMode;
packet.customMode = this.customMode;
packet.systemStatus = this.systemStatus;
packet.mavlinkVersion = this.mavlinkVersion;
await (0, node_mavlink_1.send)(this.port, packet, this.protocol);
}
/**
* Start sending heartbeat packets periodically every <code>interval<code> millisecond
*/
start(interval = 1000) {
if (this.timer)
throw new Error('Heartbeat already running');
this.timer = setInterval(this.send, interval);
}
/**
* Stop sending heartbeat packets periodically
*/
stop() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}
}
exports.Heartbeat = Heartbeat;