MeshCtrl wake improvements.

Ylian Saint-Hilaire committed Nov 5, 2021 at 18:22 UTC 3e115c272b7b4ffd949dda0ca8717cd6a9ff0baa
1 file changed +23 -9
meshuser.js
+23 -9
@@ -3373,14 +3373,23 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3373 var nodeid = command.nodeids[i];
3374
3375 // Argument validation
3376 - if (common.validateString(nodeid, 8, 128) == false) { continue; } // Check the nodeid
3376 + if (common.validateString(nodeid, 8, 128) == false) { // Check the nodeid
3377 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'Invalid nodeid' })); } catch (ex) { } }
3378 + continue;
3379 + }
3380 else if (nodeid.indexOf('/') == -1) { nodeid = 'node/' + domain.id + '/' + nodeid; }
3378 - else if ((nodeid.split('/').length != 3) || (nodeid.split('/')[1] != domain.id)) { continue; } // Invalid domain, operation only valid for current domain
3381 + else if ((nodeid.split('/').length != 3) || (nodeid.split('/')[1] != domain.id)) { // Invalid domain, operation only valid for current domain
3382 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'Invalid domain' })); } catch (ex) { } }
3383 + continue;
3384 + }
3385
3386 // Get the node and the rights for this node
3387 parent.GetNodeWithRights(domain, user, nodeid, function (node, rights, visible) {
3388 // Check we have the rights to delete this device
3383 - if ((rights & MESHRIGHT_WAKEDEVICE) == 0) return;
3389 + if ((node == null) || (visible == false) || (rights & MESHRIGHT_WAKEDEVICE) == 0) {
3390 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'Invalid nodeid' })); } catch (ex) { } }
3391 + return;
3392 + }
3393
3394 // If this device is connected on MQTT, send a wake action.
3395 if (parent.parent.mqttbroker != null) { parent.parent.mqttbroker.publish(node._id, 'powerAction', 'wake'); }
@@ -3394,7 +3403,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3403 } else if (nodeif.netif2) {
3404 for (var j in nodeif.netif2) { for (var k in nodeif.netif2[j]) { if (nodeif.netif2[j][k].mac && (nodeif.netif2[j][k].mac != '00:00:00:00:00:00') && (macs.indexOf(nodeif.netif2[j][k].mac) == -1)) { macs.push(nodeif.netif2[j][k].mac); } } }
3405 }
3397 - if (macs.length == 0) return;
3406 + if (macs.length == 0) {
3407 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'No known MAC addresses for this device' })); } catch (ex) { } }
3408 + return;
3409 + }
3410
3411 // Have the server send a wake-on-lan packet (Will not work in WAN-only)
3412 if (parent.parent.meshScanner != null) { parent.parent.meshScanner.wakeOnLan(macs, node.host); }
@@ -3405,22 +3417,24 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3417 for (j in user.links) { if ((j.startsWith('node/')) && (typeof user.links[j].rights == 'number') && ((user.links[j].rights & MESHRIGHT_WAKEDEVICE) != 0)) { targets.push(j); } }
3418
3419 // Go thru all the connected agents and send wake-on-lan on all the ones in the target mesh list
3420 + var wakeCount = 0;
3421 for (j in parent.wsagents) {
3422 var agent = parent.wsagents[j];
3423 if ((agent.authenticated == 2) && ((targets.indexOf(agent.dbMeshKey) >= 0) || (targets.indexOf(agent.dbNodeKey) >= 0))) {
3424 //console.log('Asking agent ' + agent.dbNodeKey + ' to wake ' + macs.join(','));
3412 - try { agent.send(JSON.stringify({ action: 'wakeonlan', macs: macs })); } catch (ex) { }
3425 + try { agent.send(JSON.stringify({ action: 'wakeonlan', macs: macs })); wakeCount++; } catch (ex) { }
3426 }
3427 }
3428 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'Used ' + wakeCount + ' device(s) to send wake packets' })); } catch (ex) { } }
3429 + } else {
3430 + if (command.nodeids.length == 1) { try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'No network information for this device' })); } catch (ex) { } }
3431 }
3432 });
3433 });
3434
3419 - // Confirm we may be doing something (TODO)
3420 - if (command.responseid != null) {
3435 + if (command.nodeids.length > 1) {
3436 + // If we are waking multiple devices, confirm we got the command.
3437 try { ws.send(JSON.stringify({ action: 'wakedevices', responseid: command.responseid, result: 'ok' })); } catch (ex) { }
3422 - } else {
3423 - try { ws.send(JSON.stringify({ action: 'wakedevices' })); } catch (ex) { }
3438 }
3439 }
3440 break;