Created
December 28, 2019 10:59
-
-
Save cprakashagr/03aafbb03cb34c266d52bb5f5eb2988d to your computer and use it in GitHub Desktop.
Rig Management Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rig Power Management System | |
* Author: CP | |
*/ | |
const puppeteer = require('puppeteer') | |
const select = require ('puppeteer-select'); | |
(async () => { | |
function printUsage() { | |
console.log('Usage: '); | |
console.log(' $> node rig.js on/off'); | |
}; | |
function delay(timeout) { | |
return new Promise((resolve) => { | |
setTimeout(resolve, timeout); | |
}); | |
}; | |
if (process.argv.length < 3) { | |
printUsage(); | |
return; | |
} | |
if (!(process.argv[2].toUpperCase() === 'ON' || process.argv[2].toUpperCase() === 'OFF')) { | |
printUsage(); | |
return; | |
} | |
console.log('Requested new state: ' + process.argv[2].toUpperCase()); | |
const browser = await puppeteer.launch({headless: true, ignoreHTTPSErrors: true}); | |
const page = await browser.newPage(); | |
await page.goto(process.env.RIG_URL); | |
await page.waitForNavigation(); | |
await page.type('#login_username', process.env.RIG_USER); | |
await page.type('#login_password', process.env.RIG_PASS); | |
await page.click('[name="Login"]'); | |
page.on('dialog', async dialog => { | |
const message = dialog.message(); | |
if (message === 'Are you sure you want to log out?') { | |
await dialog.accept(); | |
} | |
await browser.close(); | |
}); | |
(async() => { | |
await delay(2500); | |
const frames = await page.frames(); | |
const headerFrame = frames.find(f => f.name() === 'header'); | |
const sideFrame = frames.find(f => f.name() === 'sidebar'); | |
const mainFrame = frames.find(f => f.name() === 'mainFrame'); | |
await headerFrame.click('#STR_TOPNAV_REMOTE_CONTROL'); | |
const powerPanel = await select(sideFrame).getElement('a:contains("Server Power Control")'); | |
await powerPanel.click(); | |
await delay(1000); | |
// Get Current Status: | |
const statusSpan = await mainFrame.$eval('#_statusMsg', e => e.innerText.substr(18, e.innerText.length)); | |
console.log('Current state: ' + statusSpan); | |
if (statusSpan === process.argv[2].toUpperCase()) { | |
console.log(`System is already ${statusSpan}`); | |
browser.close(); | |
return; | |
} | |
var panelSelector = ''; | |
if (process.argv[2].toUpperCase() === 'ON') { | |
panelSelector = '#_pwrOnSrvr'; | |
} | |
else { | |
panelSelector = '#_iPwrOffSrvr'; | |
} | |
const inputPowerButton = await mainFrame.click(panelSelector); | |
await mainFrame.click('#_prfmAction'); | |
const newStatus = await mainFrame.$eval('#_statusMsg', e => e.innerText); | |
console.log("Final state: " + newStatus); | |
// Perform logout. | |
await headerFrame.click('#_navbarlogoutlink'); | |
})(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment