Skip to content

Commit

Permalink
Reapply SAM profile (#4)
Browse files Browse the repository at this point in the history
* Reapply SAM profile

* Remove the SAM timer

* Comment out the SAM timer

Co-authored-by: Kasmar <Kasmar@example.com>
  • Loading branch information
Kneckter and Kasmar authored Jan 28, 2021
1 parent 089b328 commit 5e12a86
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
75 changes: 70 additions & 5 deletions RDMMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const warningTime = config.warningTime * 60000;
const offlineTime = config.offlineTime * 60000;
const rebuildTime = config.rebuildTime * 60000;
const reopenTime = config.reopenTime * 60000;
//const reapplySAMTime = config.reapplySAMTime * 60000;
const okColor = 0x008000;
const warningColor = 0xFFFF00;
const offlineColor = 0xFF0000;
Expand Down Expand Up @@ -95,9 +96,9 @@ bot.on('message', async message => {
message.delete();
cmds = "`" + config.cmdPrefix + "restart` \\\u00BB to manually restart the whole bot.\n" +
"`" + config.cmdPrefix + "reopen <DEVICE-NAMES>` \\\u00BB to reopen the game on specific devices.\n" +
"`" + config.cmdPrefix + "reboot <DEVICE-NAMES>` \\\u00BB to reboot the specific devices.\n" /*+
"`" + config.cmdPrefix + "sam <DEVICE-NAMES>` \\\u00BB to add more time to a temporary role assignment\n"*/ +
"Reopen and reboot accept multiple names separated by commas.\n" +
"`" + config.cmdPrefix + "reboot <DEVICE-NAMES>` \\\u00BB to reboot the specific devices.\n" +
"`" + config.cmdPrefix + "sam <DEVICE-NAMES>` \\\u00BB to reapply the SAM profile to the specific devices\n" +
"The commands with `<DEVICE-NAMES>` accept multiple names separated by commas.\n" +
"They can be used to skip the exclusion list if you specify a name on the list.\n" +
"They can accept `all`, `allwarn`, or `alloff` to apply to groups but will omit devices on the exclude lists."
bot.channels.cache.get(config.channel).send(cmds).then((message) => {
Expand Down Expand Up @@ -136,7 +137,7 @@ bot.on('message', async message => {
exclude = config.excludeFromReopen;
}
else if(command === "sam") {
// WIP
exclude = config.excludeFromReapplySAM;
}

if (args.length == 1 && args[0] == "all") {
Expand Down Expand Up @@ -189,14 +190,15 @@ bot.on('message', async message => {

if(command === "reboot") {
// Reboot a specific device
RebootWarnDevice(manDevices)
RebootWarnDevice(manDevices);
}
else if(command === "reopen") {
// Reopen a game for a specific device
ReopenWarnGame(manDevices);
}
else if(command === "sam") {
// Reapply the SAM profile
ReapplySAM(manDevices);
}
}
else {
Expand Down Expand Up @@ -484,6 +486,7 @@ function AddDevice(device) {
"alerted": false,
"rebooted": false,
"reopened": false,
"reapplied": false,
"builds": 0
};
if(!devices[device.uuid].lastSeen) {
Expand Down Expand Up @@ -532,6 +535,7 @@ async function PostStatus() {
await PostGroupedDevices();
await SendOfflineDeviceDMs();
await ReopenWarnGame();
//await ReapplySAM();
await RebootWarnDevice();
}

Expand Down Expand Up @@ -706,6 +710,67 @@ function ReopenWarnGame(manDevices) {
setTimeout(ReopenWarnGame, 60000);
}

function ReapplySAM(manDevices) {
if(!config.allowReapplySAM) {
return;
}
let now = new Date();
now = now.getTime();
let reapplyDevices = [];
if (manDevices) {
reapplyDevices = manDevices;
}
else {
for(var deviceName in devices) {
let device = devices[deviceName];
let lastSeen = new Date(0);
lastSeen.setUTCSeconds(device.lastSeen);
lastSeen = lastSeen.getTime();
lastSeen = now - lastSeen;
if(lastSeen > reapplySAMTime) {
if(!config.excludeFromReapplySAM.includes(deviceName)) {
reapplyDevices.push(device.name);
}
}
}
}
for(var i = 0; i < reapplyDevices.length; i++) {
if(!devices[reapplyDevices[i]].reapplied || manDevices) {
for(var ii = 0; ii < config.reapplySAMMonitorURL.length; ii++) {
const options = {
url: config.reapplySAMMonitorURL[ii],
json: true,
method: 'POST',
body: {
'type': 'profile',
'device': devices[reapplyDevices[i]].name
},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
};
console.info(GetTimestamp() + `Sending a request to reapply the SAM profile for ${devices[reapplyDevices[i]].name} to remote listener ${config.reapplySAMMonitorURL[ii]}`);
if (manDevices) { bot.channels.cache.get(config.channel).send(`Sending a request to reapply the SAM profile for ${devices[reapplyDevices[i]].name} to remote listener`); }
request(options, (err, res, body) => {
if(err) {
console.error(GetTimestamp() + `Failed to send request to reapply the SAM profile to remote listener for ${options.body.device}`);
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;
}
}
}
for(var deviceName in devices) {
if(devices[deviceName].reapplied && reapplyDevices.indexOf(deviceName) == -1) {
devices[deviceName].reapplied = false;
console.info(GetTimestamp() + `Device ${devices[deviceName].name} has come back online from reapplying the profile`);
}
}
setTimeout(ReopenWarnGame, 60000);
}

function RebootWarnDevice(manDevices) {
if(!config.allowWarnReboots) {
return;
Expand Down
4 changes: 4 additions & 0 deletions RDMMonitorConfig.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"reopenMonitorURL": ["http://192.168.0.1:6542","http://192.168.0.1:6543"],
"excludeFromReopen": ["Device01","Device02"],

"allowReapplySAM": true,
"reapplySAMMonitorURL": ["http://192.168.0.1:6542","http://192.168.0.1:6543"],
"excludeFromReapplySAM": ["Device01","Device02"],

"allowWarnReboots": true,
"rebootMonitorURL": ["http://192.168.0.1:6542","http://192.168.0.1:6543"],
"sendRebootAlerts": false,
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ reopenTime: The time in minutes to request a device to reopen the game
reopenMonitorURL: An array of strings for the URLs of the reopen game monitors you are using like iPhone Controller or DCM Listener
excludeFromReopen: An array of strings that are the unique names of the devices to exclude from the reopen game request
allowReapplySAM: true/false - Bool to enable RDMDeviceMonitor to send a request to a monitor for reapplying the SAM profile.
reapplySAMMonitorURL: An array of strings for the URLs of the reapply SAM monitors you are using like iPhone Controller or DCM Listener
excludeFromReapplySAM: An array of strings that are the unique names of the devices to exclude from the reapply SAM request
allowWarnReboots: true/false - Bool to enable RDMDeviceMonitor to send a reboot request to a monitor
rebootMonitorURL: An array of strings for the URLs of the reboot monitors you are using like iPhone Controller or DCM Listener
sendRebootAlerts: true/false - Bool to enable the DM message for rebooting a device
Expand All @@ -99,7 +103,8 @@ Instead, add it to PM2 with `pm2 start ecosystem.config.js`
--`.restart` » to manually restart the whole bot<br>
--`.reopen <DEVICE-NAMES>` » to reopen the game on specific devices<br>
--`.reboot <DEVICE-NAMES>` » to reboot the specific devices<br>
--`.sam <DEVICE-NAMES>` » to reapply the SAM profile to the specific devices<br>

Reopen and reboot accept multiple names separated by commas.<br>
The commands with `<DEVICE-NAMES>` accept multiple names separated by commas.<br>
They can be used to skip the exclusion list if you specify a name on the list.<br>
They can accept `all`, `allwarn`, or `alloff` to apply to groups but will omit devices on the exclude lists.

0 comments on commit 5e12a86

Please sign in to comment.