adding comments

This commit is contained in:
raven 2022-12-22 04:19:27 -05:00
parent 1a9b8532d5
commit 65c7735804
2 changed files with 100 additions and 48 deletions

View File

@ -2,34 +2,45 @@ var fs = require('fs');
const express = require('express') const express = require('express')
const { Samsung, KEYS, APPS } = require('samsung-tv-control') const { Samsung, KEYS, APPS } = require('samsung-tv-control')
const jsonfile = require('jsonfile') const jsonfile = require('jsonfile')
// Location of File Cache
const file = '/home/token.json' const file = '/home/token.json'
var https = require('https'); var https = require('https');
var privateKey = fs.readFileSync('/cert/cert.key', 'utf8');
var certificate = fs.readFileSync('/cert/cert.crt', 'utf8'); // Set up SSL
var privateKey = fs.readFileSync('/cert/grwh.work.key', 'utf8');
var certificate = fs.readFileSync('/cert/grwh.work.cert.combined', 'utf8');
var credentials = {key: privateKey, cert: certificate}; var credentials = {key: privateKey, cert: certificate};
// Keep BASE URL in case its needed
let baseURL = "https://tv.grwh.work"
// Enable CORS for Express
var cors = require('cors') var cors = require('cors')
// Use These options to allow ALL
const corsOptions ={ const corsOptions ={
origin:'*', origin:'*',
credentials:true, credentials:true,
optionSuccessStatus:200, optionSuccessStatus:200,
} }
// Use JSON NPM to read the token from cache
jsonfile.readFile(file, function (err, obj) { jsonfile.readFile(file, function (err, obj) {
if (err) console.error(err) if (err) console.error(err)
// The token was read, here it is
console.log("token: " + obj.data) console.log("token: " + obj.data)
// Lets add that to the configuration
const config = { const config = {
debug: false, // Default: false debug: false, // Default: false
ip: '192.168.0.2', ip: '192.168.0.2',
mac: 'A4:30:7A:08:57:6A', mac: 'A4:30:7A:08:57:6A',
nameApp: 'OurControlApp', // Default: NodeJS nameApp: 'WebControl', // Default: NodeJS
port: 8002, // Default: 8002 port: 8002, // Default: 8002
token: obj.data, token: obj.data,
} }
// Now we have a config, lets set it
const control = new Samsung(config) const control = new Samsung(config)
// Get all installed apps from TV // Get all installed apps from TV - FUNCTION
control.getAppsFromTV((err, res) => { control.getAppsFromTV((err, res) => {
console.log(err) console.log(err)
if (!err) { if (!err) {
@ -37,6 +48,7 @@ jsonfile.readFile(file, function (err, obj) {
} }
}) })
// Function to send KEY!
async function sendKey(key) { async function sendKey(key) {
// Send key to TV // Send key to TV
control.sendKey(key, function (err, res) { control.sendKey(key, function (err, res) {
@ -45,18 +57,31 @@ jsonfile.readFile(file, function (err, obj) {
} }
}) })
} }
// Function to send INPUT!
async function sendInput(input){
// Send text to focused input on TV
control.sendText(input, function (err, res) {
if (!err) {
console.log("Sending: " + input)
console.log('# Response sendText', res)
}
})
}
// Defube Express and https serer
const app = express() const app = express()
var httpsServer = https.createServer(credentials, app); var httpsServer = https.createServer(credentials, app);
// Set port
const port = 3031 const port = 3031
// USe CORS
app.use(cors(corsOptions)) app.use(cors(corsOptions))
// Lets handle BLANK Traffic!
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.end('Welcome to the TV API!') res.end('Welcome to the TV API!')
}) })
// Handling home
app.get('/home', (req, res) => { app.get('/home', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_HOME) await sendKey(KEYS.KEY_HOME)
@ -64,6 +89,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling right
app.get('/right', (req, res) => { app.get('/right', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_RIGHT) await sendKey(KEYS.KEY_RIGHT)
@ -71,6 +97,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling left
app.get('/left', (req, res) => { app.get('/left', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_LEFT) await sendKey(KEYS.KEY_LEFT)
@ -78,6 +105,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// up
app.get('/up', (req, res) => { app.get('/up', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_UP) await sendKey(KEYS.KEY_UP)
@ -85,6 +113,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling down
app.get('/down', (req, res) => { app.get('/down', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_DOWN) await sendKey(KEYS.KEY_DOWN)
@ -92,6 +121,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling enter
app.get('/enter', (req, res) => { app.get('/enter', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_ENTER) await sendKey(KEYS.KEY_ENTER)
@ -99,6 +129,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling back
app.get('/back', (req, res) => { app.get('/back', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_RETURN) await sendKey(KEYS.KEY_RETURN)
@ -106,6 +137,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling volume down
app.get('/volumeDown', (req, res) => { app.get('/volumeDown', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_VOLDOWN) await sendKey(KEYS.KEY_VOLDOWN)
@ -113,6 +145,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling volume up
app.get('/volumeUp', (req, res) => { app.get('/volumeUp', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_VOLUP) await sendKey(KEYS.KEY_VOLUP)
@ -120,6 +153,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling mute
app.get('/mute', (req, res) => { app.get('/mute', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_MUTE) await sendKey(KEYS.KEY_MUTE)
@ -127,6 +161,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling picture mode
app.get('/pmode', (req, res) => { app.get('/pmode', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_PMODE) await sendKey(KEYS.KEY_PMODE)
@ -134,6 +169,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling off
app.get('/off', (req, res) => { app.get('/off', (req, res) => {
(async () => { (async () => {
await sendKey(KEYS.KEY_POWEROFF) await sendKey(KEYS.KEY_POWEROFF)
@ -141,6 +177,19 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Handling input
app.get('/input', (req, res) => {
const path = require('path');
(async () => {
let sent = req.query.data
await sendInput(sent)
res.sendFile(path.join(__dirname, '/finish_input.html'));
})();
})
// Generating a new token
app.get('/newToken', (req, res) => { app.get('/newToken', (req, res) => {
(async () => { (async () => {
control control
@ -150,7 +199,7 @@ jsonfile.readFile(file, function (err, obj) {
control.getToken((token) => { control.getToken((token) => {
console.info('# Response getToken:', token) console.info('# Response getToken:', token)
const obj = { data: token } const obj = { data: token }
if (token == "null") return if (token == null) return
jsonfile.writeFile(file, obj, function (err) { jsonfile.writeFile(file, obj, function (err) {
if (err) console.error(err) if (err) console.error(err)
console.log("new key, exiting to refresh") console.log("new key, exiting to refresh")
@ -162,33 +211,10 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
app.get('/openNetFlix', (req, res) => { // Hadling checking password
(async () => {
// Open app by appId which you can get from getAppsFromTV
control.openApp(APPS.Netflix, (err, res) => {
if (!err) {
console.log('# Response openApp', res)
}
})
res.end('1')
})();
})
app.get('/OpenYouTube', (req, res) => {
(async () => {
// Open app by appId which you can get from getAppsFromTV
control.openApp(APPS.Netflix, (err, res) => {
if (!err) {
console.log('# Response openApp', res)
}
})
res.end('1')
})();
})
app.get('/checkPass', (req, res) => { app.get('/checkPass', (req, res) => {
(async () => { (async () => {
let pass = "YourPassHere" let pass = "scott"
let sent = req.query.pass let sent = req.query.pass
if (pass == sent){ if (pass == sent){
console.log("User Authed Properly!") console.log("User Authed Properly!")
@ -199,6 +225,7 @@ jsonfile.readFile(file, function (err, obj) {
})(); })();
}) })
// Listen
httpsServer.listen(port, () => { httpsServer.listen(port, () => {
console.log(`TV Control API listening on port ${port}`) console.log(`TV Control API listening on port ${port}`)
}) })

View File

@ -7,6 +7,7 @@
</head> </head>
<title>Samsung TVController</title> <title>Samsung TVController</title>
<!--Styles-->+
<style> <style>
body { body {
background-color: #101010; background-color: #101010;
@ -25,16 +26,22 @@
color: #ffffff; color: #ffffff;
} }
</style> </style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<!--END Styles-->
<!--MAIN JS-->
<script> <script>
// Set API URL
let rootURL = "https://grwh.work:3031" let rootURL = "https://grwh.work:3031"
// Init timer VAR
var timer; var timer;
// Youtube Shorts Timer - // Youtube Shorts Timer - Skip Every 55 Seconds
function startTimer() { function startTimer() {
console.log("Shorts timer started!") console.log("Shorts timer started!")
showSuccessAlert("Started Shorts Timer") showSuccessAlert("Started Shorts Timer")
@ -56,11 +63,13 @@
}, 55000); }, 55000);
} }
// Kill the Timer
function stopTimer() { function stopTimer() {
showSuccessAlert("Shorts Timer Disabled!") showSuccessAlert("Shorts Timer Disabled!")
clearInterval(timer); clearInterval(timer);
} }
// Settng up success alerts
function showSuccessAlert(message) { function showSuccessAlert(message) {
document.getElementById("view2").innerHTML = "<div class=\"alert alert-success\" role=\"alert\"><CENTER>" + message + "</CENTER></div>"; document.getElementById("view2").innerHTML = "<div class=\"alert alert-success\" role=\"alert\"><CENTER>" + message + "</CENTER></div>";
@ -70,6 +79,7 @@
}, 15000); }, 15000);
} }
// Setting up Failed Alerts
function showFailAlert(message) { function showFailAlert(message) {
document.getElementById("view2").innerHTML = "<div class=\"alert alert-danger\" role=\"alert\"><CENTER>" + message + "</CENTER></div>"; document.getElementById("view2").innerHTML = "<div class=\"alert alert-danger\" role=\"alert\"><CENTER>" + message + "</CENTER></div>";
@ -81,9 +91,8 @@
} }
showSuccessAlert("You have logged in successfully!")
// This function is only used for the Refresh Button
function generateNewToken() { function generateNewToken() {
fetch(rootURL + "/newToken") fetch(rootURL + "/newToken")
.then(function (response) { .then(function (response) {
@ -100,23 +109,26 @@
}) })
} }
// Ask the user for the password.
let pass = prompt("Enter the password!", ""); let pass = prompt("Enter the password!", "");
let text; let text;
// Ask the API if the password is correct
fetch(rootURL + "/checkPass?pass=" + pass) fetch(rootURL + "/checkPass?pass=" + pass)
.then(function (response) { .then(function (response) {
return response.json(); return response.json();
}) }) // Check for a positive 1 response back
.then(function (data) { .then(function (data) {
if (data == "1") { if (data == "1") {
// If they do not submit a password OR hit cancel, fail the login!
if (pass == null || pass == "") { if (pass == null || pass == "") {
document.getElementById("view").innerHTML = "WOO" document.getElementById("view").innerHTML = "WOO"
} else { } else {
// If we got this far, we have logged in, lets tell the user that
showSuccessAlert("You have logged in successfully!")
document.getElementById("view").innerHTML = "<CENTER><IMG SRC=\"../images/icon.png\"></CENTER><a href=\"javascript:window.open('help.html','Click for Key ShortCuts!','width=600,height=400')\">Click for Key ShortCuts!</a><BR><BR><a href=\"javascript:window.open('input.html','Click to Send Text Imput!','width=600,height=400')\"><button type=\"button\" class=\"btn btn-primary\">Open Text Input</button></a><BR><BR><button type=\"button\" class=\"btn btn-primary\" onclick=\"startTimer()\">Enable AutoScroll</button> / <button type=\"button\" class=\"btn btn-primary\" onclick=\"stopTimer()\"> Disable</button> <BR><BR>Commands Not Working? Issue a new Token using: <BR> <button type=\"button\" class=\"btn btn-primary\" onclick=\"generateNewToken()\">Request new Token</button>"; document.getElementById("view").innerHTML = "<CENTER><IMG SRC=\"../images/icon.png\"></CENTER><a href=\"javascript:window.open('help.html','Click for Key ShortCuts!','width=600,height=400')\">Click for Key ShortCuts!</a><BR><BR><a href=\"javascript:window.open('input.html','Click to Send Text Imput!','width=600,height=400')\"><button type=\"button\" class=\"btn btn-primary\">Open Text Input</button></a><BR><BR><button type=\"button\" class=\"btn btn-primary\" onclick=\"startTimer()\">Enable AutoScroll</button> / <button type=\"button\" class=\"btn btn-primary\" onclick=\"stopTimer()\"> Disable</button> <BR><BR>Commands Not Working? Issue a new Token using: <BR> <button type=\"button\" class=\"btn btn-primary\" onclick=\"generateNewToken()\">Request new Token</button>";
// Adding the event Listening Function
function addEvent(element, eventName, callback) { function addEvent(element, eventName, callback) {
if (element.addEventListener) { if (element.addEventListener) {
element.addEventListener(eventName, callback, false); element.addEventListener(eventName, callback, false);
@ -127,9 +139,11 @@
} }
} }
// Lets now listen for events
addEvent(document, "keypress", function (e) { addEvent(document, "keypress", function (e) {
e = e || window.event; e = e || window.event;
// Command UP
if (e.key == "w" || e.code == "Numpad8") { if (e.key == "w" || e.code == "Numpad8") {
fetch(rootURL + "/up") fetch(rootURL + "/up")
.then(function (response) { .then(function (response) {
@ -146,6 +160,7 @@
}) })
} }
// Command LEFT
if (e.key == "a" || e.code == "Numpad4") { if (e.key == "a" || e.code == "Numpad4") {
fetch(rootURL + "/left") fetch(rootURL + "/left")
.then(function (response) { .then(function (response) {
@ -162,6 +177,7 @@
}) })
} }
// Command DOWN
if (e.key == "s" || e.key == "2") { if (e.key == "s" || e.key == "2") {
fetch(rootURL + "/down") fetch(rootURL + "/down")
.then(function (response) { .then(function (response) {
@ -178,6 +194,7 @@
}) })
} }
// Command RIGHT
if (e.key == "d" || e.code == "Numpad6") { if (e.key == "d" || e.code == "Numpad6") {
fetch(rootURL + "/right") fetch(rootURL + "/right")
.then(function (response) { .then(function (response) {
@ -194,6 +211,7 @@
}) })
} }
// Command ENTER
if (e.key == "Enter" || e.code == "Numpad5") { if (e.key == "Enter" || e.code == "Numpad5") {
fetch(rootURL + "/enter") fetch(rootURL + "/enter")
.then(function (response) { .then(function (response) {
@ -210,6 +228,7 @@
}) })
} }
// Command BACK
if (e.key == "x" || e.code == "NumpadDecimal") { if (e.key == "x" || e.code == "NumpadDecimal") {
fetch(rootURL + "/back") fetch(rootURL + "/back")
.then(function (response) { .then(function (response) {
@ -226,6 +245,7 @@
}) })
} }
// Command VOL UP
if (e.key == "p" || e.code == "NumpadAdd") { if (e.key == "p" || e.code == "NumpadAdd") {
fetch(rootURL + "/volumeUp") fetch(rootURL + "/volumeUp")
.then(function (response) { .then(function (response) {
@ -242,6 +262,7 @@
}) })
} }
// Command VOL DOWN
if (e.key == "o" || e.code == "NumpadSubtract") { if (e.key == "o" || e.code == "NumpadSubtract") {
fetch(rootURL + "/volumeDown") fetch(rootURL + "/volumeDown")
.then(function (response) { .then(function (response) {
@ -258,6 +279,7 @@
}) })
} }
// Request new token using the R shortcut
if (e.key == "r") { if (e.key == "r") {
fetch(rootURL + "/newToken") fetch(rootURL + "/newToken")
.then(function (response) { .then(function (response) {
@ -274,7 +296,7 @@
}) })
} }
// Command MUTE
if (e.key == "m" || e.code == "Numpad0") { if (e.key == "m" || e.code == "Numpad0") {
fetch(rootURL + "/mute") fetch(rootURL + "/mute")
.then(function (response) { .then(function (response) {
@ -291,6 +313,7 @@
}) })
} }
// Command Picture Mode
if (e.key == "l") { if (e.key == "l") {
fetch(rootURL + "/pmode") fetch(rootURL + "/pmode")
.then(function (response) { .then(function (response) {
@ -307,6 +330,7 @@
}) })
} }
// Command HOME
if (e.key == "h") { if (e.key == "h") {
fetch(rootURL + "/home") fetch(rootURL + "/home")
.then(function (response) { .then(function (response) {
@ -323,26 +347,27 @@
}) })
} }
}); });
} }
} else { } else {
// Login Failed!
showFailAlert("The password was incorrect!") showFailAlert("The password was incorrect!")
document.getElementById("view").innerHTML = "<meta http-equiv=\"refresh\" content=3;>" document.getElementById("view").innerHTML = "<meta http-equiv=\"refresh\" content=3;>"
} }
}) })
</script> </script>
<!--END MAIN JS-->
</head> </head>
<body> <body>
<!--MAIN VIEWPORT-->
<center> <center>
<div id="view"></div> <div id="view"></div>
<center> <center>
<!--END MAIN VIEWPORT-->
</body> </body>
</html> </html>