summaryrefslogtreecommitdiffstats
path: root/server/bin/scheduler.js
blob: 51f1994ec101c2b7bc979fe0fff26059f3533193 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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)
}