mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 09:31:20 -04:00
better DRD (dead rover detection)
This commit is contained in:
@@ -5,6 +5,8 @@ const { sendAlert } = require('./alertService');
|
|||||||
const ALERT_COLOR = '#00bcd4';
|
const ALERT_COLOR = '#00bcd4';
|
||||||
const { handleAck } = require('./commandService');
|
const { handleAck } = require('./commandService');
|
||||||
|
|
||||||
|
const HEARTBEAT_INTERVAL_MS = 15000;
|
||||||
|
|
||||||
function handleMessage(roverId, msg) {
|
function handleMessage(roverId, msg) {
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
case 'hello':
|
case 'hello':
|
||||||
@@ -24,7 +26,24 @@ function handleMessage(roverId, msg) {
|
|||||||
|
|
||||||
roverWSS.on('connection', (ws) => {
|
roverWSS.on('connection', (ws) => {
|
||||||
let roverId = null;
|
let roverId = null;
|
||||||
|
ws.isAlive = true;
|
||||||
|
|
||||||
|
const heartbeat = setInterval(() => {
|
||||||
|
if (!ws.isAlive) {
|
||||||
|
logger.warn('Rover websocket unresponsive', roverId || 'unknown');
|
||||||
|
ws.terminate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ws.isAlive = false;
|
||||||
|
ws.ping();
|
||||||
|
}, HEARTBEAT_INTERVAL_MS);
|
||||||
|
|
||||||
|
ws.on('pong', () => {
|
||||||
|
ws.isAlive = true;
|
||||||
|
});
|
||||||
|
|
||||||
ws.on('message', (raw) => {
|
ws.on('message', (raw) => {
|
||||||
|
ws.isAlive = true;
|
||||||
let msg;
|
let msg;
|
||||||
try {
|
try {
|
||||||
msg = JSON.parse(raw.toString());
|
msg = JSON.parse(raw.toString());
|
||||||
@@ -55,6 +74,7 @@ roverWSS.on('connection', (ws) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ws.on('close', () => {
|
ws.on('close', () => {
|
||||||
|
clearInterval(heartbeat);
|
||||||
if (roverId) {
|
if (roverId) {
|
||||||
roverManager.removeRover(roverId);
|
roverManager.removeRover(roverId);
|
||||||
sendAlert({ color: ALERT_COLOR, title: 'Rover Offline', message: roverId });
|
sendAlert({ color: ALERT_COLOR, title: 'Rover Offline', message: roverId });
|
||||||
|
|||||||
Reference in New Issue
Block a user