Skip to content

Instantly share code, notes, and snippets.

@konecnyna
Last active December 16, 2022 17:17
Show Gist options
  • Save konecnyna/e13e6e6567e285d84e3ff878d00748d3 to your computer and use it in GitHub Desktop.
Save konecnyna/e13e6e6567e285d84e3ff878d00748d3 to your computer and use it in GitHub Desktop.
const http = require('http');
const URL = require('url');
const { execSync } = require('child_process');
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
routes(req,res);
});
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
const status = () => {
const result = execSync(`vcgencmd display_power`).toString().trim()
// 1 = on ; 0 = off
return result == "display_power=1" ? "on" : "off"
}
const routes = (req,res) => {
const {query , pathname} = URL.parse(req.url,true);
switch(pathname) {
case "/turn-on":
execSync('/usr/bin/vcgencmd display_power 1');
res.end(JSON.stringify({ status: status() }));
break;
case "/turn-off":
execSync('/usr/bin/vcgencmd display_power 0');
res.end(JSON.stringify({ status: status() }));
break;
case "/restart":
execSync('/home/pi/.npm-global/bin/pm2 restart magic-mirror');
res.end(JSON.stringify({ status: status() }));
break;
default:
res.end(JSON.stringify({ status: status() }));
}
}
@konecnyna
Copy link
Author

pm2 start magic-mirror-power.js
pm2 save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment