summaryrefslogblamecommitdiffstats
path: root/server/api/wakerequests.js
blob: f619bd99d7c9268f0360763fb8027fbccdadb62e (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                              












                                                                                                                                         



                              
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var wol = require('node-wol')
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())

router.postAsync('', async (req, res) => {
  const clients = await db.client.findAll({ where: { id: req.body.clients } })

  let i = 0
  const loop = () => {
    setTimeout(() => {
      let client = clients[i]
      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) })
      i++
      if (i < clients.length) loop()
    }, 10)
  }
  loop()

  res.status(200).end()
})

module.exports.router = router