Skip to content

Commit efefcde

Browse files
authored
Merge pull request #72 from versx/exclude-devices-reboot
Exclude Devices From Reboots
2 parents 7f0ca48 + a5d2d4e commit efefcde

13 files changed

Lines changed: 61 additions & 30 deletions

File tree

migrations/14.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `devices` ADD COLUMN `exclude_reboots` tinyint DEFAULT 0;

src/models/device.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const query = require('../services/db.js');
44

55
class Device {
6-
constructor(uuid, model, config, lastSeen, clientip, iosVersion, ipaVersion, webserverPort = 8080, notes = null, enabled = false) {
6+
constructor(uuid, model, config, lastSeen, clientip, iosVersion, ipaVersion, webserverPort = 8080, notes = null, excludeReboots = false, enabled = false) {
77
this.uuid = uuid;
88
this.model = model;
99
this.config = config;
@@ -13,40 +13,47 @@ class Device {
1313
this.ipaVersion = ipaVersion;
1414
this.webserverPort = webserverPort || 8080;
1515
this.notes = notes;
16+
this.excludeReboots = excludeReboots;
1617
this.enabled = enabled;
1718
}
1819
static async getAll() {
19-
const devices = await query('SELECT uuid, model, config, last_seen, clientip, ios_version, ipa_version, webserver_port, notes, enabled FROM devices');
20+
const sql = `
21+
SELECT uuid, model, config, last_seen, clientip, ios_version, ipa_version, webserver_port, notes, exclude_reboots, enabled
22+
FROM devices
23+
`;
24+
const devices = await query(sql);
2025
return devices;
2126
}
2227
static async getByName(uuid) {
2328
const sql = `
24-
SELECT uuid, model, config, last_seen, clientip, ios_version, ipa_version, notes, enabled
29+
SELECT uuid, model, config, last_seen, clientip, ios_version, ipa_version, webserver_port, notes, exclude_reboots, enabled
2530
FROM devices
2631
WHERE uuid = ?`;
2732
const args = [uuid];
28-
const result = await query(sql, args);
29-
if (result.length === 0) {
33+
const results = await query(sql, args);
34+
if (results.length === 0) {
3035
return null;
3136
}
37+
const result = results[0];
3238
return new Device(
33-
result[0].uuid,
34-
result[0].model,
35-
result[0].config,
36-
result[0].last_seen,
37-
result[0].clientip,
38-
result[0].ios_version,
39-
result[0].ipa_version,
40-
result[0].webserverPort,
41-
result[0].notes,
42-
result[0].enabled
39+
result.uuid,
40+
result.model,
41+
result.config,
42+
result.last_seen,
43+
result.clientip,
44+
result.ios_version,
45+
result.ipa_version,
46+
result.webserver_port,
47+
result.notes,
48+
result.exclude_reboots,
49+
result.enabled
4350
);
4451
}
45-
static async create(uuid, model = null, config = null, lastSeen = null, clientip = null, iosVersion = null, ipaVersion = null, webserverPort = 8080, notes = null, enabled = true) {
52+
static async create(uuid, model = null, config = null, lastSeen = null, clientip = null, iosVersion = null, ipaVersion = null, webserverPort = 8080, notes = null, excludeReboots = false, enabled = true) {
4653
const sql = `
47-
INSERT INTO devices (uuid, model, config, last_seen, clientip, ios_version, ipa_version, webserver_port, notes, enabled)
48-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
49-
const args = [uuid, model, config, lastSeen, clientip, iosVersion, ipaVersion, webserverPort, notes, enabled];
54+
INSERT INTO devices (uuid, model, config, last_seen, clientip, ios_version, ipa_version, webserver_port, notes, exclude_reboots, enabled)
55+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
56+
const args = [uuid, model, config, lastSeen, clientip, iosVersion, ipaVersion, webserverPort, notes, excludeReboots, enabled];
5057
const result = await query(sql, args);
5158
if (result.affectedRows === 1) {
5259
return new Device(
@@ -59,6 +66,7 @@ class Device {
5966
ipaVersion,
6067
webserverPort,
6168
notes,
69+
excludeReboots,
6270
enabled
6371
);
6472
}
@@ -78,9 +86,9 @@ class Device {
7886
async save() {
7987
const sql = `
8088
UPDATE devices
81-
SET model = ?, config = ?, last_seen = ?, clientip = ?, ios_version = ?, ipa_version = ?, webserver_port = ?, notes = ?, enabled = ?
89+
SET model = ?, config = ?, last_seen = ?, clientip = ?, ios_version = ?, ipa_version = ?, webserver_port = ?, notes = ?, exclude_reboots = ?, enabled = ?
8290
WHERE uuid = ?`;
83-
const args = [this.model, this.config, this.lastSeen, this.clientip, this.iosVersion, this.ipaVersion, this.webserverPort, this.notes, this.enabled, this.uuid];
91+
const args = [this.model, this.config, this.lastSeen, this.clientip, this.iosVersion, this.ipaVersion, this.webserverPort, this.notes, this.excludeReboots, this.enabled, this.uuid];
8492
const result = await query(sql, args);
8593
return result.affectedRows === 1;
8694
}

src/routes/api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ router.get('/devices', async (req, res) => {
195195
<a href="/device/edit/${encodedUuid}" class="dropdown-item">Edit</a>
196196
<a href="/device/logs/${encodedUuid}" class="dropdown-item">Logs</a>
197197
<h6 class="dropdown-header">Actions</h6>
198-
<button type="button" class="dropdown-item" onclick='reboot("${config.listeners}", "${device.uuid}")'>Reboot Device</button>
198+
<button type="button" class="dropdown-item" onclick='reboot("${config.listeners}", "${device.uuid}", "${device.exclude_reboots}")'>Reboot Device</button>
199199
</div>
200200
</div>`;
201+
device.exclude_reboots = device.exclude_reboots ? 'Yes' : 'No';
201202
device.enabled = device.enabled ? 'Yes' : 'No';
202203
const date = utils.convertTz(new Date());
203204
const today = date.format('YYYY-M-D');
@@ -265,9 +266,10 @@ router.post('/device/new', async (req, res) => {
265266
data.config || null,
266267
null,
267268
data.clientip || null,
268-
null,
269+
8080,
269270
null,
270271
data.notes || null,
272+
data.exclude_reboots === 'on',
271273
data.enabled === 'on'
272274
);
273275
if (result) {
@@ -283,13 +285,15 @@ router.post('/device/edit/:uuid', async (req, res) => {
283285
config,
284286
clientip,
285287
notes,
288+
exclude_reboots,
286289
enabled
287290
} = req.body;
288291
let device = await Device.getByName(uuid);
289292
if (device) {
290293
device.config = config || null;
291294
device.clientip = clientip || null;
292295
device.notes = notes || null;
296+
device.excludeReboots = exclude_reboots === 'on';
293297
device.enabled = enabled === 'on';
294298
const result = await device.save();
295299
if (!result) {

src/routes/ui.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ router.get(['/', '/index'], async (req, res) => {
4242
data.devices_offline = devices.filter(x => x.last_seen < (Math.round(utils.convertTz(new Date()).format('x') / 1000) - delta));
4343
data.devices_offline.forEach((device) => {
4444
device.last_seen = utils.getDateTime(device.last_seen * 1000);
45-
device.buttons = `<button type='button' class='btn btn-success' onclick='reboot("${config.listeners}", "${device.uuid}")'>Reboot</button>`; // TODO: Localize
45+
device.buttons = `<button type='button' class='btn btn-success' onclick='reboot("${config.listeners}", "${device.uuid}", "${device.exclude_reboots}")'>Reboot</button>`; // TODO: Localize
4646
device.uuid = `<a href='/device/manage/${device.uuid}' target='_blank' class='text-light'>${device.uuid}</a>`;
4747
});
4848
data.devices_online_count = devices.filter(x => x.last_seen >= (Math.round(utils.convertTz(new Date()).format('x') / 1000) - delta)).length;
@@ -116,6 +116,7 @@ router.get('/device/edit/:uuid', async (req, res) => {
116116
data.clientip = device.clientip;
117117
data.webserver_port = device.webserverPort;
118118
data.notes = device.notes;
119+
data.exclude_reboots = device.excludeReboots ? 'checked' : '';
119120
data.enabled = device.enabled ? 'checked' : '';
120121
res.render('device-edit', data);
121122
});
@@ -146,6 +147,7 @@ router.get('/device/manage/:uuid', async (req, res) => {
146147
logger('dcm').error(`Failed to get IP address for device ${uuid}`);
147148
}
148149
data.webserver_port = device.webserverPort;
150+
data.exclude_reboots = device.excludeReboots;
149151
const config = await Config.getByName(device.config);
150152
data.providers = providers;
151153
data.kevin_selected = config.provider === data.providers[1].name;

src/services/device-monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const checkDevices = async () => {
2828
for (let i = 0; i < devices.length; i++) {
2929
const device = devices[i];
3030
const isOffline = device.last_seen > (Math.round(utils.convertTz(new Date()).format('x') / 1000) - delta) ? 0 : 1;
31-
if (!isOffline || !device.enabled) {
31+
if (!isOffline || !device.enabled || device.exclude_reboots) {
3232
continue;
3333
}
3434

src/views/device-edit.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
{{Notes}}
3737
<input type="text" class="form-control" name="notes" value="{{notes}}" placeholder="">
3838
</div>
39+
<div class="form-check">
40+
<input type="checkbox" class="form-check-input" name="exclude_reboots" {{exclude_reboots}}>
41+
<label class="form-check-label" for="exclude_reboots">{{Exclude From Reboots}}</label>
42+
</div>
3943
<div class="form-check">
4044
<input type="checkbox" class="form-check-input" name="enabled" {{enabled}}>
4145
<label class="form-check-label" for="enabled">{{Enabled}}</label>

src/views/device-manage.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</tr>
5757
<tr>
5858
<th>{{Reboot Device}}</th>
59-
<th><button type='button' class='btn btn-success' onclick='reboot("{{{listeners}}}", "{{name}}")'>{{REBOOT}}</button></th>
59+
<th><button type='button' class='btn btn-success' onclick='reboot("{{{listeners}}}", "{{name}}", "{{exclude_reboots}}")'>{{REBOOT}}</button></th>
6060
<th><div id='reboot'></div></th>
6161
</tr>
6262
<tr>

src/views/device-new.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
{{Notes}}
2929
<input type="text" class="form-control" name="notes" value="" placeholder="">
3030
</div>
31+
<div class="form-check">
32+
<input type="checkbox" class="form-check-input" name="exclude_reboots">
33+
<label class="form-check-label" for="exclude_reboots">{{Exclude From Reboots}}</label>
34+
</div>
3135
<div class="form-check">
3236
<input type="checkbox" class="form-check-input" name="enabled" checked>
3337
<label class="form-check-label" for="enabled">{{Enabled}}</label>

src/views/devices.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<th class="all">{{Last Config}}</th>
7171
<th class="min-desktop">{{Notes}}</th>
7272
<th class="all">{{Game Restarts Today}}</th>
73+
<th class="min-desktop">{{Exclude From Reboots}}</th>
7374
<th class="all">{{Enabled}}</th>
7475
<th class="all" width="5%"></th>
7576
</tr>
@@ -141,6 +142,7 @@
141142
{ "data": "last_seen" },
142143
{ "data": "notes" },
143144
{ "data": "game_restarts_today" },
145+
{ "data": "exclude_reboots" },
144146
{ "data": "enabled" },
145147
{ "data": "buttons" }
146148
],

static/js/device.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function reboot(listeners, uuid) {
1+
function reboot(listeners, uuid, excludeReboots) {
2+
if (parseInt(excludeReboots) === 1) {
3+
return;
4+
}
25
const listenersList = (listeners || '').split(',');
36
console.log('Listeners:', listenersList);
47
for (let i = 0; i < listenersList.length; i++) {

0 commit comments

Comments
 (0)