summaryrefslogtreecommitdiffstats
path: root/server/lib/wolhelper.js
blob: eaca0e67ff73228d0bc5b9263ed6397e086653dc (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
/* global __appdir */
var path = require('path')
const log = require(path.join(__appdir, 'lib', 'log'))
const wol = require('node-wol')

function wakeUp (clients) {
  let i = 0
  const loop = () => {
    setTimeout(() => {
      let client = clients[i]

      // Regex for mac address
      const regex = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/
      if (client.mac !== null && client.ip !== null && regex.test(client.mac)) {
        console.log('Waking up: ' + client.name + ' (' + client.mac + ')')
        wol.wake(client.mac, { address: client.ip.slice(0, client.ip.lastIndexOf('.') + 1) + '255' }, err => { if (err) console.log(err) })
        log({
          category: 'WAKE_ON_LAN',
          description: 'Wake on Lan signal sent to client.',
          client,
          clientId: client.id
        })
      } else {
        log({
          category: 'ERROR_WAKE_ON_LAN',
          description: 'Client has an invalid ip or mac address.',
          client,
          clientId: client.id
        })
      }
      i++
      if (i < clients.length) loop()
    }, 10)
  }
  if (clients.length !== 0) loop()
}

module.exports = { wakeUp }