3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2016 Spring Signage Ltd
5
* (MaintenanceRegularTask.php)
10
use Xibo\Helper\WakeOnLan;
13
* Class MaintenanceRegularTask
16
class MaintenanceRegularTask implements TaskInterface
23
$this->runMessage = '# ' . __('Regular Maintenance') . PHP_EOL . PHP_EOL;
25
$this->displayDownEmailAlerts();
27
$this->licenceSlotValidation();
31
$this->buildLayouts();
37
* Display Down email alerts
39
private function displayDownEmailAlerts()
41
$this->runMessage .= '## ' . __('Email Alerts') . PHP_EOL;
43
$emailAlerts = ($this->config->GetSetting("MAINTENANCE_EMAIL_ALERTS") == 'On');
44
$alwaysAlert = ($this->config->GetSetting("MAINTENANCE_ALWAYS_ALERT") == 'On');
45
$alertForViewUsers = ($this->config->GetSetting('MAINTENANCE_ALERTS_FOR_VIEW_USERS') == 1);
47
foreach ($this->app->container->get('\Xibo\Controller\Display')->setApp($this->app)->validateDisplays($this->displayFactory->query()) as $display) {
48
/* @var \Xibo\Entity\Display $display */
49
// Is this the first time this display has gone "off-line"
50
$displayGoneOffline = ($display->loggedIn == 1);
52
// Should we send an email?
54
// Alerts enabled for this display
55
if ($display->emailAlert == 1) {
56
// Display just gone offline, or always alert
57
if ($displayGoneOffline || $alwaysAlert) {
59
$subject = sprintf(__("Email Alert for Display %s"), $display->display);
60
$body = sprintf(__("Display %s with ID %d was last seen at %s."), $display->display, $display->displayId, $this->date->getLocalDate($display->lastAccessed));
63
$notification = $this->notificationFactory->createEmpty();
64
$notification->subject = $subject;
65
$notification->body = $body;
66
$notification->createdDt = $this->date->getLocalDate(null, 'U');
67
$notification->releaseDt = $this->date->getLocalDate(null, 'U');
68
$notification->isEmail = 1;
69
$notification->isInterrupt = 0;
70
$notification->userId = $this->user->userId;
71
$notification->isSystem = 1;
73
// Add the system notifications group - if there is one.
74
foreach ($this->userGroupFactory->getSystemNotificationGroups() as $group) {
75
/* @var \Xibo\Entity\UserGroup $group */
76
$notification->assignUserGroup($group);
79
// Get a list of people that have view access to the display?
80
if ($alertForViewUsers) {
82
foreach ($this->userGroupFactory->getByDisplayGroupId($display->displayGroupId) as $group) {
83
/* @var \Xibo\Entity\UserGroup $group */
84
$notification->assignUserGroup($group);
88
$notification->save();
90
$this->runMessage .= ' - A' . PHP_EOL;
92
$this->runMessage .= ' - U' . PHP_EOL;
96
// Alert disabled for this display
97
$this->runMessage .= ' - D' . PHP_EOL;
101
// Email alerts disabled globally
102
$this->runMessage .= ' - X' . PHP_EOL;
108
* Licence Slot Validation
110
private function licenceSlotValidation()
112
$maxDisplays = $this->config->GetSetting('MAX_LICENSED_DISPLAYS');
114
if ($maxDisplays > 0) {
115
$this->runMessage .= '## ' . __('Licence Slot Validation') . PHP_EOL;
117
// Get a list of all displays
119
$dbh = $this->store->getConnection();
120
$sth = $dbh->prepare('SELECT displayId, display FROM `display` WHERE licensed = 1 ORDER BY lastAccessed');
123
$displays = $sth->fetchAll(\PDO::FETCH_ASSOC);
125
if (count($displays) > $maxDisplays) {
127
// We need to un-licence some displays
128
$difference = count($displays) - $maxDisplays;
130
$update = $dbh->prepare('UPDATE `display` SET licensed = 0 WHERE displayId = :displayId');
132
foreach ($displays as $display) {
134
// If we are down to 0 difference, then stop
135
if ($difference == 0)
138
echo sprintf(__('Disabling %s'), $this->sanitizer->string($display['display'])) . '<br/>' . PHP_EOL;
139
$update->execute(['displayId' => $display['displayId']]);
145
$this->runMessage .= ' - Done' . PHP_EOL . PHP_EOL;
147
catch (\Exception $e) {
148
$this->log->error($e);
156
private function wakeOnLan()
158
$this->runMessage = '# ' . __('Wake On LAN') . PHP_EOL;
161
// Get a list of all displays which have WOL enabled
162
foreach($this->displayFactory->query(null, ['wakeOnLan' => 1]) as $display) {
163
/** @var \Xibo\Entity\Display $display */
164
// Time to WOL (with respect to today)
165
$timeToWake = strtotime(date('Y-m-d') . ' ' . $display->wakeOnLanTime);
168
// Should the display be awake?
169
if ($timeNow >= $timeToWake) {
170
// Client should be awake, so has this displays WOL time been passed
171
if ($display->lastWakeOnLanCommandSent < $timeToWake) {
172
// Call the Wake On Lan method of the display object
173
if ($display->macAddress == '' || $display->broadCastAddress == '')
174
throw new \InvalidArgumentException(__('This display has no mac address recorded against it yet. Make sure the display is running.'));
176
$this->log->notice('About to send WOL packet to ' . $display->broadCastAddress . ' with Mac Address ' . $display->macAddress);
179
WakeOnLan::TransmitWakeOnLan($display->macAddress, $display->secureOn, $display->broadCastAddress, $display->cidr, '9', $this->log);
180
$this->runMessage .= ' - ' . $display->display . ' Sent WOL Message. Previous WOL send time: ' . $this->date->getLocalDate($display->lastWakeOnLanCommandSent) . PHP_EOL;
182
$display->lastWakeOnLanCommandSent = time();
183
$display->save(['validate' => false, 'audit' => true]);
185
catch (\Exception $e) {
186
$this->runMessage .= ' - ' . $display->display . ' Error=' . $e->getMessage() . PHP_EOL;
190
$this->runMessage .= ' - ' . $display->display . ' Display already awake. Previous WOL send time: ' . $this->date->getLocalDate($display->lastWakeOnLanCommandSent) . PHP_EOL;
193
$this->runMessage .= ' - ' . $display->display . ' Sleeping' . PHP_EOL;
195
$this->runMessage .= ' - ' . $display->display . ' N/A' . PHP_EOL;
198
$this->runMessage .= ' - Done' . PHP_EOL . PHP_EOL;
200
catch (\PDOException $e) {
201
$this->log->error($e->getMessage());
202
$this->runMessage .= ' - Error' . PHP_EOL . PHP_EOL;
209
private function buildLayouts()
211
$this->runMessage .= '## ' . __('Build Layouts') . PHP_EOL;
214
foreach ($this->layoutFactory->query(null, ['status' => 3]) as $layout) {
215
/* @var \Xibo\Entity\Layout $layout */
217
$layout->xlfToDisk(['notify' => false]);
218
} catch (\Exception $e) {
219
$this->log->error('Maintenance cannot build Layout %d, %s.', $layout->layoutId, $e->getMessage());
223
$this->runMessage .= ' - Done' . PHP_EOL . PHP_EOL;
229
private function tidyLibrary()
231
$this->runMessage .= '## ' . __('Tidy Library') . PHP_EOL;
234
/** @var \Xibo\Controller\Library $libraryController */
235
$libraryController = $this->app->container->get('\Xibo\Controller\Library');
236
$libraryController->removeExpiredFiles();
237
$libraryController->removeTempFiles();
239
$this->runMessage .= ' - Done' . PHP_EOL . PHP_EOL;
b'\\ No newline at end of file'