Skip to content

Commit

Permalink
Limit commands sent to devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasmar authored and Kasmar committed Dec 11, 2023
1 parent 112e311 commit 47f48a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Rotomonitor is a simple discord bot to monitor device status for Rotom <https://

# SETTING UP THE BOT:

1. Download `Node.js ver v16.14.2` from https://nodejs.org/en/download/
1. Download `Node.js ver v18.19.0` from https://nodejs.org/en/download/

2. Run `git clone https://github.com/Kneckter/Rotomonitor` to copy the bot.

Expand Down
54 changes: 43 additions & 11 deletions Rotomonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var offlineDeviceMessage = "";
var lastUpdatedMessage = "";
var channelsCleared = false;
var sql = new sqlite3.Database('./deviceDB.sqlite');
var initDBRead = 1;

Login();

Expand Down Expand Up @@ -291,7 +292,10 @@ function UpdateDevices() {
return resolve(true);
}
// Read local SQLite DB for stored devices before requesting the Rotom list.
await ReadDB();
if(initDBRead) {
await ReadDB();
initDBRead = 0;
}
// Request the Rotom list of devices
request.get(config.rotomURL + DEVICE_QUERY, WEBSITE_AUTH, (err, res, body) => {
if(err) {
Expand Down Expand Up @@ -454,6 +458,7 @@ async function AddDevice(device) {
}
devices[name] = {
"name": name,
"parent": device.worker.origin,
"lastSeen": Math.trunc(device.worker.dateLastMessageReceived / 1000),
"alerted": false,
"rebooted": false,
Expand Down Expand Up @@ -620,7 +625,7 @@ function ReopenWarnGame(manDevices) {
method: 'POST',
body: {
'type': 'reopen',
'device': devices[reopenDevices[i]].name.slice(0, -4)
'device': devices[reopenDevices[i]].parent
},
headers: {
'Accept': 'application/json',
Expand All @@ -635,7 +640,16 @@ function ReopenWarnGame(manDevices) {
if (manDevices) { bot.channels.cache.get(config.channel).send(`Failed to send reopen game request to remote listener for ${options.body.device}`); }
}
});
devices[reopenDevices[i]].reopened = true;
// Update all workers with the same parent device
for(var deviceName in devices) {
if(reopenDevices.indexOf(deviceName) == -1) {
continue;
}
if(deviceName.parent != devices[reopenDevices[i]].parent) {
continue;
}
devices[deviceName].reopened = true;
}
}
}
}
Expand Down Expand Up @@ -692,7 +706,7 @@ function ReapplySAM(manDevices) {
method: 'POST',
body: {
'type': 'profile',
'device': devices[reapplyDevices[i]].name.slice(0, -4)
'device': devices[reapplyDevices[i]].parent
},
headers: {
'Accept': 'application/json',
Expand All @@ -707,7 +721,16 @@ function ReapplySAM(manDevices) {
if (manDevices) { bot.channels.cache.get(config.channel).send(`Failed to send request to reapply the SAM profile to remote listener for ${options.body.device}`); }
}
});
devices[reapplyDevices[i]].reapplied = true;
// Update all workers with the same parent device
for(var deviceName in devices) {
if(reapplyDevices.indexOf(deviceName) == -1) {
continue;
}
if(deviceName.parent != devices[reapplyDevices[i]].parent) {
continue;
}
devices[deviceName].reopened = true;
}
}
}
}
Expand Down Expand Up @@ -771,7 +794,7 @@ function RebootWarnDevice(manDevices) {
method: 'POST',
body: {
'type': 'restart',
'device': devices[warnedDevices[i]].name.slice(0, -4)
'device': devices[warnedDevices[i]].parent
},
headers: {
'Accept': 'application/json',
Expand All @@ -786,11 +809,20 @@ function RebootWarnDevice(manDevices) {
if (manDevices) { bot.channels.cache.get(config.channel).send(`Failed to send reboot request to remote listener for ${options.body.device}`); }
}
});
SendRebootAlert(warnedDevices[i].name);
devices[warnedDevices[i]].rebooted = true;
devices[warnedDevices[i]].rebooted_time = Date.now();
devices[warnedDevices[i]].retry_reboot = false;
devices[warnedDevices[i]].reboots = devices[warnedDevices[i]].reboots + 1;
SendRebootAlert(devices[warnedDevices[i]].parent);
// Update all workers with the same parent device
for(var deviceName in devices) {
if(warnedDevices.indexOf(deviceName) == -1) {
continue;
}
if(deviceName.parent != devices[warnedDevices[i]].parent) {
continue;
}
devices[deviceName].rebooted = true;
devices[deviceName].rebooted_time = Date.now();
devices[deviceName].retry_reboot = false;
devices[deviceName].reboots = devices[deviceName].reboots + 1;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Rotomonitor",
"version": "1.4.0",
"version": "1.4.1",
"description": "RDMDeviceMonitor is a simple discord bot to monitor device status for RDM.",
"main": "Rotomonitor.js",
"scripts": {
Expand Down

0 comments on commit 47f48a9

Please sign in to comment.