Last active
December 16, 2022 17:17
-
-
Save konecnyna/e13e6e6567e285d84e3ff878d00748d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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() })); | |
} | |
} |
Author
konecnyna
commented
Dec 6, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment