summaryrefslogblamecommitdiffstats
path: root/server/bin/scheduler.js
blob: dab37a4901bb93b1c6ec4d628c168baab526cf07 (plain) (tree)
1
2
3
4
5
6
7




                                                                      
                                                                  
                                                        






                                               








                                               



                                                                                           
                                                                 
                                           
                               








                                     


                                                      















                                                  
                                                                                      



















                                                                                           
                                                                                          
                                          
 

                           
 
/* global __appdir */
const path = require('path')
global.__appdir = path.join(__dirname, '..')
const db = require(path.join(__appdir, 'lib', 'sequelize'))
const groupHelper = require(path.join(__appdir, 'lib', 'grouphelper'))
const wolHelper = require(path.join(__appdir, 'lib', 'wolhelper'))
const setTimeoutAt = require('safe-timers').setTimeoutAt
const zmq = require('zeromq')
const socket = zmq.socket('pull')
socket.bindSync('ipc:///tmp/bas_zeromq_events')
socket.on('message', calcNextWake)

const runningTimeouts = {}

scheduleAllEvents()

async function scheduleAllEvents () {
  var events = await db.event.findAll()
  events.forEach(event => {
    if (event.wakeonlan) calcNextWake(event.id)
  })
}

async function calcNextWake (id) {
  id = id.toString()
  var event = await db.event.findOne({ where: { id: id }, include: ['groups', 'clients'] })

  // Event got deleted or was updated to be not wakeonlan anymore
  if (event === null || !event.wakeonlan) {
    runningTimeouts[id].clear()
    delete runningTimeouts[id]
    return
  }

  var nextWake
  var times = JSON.parse(event.times)
  var now = new Date()

  if (!times.repetitive) {
    nextWake = new Date(times.start * 1000)
    if (nextWake > now) {
      runningTimeouts[id] = setTimeoutAt(function () {
        wakeUpClients(id)
      }, nextWake)
    }
  } else {
    return false
    /*
    // 1. Calculate next wake
    nextWake = 99999999999

    // 2. Set timeout
    runningTimeouts[id] = setTimeout(function () {
      wakeUpClients(id)
      calcNextWake(id)
    }, nextWake)
    */
  }
  if (nextWake > now) console.log('Scheduled event ' + event.name + ' at ' + nextWake)
}

// Wake all clients of event
async function wakeUpClients (id) {
  var event = await db.event.findOne({ where: { id: id }, include: ['groups', 'clients'] })
  if (!event.wakeonlan) return

  // 1. Fetch all clients
  var clients = []
  var groups = []
  var blacklistClients = []
  var blacklistGroups = []
  for (let i = 0; i < event.clients.length; i++) {
    if (event.clients[i].client_x_event.blacklist) blacklistClients.push(event.clients[i])
    else clients.push(event.clients[i])
  }
  for (let i = 0; i < event.groups.length; i++) {
    if (event.groups[i].group_x_event.blacklist) blacklistGroups.push(event.groups[i])
    else groups.push(event.groups[i])
  }
  var childs = await groupHelper.getAllChildren(groups, blacklistGroups, blacklistClients)
  clients = clients.concat(childs.clients)

  // 2. Wake all clients
  wolHelper.wakeUp(clients)
}