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




                                                                      
                                                                       
                                                                  
                                                        






                                               








                                               



                                                                                           
                                                                 
                                           

                                       








                                     
                                    

                                                      



                         
                                           
                         
                                                    


                       
   
                                                                                      



















                                                                                           
                                                                                          
                                          
 

                           

                                                        
 
/* 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 { Schedule } = require(path.join(__appdir, 'lib', 'eventhelper'))
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) {
    const timeout = runningTimeouts[id]
    if (timeout) timeout.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)
    if (nextWake > now) {
      runningTimeouts[id] = setTimeoutAt(function () {
        wakeUpClients(id)
      }, nextWake)
    }
  } else {
    nextWake = (new Schedule(event)).next()
    if (!nextWake) return
    runningTimeouts[id] = setTimeoutAt(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)

  console.log("Waking up clients. Event: " + event.name)
}